001    /**
002     *
003     * Copyright 2003-2004 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    //
019    // This source code implements specifications defined by the Java
020    // Community Process. In order to remain compliant with the specification
021    // DO NOT add / change / or delete method signatures!
022    //
023    
024    package javax.servlet;
025    
026    import java.io.BufferedReader;
027    import java.io.IOException;
028    import java.util.Enumeration;
029    import java.util.Locale;
030    import java.util.Map;
031    
032    /**
033     * Provides a convenient implementation of the ServletRequest interface that
034     * can be subclassed by developers wishing to adapt the request to a Servlet.
035     * This class implements the Wrapper or Decorator pattern. Methods default to
036     * calling through to the wrapped request object.
037     *
038     * @since Servlet 2.3
039     *
040     * @see ServletRequest
041     *
042     * @version $Rev: 46019 $ $Date: 2004-09-14 02:56:06 -0700 (Tue, 14 Sep 2004) $
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        public ServletRequestWrapper(ServletRequest request) {
052            if (request == null) {
053                throw new IllegalArgumentException("Request cannot be null");
054            }
055            this.request = request;
056        }
057    
058        /**
059         * Return the wrapped request object.
060         */
061        public ServletRequest getRequest() {
062            return this.request;
063        }
064    
065        /**
066         * Sets the request object being wrapped.
067         * @throws java.lang.IllegalArgumentException if the request is null.
068         */
069        public void setRequest(ServletRequest request) {
070            if (request == null) {
071                throw new IllegalArgumentException("Request cannot be null");
072            }
073            this.request = request;
074        }
075    
076        /**
077         * The default behavior of this method is to call getAttribute(String name)
078         * on the wrapped request object.
079         */
080        public Object getAttribute(String name) {
081            return this.request.getAttribute(name);
082        }
083    
084        /**
085         * The default behavior of this method is to return getAttributeNames()
086         * on the wrapped request object.
087         */
088        public Enumeration getAttributeNames() {
089            return this.request.getAttributeNames();
090        }
091    
092        /**
093         * The default behavior of this method is to return getCharacterEncoding()
094         * on the wrapped request object.
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        public void setCharacterEncoding(String enc) throws java.io.UnsupportedEncodingException {
105            this.request.setCharacterEncoding(enc);
106        }
107    
108        /**
109         * The default behavior of this method is to return getContentLength()
110         * on the wrapped request object.
111         */
112        public int getContentLength() {
113            return this.request.getContentLength();
114        }
115    
116        /**
117         * The default behavior of this method is to return getContentType()
118         * on the wrapped request object.
119         */
120        public String getContentType() {
121            return this.request.getContentType();
122        }
123    
124        /**
125         * The default behavior of this method is to return getInputStream()
126         * on the wrapped request object.
127         */
128    
129        public ServletInputStream getInputStream() throws IOException {
130            return this.request.getInputStream();
131        }
132    
133        /**
134         * The default behavior of this method is to return getParameter(String name)
135         * on the wrapped request object.
136         */
137        public String getParameter(String name) {
138            return this.request.getParameter(name);
139        }
140    
141        /**
142         * The default behavior of this method is to return getParameterMap()
143         * on the wrapped request object.
144         */
145        public Map getParameterMap() {
146            return this.request.getParameterMap();
147        }
148    
149        /**
150         * The default behavior of this method is to return getParameterNames()
151         * on the wrapped request object.
152         */
153        public Enumeration getParameterNames() {
154            return this.request.getParameterNames();
155        }
156    
157        /**
158         * The default behavior of this method is to return getParameterValues(String name)
159         * on the wrapped request object.
160         */
161        public String[] getParameterValues(String name) {
162            return this.request.getParameterValues(name);
163        }
164    
165        /**
166         * The default behavior of this method is to return getProtocol()
167         * on the wrapped request object.
168         */
169        public String getProtocol() {
170            return this.request.getProtocol();
171        }
172    
173        /**
174         * The default behavior of this method is to return getScheme()
175         * on the wrapped request object.
176         */
177        public String getScheme() {
178            return this.request.getScheme();
179        }
180    
181        /**
182         * The default behavior of this method is to return getServerName()
183         * on the wrapped request object.
184         */
185        public String getServerName() {
186            return this.request.getServerName();
187        }
188    
189        /**
190         * The default behavior of this method is to return getServerPort()
191         * on the wrapped request object.
192         */
193        public int getServerPort() {
194            return this.request.getServerPort();
195        }
196    
197        /**
198         * The default behavior of this method is to return getReader()
199         * on the wrapped request object.
200         */
201        public BufferedReader getReader() throws IOException {
202            return this.request.getReader();
203        }
204    
205        /**
206         * The default behavior of this method is to return getRemoteAddr()
207         * on the wrapped request object.
208         */
209        public String getRemoteAddr() {
210            return this.request.getRemoteAddr();
211        }
212    
213        /**
214         * The default behavior of this method is to return getRemoteHost()
215         * on the wrapped request object.
216         */
217        public String getRemoteHost() {
218            return this.request.getRemoteHost();
219        }
220    
221        /**
222         * The default behavior of this method is to return setAttribute(String name, Object o)
223         * on the wrapped request object.
224         */
225        public void setAttribute(String name, Object o) {
226            this.request.setAttribute(name, o);
227        }
228    
229        /**
230         * The default behavior of this method is to call removeAttribute(String name)
231         * on the wrapped request object.
232         */
233        public void removeAttribute(String name) {
234            this.request.removeAttribute(name);
235        }
236    
237        /**
238         * The default behavior of this method is to return getLocale()
239         * on the wrapped request object.
240         */
241        public Locale getLocale() {
242            return this.request.getLocale();
243        }
244    
245        /**
246         * The default behavior of this method is to return getLocales()
247         * on the wrapped request object.
248         */
249        public Enumeration getLocales() {
250            return this.request.getLocales();
251        }
252    
253        /**
254         * The default behavior of this method is to return isSecure()
255         * on the wrapped request object.
256         */
257        public boolean isSecure() {
258            return this.request.isSecure();
259        }
260    
261        /**
262         * The default behavior of this method is to return getRequestDispatcher(String path)
263         * on the wrapped request object.
264         */
265        public RequestDispatcher getRequestDispatcher(String path) {
266            return this.request.getRequestDispatcher(path);
267        }
268    
269        /**
270         * The default behavior of this method is to return getRealPath(String path)
271         * on the wrapped request object.
272         */
273        public String getRealPath(String path) {
274            return this.request.getRealPath(path);
275        }
276    
277        /**
278         * The default behavior of this method is to return
279         * getRemotePort() on the wrapped request object.
280         *
281         * @since Servlet 2.4
282         */
283        public int getRemotePort() {
284            return this.request.getRemotePort();
285        }
286    
287        /**
288         * The default behavior of this method is to return
289         * getLocalName() on the wrapped request object.
290         *
291         * @since Servlet 2.4
292         */
293        public String getLocalName() {
294            return this.request.getLocalName();
295        }
296    
297        /**
298         * The default behavior of this method is to return
299         * getLocalAddr() on the wrapped request object.
300         *
301         * @since Servlet 2.4
302         */
303        public String getLocalAddr() {
304            return this.request.getLocalAddr();
305        }
306    
307        /**
308         * The default behavior of this method is to return
309         * getLocalPort() on the wrapped request object.
310         *
311         * @since Servlet 2.4
312         */
313        public int getLocalPort() {
314            return this.request.getLocalPort();
315        }
316    }
317