KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > html > RewriteURL


1 /*
2  * Copyright 2004 The Apache Software Foundation.
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  * $Header:$
17  */

18 package org.apache.beehive.netui.tags.html;
19
20 import org.apache.beehive.netui.core.urls.FreezableMutableURI;
21 import org.apache.beehive.netui.core.urls.URIContext;
22 import org.apache.beehive.netui.core.urls.URLType;
23 import org.apache.beehive.netui.core.urls.URLRewriterService;
24 import org.apache.beehive.netui.pageflow.PageFlowUtils;
25 import org.apache.beehive.netui.pageflow.internal.URIContextFactory;
26 import org.apache.beehive.netui.tags.AbstractClassicTag;
27 import org.apache.beehive.netui.tags.rendering.TagRenderingBase;
28 import org.apache.beehive.netui.util.Bundle;
29 import org.apache.beehive.netui.util.ParamHelper;
30 import org.apache.beehive.netui.util.config.ConfigUtil;
31 import org.apache.beehive.netui.util.config.bean.UrlConfig;
32
33 import javax.servlet.ServletContext JavaDoc;
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
36 import javax.servlet.jsp.JspException JavaDoc;
37 import java.net.URISyntaxException JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.Map JavaDoc;
40
41 /**
42  * Allow a URL to participate in rewritting. Some containers rewrite URLs.
43  * This tag will pass the URL attribute through the rewriters to generate a
44  * rewritten URL before it is output into the HTML stream.
45  * @jsptagref.tagdescription Allows a URL to participate in URL rewritting.
46  * Some containers rewrite URLs. This tag will pass the URL attribute through
47  * the rewriters to generate a rewritten URL before it is output into the HTML stream.
48  * @example In this sample, the URL attribute will be rewritten and output within
49  * the span tags. The actual value that will be written to the rendered HTML may
50  * change depending on the application container.
51  * <pre> &lt;span>URL: &lt;netui:rewriteURL URL="foo.do"/&gt;&lt;/span></pre>
52  * @netui:tag name="rewriteURL" description="Allows the URL Rewriter to rewrite the url attribute before it is output into the HTML stream."
53  */

54 public class RewriteURL extends AbstractClassicTag implements IUrlParams
55 {
56     private Map JavaDoc _params; // Any parameters to the submit
57
protected String JavaDoc url = null;
58
59     /**
60      * Return the name of the Tag.
61      */

62     public String JavaDoc getTagName()
63     {
64         return "RewriteURL";
65     }
66
67     /**
68      * Sets the URL to be rewritten.
69      * @param url the value of the URL that will be rewritten.
70      * @jsptagref.attributedescription The value of the URL that will be rewritten.
71      * @jsptagref.databindable false
72      * @jsptagref.attributesyntaxvalue <i>string_url</i>
73      * @netui:attribute required="true" rtexprvalue="true"
74      * description="The value of the URL that will be rewritten."
75      */

76     public void setURL(String JavaDoc url)
77     {
78         this.url = url;
79     }
80
81     /**
82      * This method will allow a tag that produces one or more Urls to have parameters set
83      * on the tag. The name and value should be required. The facet is optional, and
84      * allows tags producing more than one URL to have parameters set on different URLs.
85      *
86      * @param name The name of the parameter to be added to the URL.
87      * @param value The value of the parameter.
88      * @param facet The name of a facet for which the parameter should be added.
89      * @throws javax.servlet.jsp.JspException
90      */

91     public void addParameter(String JavaDoc name, Object JavaDoc value, String JavaDoc facet)
92             throws JspException JavaDoc
93     {
94         if (_params == null) {
95             _params = new HashMap JavaDoc();
96         }
97         ParamHelper.addParam(_params, name, value);
98     }
99
100     /**
101      * Render the beginning of the rewriteURL tag.
102      * @throws JspException if a JSP exception has occurred
103      */

104     public int doStartTag() throws JspException JavaDoc
105     {
106         // Evaluate the body of this tag
107
return EVAL_BODY_BUFFERED;
108     }
109
110     /**
111      * Render the end of the rewriteURL tag.
112      * @throws JspException if a JSP exception has occurred
113      */

114     public int doEndTag() throws JspException JavaDoc
115     {
116         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
117         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) pageContext.getResponse();
118         ServletContext JavaDoc context = pageContext.getServletContext();
119
120         try {
121             boolean encoded = false;
122             UrlConfig urlConfig = ConfigUtil.getConfig().getUrlConfig();
123             if (urlConfig != null && urlConfig.isSetUrlEncodeUrls()) {
124                 encoded = !urlConfig.getUrlEncodeUrls();
125             }
126
127             FreezableMutableURI uri = new FreezableMutableURI();
128             uri.setEncoding(response.getCharacterEncoding());
129             uri.setURI(url, encoded);
130             boolean needsToBeSecure = false;
131             if (_params != null) {
132                 uri.addParameters(_params, false );
133             }
134             if (!uri.isAbsolute() && PageFlowUtils.needsToBeSecure(context, request, url, true)) {
135                 needsToBeSecure = true;
136             }
137
138             URLRewriterService.rewriteURL(context, request, response, uri, URLType.ACTION, needsToBeSecure);
139             String JavaDoc key = PageFlowUtils.getURLTemplateKey(URLType.ACTION, needsToBeSecure);
140             boolean forXML = TagRenderingBase.Factory.isXHTML(request);
141             URIContext uriContext = URIContextFactory.getInstance(forXML);
142             String JavaDoc uriString = URLRewriterService.getTemplatedURL(request, uri, key, uriContext);
143             write(response.encodeURL(uriString));
144         }
145         catch (URISyntaxException JavaDoc e) {
146             // report the error...
147
String JavaDoc s = Bundle.getString("Tags_RewriteURL_URLException",
148                     new Object JavaDoc[]{url, e.getMessage()});
149             registerTagError(s, e);
150             reportErrors();
151         }
152
153         localRelease();
154         return EVAL_PAGE;
155     }
156
157     /**
158      * Release any acquired resources.
159      */

160     protected void localRelease()
161     {
162         super.localRelease();
163         _params = null;
164         url = null;
165     }
166
167 }
168
Popular Tags