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