KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > taglib > core > html > RewriteUrlTag


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.html;
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>Rewrites URL. If <b>action</b> or <b>forward</b> is given, corresponding
42  * URL will be taken for rewriting, else <b>href</b> will be rewritten.
43  * 'Rewriting' means that internal URL will be prepended with context path (if
44  * it does not already contain it), supplied with RW-prefix and locale suffix
45  * (if needed). Also, if <b>anchor</b> is specified, any existing anchor will
46  * be replaced (if it does not exist, it will be appended). Some parameters
47  * may be added to query string.
48  * </p>
49  * <p>
50  * Allowed attributes are:
51  * <ul>
52  * <li>
53  * <b>action</b> - name of Action to construct URL for
54  * </li>
55  * <li>
56  * <b>forward</b> - name of Global Action forward to construct URL for
57  * </li>
58  * <li>
59  * <b>href</b> - URL to rewrite
60  * </li>
61  * <li>
62  * <b>anchor</b> - the anchor to be added to the end of the generated hyperlink
63  * </li>
64  * <li>
65  * <b>transaction</b> - whether to include transaction token (if any) in the
66  * URL. May be "false" or "true", default is "false"
67  * </li>
68  * <li>
69  * <b>addLocaleSuffix</b> - whether to add locale suffix before extension. May
70  * be "false" or "true", default is "true".
71  * </li>
72  * <li>
73  * <b>var</b> - name of variable that will accept the result. If not specified,
74  * result will be written to page.
75  * </li>
76  * <li>
77  * <b>scope</b> - scope of variable that will accept the result. If <b>var</b>
78  * is not specified, this is ignored.
79  * </li>
80  * </ul>
81  * </p>
82  * <p>
83  * This tag may also accept parameters through &lt;atleap:param&gt; tag. They
84  * are added to resulting URL.
85  * </p>
86  * <p>
87  * Only one of <b>action</b>, <b>forward</b> or <b>href</b> must be
88  * specified.
89  * </p>
90  * <p>
91  * Here's an example:
92  * <pre>
93  * &lt;atleap:rewriteUrl action="login" var="loginUrl" scope="session"&gt;
94  * &lt;atleap:param name="error" value="true"&gt;
95  * &lt;/atleap:rewriteUrl&gt;
96  * </pre>
97  * This code will compute URL for 'login' action and save it to session scope
98  * variable named 'loginUrl'. URL will contain a parameter with name 'error'
99  * and value 'true'.
100  * </p>
101  * <p><a HREF="RewriteUrlTag.java.htm"><i>View Source</i></a></p>
102  *
103  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
104  * @version $Revision: 1.4 $ $Date: 2005/09/21 13:46:02 $
105  * @see com.blandware.atleap.webapp.taglib.core.ParamTag
106  * @jsp.tag name="rewriteUrl"
107  * body-content="scriptless"
108  */

