001: /*
002: * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/ApplicationRequest.java,v 1.7 2001/07/22 20:25:08 pier Exp $
003: * $Revision: 1.7 $
004: * $Date: 2001/07/22 20:25:08 $
005: *
006: * ====================================================================
007: *
008: * The Apache Software License, Version 1.1
009: *
010: * Copyright (c) 1999 The Apache Software Foundation. All rights
011: * reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions
015: * are met:
016: *
017: * 1. Redistributions of source code must retain the above copyright
018: * notice, this list of conditions and the following disclaimer.
019: *
020: * 2. Redistributions in binary form must reproduce the above copyright
021: * notice, this list of conditions and the following disclaimer in
022: * the documentation and/or other materials provided with the
023: * distribution.
024: *
025: * 3. The end-user documentation included with the redistribution, if
026: * any, must include the following acknowlegement:
027: * "This product includes software developed by the
028: * Apache Software Foundation (http://www.apache.org/)."
029: * Alternately, this acknowlegement may appear in the software itself,
030: * if and wherever such third-party acknowlegements normally appear.
031: *
032: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
033: * Foundation" must not be used to endorse or promote products derived
034: * from this software without prior written permission. For written
035: * permission, please contact apache@apache.org.
036: *
037: * 5. Products derived from this software may not be called "Apache"
038: * nor may "Apache" appear in their names without prior written
039: * permission of the Apache Group.
040: *
041: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
042: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
043: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
044: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
045: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
046: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
047: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
048: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
049: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
050: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
051: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
052: * SUCH DAMAGE.
053: * ====================================================================
054: *
055: * This software consists of voluntary contributions made by many
056: * individuals on behalf of the Apache Software Foundation. For more
057: * information on the Apache Software Foundation, please see
058: * <http://www.apache.org/>.
059: *
060: * [Additional notices, if required by prior licensing conditions]
061: *
062: */
063:
064: package org.apache.catalina.core;
065:
066: import java.io.IOException;
067: import java.util.Enumeration;
068: import java.util.HashMap;
069: import javax.servlet.ServletRequest;
070: import javax.servlet.ServletRequestWrapper;
071: import org.apache.catalina.Globals;
072: import org.apache.catalina.Request;
073: import org.apache.catalina.connector.RequestFacade;
074: import org.apache.catalina.util.Enumerator;
075: import org.apache.catalina.util.StringManager;
076:
077: /**
078: * Wrapper around a <code>javax.servlet.ServletRequest</code>
079: * that transforms an application request object (which might be the original
080: * one passed to a servlet, or might be based on the 2.3
081: * <code>javax.servlet.ServletRequestWrapper</code> class)
082: * back into an internal <code>org.apache.catalina.Request</code>.
083: * <p>
084: * <strong>WARNING</strong>: Due to Java's lack of support for multiple
085: * inheritance, all of the logic in <code>ApplicationRequest</code> is
086: * duplicated in <code>ApplicationHttpRequest</code>. Make sure that you
087: * keep these two classes in synchronization when making changes!
088: *
089: * @author Craig R. McClanahan
090: * @version $Revision: 1.7 $ $Date: 2001/07/22 20:25:08 $
091: */
092:
093: class ApplicationRequest extends ServletRequestWrapper {
094:
095: // ------------------------------------------------------- Static Variables
096:
097: /**
098: * The set of attribute names that are special for request dispatchers.
099: */
100: protected static final String specials[] = {
101: Globals.REQUEST_URI_ATTR, Globals.CONTEXT_PATH_ATTR,
102: Globals.SERVLET_PATH_ATTR, Globals.PATH_INFO_ATTR,
103: Globals.QUERY_STRING_ATTR };
104:
105: // ----------------------------------------------------------- Constructors
106:
107: /**
108: * Construct a new wrapped request around the specified servlet request.
109: *
110: * @param request The servlet request being wrapped
111: */
112: public ApplicationRequest(ServletRequest request) {
113:
114: super (request);
115: setRequest(request);
116:
117: }
118:
119: // ----------------------------------------------------- Instance Variables
120:
121: /**
122: * The request attributes for this request. This is initialized from the
123: * wrapped request, but updates are allowed.
124: */
125: protected HashMap attributes = new HashMap();
126:
127: /**
128: * The string manager for this package.
129: */
130: protected static StringManager sm = StringManager
131: .getManager(Constants.Package);
132:
133: // ------------------------------------------------- ServletRequest Methods
134:
135: /**
136: * Override the <code>getAttribute()</code> method of the wrapped request.
137: *
138: * @param name Name of the attribute to retrieve
139: */
140: public Object getAttribute(String name) {
141:
142: synchronized (attributes) {
143: return (attributes.get(name));
144: }
145:
146: }
147:
148: /**
149: * Override the <code>getAttributeNames()</code> method of the wrapped
150: * request.
151: */
152: public Enumeration getAttributeNames() {
153:
154: synchronized (attributes) {
155: return (new Enumerator(attributes.keySet()));
156: }
157:
158: }
159:
160: /**
161: * Override the <code>removeAttribute()</code> method of the
162: * wrapped request.
163: *
164: * @param name Name of the attribute to remove
165: */
166: public void removeAttribute(String name) {
167:
168: synchronized (attributes) {
169: attributes.remove(name);
170: if (!isSpecial(name))
171: getRequest().removeAttribute(name);
172: }
173:
174: }
175:
176: /**
177: * Override the <code>setAttribute()</code> method of the
178: * wrapped request.
179: *
180: * @param name Name of the attribute to set
181: * @param value Value of the attribute to set
182: */
183: public void setAttribute(String name, Object value) {
184:
185: synchronized (attributes) {
186: attributes.put(name, value);
187: if (!isSpecial(name))
188: getRequest().setAttribute(name, value);
189: }
190:
191: }
192:
193: // ------------------------------------------ ServletRequestWrapper Methods
194:
195: /**
196: * Set the request that we are wrapping.
197: *
198: * @param request The new wrapped request
199: */
200: public void setRequest(ServletRequest request) {
201:
202: super .setRequest(request);
203:
204: // Initialize the attributes for this request
205: synchronized (attributes) {
206: attributes.clear();
207: Enumeration names = request.getAttributeNames();
208: while (names.hasMoreElements()) {
209: String name = (String) names.nextElement();
210: Object value = request.getAttribute(name);
211: attributes.put(name, value);
212: }
213: }
214:
215: }
216:
217: // ------------------------------------------------------ Protected Methods
218:
219: /**
220: * Is this attribute name one of the special ones that is added only for
221: * included servlets?
222: *
223: * @param name Attribute name to be tested
224: */
225: protected boolean isSpecial(String name) {
226:
227: for (int i = 0; i < specials.length; i++) {
228: if (specials[i].equals(name))
229: return (true);
230: }
231: return (false);
232:
233: }
234:
235: }
|