001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Portions Copyright Apache Software Foundation.
007: *
008: * The contents of this file are subject to the terms of either the GNU
009: * General Public License Version 2 only ("GPL") or the Common Development
010: * and Distribution License("CDDL") (collectively, the "License"). You
011: * may not use this file except in compliance with the License. You can obtain
012: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
013: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
014: * language governing permissions and limitations under the License.
015: *
016: * When distributing the software, include this License Header Notice in each
017: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
018: * Sun designates this particular file as subject to the "Classpath" exception
019: * as provided by Sun in the GPL Version 2 section of the License file that
020: * accompanied this code. If applicable, add the following below the License
021: * Header, with the fields enclosed by brackets [] replaced by your own
022: * identifying information: "Portions Copyrighted [year]
023: * [name of copyright owner]"
024: *
025: * Contributor(s):
026: *
027: * If you wish your version of this file to be governed by only the CDDL or
028: * only the GPL Version 2, indicate your decision by adding "[Contributor]
029: * elects to include this software in this distribution under the [CDDL or GPL
030: * Version 2] license." If you don't indicate a single choice of license, a
031: * recipient has the option to distribute your version of this file under
032: * either the CDDL, the GPL Version 2 or to extend the choice of license to
033: * its licensees as provided above. However, if you add GPL Version 2 code
034: * and therefore, elected the GPL Version 2 license, then the option applies
035: * only if the new code is made subject to such option by the copyright
036: * holder.
037: */
038:
039: package org.apache.taglibs.standard.tag.common.core;
040:
041: import javax.servlet.http.HttpServletRequest;
042: import javax.servlet.http.HttpServletResponse;
043: import javax.servlet.jsp.JspException;
044: import javax.servlet.jsp.JspTagException;
045: import javax.servlet.jsp.PageContext;
046: import javax.servlet.jsp.tagext.BodyTagSupport;
047:
048: import org.apache.taglibs.standard.resources.Resources;
049:
050: /**
051: * <p>Support for tag handlers for <url>, the URL creation
052: * and rewriting tag in JSTL 1.0.</p>
053: *
054: * @author Shawn Bayern
055: */
056:
057: public abstract class UrlSupport extends BodyTagSupport implements
058: ParamParent {
059:
060: //*********************************************************************
061: // Protected state
062:
063: protected String value; // 'value' attribute
064: protected String context; // 'context' attribute
065:
066: //*********************************************************************
067: // Private state
068:
069: private String var; // 'var' attribute
070: private int scope; // processed 'scope' attr
071: private ParamSupport.ParamManager params; // added parameters
072:
073: //*********************************************************************
074: // Constructor and initialization
075:
076: public UrlSupport() {
077: super ();
078: init();
079: }
080:
081: private void init() {
082: value = var = null;
083: params = null;
084: context = null;
085: scope = PageContext.PAGE_SCOPE;
086: }
087:
088: //*********************************************************************
089: // Tag attributes known at translation time
090:
091: public void setVar(String var) {
092: this .var = var;
093: }
094:
095: public void setScope(String scope) {
096: this .scope = Util.getScope(scope);
097: }
098:
099: //*********************************************************************
100: // Collaboration with subtags
101:
102: // inherit Javadoc
103: public void addParameter(String name, String value) {
104: params.addParameter(name, value);
105: }
106:
107: //*********************************************************************
108: // Tag logic
109:
110: // resets any parameters that might be sent
111: public int doStartTag() throws JspException {
112: params = new ParamSupport.ParamManager();
113: return EVAL_BODY_BUFFERED;
114: }
115:
116: // gets the right value, encodes it, and prints or stores it
117: public int doEndTag() throws JspException {
118: String result; // the eventual result
119:
120: // add (already encoded) parameters
121: String baseUrl = resolveUrl(value, context, pageContext);
122: result = params.aggregateParams(baseUrl);
123:
124: // if the URL is relative, rewrite it
125: if (!ImportSupport.isAbsoluteUrl(result)) {
126: HttpServletResponse response = ((HttpServletResponse) pageContext
127: .getResponse());
128: result = response.encodeURL(result);
129: }
130:
131: // store or print the output
132: if (var != null)
133: pageContext.setAttribute(var, result, scope);
134: else {
135: try {
136: pageContext.getOut().print(result);
137: } catch (java.io.IOException ex) {
138: throw new JspTagException(ex.toString(), ex);
139: }
140: }
141:
142: return EVAL_PAGE;
143: }
144:
145: // Releases any resources we may have (or inherit)
146: public void release() {
147: init();
148: }
149:
150: //*********************************************************************
151: // Utility methods
152:
153: public static String resolveUrl(String url, String context,
154: PageContext pageContext) throws JspException {
155: // don't touch absolute URLs
156: if (ImportSupport.isAbsoluteUrl(url))
157: return url;
158:
159: // normalize relative URLs against a context root
160: HttpServletRequest request = (HttpServletRequest) pageContext
161: .getRequest();
162: if (context == null) {
163: if (url.startsWith("/"))
164: return (request.getContextPath() + url);
165: else
166: return url;
167: } else {
168: if (!context.startsWith("/") || !url.startsWith("/")) {
169: throw new JspTagException(Resources
170: .getMessage("IMPORT_BAD_RELATIVE"));
171: }
172: if (context.equals("/")) {
173: // Don't produce string starting with '//', many
174: // browsers interpret this as host name, not as
175: // path on same host.
176: return url;
177: } else {
178: return (context + url);
179: }
180: }
181: }
182:
183: }
|