View Javadoc

1   /*
2    * $Id: RedirectTag.java 376842 2006-02-10 21:02:03Z husted $
3    *
4    * Copyright 2000-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  package org.apache.struts.taglib.logic;
19  
20  import org.apache.struts.taglib.TagUtils;
21  import org.apache.struts.util.MessageResources;
22  
23  import javax.servlet.http.HttpServletResponse;
24  import javax.servlet.jsp.JspException;
25  import javax.servlet.jsp.tagext.TagSupport;
26  
27  import java.io.IOException;
28  
29  import java.net.MalformedURLException;
30  
31  import java.util.Map;
32  
33  /***
34   * Generate a URL-encoded redirect to the specified URI.
35   *
36   * @version $Rev: 376842 $ $Date: 2004-10-16 12:38:42 -0400 (Sat, 16 Oct 2004)
37   *          $
38   */
39  public class RedirectTag extends TagSupport {
40      /***
41       * The message resources for this package.
42       */
43      protected static MessageResources messages =
44          MessageResources.getMessageResources(
45              "org.apache.struts.taglib.logic.LocalStrings");
46  
47      // ------------------------------------------------------------- Properties
48  
49      /***
50       * The anchor to be added to the end of the generated hyperlink.
51       */
52      protected String anchor = null;
53  
54      /***
55       * The logical forward name from which to retrieve the redirect URI.
56       */
57      protected String forward = null;
58  
59      /***
60       * The redirect URI.
61       */
62      protected String href = null;
63  
64      /***
65       * The JSP bean name for query parameters.
66       */
67      protected String name = null;
68  
69      /***
70       * The module-relative page URL (beginning with a slash) to which this
71       * redirect will be rendered.
72       */
73      protected String page = null;
74  
75      /***
76       * The module-relative action (beginning with a slash) which will be
77       * called by this link
78       */
79      protected String action = null;
80  
81      /***
82       * The module prefix (beginning with a slash) which will be used to find
83       * the action for this link.
84       */
85      protected String module = null;
86  
87      /***
88       * The single-parameter request parameter name to generate.
89       */
90      protected String paramId = null;
91  
92      /***
93       * The single-parameter JSP bean name.
94       */
95      protected String paramName = null;
96  
97      /***
98       * The single-parameter JSP bean property.
99       */
100     protected String paramProperty = null;
101 
102     /***
103      * The single-parameter JSP bean scope.
104      */
105     protected String paramScope = null;
106 
107     /***
108      * The JSP bean property name for query parameters.
109      */
110     protected String property = null;
111 
112     /***
113      * The scope of the bean specified by the name property, if any.
114      */
115     protected String scope = null;
116 
117     /***
118      * Include our transaction control token?
119      */
120     protected boolean transaction = false;
121 
122     /***
123      * Use character encoding from ServletResponse#getCharacterEncoding to get
124      * bytes of the url string for urlencoding?
125      */
126     protected boolean useLocalEncoding = false;
127 
128     public String getAnchor() {
129         return (this.anchor);
130     }
131 
132     public void setAnchor(String anchor) {
133         this.anchor = anchor;
134     }
135 
136     public String getForward() {
137         return (this.forward);
138     }
139 
140     public void setForward(String forward) {
141         this.forward = forward;
142     }
143 
144     public String getHref() {
145         return (this.href);
146     }
147 
148     public void setHref(String href) {
149         this.href = href;
150     }
151 
152     public String getName() {
153         return (this.name);
154     }
155 
156     public void setName(String name) {
157         this.name = name;
158     }
159 
160     public String getPage() {
161         return (this.page);
162     }
163 
164     public void setPage(String page) {
165         this.page = page;
166     }
167 
168     public String getAction() {
169         return (this.action);
170     }
171 
172     public void setAction(String action) {
173         this.action = action;
174     }
175 
176     public String getModule() {
177         return (this.module);
178     }
179 
180     public void setModule(String module) {
181         this.module = module;
182     }
183 
184     public String getParamId() {
185         return (this.paramId);
186     }
187 
188     public void setParamId(String paramId) {
189         this.paramId = paramId;
190     }
191 
192     public String getParamName() {
193         return (this.paramName);
194     }
195 
196     public void setParamName(String paramName) {
197         this.paramName = paramName;
198     }
199 
200     public String getParamProperty() {
201         return (this.paramProperty);
202     }
203 
204     public void setParamProperty(String paramProperty) {
205         this.paramProperty = paramProperty;
206     }
207 
208     public String getParamScope() {
209         return (this.paramScope);
210     }
211 
212     public void setParamScope(String paramScope) {
213         this.paramScope = paramScope;
214     }
215 
216     public String getProperty() {
217         return (this.property);
218     }
219 
220     public void setProperty(String property) {
221         this.property = property;
222     }
223 
224     public String getScope() {
225         return (this.scope);
226     }
227 
228     public void setScope(String scope) {
229         this.scope = scope;
230     }
231 
232     public boolean getTransaction() {
233         return (this.transaction);
234     }
235 
236     public void setTransaction(boolean transaction) {
237         this.transaction = transaction;
238     }
239 
240     public boolean isUseLocalEncoding() {
241         return useLocalEncoding;
242     }
243 
244     public void setUseLocalEncoding(boolean b) {
245         useLocalEncoding = b;
246     }
247 
248     // --------------------------------------------------------- Public Methods
249 
250     /***
251      * Defer generation until the end of this tag is encountered.
252      *
253      * @throws JspException if a JSP exception has occurred
254      */
255     public int doStartTag() throws JspException {
256         return (SKIP_BODY);
257     }
258 
259     /***
260      * Render the redirect and skip the remainder of this page.
261      *
262      * @throws JspException if a JSP exception has occurred
263      */
264     public int doEndTag() throws JspException {
265         this.doRedirect(this.generateRedirectURL());
266 
267         return (SKIP_PAGE);
268     }
269 
270     /***
271      * Calculate the url to redirect to.
272      *
273      * @throws JspException
274      * @since Struts 1.2
275      */
276     protected String generateRedirectURL()
277         throws JspException {
278         Map params =
279             TagUtils.getInstance().computeParameters(pageContext, paramId,
280                 paramName, paramProperty, paramScope, name, property, scope,
281                 transaction);
282 
283         String url = null;
284 
285         try {
286             url = TagUtils.getInstance().computeURLWithCharEncoding(pageContext,
287                     forward, href, page, action, module, params, anchor, true,
288                     useLocalEncoding);
289         } catch (MalformedURLException e) {
290             TagUtils.getInstance().saveException(pageContext, e);
291             throw new JspException(messages.getMessage("redirect.url",
292                     e.toString()));
293         }
294 
295         return url;
296     }
297 
298     /***
299      * Redirect to the given url converting exceptions to JspException.
300      *
301      * @param url The path to redirect to.
302      * @throws JspException
303      * @since Struts 1.2
304      */
305     protected void doRedirect(String url)
306         throws JspException {
307         HttpServletResponse response =
308             (HttpServletResponse) pageContext.getResponse();
309 
310         try {
311             response.sendRedirect(url);
312         } catch (IOException e) {
313             TagUtils.getInstance().saveException(pageContext, e);
314             throw new JspException(e.getMessage());
315         }
316     }
317 
318     /***
319      * Release any acquired resources.
320      */
321     public void release() {
322         super.release();
323         anchor = null;
324         forward = null;
325         href = null;
326         name = null;
327         page = null;
328         action = null;
329         paramId = null;
330         paramName = null;
331         paramProperty = null;
332         paramScope = null;
333         property = null;
334         scope = null;
335     }
336 }