1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package javax.servlet;
17
18 import java.io.BufferedReader;
19 import java.io.IOException;
20 import java.util.Enumeration;
21 import java.util.Locale;
22 import java.util.Map;
23
24
25
26 /**
27 *
28 * Provides a convenient implementation of the ServletRequest interface that
29 * can be subclassed by developers wishing to adapt the request to a Servlet.
30 * This class implements the Wrapper or Decorator pattern. Methods default to
31 * calling through to the wrapped request object.
32 * @since v 2.3
33 *
34 *
35 *
36 * @see javax.servlet.ServletRequest
37 *
38 */
39
40 public class ServletRequestWrapper implements ServletRequest {
41 private ServletRequest request;
42
43 /**
44 * Creates a ServletRequest adaptor wrapping the given request object.
45 * @throws java.lang.IllegalArgumentException if the request is null
46 */
47
48 public ServletRequestWrapper(ServletRequest request) {
49 if (request == null) {
50 throw new IllegalArgumentException("Request cannot be null");
51 }
52 this.request = request;
53 }
54
55 /**
56 * Return the wrapped request object.
57 */
58 public ServletRequest getRequest() {
59 return this.request;
60 }
61
62 /**
63 * Sets the request object being wrapped.
64 * @throws java.lang.IllegalArgumentException if the request is null.
65 */
66
67 public void setRequest(ServletRequest request) {
68 if (request == null) {
69 throw new IllegalArgumentException("Request cannot be null");
70 }
71 this.request = request;
72 }
73
74 /**
75 *
76 * The default behavior of this method is to call getAttribute(String name)
77 * on the wrapped request object.
78 */
79
80 public Object getAttribute(String name) {
81 return this.request.getAttribute(name);
82 }
83
84
85
86 /**
87 * The default behavior of this method is to return getAttributeNames()
88 * on the wrapped request object.
89 */
90
91 public Enumeration getAttributeNames() {
92 return this.request.getAttributeNames();
93 }
94
95
96
97 /**
98 * The default behavior of this method is to return getCharacterEncoding()
99 * on the wrapped request object.
100 */
101
102 public String getCharacterEncoding() {
103 return this.request.getCharacterEncoding();
104 }
105
106 /**
107 * The default behavior of this method is to set the character encoding
108 * on the wrapped request object.
109 */
110
111 public void setCharacterEncoding(String enc) throws java.io.UnsupportedEncodingException {
112 this.request.setCharacterEncoding(enc);
113 }
114
115
116 /**
117 * The default behavior of this method is to return getContentLength()
118 * on the wrapped request object.
119 */
120
121 public int getContentLength() {
122 return this.request.getContentLength();
123 }
124
125
126
127
128 /**
129 * The default behavior of this method is to return getContentType()
130 * on the wrapped request object.
131 */
132 public String getContentType() {
133 return this.request.getContentType();
134 }
135
136
137
138
139 /**
140 * The default behavior of this method is to return getInputStream()
141 * on the wrapped request object.
142 */
143
144 public ServletInputStream getInputStream() throws IOException {
145 return this.request.getInputStream();
146 }
147
148
149
150
151 /**
152 * The default behavior of this method is to return getParameter(String name)
153 * on the wrapped request object.
154 */
155
156 public String getParameter(String name) {
157 return this.request.getParameter(name);
158 }
159
160 /**
161 * The default behavior of this method is to return getParameterMap()
162 * on the wrapped request object.
163 */
164 public Map getParameterMap() {
165 return this.request.getParameterMap();
166 }
167
168
169
170
171 /**
172 * The default behavior of this method is to return getParameterNames()
173 * on the wrapped request object.
174 */
175
176 public Enumeration getParameterNames() {
177 return this.request.getParameterNames();
178 }
179
180
181
182
183 /**
184 * The default behavior of this method is to return getParameterValues(String name)
185 * on the wrapped request object.
186 */
187 public String[] getParameterValues(String name) {
188 return this.request.getParameterValues(name);
189 }
190
191
192
193
194 /**
195 * The default behavior of this method is to return getProtocol()
196 * on the wrapped request object.
197 */
198
199 public String getProtocol() {
200 return this.request.getProtocol();
201 }
202
203
204
205
206 /**
207 * The default behavior of this method is to return getScheme()
208 * on the wrapped request object.
209 */
210
211
212 public String getScheme() {
213 return this.request.getScheme();
214 }
215
216
217
218
219 /**
220 * The default behavior of this method is to return getServerName()
221 * on the wrapped request object.
222 */
223 public String getServerName() {
224 return this.request.getServerName();
225 }
226
227
228
229
230 /**
231 * The default behavior of this method is to return getServerPort()
232 * on the wrapped request object.
233 */
234
235 public int getServerPort() {
236 return this.request.getServerPort();
237 }
238
239
240
241 /**
242 * The default behavior of this method is to return getReader()
243 * on the wrapped request object.
244 */
245
246 public BufferedReader getReader() throws IOException {
247 return this.request.getReader();
248 }
249
250
251
252
253 /**
254 * The default behavior of this method is to return getRemoteAddr()
255 * on the wrapped request object.
256 */
257
258 public String getRemoteAddr() {
259 return this.request.getRemoteAddr();
260 }
261
262
263
264
265 /**
266 * The default behavior of this method is to return getRemoteHost()
267 * on the wrapped request object.
268 */
269
270 public String getRemoteHost() {
271 return this.request.getRemoteHost();
272 }
273
274
275
276
277 /**
278 * The default behavior of this method is to return setAttribute(String name, Object o)
279 * on the wrapped request object.
280 */
281
282 public void setAttribute(String name, Object o) {
283 this.request.setAttribute(name, o);
284 }
285
286
287
288
289 /**
290 * The default behavior of this method is to call removeAttribute(String name)
291 * on the wrapped request object.
292 */
293 public void removeAttribute(String name) {
294 this.request.removeAttribute(name);
295 }
296
297
298
299
300 /**
301 * The default behavior of this method is to return getLocale()
302 * on the wrapped request object.
303 */
304
305 public Locale getLocale() {
306 return this.request.getLocale();
307 }
308
309
310
311
312 /**
313 * The default behavior of this method is to return getLocales()
314 * on the wrapped request object.
315 */
316
317 public Enumeration getLocales() {
318 return this.request.getLocales();
319 }
320
321
322
323
324 /**
325 * The default behavior of this method is to return isSecure()
326 * on the wrapped request object.
327 */
328
329 public boolean isSecure() {
330 return this.request.isSecure();
331 }
332
333
334
335
336 /**
337 * The default behavior of this method is to return getRequestDispatcher(String path)
338 * on the wrapped request object.
339 */
340
341 public RequestDispatcher getRequestDispatcher(String path) {
342 return this.request.getRequestDispatcher(path);
343 }
344
345
346
347
348 /**
349 * The default behavior of this method is to return getRealPath(String path)
350 * on the wrapped request object.
351 */
352
353 public String getRealPath(String path) {
354 return this.request.getRealPath(path);
355 }
356
357 /**
358 * The default behavior of this method is to return
359 * getRemotePort() on the wrapped request object.
360 *
361 * @since 2.4
362 */
363 public int getRemotePort(){
364 return this.request.getRemotePort();
365 }
366
367
368 /**
369 * The default behavior of this method is to return
370 * getLocalName() on the wrapped request object.
371 *
372 * @since 2.4
373 */
374 public String getLocalName(){
375 return this.request.getLocalName();
376 }
377
378 /**
379 * The default behavior of this method is to return
380 * getLocalAddr() on the wrapped request object.
381 *
382 * @since 2.4
383 */
384 public String getLocalAddr(){
385 return this.request.getLocalAddr();
386 }
387
388
389 /**
390 * The default behavior of this method is to return
391 * getLocalPort() on the wrapped request object.
392 *
393 * @since 2.4
394 */
395 public int getLocalPort(){
396 return this.request.getLocalPort();
397 }
398
399 }
400