001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020 package javax.servlet;
021
022 import java.io.BufferedReader;
023 import java.io.IOException;
024 import java.util.Enumeration;
025 import java.util.Locale;
026 import java.util.Map;
027
028
029 /**
030 * Provides a convenient implementation of the ServletRequest interface that
031 * can be subclassed by developers wishing to adapt the request to a Servlet.
032 * This class implements the Wrapper or Decorator pattern. Methods default to
033 * calling through to the wrapped request object.
034 *
035 * @version $Rev: 835965 $ $Date: 2009-11-13 14:40:44 -0500 (Fri, 13 Nov 2009) $
036 * @see javax.servlet.ServletRequest
037 * @since v 2.3
038 */
039
040 public class ServletRequestWrapper implements ServletRequest {
041 private ServletRequest request;
042
043 /**
044 * Creates a ServletRequest adaptor wrapping the given request object.
045 *
046 * @param request request for this wrapper
047 * @throws java.lang.IllegalArgumentException
048 * if the request is null
049 */
050 public ServletRequestWrapper(ServletRequest request) {
051 if (request == null) {
052 throw new IllegalArgumentException("Request cannot be null");
053 }
054 this.request = request;
055 }
056
057 /**
058 * Return the wrapped request object.
059 *
060 * @return the request for this wrapper
061 */
062 public ServletRequest getRequest() {
063 return this.request;
064 }
065
066 /**
067 * Sets the request object being wrapped.
068 *
069 * @param request request to set
070 * @throws java.lang.IllegalArgumentException
071 * if the request is null.
072 */
073 public void setRequest(ServletRequest request) {
074 if (request == null) {
075 throw new IllegalArgumentException("Request cannot be null");
076 }
077 this.request = request;
078 }
079
080 /**
081 * The default behavior of this method is to call getAttribute(String name)
082 * on the wrapped request object.
083 */
084 public Object getAttribute(String name) {
085 return this.request.getAttribute(name);
086 }
087
088 /**
089 * The default behavior of this method is to return getAttributeNames()
090 * on the wrapped request object.
091 */
092 public Enumeration<String> getAttributeNames() {
093 return this.request.getAttributeNames();
094 }
095
096 /**
097 * The default behavior of this method is to return getCharacterEncoding()
098 * on the wrapped request object.
099 */
100 public String getCharacterEncoding() {
101 return this.request.getCharacterEncoding();
102 }
103
104 /**
105 * The default behavior of this method is to set the character encoding
106 * on the wrapped request object.
107 */
108 public void setCharacterEncoding(String enc) throws java.io.UnsupportedEncodingException {
109 this.request.setCharacterEncoding(enc);
110 }
111
112 /**
113 * The default behavior of this method is to return getContentLength()
114 * on the wrapped request object.
115 */
116 public int getContentLength() {
117 return this.request.getContentLength();
118 }
119
120 /**
121 * The default behavior of this method is to return getContentType()
122 * on the wrapped request object.
123 */
124 public String getContentType() {
125 return this.request.getContentType();
126 }
127
128 /**
129 * The default behavior of this method is to return getInputStream()
130 * on the wrapped request object.
131 */
132 public ServletInputStream getInputStream() throws IOException {
133 return this.request.getInputStream();
134 }
135
136 /**
137 * The default behavior of this method is to return getParameter(String name)
138 * on the wrapped request object.
139 */
140 public String getParameter(String name) {
141 return this.request.getParameter(name);
142 }
143
144 /**
145 * The default behavior of this method is to return getParameterMap()
146 * on the wrapped request object.
147 */
148 public Map<String, String[]> getParameterMap() {
149 return this.request.getParameterMap();
150 }
151
152 /**
153 * The default behavior of this method is to return getParameterNames()
154 * on the wrapped request object.
155 */
156 public Enumeration<String> getParameterNames() {
157 return this.request.getParameterNames();
158 }
159
160 /**
161 * The default behavior of this method is to return getParameterValues(String name)
162 * on the wrapped request object.
163 */
164 public String[] getParameterValues(String name) {
165 return this.request.getParameterValues(name);
166 }
167
168 /**
169 * The default behavior of this method is to return getProtocol()
170 * on the wrapped request object.
171 */
172 public String getProtocol() {
173 return this.request.getProtocol();
174 }
175
176 /**
177 * The default behavior of this method is to return getScheme()
178 * on the wrapped request object.
179 */
180 public String getScheme() {
181 return this.request.getScheme();
182 }
183
184 /**
185 * The default behavior of this method is to return getServerName()
186 * on the wrapped request object.
187 */
188 public String getServerName() {
189 return this.request.getServerName();
190 }
191
192 /**
193 * The default behavior of this method is to return getServerPort()
194 * on the wrapped request object.
195 */
196 public int getServerPort() {
197 return this.request.getServerPort();
198 }
199
200 /**
201 * The default behavior of this method is to return getReader()
202 * on the wrapped request object.
203 */
204 public BufferedReader getReader() throws IOException {
205 return this.request.getReader();
206 }
207
208 /**
209 * The default behavior of this method is to return getRemoteAddr()
210 * on the wrapped request object.
211 */
212 public String getRemoteAddr() {
213 return this.request.getRemoteAddr();
214 }
215
216 /**
217 * The default behavior of this method is to return getRemoteHost()
218 * on the wrapped request object.
219 */
220 public String getRemoteHost() {
221 return this.request.getRemoteHost();
222 }
223
224 /**
225 * The default behavior of this method is to return setAttribute(String name, Object o)
226 * on the wrapped request object.
227 */
228 public void setAttribute(String name, Object o) {
229 this.request.setAttribute(name, o);
230 }
231
232 /**
233 * The default behavior of this method is to call removeAttribute(String name)
234 * on the wrapped request object.
235 */
236 public void removeAttribute(String name) {
237 this.request.removeAttribute(name);
238 }
239
240 /**
241 * The default behavior of this method is to return getLocale()
242 * on the wrapped request object.
243 */
244 public Locale getLocale() {
245 return this.request.getLocale();
246 }
247
248 /**
249 * The default behavior of this method is to return getLocales()
250 * on the wrapped request object.
251 */
252 public Enumeration<Locale> getLocales() {
253 return this.request.getLocales();
254 }
255
256 /**
257 * The default behavior of this method is to return isSecure()
258 * on the wrapped request object.
259 */
260 public boolean isSecure() {
261 return this.request.isSecure();
262 }
263
264 /**
265 * The default behavior of this method is to return getRequestDispatcher(String path)
266 * on the wrapped request object.
267 */
268 public RequestDispatcher getRequestDispatcher(String path) {
269 return this.request.getRequestDispatcher(path);
270 }
271
272 /**
273 * The default behavior of this method is to return getRealPath(String path)
274 * on the wrapped request object.
275 */
276 public String getRealPath(String path) {
277 return this.request.getRealPath(path);
278 }
279
280 /**
281 * The default behavior of this method is to return
282 * getRemotePort() on the wrapped request object.
283 *
284 * @since 2.4
285 */
286 public int getRemotePort() {
287 return this.request.getRemotePort();
288 }
289
290 /**
291 * The default behavior of this method is to return
292 * getLocalName() on the wrapped request object.
293 *
294 * @since 2.4
295 */
296 public String getLocalName() {
297 return this.request.getLocalName();
298 }
299
300 /**
301 * The default behavior of this method is to return
302 * getLocalAddr() on the wrapped request object.
303 *
304 * @since 2.4
305 */
306 public String getLocalAddr() {
307 return this.request.getLocalAddr();
308 }
309
310 /**
311 * The default behavior of this method is to return
312 * getLocalPort() on the wrapped request object.
313 *
314 * @since 2.4
315 */
316 public int getLocalPort() {
317 return this.request.getLocalPort();
318 }
319
320 /**
321 * Get the servlet context the request-response pair was last dispatched through.
322 *
323 * @return the latest ServletContext on the dispatch chain.
324 * @since 3.0
325 */
326 public ServletContext getServletContext() {
327 return this.request.getServletContext();
328 }
329
330 public AsyncContext getAsyncContext() {
331 return this.request.getAsyncContext();
332 }
333
334 public boolean isAsyncStarted() {
335 return this.request.isAsyncStarted();
336 }
337
338 public boolean isAsyncSupported() {
339 return this.request.isAsyncSupported();
340 }
341
342 public AsyncContext startAsync() {
343 return this.request.startAsync();
344 }
345
346 public AsyncContext startAsync(ServletRequest request, ServletResponse response) {
347 return this.request.startAsync(request, response);
348 }
349
350 public DispatcherType getDispatcherType() {
351 return this.request.getDispatcherType();
352 }
353
354 public boolean isWrapperFor(Class wrappedType) {
355 if (this.request.getClass().isAssignableFrom(wrappedType)) {
356 return true;
357 }
358 if (this.request instanceof ServletRequestWrapper) {
359 return ((ServletRequestWrapper)this.request).isWrapperFor(wrappedType);
360 }
361 return false;
362 }
363
364 public boolean isWrapperFor(ServletRequest instance) {
365 if (instance == this.request) {
366 return true;
367 }
368 if (this.request instanceof ServletRequestWrapper) {
369 return ((ServletRequestWrapper)this.request).isWrapperFor(instance);
370 }
371 return false;
372 }
373
374 }
375