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