KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > core > ApplicationRequest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 package org.apache.catalina.core;
20
21
22 import java.util.Enumeration JavaDoc;
23 import java.util.HashMap JavaDoc;
24
25 import javax.servlet.ServletRequest JavaDoc;
26 import javax.servlet.ServletRequestWrapper JavaDoc;
27
28 import org.apache.catalina.Globals;
29 import org.apache.catalina.util.Enumerator;
30 import org.apache.catalina.util.StringManager;
31
32
33 /**
34  * Wrapper around a <code>javax.servlet.ServletRequest</code>
35  * that transforms an application request object (which might be the original
36  * one passed to a servlet, or might be based on the 2.3
37  * <code>javax.servlet.ServletRequestWrapper</code> class)
38  * back into an internal <code>org.apache.catalina.Request</code>.
39  * <p>
40  * <strong>WARNING</strong>: Due to Java's lack of support for multiple
41  * inheritance, all of the logic in <code>ApplicationRequest</code> is
42  * duplicated in <code>ApplicationHttpRequest</code>. Make sure that you
43  * keep these two classes in synchronization when making changes!
44  *
45  * @author Craig R. McClanahan
46  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
47  */

48
49 class ApplicationRequest extends ServletRequestWrapper JavaDoc {
50
51
52     // ------------------------------------------------------- Static Variables
53

54
55     /**
56      * The set of attribute names that are special for request dispatchers.
57      */

58     protected static final String JavaDoc specials[] =
59     { Globals.INCLUDE_REQUEST_URI_ATTR, Globals.INCLUDE_CONTEXT_PATH_ATTR,
60       Globals.INCLUDE_SERVLET_PATH_ATTR, Globals.INCLUDE_PATH_INFO_ATTR,
61       Globals.INCLUDE_QUERY_STRING_ATTR, Globals.FORWARD_REQUEST_URI_ATTR,
62       Globals.FORWARD_CONTEXT_PATH_ATTR, Globals.FORWARD_SERVLET_PATH_ATTR,
63       Globals.FORWARD_PATH_INFO_ATTR, Globals.FORWARD_QUERY_STRING_ATTR };
64
65
66     // ----------------------------------------------------------- Constructors
67

68
69     /**
70      * Construct a new wrapped request around the specified servlet request.
71      *
72      * @param request The servlet request being wrapped
73      */

74     public ApplicationRequest(ServletRequest JavaDoc request) {
75
76         super(request);
77         setRequest(request);
78
79     }
80
81
82     // ----------------------------------------------------- Instance Variables
83

84
85     /**
86      * The request attributes for this request. This is initialized from the
87      * wrapped request, but updates are allowed.
88      */

89     protected HashMap JavaDoc attributes = new HashMap JavaDoc();
90
91
92     /**
93      * The string manager for this package.
94      */

95     protected static StringManager sm =
96         StringManager.getManager(Constants.Package);
97
98
99     // ------------------------------------------------- ServletRequest Methods
100

101
102     /**
103      * Override the <code>getAttribute()</code> method of the wrapped request.
104      *
105      * @param name Name of the attribute to retrieve
106      */

107     public Object JavaDoc getAttribute(String JavaDoc name) {
108
109         synchronized (attributes) {
110             return (attributes.get(name));
111         }
112
113     }
114
115
116     /**
117      * Override the <code>getAttributeNames()</code> method of the wrapped
118      * request.
119      */

120     public Enumeration JavaDoc getAttributeNames() {
121
122         synchronized (attributes) {
123             return (new Enumerator(attributes.keySet()));
124         }
125
126     }
127
128
129     /**
130      * Override the <code>removeAttribute()</code> method of the
131      * wrapped request.
132      *
133      * @param name Name of the attribute to remove
134      */

135     public void removeAttribute(String JavaDoc name) {
136
137         synchronized (attributes) {
138             attributes.remove(name);
139             if (!isSpecial(name))
140                 getRequest().removeAttribute(name);
141         }
142
143     }
144
145
146     /**
147      * Override the <code>setAttribute()</code> method of the
148      * wrapped request.
149      *
150      * @param name Name of the attribute to set
151      * @param value Value of the attribute to set
152      */

153     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
154
155         synchronized (attributes) {
156             attributes.put(name, value);
157             if (!isSpecial(name))
158                 getRequest().setAttribute(name, value);
159         }
160
161     }
162
163
164     // ------------------------------------------ ServletRequestWrapper Methods
165

166
167     /**
168      * Set the request that we are wrapping.
169      *
170      * @param request The new wrapped request
171      */

172     public void setRequest(ServletRequest JavaDoc request) {
173
174         super.setRequest(request);
175
176         // Initialize the attributes for this request
177
synchronized (attributes) {
178             attributes.clear();
179             Enumeration JavaDoc names = request.getAttributeNames();
180             while (names.hasMoreElements()) {
181                 String JavaDoc name = (String JavaDoc) names.nextElement();
182                 Object JavaDoc value = request.getAttribute(name);
183                 attributes.put(name, value);
184             }
185         }
186
187     }
188
189
190     // ------------------------------------------------------ Protected Methods
191

192
193     /**
194      * Is this attribute name one of the special ones that is added only for
195      * included servlets?
196      *
197      * @param name Attribute name to be tested
198      */

199     protected boolean isSpecial(String JavaDoc name) {
200
201         for (int i = 0; i < specials.length; i++) {
202             if (specials[i].equals(name))
203                 return (true);
204         }
205         return (false);
206
207     }
208
209
210 }
211
Popular Tags