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:
018: package org.apache.catalina.core;
019:
020: import java.util.Enumeration;
021: import java.util.HashMap;
022:
023: import javax.servlet.ServletRequest;
024: import javax.servlet.ServletRequestWrapper;
025:
026: import org.apache.catalina.Globals;
027: import org.apache.catalina.util.Enumerator;
028: import org.apache.catalina.util.StringManager;
029:
030: /**
031: * Wrapper around a <code>javax.servlet.ServletRequest</code>
032: * that transforms an application request object (which might be the original
033: * one passed to a servlet, or might be based on the 2.3
034: * <code>javax.servlet.ServletRequestWrapper</code> class)
035: * back into an internal <code>org.apache.catalina.Request</code>.
036: * <p>
037: * <strong>WARNING</strong>: Due to Java's lack of support for multiple
038: * inheritance, all of the logic in <code>ApplicationRequest</code> is
039: * duplicated in <code>ApplicationHttpRequest</code>. Make sure that you
040: * keep these two classes in synchronization when making changes!
041: *
042: * @author Craig R. McClanahan
043: * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
044: */
045:
046: class ApplicationRequest extends ServletRequestWrapper {
047:
048: // ------------------------------------------------------- Static Variables
049:
050: /**
051: * The set of attribute names that are special for request dispatchers.
052: */
053: protected static final String specials[] = {
054: Globals.INCLUDE_REQUEST_URI_ATTR,
055: Globals.INCLUDE_CONTEXT_PATH_ATTR,
056: Globals.INCLUDE_SERVLET_PATH_ATTR,
057: Globals.INCLUDE_PATH_INFO_ATTR,
058: Globals.INCLUDE_QUERY_STRING_ATTR,
059: Globals.FORWARD_REQUEST_URI_ATTR,
060: Globals.FORWARD_CONTEXT_PATH_ATTR,
061: Globals.FORWARD_SERVLET_PATH_ATTR,
062: Globals.FORWARD_PATH_INFO_ATTR,
063: Globals.FORWARD_QUERY_STRING_ATTR };
064:
065: // ----------------------------------------------------------- Constructors
066:
067: /**
068: * Construct a new wrapped request around the specified servlet request.
069: *
070: * @param request The servlet request being wrapped
071: */
072: public ApplicationRequest(ServletRequest request) {
073:
074: super (request);
075: setRequest(request);
076:
077: }
078:
079: // ----------------------------------------------------- Instance Variables
080:
081: /**
082: * The request attributes for this request. This is initialized from the
083: * wrapped request, but updates are allowed.
084: */
085: protected HashMap attributes = new HashMap();
086:
087: /**
088: * The string manager for this package.
089: */
090: protected static StringManager sm = StringManager
091: .getManager(Constants.Package);
092:
093: // ------------------------------------------------- ServletRequest Methods
094:
095: /**
096: * Override the <code>getAttribute()</code> method of the wrapped request.
097: *
098: * @param name Name of the attribute to retrieve
099: */
100: public Object getAttribute(String name) {
101:
102: synchronized (attributes) {
103: return (attributes.get(name));
104: }
105:
106: }
107:
108: /**
109: * Override the <code>getAttributeNames()</code> method of the wrapped
110: * request.
111: */
112: public Enumeration getAttributeNames() {
113:
114: synchronized (attributes) {
115: return (new Enumerator(attributes.keySet()));
116: }
117:
118: }
119:
120: /**
121: * Override the <code>removeAttribute()</code> method of the
122: * wrapped request.
123: *
124: * @param name Name of the attribute to remove
125: */
126: public void removeAttribute(String name) {
127:
128: synchronized (attributes) {
129: attributes.remove(name);
130: if (!isSpecial(name))
131: getRequest().removeAttribute(name);
132: }
133:
134: }
135:
136: /**
137: * Override the <code>setAttribute()</code> method of the
138: * wrapped request.
139: *
140: * @param name Name of the attribute to set
141: * @param value Value of the attribute to set
142: */
143: public void setAttribute(String name, Object value) {
144:
145: synchronized (attributes) {
146: attributes.put(name, value);
147: if (!isSpecial(name))
148: getRequest().setAttribute(name, value);
149: }
150:
151: }
152:
153: // ------------------------------------------ ServletRequestWrapper Methods
154:
155: /**
156: * Set the request that we are wrapping.
157: *
158: * @param request The new wrapped request
159: */
160: public void setRequest(ServletRequest request) {
161:
162: super .setRequest(request);
163:
164: // Initialize the attributes for this request
165: synchronized (attributes) {
166: attributes.clear();
167: Enumeration names = request.getAttributeNames();
168: while (names.hasMoreElements()) {
169: String name = (String) names.nextElement();
170: Object value = request.getAttribute(name);
171: attributes.put(name, value);
172: }
173: }
174:
175: }
176:
177: // ------------------------------------------------------ Protected Methods
178:
179: /**
180: * Is this attribute name one of the special ones that is added only for
181: * included servlets?
182: *
183: * @param name Attribute name to be tested
184: */
185: protected boolean isSpecial(String name) {
186:
187: for (int i = 0; i < specials.length; i++) {
188: if (specials[i].equals(name))
189: return (true);
190: }
191: return (false);
192:
193: }
194:
195: }
|