Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/struts/taglib/logic/RedirectTag.java


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