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.HttpServletResponse;
042: import javax.servlet.jsp.JspException;
043: import javax.servlet.jsp.JspTagException;
044: import javax.servlet.jsp.PageContext;
045: import javax.servlet.jsp.tagext.BodyTagSupport;
046:
047: /**
048: * <p>Support for tag handlers for <redirect>, JSTL 1.0's tag
049: * for redirecting to a new URL (with optional query parameters).</p>
050: *
051: * @author Shawn Bayern
052: */
053:
054: public abstract class RedirectSupport extends BodyTagSupport implements
055: ParamParent {
056:
057: //*********************************************************************
058: // Protected state
059:
060: protected String url; // 'url' attribute
061: protected String context; // 'context' attribute
062:
063: //*********************************************************************
064: // Private state
065:
066: private String var; // 'var' attribute
067: private int scope; // processed 'scope' attr
068: private ParamSupport.ParamManager params; // added parameters
069:
070: //*********************************************************************
071: // Constructor and initialization
072:
073: public RedirectSupport() {
074: super ();
075: init();
076: }
077:
078: private void init() {
079: url = var = null;
080: params = null;
081: scope = PageContext.PAGE_SCOPE;
082: }
083:
084: //*********************************************************************
085: // Tag attributes known at translation time
086:
087: public void setVar(String var) {
088: this .var = var;
089: }
090:
091: public void setScope(String scope) {
092: this .scope = Util.getScope(scope);
093: }
094:
095: //*********************************************************************
096: // Collaboration with subtags
097:
098: // inherit Javadoc
099: public void addParameter(String name, String value) {
100: params.addParameter(name, value);
101: }
102:
103: //*********************************************************************
104: // Tag logic
105:
106: // resets any parameters that might be sent
107: public int doStartTag() throws JspException {
108: params = new ParamSupport.ParamManager();
109: return EVAL_BODY_BUFFERED;
110: }
111:
112: // gets the right value, encodes it, and prints or stores it
113: public int doEndTag() throws JspException {
114: String result; // the eventual result
115:
116: // add (already encoded) parameters
117: String baseUrl = UrlSupport.resolveUrl(url, context,
118: pageContext);
119: result = params.aggregateParams(baseUrl);
120:
121: // if the URL is relative, rewrite it with 'redirect' encoding rules
122: HttpServletResponse response = ((HttpServletResponse) pageContext
123: .getResponse());
124: if (!ImportSupport.isAbsoluteUrl(result))
125: result = response.encodeRedirectURL(result);
126:
127: // redirect!
128: try {
129: response.sendRedirect(result);
130: } catch (java.io.IOException ex) {
131: throw new JspTagException(ex.toString(), ex);
132: }
133:
134: return SKIP_PAGE;
135: }
136:
137: // Releases any resources we may have (or inherit)
138: public void release() {
139: init();
140: }
141: }
|