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