001 /*
002 * Copyright 2004 The Apache Software Foundation
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package javax.servlet;
017
018 import java.io.BufferedReader;
019 import java.io.IOException;
020 import java.util.Enumeration;
021 import java.util.Locale;
022 import java.util.Map;
023
024
025
026 /**
027 *
028 * Provides a convenient implementation of the ServletRequest interface that
029 * can be subclassed by developers wishing to adapt the request to a Servlet.
030 * This class implements the Wrapper or Decorator pattern. Methods default to
031 * calling through to the wrapped request object.
032 * @since v 2.3
033 *
034 *
035 *
036 * @see javax.servlet.ServletRequest
037 *
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 * @throws java.lang.IllegalArgumentException if the request is null
046 */
047
048 public ServletRequestWrapper(ServletRequest request) {
049 if (request == null) {
050 throw new IllegalArgumentException("Request cannot be null");
051 }
052 this.request = request;
053 }
054
055 /**
056 * Return the wrapped request object.
057 */
058 public ServletRequest getRequest() {
059 return this.request;
060 }
061
062 /**
063 * Sets the request object being wrapped.
064 * @throws java.lang.IllegalArgumentException if the request is null.
065 */
066
067 public void setRequest(ServletRequest request) {
068 if (request == null) {
069 throw new IllegalArgumentException("Request cannot be null");
070 }
071 this.request = request;
072 }
073
074 /**
075 *
076 * The default behavior of this method is to call getAttribute(String name)
077 * on the wrapped request object.
078 */
079
080 public Object getAttribute(String name) {
081 return this.request.getAttribute(name);
082 }
083
084
085
086 /**
087 * The default behavior of this method is to return getAttributeNames()
088 * on the wrapped request object.
089 */
090
091 public Enumeration getAttributeNames() {
092 return this.request.getAttributeNames();
093 }
094
095
096
097 /**
098 * The default behavior of this method is to return getCharacterEncoding()
099 * 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