001: /*
002: * $Id: LinkUserTag.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: package org.apache.struts.webapp.example2;
023:
024: import java.io.IOException;
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.http.HttpServletResponse;
027: import javax.servlet.jsp.JspException;
028: import javax.servlet.jsp.JspWriter;
029: import javax.servlet.jsp.tagext.TagSupport;
030: import org.apache.struts.util.MessageResources;
031: import org.apache.struts.util.ResponseUtils;
032:
033: /**
034: * Generate a URL-encoded hyperlink to the specified URI, with
035: * associated query parameters selecting a specified User.
036: *
037: * @author Craig R. McClanahan
038: * @version $Rev: 471754 $ $Date: 2006-11-06 08:55:09 -0600 (Mon, 06 Nov 2006) $
039: */
040:
041: public class LinkUserTag extends TagSupport {
042:
043: // ------------------------------------------------------ Instance Variables
044:
045: /**
046: * The hyperlink URI.
047: */
048: protected String page = null;
049:
050: /**
051: * The message resources for this package.
052: */
053: protected static MessageResources messages = MessageResources
054: .getMessageResources("org.apache.struts.webapp.example.ApplicationResources");
055:
056: /**
057: * The attribute name.
058: */
059: private String name = "user";
060:
061: // ------------------------------------------------------------- Properties
062:
063: /**
064: * Return the hyperlink URI.
065: */
066: public String getPage() {
067:
068: return (this .page);
069:
070: }
071:
072: /**
073: * Set the hyperlink URI.
074: *
075: * @param page Set the hyperlink URI
076: */
077: public void setPage(String page) {
078:
079: this .page = page;
080:
081: }
082:
083: /**
084: * Return the attribute name.
085: */
086: public String getName() {
087:
088: return (this .name);
089:
090: }
091:
092: /**
093: * Set the attribute name.
094: *
095: * @param name The new attribute name
096: */
097: public void setName(String name) {
098:
099: this .name = name;
100:
101: }
102:
103: // --------------------------------------------------------- Public Methods
104:
105: /**
106: * Render the beginning of the hyperlink.
107: *
108: * @exception JspException if a JSP exception has occurred
109: */
110: public int doStartTag() throws JspException {
111:
112: // Generate the URL to be encoded
113: HttpServletRequest request = (HttpServletRequest) pageContext
114: .getRequest();
115: StringBuffer url = new StringBuffer(request.getContextPath());
116: url.append(page);
117: User user = null;
118: try {
119: user = (User) pageContext.findAttribute(name);
120: } catch (ClassCastException e) {
121: user = null;
122: }
123: if (user == null)
124: throw new JspException(messages.getMessage(
125: "linkUser.noUser", name));
126: if (page.indexOf("?") < 0)
127: url.append("?");
128: else
129: url.append("&");
130: url.append("username=");
131: url.append(ResponseUtils.filter(user.getUsername()));
132:
133: // Generate the hyperlink start element
134: HttpServletResponse response = (HttpServletResponse) pageContext
135: .getResponse();
136: StringBuffer results = new StringBuffer("<a href=\"");
137: results.append(response.encodeURL(url.toString()));
138: results.append("\">");
139:
140: // Print this element to our output writer
141: JspWriter writer = pageContext.getOut();
142: try {
143: writer.print(results.toString());
144: } catch (IOException e) {
145: throw new JspException(messages.getMessage("linkUser.io", e
146: .toString()));
147: }
148:
149: // Evaluate the body of this tag
150: return (EVAL_BODY_INCLUDE);
151:
152: }
153:
154: /**
155: * Render the end of the hyperlink.
156: *
157: * @exception JspException if a JSP exception has occurred
158: */
159: public int doEndTag() throws JspException {
160:
161: // Print the ending element to our output writer
162: JspWriter writer = pageContext.getOut();
163: try {
164: writer.print("</a>");
165: } catch (IOException e) {
166: throw new JspException(messages.getMessage("link.io", e
167: .toString()));
168: }
169:
170: return (EVAL_PAGE);
171:
172: }
173:
174: /**
175: * Release any acquired resources.
176: */
177: public void release() {
178:
179: super .release();
180: this .page = null;
181: this .name = "user";
182:
183: }
184:
185: }
|