001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.tags.html;
020:
021: import org.apache.beehive.netui.core.urls.FreezableMutableURI;
022: import org.apache.beehive.netui.core.urls.URIContext;
023: import org.apache.beehive.netui.core.urls.URLType;
024: import org.apache.beehive.netui.core.urls.URLRewriterService;
025: import org.apache.beehive.netui.pageflow.PageFlowUtils;
026: import org.apache.beehive.netui.pageflow.internal.URIContextFactory;
027: import org.apache.beehive.netui.tags.AbstractClassicTag;
028: import org.apache.beehive.netui.tags.rendering.TagRenderingBase;
029: import org.apache.beehive.netui.util.Bundle;
030: import org.apache.beehive.netui.util.ParamHelper;
031: import org.apache.beehive.netui.util.config.ConfigUtil;
032: import org.apache.beehive.netui.util.config.bean.UrlConfig;
033:
034: import javax.servlet.ServletContext;
035: import javax.servlet.http.HttpServletRequest;
036: import javax.servlet.http.HttpServletResponse;
037: import javax.servlet.jsp.JspException;
038: import java.net.URISyntaxException;
039: import java.util.HashMap;
040: import java.util.Map;
041:
042: /**
043: * Allow a URL to participate in rewritting. Some containers rewrite URLs.
044: * This tag will pass the URL attribute through the rewriters to generate a
045: * rewritten URL before it is output into the HTML stream.
046: * @jsptagref.tagdescription Allows a URL to participate in URL rewritting.
047: * Some containers rewrite URLs. This tag will pass the URL attribute through
048: * the rewriters to generate a rewritten URL before it is output into the HTML stream.
049: * @example In this sample, the URL attribute will be rewritten and output within
050: * the span tags. The actual value that will be written to the rendered HTML may
051: * change depending on the application container.
052: * <pre> <span>URL: <netui:rewriteURL URL="foo.do"/></span></pre>
053: * @netui:tag name="rewriteURL" description="Allows the URL Rewriter to rewrite the url attribute before it is output into the HTML stream."
054: */
055: public class RewriteURL extends AbstractClassicTag implements
056: IUrlParams {
057: private Map _params; // Any parameters to the submit
058: protected String url = null;
059:
060: /**
061: * Return the name of the Tag.
062: */
063: public String getTagName() {
064: return "RewriteURL";
065: }
066:
067: /**
068: * Sets the URL to be rewritten.
069: * @param url the value of the URL that will be rewritten.
070: * @jsptagref.attributedescription The value of the URL that will be rewritten.
071: * @jsptagref.attributesyntaxvalue <i>string_url</i>
072: * @netui:attribute required="true" rtexprvalue="true"
073: * description="The value of the URL that will be rewritten."
074: */
075: public void setURL(String url) {
076: this .url = url;
077: }
078:
079: /**
080: * This method will allow a tag that produces one or more Urls to have parameters set
081: * on the tag. The name and value should be required. The facet is optional, and
082: * allows tags producing more than one URL to have parameters set on different URLs.
083: *
084: * @param name The name of the parameter to be added to the URL.
085: * @param value The value of the parameter.
086: * @param facet The name of a facet for which the parameter should be added.
087: * @throws javax.servlet.jsp.JspException
088: */
089: public void addParameter(String name, Object value, String facet)
090: throws JspException {
091: if (_params == null) {
092: _params = new HashMap();
093: }
094: ParamHelper.addParam(_params, name, value);
095: }
096:
097: /**
098: * Render the beginning of the rewriteURL tag.
099: * @throws JspException if a JSP exception has occurred
100: */
101: public int doStartTag() throws JspException {
102: // Evaluate the body of this tag
103: return EVAL_BODY_BUFFERED;
104: }
105:
106: /**
107: * Render the end of the rewriteURL tag.
108: * @throws JspException if a JSP exception has occurred
109: */
110: public int doEndTag() throws JspException {
111: HttpServletRequest request = (HttpServletRequest) pageContext
112: .getRequest();
113: HttpServletResponse response = (HttpServletResponse) pageContext
114: .getResponse();
115: ServletContext context = pageContext.getServletContext();
116:
117: try {
118: boolean encoded = false;
119: UrlConfig urlConfig = ConfigUtil.getConfig().getUrlConfig();
120: if (urlConfig != null) {
121: encoded = !urlConfig.isUrlEncodeUrls();
122: }
123:
124: FreezableMutableURI uri = new FreezableMutableURI();
125: uri.setEncoding(response.getCharacterEncoding());
126: uri.setURI(url, encoded);
127: boolean needsToBeSecure = false;
128: if (_params != null) {
129: uri.addParameters(_params, false);
130: }
131: if (!uri.isAbsolute()
132: && PageFlowUtils.needsToBeSecure(context, request,
133: url, true)) {
134: needsToBeSecure = true;
135: }
136:
137: URLRewriterService.rewriteURL(context, request, response,
138: uri, URLType.ACTION, needsToBeSecure);
139: String key = PageFlowUtils.getURLTemplateKey(
140: URLType.ACTION, needsToBeSecure);
141: boolean forXML = TagRenderingBase.Factory.isXHTML(request);
142: URIContext uriContext = URIContextFactory
143: .getInstance(forXML);
144: String uriString = URLRewriterService.getTemplatedURL(
145: context, request, uri, key, uriContext);
146: write(response.encodeURL(uriString));
147: } catch (URISyntaxException e) {
148: // report the error...
149: String s = Bundle.getString("Tags_RewriteURL_URLException",
150: new Object[] { url, e.getMessage() });
151: registerTagError(s, e);
152: reportErrors();
153: }
154:
155: localRelease();
156: return EVAL_PAGE;
157: }
158:
159: /**
160: * Release any acquired resources.
161: */
162: protected void localRelease() {
163: super.localRelease();
164: _params = null;
165: url = null;
166: }
167:
168: }
|