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 * Provides a convenient implementation of the ServletRequest interface that
027 * can be subclassed by developers wishing to adapt the request to a Servlet.
028 * This class implements the Wrapper or Decorator pattern. Methods default to
029 * calling through to the wrapped request object.
030 * @since v 2.3
031 *
032 *
033 *
034 * @see javax.servlet.ServletRequest
035 *
036 */
037
038 public class ServletRequestWrapper implements ServletRequest {
039 private ServletRequest request;
040
041 /**
042 * Creates a ServletRequest adaptor wrapping the given request object.
043 * @throws java.lang.IllegalArgumentException if the request is null
044 */
045
046 public ServletRequestWrapper(ServletRequest request) {
047 if (request == null) {
048 throw new IllegalArgumentException("Request cannot be null");
049 }
050 this .request = request;
051 }
052
053 /**
054 * Return the wrapped request object.
055 */
056 public ServletRequest getRequest() {
057 return this .request;
058 }
059
060 /**
061 * Sets the request object being wrapped.
062 * @throws java.lang.IllegalArgumentException if the request is null.
063 */
064
065 public void setRequest(ServletRequest request) {
066 if (request == null) {
067 throw new IllegalArgumentException("Request cannot be null");
068 }
069 this .request = request;
070 }
071
072 /**
073 *
074 * The default behavior of this method is to call getAttribute(String name)
075 * on the wrapped request object.
076 */
077
078 public Object getAttribute(String name) {
079 return this .request.getAttribute(name);
080 }
081
082 /**
083 * The default behavior of this method is to return getAttributeNames()
084 * on the wrapped request object.
085 */
086
087 public Enumeration getAttributeNames() {
088 return this .request.getAttributeNames();
089 }
090
091 /**
092 * The default behavior of this method is to return getCharacterEncoding()
093 * on the wrapped request object.
094 */
095
096 public String getCharacterEncoding() {
097 return this .request.getCharacterEncoding();
098 }
099
100 /**
101 * The default behavior of this method is to set the character encoding
102 * on the wrapped request object.
103 */
104
105 public void setCharacterEncoding(String enc)
106 throws java.io.UnsupportedEncodingException {
107 this .request.setCharacterEncoding(enc);
108 }
109
110 /**
111 * The default behavior of this method is to return getContentLength()
112 * on the wrapped request object.
113 */
114
115 public int getContentLength() {
116 return this .request.getContentLength();
117 }
118
119 /**
120 * The default behavior of this method is to return getContentType()
121 * on the wrapped request object.
122 */
123 public String getContentType() {
124 return this .request.getContentType();
125 }
126
127 /**
128 * The default behavior of this method is to return getInputStream()
129 * on the wrapped request object.
130 */
131
132 public ServletInputStream getInputStream() throws IOException {
133 return this .request.getInputStream();
134 }
135
136 /**
137 * The default behavior of this method is to return getParameter(String name)
138 * on the wrapped request object.
139 */
140
141 public String getParameter(String name) {
142 return this .request.getParameter(name);
143 }
144
145 /**
146 * The default behavior of this method is to return getParameterMap()
147 * on the wrapped request object.
148 */
149 public Map getParameterMap() {
150 return this .request.getParameterMap();
151 }
152
153 /**
154 * The default behavior of this method is to return getParameterNames()
155 * on the wrapped request object.
156 */
157
158 public Enumeration getParameterNames() {
159 return this .request.getParameterNames();
160 }
161
162 /**
163 * The default behavior of this method is to return getParameterValues(String name)
164 * on the wrapped request object.
165 */
166 public String[] getParameterValues(String name) {
167 return this .request.getParameterValues(name);
168 }
169
170 /**
171 * The default behavior of this method is to return getProtocol()
172 * on the wrapped request object.
173 */
174
175 public String getProtocol() {
176 return this .request.getProtocol();
177 }
178
179 /**
180 * The default behavior of this method is to return getScheme()
181 * on the wrapped request object.
182 */
183
184 public String getScheme() {
185 return this .request.getScheme();
186 }
187
188 /**
189 * The default behavior of this method is to return getServerName()
190 * on the wrapped request object.
191 */
192 public String getServerName() {
193 return this .request.getServerName();
194 }
195
196 /**
197 * The default behavior of this method is to return getServerPort()
198 * on the wrapped request object.
199 */
200
201 public int getServerPort() {
202 return this .request.getServerPort();
203 }
204
205 /**
206 * The default behavior of this method is to return getReader()
207 * on the wrapped request object.
208 */
209
210 public BufferedReader getReader() throws IOException {
211 return this .request.getReader();
212 }
213
214 /**
215 * The default behavior of this method is to return getRemoteAddr()
216 * on the wrapped request object.
217 */
218
219 public String getRemoteAddr() {
220 return this .request.getRemoteAddr();
221 }
222
223 /**
224 * The default behavior of this method is to return getRemoteHost()
225 * on the wrapped request object.
226 */
227
228 public String getRemoteHost() {
229 return this .request.getRemoteHost();
230 }
231
232 /**
233 * The default behavior of this method is to return setAttribute(String name, Object o)
234 * on the wrapped request object.
235 */
236
237 public void setAttribute(String name, Object o) {
238 this .request.setAttribute(name, o);
239 }
240
241 /**
242 * The default behavior of this method is to call removeAttribute(String name)
243 * on the wrapped request object.
244 */
245 public void removeAttribute(String name) {
246 this .request.removeAttribute(name);
247 }
248
249 /**
250 * The default behavior of this method is to return getLocale()
251 * on the wrapped request object.
252 */
253
254 public Locale getLocale() {
255 return this .request.getLocale();
256 }
257
258 /**
259 * The default behavior of this method is to return getLocales()
260 * on the wrapped request object.
261 */
262
263 public Enumeration getLocales() {
264 return this .request.getLocales();
265 }
266
267 /**
268 * The default behavior of this method is to return isSecure()
269 * on the wrapped request object.
270 */
271
272 public boolean isSecure() {
273 return this .request.isSecure();
274 }
275
276 /**
277 * The default behavior of this method is to return getRequestDispatcher(String path)
278 * on the wrapped request object.
279 */
280
281 public RequestDispatcher getRequestDispatcher(String path) {
282 return this .request.getRequestDispatcher(path);
283 }
284
285 /**
286 * The default behavior of this method is to return getRealPath(String path)
287 * on the wrapped request object.
288 */
289
290 public String getRealPath(String path) {
291 return this .request.getRealPath(path);
292 }
293
294 /**
295 * The default behavior of this method is to return
296 * getRemotePort() on the wrapped request object.
297 *
298 * @since 2.4
299 */
300 public int getRemotePort() {
301 return this .request.getRemotePort();
302 }
303
304 /**
305 * The default behavior of this method is to return
306 * getLocalName() on the wrapped request object.
307 *
308 * @since 2.4
309 */
310 public String getLocalName() {
311 return this .request.getLocalName();
312 }
313
314 /**
315 * The default behavior of this method is to return
316 * getLocalAddr() on the wrapped request object.
317 *
318 * @since 2.4
319 */
320 public String getLocalAddr() {
321 return this .request.getLocalAddr();
322 }
323
324 /**
325 * The default behavior of this method is to return
326 * getLocalPort() on the wrapped request object.
327 *
328 * @since 2.4
329 */
330 public int getLocalPort() {
331 return this.request.getLocalPort();
332 }
333
334 }
|