1 /**
2 *
3 * Copyright 2003-2004 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * 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
20
21
22
23
24 package javax.servlet;
25
26 import java.io.BufferedReader;
27 import java.io.IOException;
28 import java.util.Enumeration;
29 import java.util.Locale;
30 import java.util.Map;
31
32 /**
33 * Provides a convenient implementation of the ServletRequest interface that
34 * can be subclassed by developers wishing to adapt the request to a Servlet.
35 * This class implements the Wrapper or Decorator pattern. Methods default to
36 * calling through to the wrapped request object.
37 *
38 * @since Servlet 2.3
39 *
40 * @see ServletRequest
41 *
42 * @version $Rev: 46019 $ $Date: 2004-09-14 02:56:06 -0700 (Tue, 14 Sep 2004) $
43 */
44 public class ServletRequestWrapper implements ServletRequest {
45 private ServletRequest request;
46
47 /**
48 * Creates a ServletRequest adaptor wrapping the given request object.
49 * @throws java.lang.IllegalArgumentException if the request is null
50 */
51 public ServletRequestWrapper(ServletRequest request) {
52 if (request == null) {
53 throw new IllegalArgumentException("Request cannot be null");
54 }
55 this.request = request;
56 }
57
58 /**
59 * Return the wrapped request object.
60 */
61 public ServletRequest getRequest() {
62 return this.request;
63 }
64
65 /**
66 * Sets the request object being wrapped.
67 * @throws java.lang.IllegalArgumentException if the request is null.
68 */
69 public void setRequest(ServletRequest request) {
70 if (request == null) {
71 throw new IllegalArgumentException("Request cannot be null");
72 }
73 this.request = request;
74 }
75
76 /**
77 * The default behavior of this method is to call getAttribute(String name)
78 * on the wrapped request object.
79 */
80 public Object getAttribute(String name) {
81 return this.request.getAttribute(name);
82 }
83
84 /**
85 * The default behavior of this method is to return getAttributeNames()
86 * on the wrapped request object.
87 */
88 public Enumeration getAttributeNames() {
89 return this.request.getAttributeNames();
90 }
91
92 /**
93 * The default behavior of this method is to return getCharacterEncoding()
94 * on the wrapped request object.
95 */
96 public String getCharacterEncoding() {
97 return this.request.getCharacterEncoding();
98 }
99
100 /**
101 * The default behavior of this method is to set the character encoding
102 * on the wrapped request object.
103 */
104 public void setCharacterEncoding(String enc) throws java.io.UnsupportedEncodingException {
105 this.request.setCharacterEncoding(enc);
106 }
107
108 /**
109 * The default behavior of this method is to return getContentLength()
110 * on the wrapped request object.
111 */
112 public int getContentLength() {
113 return this.request.getContentLength();
114 }
115
116 /**
117 * The default behavior of this method is to return getContentType()
118 * on the wrapped request object.
119 */
120 public String getContentType() {
121 return this.request.getContentType();
122 }
123
124 /**
125 * The default behavior of this method is to return getInputStream()
126 * on the wrapped request object.
127 */
128
129 public ServletInputStream getInputStream() throws IOException {
130 return this.request.getInputStream();
131 }
132
133 /**
134 * The default behavior of this method is to return getParameter(String name)
135 * on the wrapped request object.
136 */
137 public String getParameter(String name) {
138 return this.request.getParameter(name);
139 }
140
141 /**
142 * The default behavior of this method is to return getParameterMap()
143 * on the wrapped request object.
144 */
145 public Map getParameterMap() {
146 return this.request.getParameterMap();
147 }
148
149 /**
150 * The default behavior of this method is to return getParameterNames()
151 * on the wrapped request object.
152 */
153 public Enumeration getParameterNames() {
154 return this.request.getParameterNames();
155 }
156
157 /**
158 * The default behavior of this method is to return getParameterValues(String name)
159 * on the wrapped request object.
160 */
161 public String[] getParameterValues(String name) {
162 return this.request.getParameterValues(name);
163 }
164
165 /**
166 * The default behavior of this method is to return getProtocol()
167 * on the wrapped request object.
168 */
169 public String getProtocol() {
170 return this.request.getProtocol();
171 }
172
173 /**
174 * The default behavior of this method is to return getScheme()
175 * on the wrapped request object.
176 */
177 public String getScheme() {
178 return this.request.getScheme();
179 }
180
181 /**
182 * The default behavior of this method is to return getServerName()
183 * on the wrapped request object.
184 */
185 public String getServerName() {
186 return this.request.getServerName();
187 }
188
189 /**
190 * The default behavior of this method is to return getServerPort()
191 * on the wrapped request object.
192 */
193 public int getServerPort() {
194 return this.request.getServerPort();
195 }
196
197 /**
198 * The default behavior of this method is to return getReader()
199 * on the wrapped request object.
200 */
201 public BufferedReader getReader() throws IOException {
202 return this.request.getReader();
203 }
204
205 /**
206 * The default behavior of this method is to return getRemoteAddr()
207 * on the wrapped request object.
208 */
209 public String getRemoteAddr() {
210 return this.request.getRemoteAddr();
211 }
212
213 /**
214 * The default behavior of this method is to return getRemoteHost()
215 * on the wrapped request object.
216 */
217 public String getRemoteHost() {
218 return this.request.getRemoteHost();
219 }
220
221 /**
222 * The default behavior of this method is to return setAttribute(String name, Object o)
223 * on the wrapped request object.
224 */
225 public void setAttribute(String name, Object o) {
226 this.request.setAttribute(name, o);
227 }
228
229 /**
230 * The default behavior of this method is to call removeAttribute(String name)
231 * on the wrapped request object.
232 */
233 public void removeAttribute(String name) {
234 this.request.removeAttribute(name);
235 }
236
237 /**
238 * The default behavior of this method is to return getLocale()
239 * on the wrapped request object.
240 */
241 public Locale getLocale() {
242 return this.request.getLocale();
243 }
244
245 /**
246 * The default behavior of this method is to return getLocales()
247 * on the wrapped request object.
248 */
249 public Enumeration getLocales() {
250 return this.request.getLocales();
251 }
252
253 /**
254 * The default behavior of this method is to return isSecure()
255 * on the wrapped request object.
256 */
257 public boolean isSecure() {
258 return this.request.isSecure();
259 }
260
261 /**
262 * The default behavior of this method is to return getRequestDispatcher(String path)
263 * on the wrapped request object.
264 */
265 public RequestDispatcher getRequestDispatcher(String path) {
266 return this.request.getRequestDispatcher(path);
267 }
268
269 /**
270 * The default behavior of this method is to return getRealPath(String path)
271 * on the wrapped request object.
272 */
273 public String getRealPath(String path) {
274 return this.request.getRealPath(path);
275 }
276
277 /**
278 * The default behavior of this method is to return
279 * getRemotePort() on the wrapped request object.
280 *
281 * @since Servlet 2.4
282 */
283 public int getRemotePort() {
284 return this.request.getRemotePort();
285 }
286
287 /**
288 * The default behavior of this method is to return
289 * getLocalName() on the wrapped request object.
290 *
291 * @since Servlet 2.4
292 */
293 public String getLocalName() {
294 return this.request.getLocalName();
295 }
296
297 /**
298 * The default behavior of this method is to return
299 * getLocalAddr() on the wrapped request object.
300 *
301 * @since Servlet 2.4
302 */
303 public String getLocalAddr() {
304 return this.request.getLocalAddr();
305 }
306
307 /**
308 * The default behavior of this method is to return
309 * getLocalPort() on the wrapped request object.
310 *
311 * @since Servlet 2.4
312 */
313 public int getLocalPort() {
314 return this.request.getLocalPort();
315 }
316 }
317