KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > taglib > core > logic > RedirectTag


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

16 package com.blandware.atleap.webapp.taglib.core.logic;
17
18 import com.blandware.atleap.webapp.taglib.core.ParamParent;
19 import com.blandware.atleap.webapp.util.core.RequestUtil;
20 import com.blandware.atleap.webapp.util.core.WebappUtil;
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.struts.Globals;
24 import org.apache.struts.taglib.TagUtils;
25 import org.apache.struts.taglib.html.Constants;
26
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29 import javax.servlet.http.HttpSession JavaDoc;
30 import javax.servlet.jsp.JspException JavaDoc;
31 import javax.servlet.jsp.JspTagException JavaDoc;
32 import javax.servlet.jsp.PageContext JavaDoc;
33 import javax.servlet.jsp.tagext.JspFragment JavaDoc;
34 import javax.servlet.jsp.tagext.SimpleTagSupport JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.net.MalformedURLException JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.Map JavaDoc;
39
40 /**
41  * <p>Performs redirect to the URL, generated from given params. Generation is
42  * made in process called ' URL rewriting'. For its definition, see
43  * {@link com.blandware.atleap.webapp.taglib.core.html.RewriteUrlTag}.
44  * </p>
45  * <p>
46  * Allowed attributes are:
47  * <ul>
48  * <li>
49  * <b>action</b> - name of Action to construct URL for
50  * </li>
51  * <li>
52  * <b>forward</b> - name of Global Action forward to construct URL for
53  * </li>
54  * <li>
55  * <b>href</b> - URL to rewrite
56  * </li>
57  * <li>
58  * <b>anchor</b> - the anchor to be added to the end of the generated hyperlink
59  * </li>
60  * <li>
61  * <b>transaction</b> - whether to include transaction token (if any) in the
62  * URL. May be "false" or "true", default is "false"
63  * </li>
64  * <li>
65  * <b>addLocaleSuffix</b> - whether to add locale suffix before extension. May
66  * be "false" or "true", default is "true".
67  * </li>
68  * </ul>
69  * </p>
70  * <p>
71  * This tag may also accept parameters through &lt;atleap:param&gt; tag. They
72  * are added to resulting URL.
73  * </p>
74  * <p>
75  * Only one of <b>action</b>, <b>forward</b> or <b>href</b> must be
76  * specified.
77  * </p>
78  * <p>
79  * Here's an example:
80  * <pre>
81  * &lt;atleap:redirect action="login"&gt;
82  * &lt;atleap:param name="error" value="true"&gt;
83  * &lt;/atleap:redirect&gt;
84  * </pre>
85  * This code will redirect user to page corresponding to 'login' action,
86  * request will contain a parameter with name 'error' and value 'true'.
87  * </p>
88  * <p><a HREF="RedirectTag.java.htm"><i>View Source</i></a></p>
89  *
90  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
91  * @version $Revision: 1.5 $ $Date: 2005/09/21 13:46:03 $
92  * @see com.blandware.atleap.webapp.taglib.core.ParamTag
93  * @jsp.tag name="redirect"
94  * body-content="scriptless"
95  */

