]> WPIA git - gigi.git/blob - lib/servlet-api/javax/servlet/ServletRequestWrapper.java
adding servlet api (from tomcat)
[gigi.git] / lib / servlet-api / javax / servlet / ServletRequestWrapper.java
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 package javax.servlet;
18
19 import java.io.BufferedReader;
20 import java.io.IOException;
21 import java.util.Enumeration;
22 import java.util.Locale;
23 import java.util.Map;
24
25 /**
26  * Provides a convenient implementation of the ServletRequest interface that can
27  * be subclassed by developers wishing to adapt the request to a Servlet. This
28  * class implements the Wrapper or Decorator pattern. Methods default to calling
29  * through to the wrapped request object.
30  *
31  * @since v 2.3
32  * @see javax.servlet.ServletRequest
33  */
34 public class ServletRequestWrapper implements ServletRequest {
35     private ServletRequest request;
36
37     /**
38      * Creates a ServletRequest adaptor wrapping the given request object.
39      *
40      * @throws java.lang.IllegalArgumentException
41      *             if the request is null
42      */
43     public ServletRequestWrapper(ServletRequest request) {
44         if (request == null) {
45             throw new IllegalArgumentException("Request cannot be null");
46         }
47         this.request = request;
48     }
49
50     /**
51      * Return the wrapped request object.
52      */
53     public ServletRequest getRequest() {
54         return this.request;
55     }
56
57     /**
58      * Sets the request object being wrapped.
59      *
60      * @throws java.lang.IllegalArgumentException
61      *             if the request is null.
62      */
63     public void setRequest(ServletRequest request) {
64         if (request == null) {
65             throw new IllegalArgumentException("Request cannot be null");
66         }
67         this.request = request;
68     }
69
70     /**
71      * The default behavior of this method is to call getAttribute(String name)
72      * on the wrapped request object.
73      */
74     @Override
75     public Object getAttribute(String name) {
76         return this.request.getAttribute(name);
77     }
78
79     /**
80      * The default behavior of this method is to return getAttributeNames() on
81      * the wrapped request object.
82      */
83     @Override
84     public Enumeration<String> getAttributeNames() {
85         return this.request.getAttributeNames();
86     }
87
88     /**
89      * The default behavior of this method is to return getCharacterEncoding()
90      * on the wrapped request object.
91      */
92     @Override
93     public String getCharacterEncoding() {
94         return this.request.getCharacterEncoding();
95     }
96
97     /**
98      * The default behavior of this method is to set the character encoding on
99      * the wrapped request object.
100      */
101     @Override
102     public void setCharacterEncoding(String enc)
103             throws java.io.UnsupportedEncodingException {
104         this.request.setCharacterEncoding(enc);
105     }
106
107     /**
108      * The default behavior of this method is to return getContentLength() on
109      * the wrapped request object.
110      */
111     @Override
112     public int getContentLength() {
113         return this.request.getContentLength();
114     }
115
116     @Override
117     public long getContentLengthLong() {
118         return this.request.getContentLengthLong();
119     }
120
121     /**
122      * The default behavior of this method is to return getContentType() on the
123      * wrapped request object.
124      */
125     @Override
126     public String getContentType() {
127         return this.request.getContentType();
128     }
129
130     /**
131      * The default behavior of this method is to return getInputStream() on the
132      * wrapped request object.
133      */
134     @Override
135     public ServletInputStream getInputStream() throws IOException {
136         return this.request.getInputStream();
137     }
138
139     /**
140      * The default behavior of this method is to return getParameter(String
141      * name) on the wrapped request object.
142      */
143     @Override
144     public String getParameter(String name) {
145         return this.request.getParameter(name);
146     }
147
148     /**
149      * The default behavior of this method is to return getParameterMap() on the
150      * wrapped request object.
151      */
152     @Override
153     public Map<String, String[]> getParameterMap() {
154         return this.request.getParameterMap();
155     }
156
157     /**
158      * The default behavior of this method is to return getParameterNames() on
159      * the wrapped request object.
160      */
161     @Override
162     public Enumeration<String> getParameterNames() {
163         return this.request.getParameterNames();
164     }
165
166     /**
167      * The default behavior of this method is to return
168      * getParameterValues(String name) on the wrapped request object.
169      */
170     @Override
171     public String[] getParameterValues(String name) {
172         return this.request.getParameterValues(name);
173     }
174
175     /**
176      * The default behavior of this method is to return getProtocol() on the
177      * wrapped request object.
178      */
179     @Override
180     public String getProtocol() {
181         return this.request.getProtocol();
182     }
183
184     /**
185      * The default behavior of this method is to return getScheme() on the
186      * wrapped request object.
187      */
188     @Override
189     public String getScheme() {
190         return this.request.getScheme();
191     }
192
193     /**
194      * The default behavior of this method is to return getServerName() on the
195      * wrapped request object.
196      */
197     @Override
198     public String getServerName() {
199         return this.request.getServerName();
200     }
201
202     /**
203      * The default behavior of this method is to return getServerPort() on the
204      * wrapped request object.
205      */
206     @Override
207     public int getServerPort() {
208         return this.request.getServerPort();
209     }
210
211     /**
212      * The default behavior of this method is to return getReader() on the
213      * wrapped request object.
214      */
215     @Override
216     public BufferedReader getReader() throws IOException {
217         return this.request.getReader();
218     }
219
220     /**
221      * The default behavior of this method is to return getRemoteAddr() on the
222      * wrapped request object.
223      */
224     @Override
225     public String getRemoteAddr() {
226         return this.request.getRemoteAddr();
227     }
228
229     /**
230      * The default behavior of this method is to return getRemoteHost() on the
231      * wrapped request object.
232      */
233     @Override
234     public String getRemoteHost() {
235         return this.request.getRemoteHost();
236     }
237
238     /**
239      * The default behavior of this method is to return setAttribute(String
240      * name, Object o) on the wrapped request object.
241      */
242     @Override
243     public void setAttribute(String name, Object o) {
244         this.request.setAttribute(name, o);
245     }
246
247     /**
248      * The default behavior of this method is to call removeAttribute(String
249      * name) on the wrapped request object.
250      */
251     @Override
252     public void removeAttribute(String name) {
253         this.request.removeAttribute(name);
254     }
255
256     /**
257      * The default behavior of this method is to return getLocale() on the
258      * wrapped request object.
259      */
260     @Override
261     public Locale getLocale() {
262         return this.request.getLocale();
263     }
264
265     /**
266      * The default behavior of this method is to return getLocales() on the
267      * wrapped request object.
268      */
269     @Override
270     public Enumeration<Locale> getLocales() {
271         return this.request.getLocales();
272     }
273
274     /**
275      * The default behavior of this method is to return isSecure() on the
276      * wrapped request object.
277      */
278     @Override
279     public boolean isSecure() {
280         return this.request.isSecure();
281     }
282
283     /**
284      * The default behavior of this method is to return
285      * getRequestDispatcher(String path) on the wrapped request object.
286      */
287     @Override
288     public RequestDispatcher getRequestDispatcher(String path) {
289         return this.request.getRequestDispatcher(path);
290     }
291
292     /**
293      * The default behavior of this method is to return getRealPath(String path)
294      * on the wrapped request object.
295      *
296      * @deprecated As of Version 3.0 of the Java Servlet API
297      */
298     @Override
299     @SuppressWarnings("dep-ann")
300     // Spec API does not use @Deprecated
301     public String getRealPath(String path) {
302         return this.request.getRealPath(path);
303     }
304
305     /**
306      * The default behavior of this method is to return getRemotePort() on the
307      * wrapped request object.
308      *
309      * @since 2.4
310      */
311     @Override
312     public int getRemotePort() {
313         return this.request.getRemotePort();
314     }
315
316     /**
317      * The default behavior of this method is to return getLocalName() on the
318      * wrapped request object.
319      *
320      * @since 2.4
321      */
322     @Override
323     public String getLocalName() {
324         return this.request.getLocalName();
325     }
326
327     /**
328      * The default behavior of this method is to return getLocalAddr() on the
329      * wrapped request object.
330      *
331      * @since 2.4
332      */
333     @Override
334     public String getLocalAddr() {
335         return this.request.getLocalAddr();
336     }
337
338     /**
339      * The default behavior of this method is to return getLocalPort() on the
340      * wrapped request object.
341      *
342      * @since 2.4
343      */
344     @Override
345     public int getLocalPort() {
346         return this.request.getLocalPort();
347     }
348
349     /**
350      * The default behavior of this method is to return getServletContext() on
351      * the wrapped request object.
352      *
353      * @since Servlet 3.0
354      */
355     @Override
356     public ServletContext getServletContext() {
357         return request.getServletContext();
358     }
359
360     /**
361      * The default behavior of this method is to return startAsync() on the
362      * wrapped request object.
363      *
364      * @throws java.lang.IllegalStateException
365      * @since Servlet 3.0
366      */
367     @Override
368     public AsyncContext startAsync() throws IllegalStateException {
369         return request.startAsync();
370     }
371
372     /**
373      * The default behavior of this method is to return startAsync(Runnable) on
374      * the wrapped request object.
375      *
376      * @param servletRequest
377      * @param servletResponse
378      * @throws java.lang.IllegalStateException
379      * @since Servlet 3.0
380      */
381     @Override
382     public AsyncContext startAsync(ServletRequest servletRequest,
383             ServletResponse servletResponse) throws IllegalStateException {
384         return request.startAsync(servletRequest, servletResponse);
385     }
386
387     /**
388      * The default behavior of this method is to return isAsyncStarted() on the
389      * wrapped request object.
390      *
391      * @since Servlet 3.0
392      */
393     @Override
394     public boolean isAsyncStarted() {
395         return request.isAsyncStarted();
396     }
397
398     /**
399      * The default behavior of this method is to return isAsyncSupported() on
400      * the wrapped request object.
401      *
402      * @since Servlet 3.0
403      */
404     @Override
405     public boolean isAsyncSupported() {
406         return request.isAsyncSupported();
407     }
408
409     /**
410      * The default behavior of this method is to return getAsyncContext() on the
411      * wrapped request object.
412      *
413      * @since Servlet 3.0
414      */
415     @Override
416     public AsyncContext getAsyncContext() {
417         return request.getAsyncContext();
418     }
419
420     /**
421      * @param wrapped
422      * @since Servlet 3.0 TODO SERVLET3 - Add comments
423      */
424     public boolean isWrapperFor(ServletRequest wrapped) {
425         if (request == wrapped) {
426             return true;
427         }
428         if (request instanceof ServletRequestWrapper) {
429             return ((ServletRequestWrapper) request).isWrapperFor(wrapped);
430         }
431         return false;
432     }
433
434     /**
435      * @param wrappedType
436      * @since Servlet 3.0 TODO SERVLET3 - Add comments
437      */
438     public boolean isWrapperFor(Class<?> wrappedType) {
439         if (wrappedType.isAssignableFrom(request.getClass())) {
440             return true;
441         }
442         if (request instanceof ServletRequestWrapper) {
443             return ((ServletRequestWrapper) request).isWrapperFor(wrappedType);
444         }
445         return false;
446     }
447
448     /**
449      * The default behavior of this method is to call getDispatcherType() on the
450      * wrapped request object.
451      *
452      * @since Servlet 3.0
453      */
454     @Override
455     public DispatcherType getDispatcherType() {
456         return this.request.getDispatcherType();
457     }
458 }