KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > webapp > example > LinkSubscriptionTag


1 /*
2  * $Id: LinkSubscriptionTag.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 1999-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19
20 package org.apache.struts.webapp.example;
21
22
23 import java.io.IOException JavaDoc;
24
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27 import javax.servlet.jsp.JspException JavaDoc;
28 import javax.servlet.jsp.JspWriter JavaDoc;
29 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
30
31 import org.apache.struts.config.ModuleConfig;
32 import org.apache.struts.taglib.TagUtils;
33 import org.apache.struts.util.MessageResources;
34
35
36 /**
37  * Generate a URL-encoded hyperlink to the specified URI, with
38  * associated query parameters selecting a specified Subscription.
39  *
40  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
41  */

42
43 public class LinkSubscriptionTag extends TagSupport JavaDoc {
44
45
46     // ----------------------------------------------------- Instance Variables
47

48
49     /**
50      * The context-relative URI.
51      */

52     protected String JavaDoc page = null;
53
54
55     /**
56      * The message resources for this package.
57      */

58     protected static MessageResources messages =
59     MessageResources.getMessageResources
60     ("org.apache.struts.webapp.example.ApplicationResources");
61
62
63     /**
64      * The attribute name.
65      */

66     private String JavaDoc name = "subscription";
67
68
69     // ------------------------------------------------------------- Properties
70

71
72     /**
73      * Return the context-relative URI.
74      */

75     public String JavaDoc getPage() {
76
77     return (this.page);
78
79     }
80
81
82     /**
83      * Set the context-relative URI.
84      *
85      * @param page Set the context-relative URI
86      */

87     public void setPage(String JavaDoc page) {
88
89     this.page = page;
90
91     }
92
93
94     /**
95      * Return the attribute name.
96      */

97     public String JavaDoc getName() {
98
99     return (this.name);
100
101     }
102
103
104     /**
105      * Set the attribute name.
106      *
107      * @param name The new attribute name
108      */

109     public void setName(String JavaDoc name) {
110
111     this.name = name;
112
113     }
114
115
116     // --------------------------------------------------------- Public Methods
117

118
119     /**
120      * Render the beginning of the hyperlink.
121      *
122      * @exception JspException if a JSP exception has occurred
123      */

124     public int doStartTag() throws JspException JavaDoc {
125
126     // Generate the URL to be encoded
127
ModuleConfig config = (ModuleConfig) pageContext.getRequest()
128             .getAttribute(org.apache.struts.Globals.MODULE_KEY);
129         HttpServletRequest JavaDoc request =
130           (HttpServletRequest JavaDoc) pageContext.getRequest();
131         StringBuffer JavaDoc url = new StringBuffer JavaDoc(request.getContextPath());
132     url.append(config.getPrefix());
133         url.append(page);
134     Subscription subscription = null;
135     try {
136         subscription = (Subscription) pageContext.findAttribute(name);
137         } catch (ClassCastException JavaDoc e) {
138         subscription = null;
139     }
140     if (subscription == null)
141         throw new JspException JavaDoc
142             (messages.getMessage("linkSubscription.noSubscription", name));
143     if (page.indexOf("?") < 0)
144         url.append("?");
145     else
146         url.append("&");
147     url.append("username=");
148     url.append(TagUtils.getInstance().filter(subscription.getUser().getUsername()));
149     url.append("&host=");
150     url.append(TagUtils.getInstance().filter(subscription.getHost()));
151
152     // Generate the hyperlink start element
153
HttpServletResponse JavaDoc response =
154       (HttpServletResponse JavaDoc) pageContext.getResponse();
155     StringBuffer JavaDoc results = new StringBuffer JavaDoc("<a HREF=\"");
156     results.append(response.encodeURL(url.toString()));
157     results.append("\">");
158
159     // Print this element to our output writer
160
JspWriter JavaDoc writer = pageContext.getOut();
161     try {
162         writer.print(results.toString());
163     } catch (IOException JavaDoc e) {
164         throw new JspException JavaDoc
165         (messages.getMessage("linkSubscription.io", e.toString()));
166     }
167
168     // Evaluate the body of this tag
169
return (EVAL_BODY_INCLUDE);
170
171     }
172
173
174
175     /**
176      * Render the end of the hyperlink.
177      *
178      * @exception JspException if a JSP exception has occurred
179      */

180     public int doEndTag() throws JspException JavaDoc {
181
182
183     // Print the ending element to our output writer
184
JspWriter JavaDoc writer = pageContext.getOut();
185     try {
186         writer.print("</a>");
187     } catch (IOException JavaDoc e) {
188         throw new JspException JavaDoc
189             (messages.getMessage("link.io", e.toString()));
190     }
191
192     return (EVAL_PAGE);
193
194     }
195
196
197     /**
198      * Release any acquired resources.
199      */

200     public void release() {
201
202         super.release();
203         this.page = null;
204         this.name = "subscription";
205
206     }
207
208
209 }
210
Popular Tags