96 public class RedirectTag extends SimpleTagSupport JavaDoc implements ParamParent {
97
98     protected transient final Log log = LogFactory.getLog(RedirectTag.class);
99
100     /**
101      * Name of Action to construct URL for
102      */

103     protected String JavaDoc action;
104
105     /**
106      * Name of Global Action forward to construct URL for
107      */

108     protected String JavaDoc forward;
109
110     /**
111      * HREF to rewrite
112      */

113     protected String JavaDoc href;
114
115     /**
116      * The anchor to be added to the end of the generated hyperlink.
117      */

118     protected String JavaDoc anchor;
119
120     /**
121      * Include transaction token (if any) in the URL?
122      */

123     protected Boolean JavaDoc transaction = Boolean.FALSE;
124
125     /**
126      * Whether or not to add locale suffix before extension
127      */

128     protected Boolean JavaDoc addLocaleSuffix = Boolean.TRUE;
129
130     /**
131      * Local parameters
132      */

133     protected Map JavaDoc parameters = new HashMap JavaDoc();
134
135     /**
136      * Returns name of Action to construct URL for
137      *
138      * @return name of Action
139      * @jsp.attribute required="false"
140      * rtexprvalue="true"
141      * type="java.lang.String"
142      * description="Name of Action to construct URL for"
143      */

144     public String JavaDoc getAction() {
145         return action;
146     }
147
148     /**
149      * Sets name of Action to construct URL for
150      *
151      * @param action name of Action to set
152      */

153     public void setAction(String JavaDoc action) {
154         this.action = action;
155     }
156
157     /**
158      * Returns name of Global Action forward to construct URL for
159      *
160      * @return name of Global Action forward
161      * @jsp.attribute required="false"
162      * rtexprvalue="true"
163      * type="java.lang.String"
164      * description="Name of Global Action forward to construct URL for"
165      */

166     public String JavaDoc getForward() {
167         return forward;
168     }
169
170     /**
171      * Sets name of Global Action forward to construct URL for
172      *
173      * @param forward name of Global Action forward
174      */

175     public void setForward(String JavaDoc forward) {
176         this.forward = forward;
177     }
178
179     /**
180      * Returns href
181      *
182      * @return href
183      * @jsp.attribute required="false"
184      * rtexprvalue="true"
185      * type="java.lang.String"
186      * description="HREF to rewrite"
187      */

188     public String JavaDoc getHref() {
189         return href;
190     }
191
192     /**
193      * Sets href
194      *
195      * @param href href to set
196      */

197     public void setHref(String JavaDoc href) {
198         this.href = href;
199     }
200
201     /**
202      * Returns anchor that will be added to link
203      *
204      * @return anchor
205      * @jsp.attribute required="false"
206      * rtexprvalue="true"
207      * type="java.lang.String"
208      * description="Anchor to append to the name of generated hyperlink"
209      */

210     public String JavaDoc getAnchor() {
211         return anchor;
212     }
213
214     /**
215      * Sets anchor that will be added to link
216      *
217      * @param anchor anchor to set
218      */

219     public void setAnchor(String JavaDoc anchor) {
220         this.anchor = anchor;
221     }
222
223     /**
224      * Returns whether or not to include transaction token in the URL
225      *
226      * @return whether to include transaction token in the URL
227      * @jsp.attribute required="false"
228      * rtexprvalue="true"
229      * type="java.lang.String"
230      * description="Whether or not to include transaction token in the URL"
231      */

232     public Boolean JavaDoc getTransaction() {
233         return transaction;
234     }
235
236     /**
237      * Sets whether or not to include transaction token in the URL
238      *
239      * @param transaction whether to include transaction token in the URL
240      */

241     public void setTransaction(Boolean JavaDoc transaction) {
242         this.transaction = transaction;
243     }
244
245     /**
246      * Returns whether or not to include locale suffix in the URL
247      *
248      * @return whether to include locale suffix in the URL
249      * @jsp.attribute required="false"
250      * rtexprvalue="true"
251      * type="java.lang.Boolean"
252      * description="Whether or not to add locale suffix before extension"
253      */

254     public Boolean JavaDoc getAddLocaleSuffix() {
255         return addLocaleSuffix;
256     }
257
258     /**
259      * Sets whether or not to include locale suffix in the URL
260      *
261      * @param addLocaleSuffix whether to include locale suffix in the URL
262      */

263     public void setAddLocaleSuffix(Boolean JavaDoc addLocaleSuffix) {
264         this.addLocaleSuffix = addLocaleSuffix;
265     }
266
267     /**
268      * Processes the tag
269      *
270      * @throws JspException
271      * @throws IOException
272      */

273     public void doTag() throws JspException JavaDoc, IOException JavaDoc {
274
275         PageContext JavaDoc pageContext = (PageContext JavaDoc) getJspContext();
276         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
277         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) pageContext.getResponse();
278
279         if ( transaction == null ) {
280             transaction = Boolean.FALSE;
281         }
282
283         if ( addLocaleSuffix == null ) {
284             addLocaleSuffix = Boolean.TRUE;
285         }
286
287         // invoke body content
288
JspFragment JavaDoc body = getJspBody();
289         if ( body != null ) {
290             body.invoke(null);
291         }
292
293         // calculate transaction token if requested
294
if ( transaction.booleanValue() ) {
295             HttpSession JavaDoc session = pageContext.getSession();
296             String JavaDoc token = null;
297             if ( session != null ) {
298                 token = (String JavaDoc) session.getAttribute(Globals.TRANSACTION_TOKEN_KEY);
299             }
300
301             if ( token != null ) {
302                 parameters.put(Constants.TOKEN_KEY, token);
303             }
304         }
305
306         String JavaDoc result = null;
307         try {
308             result = WebappUtil.computeURL(action, forward, href, anchor, parameters, request, response, true, true, addLocaleSuffix.booleanValue());
309         } catch ( MalformedURLException JavaDoc e ) {
310             TagUtils.getInstance().saveException(pageContext, e);
311             throw new JspTagException JavaDoc(e);
312         }
313
314         // perform redirect
315
response.sendRedirect(result);
316     }
317
318     /**
319      * @see com.blandware.atleap.webapp.taglib.core.ParamParent#addParameter(String, Object)
320      */

321     public void addParameter(String JavaDoc name, Object JavaDoc value) {
322         parameters.put(name, RequestUtil.mergeValues(parameters.get(name), value));
323     }
324
325 }
326
Popular Tags