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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package javax.servlet;
19 import java.io.BufferedReader;
20 import java.io.IOException;
21 import java.util.Enumeration;
22 import java.util.Locale;
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.
32 * @see javax.servlet.ServletRequest
34 public class ServletRequestWrapper implements ServletRequest {
35 private ServletRequest request;
38 * Creates a ServletRequest adaptor wrapping the given request object.
40 * @throws java.lang.IllegalArgumentException
41 * if the request is null
43 public ServletRequestWrapper(ServletRequest request) {
44 if (request == null) {
45 throw new IllegalArgumentException("Request cannot be null");
47 this.request = request;
51 * Return the wrapped request object.
53 public ServletRequest getRequest() {
58 * Sets the request object being wrapped.
60 * @throws java.lang.IllegalArgumentException
61 * if the request is null.
63 public void setRequest(ServletRequest request) {
64 if (request == null) {
65 throw new IllegalArgumentException("Request cannot be null");
67 this.request = request;
71 * The default behavior of this method is to call getAttribute(String name)
72 * on the wrapped request object.
75 public Object getAttribute(String name) {
76 return this.request.getAttribute(name);
80 * The default behavior of this method is to return getAttributeNames() on
81 * the wrapped request object.
84 public Enumeration<String> getAttributeNames() {
85 return this.request.getAttributeNames();
89 * The default behavior of this method is to return getCharacterEncoding()
90 * on the wrapped request object.
93 public String getCharacterEncoding() {
94 return this.request.getCharacterEncoding();
98 * The default behavior of this method is to set the character encoding on
99 * the wrapped request object.
102 public void setCharacterEncoding(String enc)
103 throws java.io.UnsupportedEncodingException {
104 this.request.setCharacterEncoding(enc);
108 * The default behavior of this method is to return getContentLength() on
109 * the wrapped request object.
112 public int getContentLength() {
113 return this.request.getContentLength();
117 public long getContentLengthLong() {
118 return this.request.getContentLengthLong();
122 * The default behavior of this method is to return getContentType() on the
123 * wrapped request object.
126 public String getContentType() {
127 return this.request.getContentType();
131 * The default behavior of this method is to return getInputStream() on the
132 * wrapped request object.
135 public ServletInputStream getInputStream() throws IOException {
136 return this.request.getInputStream();
140 * The default behavior of this method is to return getParameter(String
141 * name) on the wrapped request object.
144 public String getParameter(String name) {
145 return this.request.getParameter(name);
149 * The default behavior of this method is to return getParameterMap() on the
150 * wrapped request object.
153 public Map<String, String[]> getParameterMap() {
154 return this.request.getParameterMap();
158 * The default behavior of this method is to return getParameterNames() on
159 * the wrapped request object.
162 public Enumeration<String> getParameterNames() {
163 return this.request.getParameterNames();
167 * The default behavior of this method is to return
168 * getParameterValues(String name) on the wrapped request object.
171 public String[] getParameterValues(String name) {
172 return this.request.getParameterValues(name);
176 * The default behavior of this method is to return getProtocol() on the
177 * wrapped request object.
180 public String getProtocol() {
181 return this.request.getProtocol();
185 * The default behavior of this method is to return getScheme() on the
186 * wrapped request object.
189 public String getScheme() {
190 return this.request.getScheme();
194 * The default behavior of this method is to return getServerName() on the
195 * wrapped request object.
198 public String getServerName() {
199 return this.request.getServerName();
203 * The default behavior of this method is to return getServerPort() on the
204 * wrapped request object.
207 public int getServerPort() {
208 return this.request.getServerPort();
212 * The default behavior of this method is to return getReader() on the
213 * wrapped request object.
216 public BufferedReader getReader() throws IOException {
217 return this.request.getReader();
221 * The default behavior of this method is to return getRemoteAddr() on the
222 * wrapped request object.
225 public String getRemoteAddr() {
226 return this.request.getRemoteAddr();
230 * The default behavior of this method is to return getRemoteHost() on the
231 * wrapped request object.
234 public String getRemoteHost() {
235 return this.request.getRemoteHost();
239 * The default behavior of this method is to return setAttribute(String
240 * name, Object o) on the wrapped request object.
243 public void setAttribute(String name, Object o) {
244 this.request.setAttribute(name, o);
248 * The default behavior of this method is to call removeAttribute(String
249 * name) on the wrapped request object.
252 public void removeAttribute(String name) {
253 this.request.removeAttribute(name);
257 * The default behavior of this method is to return getLocale() on the
258 * wrapped request object.
261 public Locale getLocale() {
262 return this.request.getLocale();
266 * The default behavior of this method is to return getLocales() on the
267 * wrapped request object.
270 public Enumeration<Locale> getLocales() {
271 return this.request.getLocales();
275 * The default behavior of this method is to return isSecure() on the
276 * wrapped request object.
279 public boolean isSecure() {
280 return this.request.isSecure();
284 * The default behavior of this method is to return
285 * getRequestDispatcher(String path) on the wrapped request object.
288 public RequestDispatcher getRequestDispatcher(String path) {
289 return this.request.getRequestDispatcher(path);
293 * The default behavior of this method is to return getRealPath(String path)
294 * on the wrapped request object.
296 * @deprecated As of Version 3.0 of the Java Servlet API
299 @SuppressWarnings("dep-ann")
300 // Spec API does not use @Deprecated
301 public String getRealPath(String path) {
302 return this.request.getRealPath(path);
306 * The default behavior of this method is to return getRemotePort() on the
307 * wrapped request object.
312 public int getRemotePort() {
313 return this.request.getRemotePort();
317 * The default behavior of this method is to return getLocalName() on the
318 * wrapped request object.
323 public String getLocalName() {
324 return this.request.getLocalName();
328 * The default behavior of this method is to return getLocalAddr() on the
329 * wrapped request object.
334 public String getLocalAddr() {
335 return this.request.getLocalAddr();
339 * The default behavior of this method is to return getLocalPort() on the
340 * wrapped request object.
345 public int getLocalPort() {
346 return this.request.getLocalPort();
350 * The default behavior of this method is to return getServletContext() on
351 * the wrapped request object.
356 public ServletContext getServletContext() {
357 return request.getServletContext();
361 * The default behavior of this method is to return startAsync() on the
362 * wrapped request object.
364 * @throws java.lang.IllegalStateException
368 public AsyncContext startAsync() throws IllegalStateException {
369 return request.startAsync();
373 * The default behavior of this method is to return startAsync(Runnable) on
374 * the wrapped request object.
376 * @param servletRequest
377 * @param servletResponse
378 * @throws java.lang.IllegalStateException
382 public AsyncContext startAsync(ServletRequest servletRequest,
383 ServletResponse servletResponse) throws IllegalStateException {
384 return request.startAsync(servletRequest, servletResponse);
388 * The default behavior of this method is to return isAsyncStarted() on the
389 * wrapped request object.
394 public boolean isAsyncStarted() {
395 return request.isAsyncStarted();
399 * The default behavior of this method is to return isAsyncSupported() on
400 * the wrapped request object.
405 public boolean isAsyncSupported() {
406 return request.isAsyncSupported();
410 * The default behavior of this method is to return getAsyncContext() on the
411 * wrapped request object.
416 public AsyncContext getAsyncContext() {
417 return request.getAsyncContext();
422 * @since Servlet 3.0 TODO SERVLET3 - Add comments
424 public boolean isWrapperFor(ServletRequest wrapped) {
425 if (request == wrapped) {
428 if (request instanceof ServletRequestWrapper) {
429 return ((ServletRequestWrapper) request).isWrapperFor(wrapped);
436 * @since Servlet 3.0 TODO SERVLET3 - Add comments
438 public boolean isWrapperFor(Class<?> wrappedType) {
439 if (wrappedType.isAssignableFrom(request.getClass())) {
442 if (request instanceof ServletRequestWrapper) {
443 return ((ServletRequestWrapper) request).isWrapperFor(wrappedType);
449 * The default behavior of this method is to call getDispatcherType() on the
450 * wrapped request object.
455 public DispatcherType getDispatcherType() {
456 return this.request.getDispatcherType();