1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts.webapp.example;
19
20
21 import java.io.IOException;
22
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
25 import javax.servlet.jsp.JspException;
26 import javax.servlet.jsp.JspWriter;
27 import javax.servlet.jsp.tagext.TagSupport;
28
29 import org.apache.struts.util.MessageResources;
30 import org.apache.struts.util.ResponseUtils;
31
32
33 /***
34 * Generate a URL-encoded hyperlink to the specified URI, with
35 * associated query parameters selecting a specified User.
36 *
37 * @author Craig R. McClanahan
38 * @version $Rev: 421493 $ $Date: 2006-07-12 20:52:31 -0700 (Wed, 12 Jul 2006) $
39 */
40
41 public class LinkUserTag extends TagSupport {
42
43
44
45
46
47 /***
48 * The hyperlink URI.
49 */
50 protected String page = null;
51
52
53 /***
54 * The message resources for this package.
55 */
56 protected static MessageResources messages =
57 MessageResources.getMessageResources
58 ("org.apache.struts.webapp.example.ApplicationResources");
59
60
61 /***
62 * The attribute name.
63 */
64 private String name = "user";
65
66
67
68
69
70 /***
71 * Return the hyperlink URI.
72 */
73 public String getPage() {
74
75 return (this.page);
76
77 }
78
79
80 /***
81 * Set the hyperlink URI.
82 *
83 * @param page Set the hyperlink URI
84 */
85 public void setPage(String page) {
86
87 this.page = page;
88
89 }
90
91
92 /***
93 * Return the attribute name.
94 */
95 public String getName() {
96
97 return (this.name);
98
99 }
100
101
102 /***
103 * Set the attribute name.
104 *
105 * @param name The new attribute name
106 */
107 public void setName(String name) {
108
109 this.name = name;
110
111 }
112
113
114
115
116
117 /***
118 * Render the beginning of the hyperlink.
119 *
120 * @exception JspException if a JSP exception has occurred
121 */
122 public int doStartTag() throws JspException {
123
124
125 HttpServletRequest request =
126 (HttpServletRequest) pageContext.getRequest();
127 StringBuffer url = new StringBuffer(request.getContextPath());
128 url.append(page);
129 User user = null;
130 try {
131 user = (User) pageContext.findAttribute(name);
132 } catch (ClassCastException e) {
133 user = null;
134 }
135 if (user == null)
136 throw new JspException
137 (messages.getMessage("linkUser.noUser", name));
138 if (page.indexOf("?") < 0)
139 url.append("?");
140 else
141 url.append("&");
142 url.append("username=");
143 url.append(ResponseUtils.filter(user.getUsername()));
144
145
146 HttpServletResponse response =
147 (HttpServletResponse) pageContext.getResponse();
148 StringBuffer results = new StringBuffer("<a href=\"");
149 results.append(response.encodeURL(url.toString()));
150 results.append("\">");
151
152
153 JspWriter writer = pageContext.getOut();
154 try {
155 writer.print(results.toString());
156 } catch (IOException e) {
157 throw new JspException
158 (messages.getMessage("linkUser.io", e.toString()));
159 }
160
161
162 return (EVAL_BODY_INCLUDE);
163
164 }
165
166
167
168 /***
169 * Render the end of the hyperlink.
170 *
171 * @exception JspException if a JSP exception has occurred
172 */
173 public int doEndTag() throws JspException {
174
175
176
177 JspWriter writer = pageContext.getOut();
178 try {
179 writer.print("</a>");
180 } catch (IOException e) {
181 throw new JspException
182 (messages.getMessage("link.io", e.toString()));
183 }
184
185 return (EVAL_PAGE);
186
187 }
188
189
190 /***
191 * Release any acquired resources.
192 */
193 public void release() {
194
195 super.release();
196 this.page = null;
197 this.name = "user";
198
199 }
200
201
202 }