109 public class RewriteUrlTag extends SimpleTagSupport JavaDoc implements ParamParent {
110
111     protected transient final Log log = LogFactory.getLog(RewriteUrlTag.class);
112
113     /**
114      * Name of Action to construct URL for
115      */

116     protected String JavaDoc action;
117
118     /**
119      * Name of Global Action forward to construct URL for
120      */

121     protected String JavaDoc forward;
122
123     /**
124      * HREF to rewrite
125      */

126     protected String JavaDoc href;
127
128     /**
129      * The anchor to be added to the end of the generated hyperlink.
130      */

131     protected String JavaDoc anchor;
132
133     /**
134      * Include transaction token (if any) in the URL?
135      */

136     protected Boolean JavaDoc transaction = Boolean.FALSE;
137
138     /**
139      * Whether or not to add locale suffix before extension
140      */

141     protected Boolean JavaDoc addLocaleSuffix = Boolean.TRUE;
142
143     /**
144      * Name of variable to export encoded URL
145      */

146     protected String JavaDoc var;
147
148     /**
149      * Scope to export variable to
150      */

151     protected String JavaDoc scope;
152
153     /**
154      * Local parameters
155      */

156     protected Map JavaDoc parameters = new HashMap JavaDoc();
157
158     /**
159      * Returns Action to construct URL for
160      *
161      * @return action
162      * @jsp.attribute required="false"
163      * rtexprvalue="true"
164      * type="java.lang.String"
165      * description="Name of Action to construct URL for"
166      */

167     public String JavaDoc getAction() {
168         return action;
169     }
170
171     /**
172      * Sets Action to construct URL for
173      *
174      * @param action action to set
175      */

176     public void setAction(String JavaDoc action) {
177         this.action = action;
178     }
179
180     /**
181      * Returns name of Global Action forward to construct URL for
182      *
183      * @return forward
184      * @jsp.attribute required="false"
185      * rtexprvalue="true"
186      * type="java.lang.String"
187      * description="Name of Global Action forward to construct URL for"
188      */

189     public String JavaDoc getForward() {
190         return forward;
191     }
192
193     /**
194      * Sets name of Global Action forward to construct URL for
195      *
196      * @param forward forward name to set
197      */

198     public void setForward(String JavaDoc forward) {
199         this.forward = forward;
200     }
201
202     /**
203      * Returns href that will be added to address
204      *
205      * @return href
206      * @jsp.attribute required="false"
207      * rtexprvalue="true"
208      * type="java.lang.String"
209      * description="HREF to rewrite"
210      */

211     public String JavaDoc getHref() {
212         return href;
213     }
214
215     /**
216      * Sets href that will be added to address
217      *
218      * @param href href to set
219      */

220     public void setHref(String JavaDoc href) {
221         this.href = href;
222     }
223
224     /**
225      * Returns anchor that will be added to link
226      *
227      * @return anchor
228      * @jsp.attribute required="false"
229      * rtexprvalue="true"
230      * type="java.lang.String"
231      * description="Anchor to append to the name of generated hyperlink"
232      */

233     public String JavaDoc getAnchor() {
234         return anchor;
235     }
236
237     /**
238      * Sets anchor that will be added to link
239      *
240      * @param anchor anchor to set
241      */

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

255     public Boolean JavaDoc getTransaction() {
256         return transaction;
257     }
258
259     /**
260      * Sets whether or not to include transaction token in the URL
261      *
262      * @param transaction whether or not to include transaction token in the URL
263      */

264     public void setTransaction(Boolean JavaDoc transaction) {
265         this.transaction = transaction;
266     }
267
268     /**
269      * Returns whether or not to add locale suffix to the URL
270      *
271      * @return whether to add locale suffix to the URL
272      * @jsp.attribute required="false"
273      * rtexprvalue="true"
274      * type="java.lang.Boolean"
275      * description="Whether or not to add locale suffix before extension"
276      */

277     public Boolean JavaDoc getAddLocaleSuffix() {
278         return addLocaleSuffix;
279     }
280
281     /**
282      * Sets whether or not to add locale suffix to the URL
283      *
284      * @param addLocaleSuffix whether to add locale suffix to the URL
285      */

286     public void setAddLocaleSuffix(Boolean JavaDoc addLocaleSuffix) {
287         this.addLocaleSuffix = addLocaleSuffix;
288     }
289
290     /**
291      * Returns name of variable that will accept result
292      *
293      * @return name of variable
294      * @jsp.attribute required="false"
295      * rtexprvalue="true"
296      * type="java.lang.String"
297      * description="Name of variable to export formatted date"
298      */

299     public String JavaDoc getVar() {
300         return var;
301     }
302
303     /**
304      * Sets name of variable that will accept result
305      *
306      * @param var name of variable to set
307      */

308     public void setVar(String JavaDoc var) {
309         this.var = var;
310     }
311
312     /**
313      * Returns variable scope
314      *
315      * @return variable scope
316      * @jsp.attribute required="false"
317      * rtexprvalue="true"
318      * type="java.lang.String"
319      * description="Scope to export variable to"
320      */

321     public String JavaDoc getScope() {
322         return scope;
323     }
324
325     /**
326      * Sets variable scope
327      *
328      * @param scope variable scope to set
329      */

330     public void setScope(String JavaDoc scope) {
331         this.scope = scope;
332     }
333
334     /**
335      * Processes the tag
336      *
337      * @throws JspException
338      * @throws IOException
339      */

340     public void doTag() throws JspException JavaDoc, IOException JavaDoc {
341
342         PageContext JavaDoc pageContext = (PageContext JavaDoc) getJspContext();
343         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
344         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) pageContext.getResponse();
345
346         if ( transaction == null ) {
347             transaction = Boolean.FALSE;
348         }
349
350         if ( addLocaleSuffix == null ) {
351             addLocaleSuffix = Boolean.TRUE;
352         }
353
354         // invoke body content
355
JspFragment JavaDoc body = getJspBody();
356         if ( body != null ) {
357             body.invoke(null);
358         }
359
360         // calculate transaction token if requested
361
if ( transaction.booleanValue() ) {
362             HttpSession JavaDoc session = pageContext.getSession();
363             String JavaDoc token = null;
364             if ( session != null ) {
365                 token = (String JavaDoc) session.getAttribute(Globals.TRANSACTION_TOKEN_KEY);
366             }
367
368             if ( token != null ) {
369                 parameters.put(Constants.TOKEN_KEY, token);
370             }
371         }
372
373         String JavaDoc result = null;
374         TagUtils tagUtils = TagUtils.getInstance();
375         try {
376             result = WebappUtil.computeURL(action, forward, href, anchor, parameters, request, response, true, false, addLocaleSuffix.booleanValue());
377         } catch ( MalformedURLException JavaDoc e ) {
378             tagUtils.saveException(pageContext, e);
379             throw new JspTagException JavaDoc(e);
380         }
381
382
383         if ( var != null ) {
384             int varScope = PageContext.PAGE_SCOPE;
385             if ( scope != null ) {
386                 varScope = tagUtils.getScope(scope);
387             }
388             pageContext.setAttribute(var, result, varScope);
389         } else {
390             tagUtils.write(pageContext, result);
391         }
392     }
393
394     /**
395      * @see com.blandware.atleap.webapp.taglib.core.ParamParent#addParameter(String, Object)
396      */

397     public void addParameter(String JavaDoc name, Object JavaDoc value) {
398         parameters.put(name, RequestUtil.mergeValues(parameters.get(name), value));
399     }
400
401 }
402
Popular Tags