1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20 package javax.servlet; 21 22 import java.io.BufferedReader; 23 import java.io.IOException; 24 import java.util.Enumeration; 25 import java.util.Locale; 26 import java.util.Map; 27 28 29 30 /** 31 * 32 * Provides a convenient implementation of the ServletRequest interface that 33 * can be subclassed by developers wishing to adapt the request to a Servlet. 34 * This class implements the Wrapper or Decorator pattern. Methods default to 35 * calling through to the wrapped request object. 36 * @since v 2.3 37 * 38 * 39 * 40 * @see javax.servlet.ServletRequest 41 * 42 */ 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 52 public ServletRequestWrapper(ServletRequest request) { 53 if (request == null) { 54 throw new IllegalArgumentException("Request cannot be null"); 55 } 56 this.request = request; 57 } 58 59 /** 60 * Return the wrapped request object. 61 */ 62 public ServletRequest getRequest() { 63 return this.request; 64 } 65 66 /** 67 * Sets the request object being wrapped. 68 * @throws java.lang.IllegalArgumentException if the request is null. 69 */ 70 71 public void setRequest(ServletRequest request) { 72 if (request == null) { 73 throw new IllegalArgumentException("Request cannot be null"); 74 } 75 this.request = request; 76 } 77 78 /** 79 * 80 * The default behavior of this method is to call getAttribute(String name) 81 * on the wrapped request object. 82 */ 83 84 public Object getAttribute(String name) { 85 return this.request.getAttribute(name); 86 } 87 88 89 90 /** 91 * The default behavior of this method is to return getAttributeNames() 92 * on the wrapped request object. 93 */ 94 95 public Enumeration getAttributeNames() { 96 return this.request.getAttributeNames(); 97 } 98 99 100 101 /** 102 * The default behavior of this method is to return getCharacterEncoding() 103 * on the wrapped request object. 104 */ 105 106 public String getCharacterEncoding() { 107 return this.request.getCharacterEncoding(); 108 } 109 110 /** 111 * The default behavior of this method is to set the character encoding 112 * on the wrapped request object. 113 */ 114 115 public void setCharacterEncoding(String enc) throws java.io.UnsupportedEncodingException { 116 this.request.setCharacterEncoding(enc); 117 } 118 119 120 /** 121 * The default behavior of this method is to return getContentLength() 122 * on the wrapped request object. 123 */ 124 125 public int getContentLength() { 126 return this.request.getContentLength(); 127 } 128 129 130 131 132 /** 133 * The default behavior of this method is to return getContentType() 134 * on the wrapped request object. 135 */ 136 public String getContentType() { 137 return this.request.getContentType(); 138 } 139 140 141 142 143 /** 144 * The default behavior of this method is to return getInputStream() 145 * on the wrapped request object. 146 */ 147 148 public ServletInputStream getInputStream() throws IOException { 149 return this.request.getInputStream(); 150 } 151 152 153 154 155 /** 156 * The default behavior of this method is to return getParameter(String name) 157 * on the wrapped request object. 158 */ 159 160 public String getParameter(String name) { 161 return this.request.getParameter(name); 162 } 163 164 /** 165 * The default behavior of this method is to return getParameterMap() 166 * on the wrapped request object. 167 */ 168 public Map getParameterMap() { 169 return this.request.getParameterMap(); 170 } 171 172 173 174 175 /** 176 * The default behavior of this method is to return getParameterNames() 177 * on the wrapped request object. 178 */ 179 180 public Enumeration getParameterNames() { 181 return this.request.getParameterNames(); 182 } 183 184 185 186 187 /** 188 * The default behavior of this method is to return getParameterValues(String name) 189 * on the wrapped request object. 190 */ 191 public String[] getParameterValues(String name) { 192 return this.request.getParameterValues(name); 193 } 194 195 196 197 198 /** 199 * The default behavior of this method is to return getProtocol() 200 * on the wrapped request object. 201 */ 202 203 public String getProtocol() { 204 return this.request.getProtocol(); 205 } 206 207 208 209 210 /** 211 * The default behavior of this method is to return getScheme() 212 * on the wrapped request object. 213 */ 214 215 216 public String getScheme() { 217 return this.request.getScheme(); 218 } 219 220 221 222 223 /** 224 * The default behavior of this method is to return getServerName() 225 * on the wrapped request object. 226 */ 227 public String getServerName() { 228 return this.request.getServerName(); 229 } 230 231 232 233 234 /** 235 * The default behavior of this method is to return getServerPort() 236 * on the wrapped request object. 237 */ 238 239 public int getServerPort() { 240 return this.request.getServerPort(); 241 } 242 243 244 245 /** 246 * The default behavior of this method is to return getReader() 247 * on the wrapped request object. 248 */ 249 250 public BufferedReader getReader() throws IOException { 251 return this.request.getReader(); 252 } 253 254 255 256 257 /** 258 * The default behavior of this method is to return getRemoteAddr() 259 * on the wrapped request object. 260 */ 261 262 public String getRemoteAddr() { 263 return this.request.getRemoteAddr(); 264 } 265 266 267 268 269 /** 270 * The default behavior of this method is to return getRemoteHost() 271 * on the wrapped request object. 272 */ 273 274 public String getRemoteHost() { 275 return this.request.getRemoteHost(); 276 } 277 278 279 280 281 /** 282 * The default behavior of this method is to return setAttribute(String name, Object o) 283 * on the wrapped request object. 284 */ 285 286 public void setAttribute(String name, Object o) { 287 this.request.setAttribute(name, o); 288 } 289 290 291 292 293 /** 294 * The default behavior of this method is to call removeAttribute(String name) 295 * on the wrapped request object. 296 */ 297 public void removeAttribute(String name) { 298 this.request.removeAttribute(name); 299 } 300 301 302 303 304 /** 305 * The default behavior of this method is to return getLocale() 306 * on the wrapped request object. 307 */ 308 309 public Locale getLocale() { 310 return this.request.getLocale(); 311 } 312 313 314 315 316 /** 317 * The default behavior of this method is to return getLocales() 318 * on the wrapped request object. 319 */ 320 321 public Enumeration getLocales() { 322 return this.request.getLocales(); 323 } 324 325 326 327 328 /** 329 * The default behavior of this method is to return isSecure() 330 * on the wrapped request object. 331 */ 332 333 public boolean isSecure() { 334 return this.request.isSecure(); 335 } 336 337 338 339 340 /** 341 * The default behavior of this method is to return getRequestDispatcher(String path) 342 * on the wrapped request object. 343 */ 344 345 public RequestDispatcher getRequestDispatcher(String path) { 346 return this.request.getRequestDispatcher(path); 347 } 348 349 350 351 352 /** 353 * The default behavior of this method is to return getRealPath(String path) 354 * on the wrapped request object. 355 */ 356 357 public String getRealPath(String path) { 358 return this.request.getRealPath(path); 359 } 360 361 /** 362 * The default behavior of this method is to return 363 * getRemotePort() on the wrapped request object. 364 * 365 * @since 2.4 366 */ 367 public int getRemotePort(){ 368 return this.request.getRemotePort(); 369 } 370 371 372 /** 373 * The default behavior of this method is to return 374 * getLocalName() on the wrapped request object. 375 * 376 * @since 2.4 377 */ 378 public String getLocalName(){ 379 return this.request.getLocalName(); 380 } 381 382 /** 383 * The default behavior of this method is to return 384 * getLocalAddr() on the wrapped request object. 385 * 386 * @since 2.4 387 */ 388 public String getLocalAddr(){ 389 return this.request.getLocalAddr(); 390 } 391 392 393 /** 394 * The default behavior of this method is to return 395 * getLocalPort() on the wrapped request object. 396 * 397 * @since 2.4 398 */ 399 public int getLocalPort(){ 400 return this.request.getLocalPort(); 401 } 402 403 /* ------------------------------------------------------------ */ 404 /** 405 * @throws IOException 406 * @see javax.servlet.ServletRequest#complete() 407 */ 408 public void complete() throws IOException 409 { 410 request.complete(); 411 } 412 413 /* ------------------------------------------------------------ */ 414 /** 415 * @return 416 * @see javax.servlet.ServletRequest#isInitial() 417 */ 418 public boolean isInitial() 419 { 420 return request.isInitial(); 421 } 422 423 /* ------------------------------------------------------------ */ 424 /** 425 * @return 426 * @see javax.servlet.ServletRequest#isResumed() 427 */ 428 public boolean isResumed() 429 { 430 return request.isResumed(); 431 } 432 433 /* ------------------------------------------------------------ */ 434 /** 435 * @return 436 * @see javax.servlet.ServletRequest#isSuspended() 437 */ 438 public boolean isSuspended() 439 { 440 return request.isSuspended(); 441 } 442 443 /* ------------------------------------------------------------ */ 444 /** 445 * @return 446 * @see javax.servlet.ServletRequest#isTimeout() 447 */ 448 public boolean isTimeout() 449 { 450 return request.isTimeout(); 451 } 452 453 /* ------------------------------------------------------------ */ 454 /** 455 * @see javax.servlet.ServletRequest#resume() 456 */ 457 public void resume() 458 { 459 request.resume(); 460 } 461 462 /* ------------------------------------------------------------ */ 463 /** 464 * @see javax.servlet.ServletRequest#suspend() 465 */ 466 public void suspend() 467 { 468 request.suspend(); 469 } 470 471 /* ------------------------------------------------------------ */ 472 /** 473 * @param timeoutMs 474 * @see javax.servlet.ServletRequest#suspend(long) 475 */ 476 public void suspend(long timeoutMs) 477 { 478 request.suspend(timeoutMs); 479 } 480 481 /* ------------------------------------------------------------ */ 482 /** 483 * @return 484 * @see javax.servlet.ServletRequest#getServletContext() 485 */ 486 public ServletContext getServletContext() 487 { 488 return request.getServletContext(); 489 } 490 491 /* ------------------------------------------------------------ */ 492 /** 493 * @return 494 * @see javax.servlet.ServletRequest#getServletResponse() 495 */ 496 public ServletResponse getServletResponse() 497 { 498 return request.getServletResponse(); 499 } 500 501 502 } 503