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