1 /*
2  * Copyright (c) 1997-2018 Oracle and/or its affiliates and others.
3  * All rights reserved.
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package javax.servlet.http;
20
21 import java.io.IOException;
22 import java.util.*;
23 import javax.servlet.ServletException;
24 import javax.servlet.ServletRequestWrapper;
25
26 /**
27  * Provides a convenient implementation of the HttpServletRequest interface that can be subclassed by developers wishing
28  * to adapt the request to a Servlet.
29  *
30  * <p>
31  * This class implements the Wrapper or Decorator pattern. Methods default to calling through to the wrapped request
32  * object.
33  * 
34  * @see javax.servlet.http.HttpServletRequest
35  * @since Servlet 2.3
36  */

37 public class HttpServletRequestWrapper extends ServletRequestWrapper implements HttpServletRequest {
38
39     /**
40      * Constructs a request object wrapping the given request.
41      *
42      * @param request the {@link HttpServletRequest} to be wrapped.
43      *
44      * @throws java.lang.IllegalArgumentException if the request is null
45      */

46     public HttpServletRequestWrapper(HttpServletRequest request) {
47         super(request);
48     }
49
50     private HttpServletRequest _getHttpServletRequest() {
51         return (HttpServletRequest) super.getRequest();
52     }
53
54     /**
55      * The default behavior of this method is to return getAuthType() on the wrapped request object.
56      */

57     @Override
58     public String getAuthType() {
59         return this._getHttpServletRequest().getAuthType();
60     }
61
62     /**
63      * The default behavior of this method is to return getCookies() on the wrapped request object.
64      */

65     @Override
66     public Cookie[] getCookies() {
67         return this._getHttpServletRequest().getCookies();
68     }
69
70     /**
71      * The default behavior of this method is to return getDateHeader(String name) on the wrapped request object.
72      */

73     @Override
74     public long getDateHeader(String name) {
75         return this._getHttpServletRequest().getDateHeader(name);
76     }
77
78     /**
79      * The default behavior of this method is to return getHeader(String name) on the wrapped request object.
80      */

81     @Override
82     public String getHeader(String name) {
83         return this._getHttpServletRequest().getHeader(name);
84     }
85
86     /**
87      * The default behavior of this method is to return getHeaders(String name) on the wrapped request object.
88      */

89     @Override
90     public Enumeration<String> getHeaders(String name) {
91         return this._getHttpServletRequest().getHeaders(name);
92     }
93
94     /**
95      * The default behavior of this method is to return getHeaderNames() on the wrapped request object.
96      */

97     @Override
98     public Enumeration<String> getHeaderNames() {
99         return this._getHttpServletRequest().getHeaderNames();
100     }
101
102     /**
103      * The default behavior of this method is to return getIntHeader(String name) on the wrapped request object.
104      */

105     @Override
106     public int getIntHeader(String name) {
107         return this._getHttpServletRequest().getIntHeader(name);
108     }
109
110     /**
111      * <p>
112      * The default behavior of this method is to return getServletMapping() on the wrapped request object.
113      * </p>
114      */

115     @Override
116     public HttpServletMapping getHttpServletMapping() {
117         return this._getHttpServletRequest().getHttpServletMapping();
118     }
119
120     /**
121      * The default behavior of this method is to return getMethod() on the wrapped request object.
122      */

123     @Override
124     public String getMethod() {
125         return this._getHttpServletRequest().getMethod();
126     }
127
128     /**
129      * The default behavior of this method is to return getPathInfo() on the wrapped request object.
130      */

131     @Override
132     public String getPathInfo() {
133         return this._getHttpServletRequest().getPathInfo();
134     }
135
136     /**
137      * The default behavior of this method is to return getPathTranslated() on the wrapped request object.
138      */

139     @Override
140     public String getPathTranslated() {
141         return this._getHttpServletRequest().getPathTranslated();
142     }
143
144     /**
145      * The default behavior of this method is to return getContextPath() on the wrapped request object.
146      */

147     @Override
148     public String getContextPath() {
149         return this._getHttpServletRequest().getContextPath();
150     }
151
152     /**
153      * The default behavior of this method is to return getQueryString() on the wrapped request object.
154      */

155     @Override
156     public String getQueryString() {
157         return this._getHttpServletRequest().getQueryString();
158     }
159
160     /**
161      * The default behavior of this method is to return getRemoteUser() on the wrapped request object.
162      */

163     @Override
164     public String getRemoteUser() {
165         return this._getHttpServletRequest().getRemoteUser();
166     }
167
168     /**
169      * The default behavior of this method is to return isUserInRole(String role) on the wrapped request object.
170      */

171     @Override
172     public boolean isUserInRole(String role) {
173         return this._getHttpServletRequest().isUserInRole(role);
174     }
175
176     /**
177      * The default behavior of this method is to return getUserPrincipal() on the wrapped request object.
178      */

179     @Override
180     public java.security.Principal getUserPrincipal() {
181         return this._getHttpServletRequest().getUserPrincipal();
182     }
183
184     /**
185      * The default behavior of this method is to return getRequestedSessionId() on the wrapped request object.
186      */

187     @Override
188     public String getRequestedSessionId() {
189         return this._getHttpServletRequest().getRequestedSessionId();
190     }
191
192     /**
193      * The default behavior of this method is to return getRequestURI() on the wrapped request object.
194      */

195     @Override
196     public String getRequestURI() {
197         return this._getHttpServletRequest().getRequestURI();
198     }
199
200     /**
201      * The default behavior of this method is to return getRequestURL() on the wrapped request object.
202      */

203     @Override
204     public StringBuffer getRequestURL() {
205         return this._getHttpServletRequest().getRequestURL();
206     }
207
208     /**
209      * The default behavior of this method is to return getServletPath() on the wrapped request object.
210      */

211     @Override
212     public String getServletPath() {
213         return this._getHttpServletRequest().getServletPath();
214     }
215
216     /**
217      * The default behavior of this method is to return getSession(boolean create) on the wrapped request object.
218      */

219     @Override
220     public HttpSession getSession(boolean create) {
221         return this._getHttpServletRequest().getSession(create);
222     }
223
224     /**
225      * The default behavior of this method is to return getSession() on the wrapped request object.
226      */

227     @Override
228     public HttpSession getSession() {
229         return this._getHttpServletRequest().getSession();
230     }
231
232     /**
233      * The default behavior of this method is to return changeSessionId() on the wrapped request object.
234      *
235      * @since Servlet 3.1
236      */

237     @Override
238     public String changeSessionId() {
239         return this._getHttpServletRequest().changeSessionId();
240     }
241
242     /**
243      * The default behavior of this method is to return isRequestedSessionIdValid() on the wrapped request object.
244      */

245     @Override
246     public boolean isRequestedSessionIdValid() {
247         return this._getHttpServletRequest().isRequestedSessionIdValid();
248     }
249
250     /**
251      * The default behavior of this method is to return isRequestedSessionIdFromCookie() on the wrapped request object.
252      */

253     @Override
254     public boolean isRequestedSessionIdFromCookie() {
255         return this._getHttpServletRequest().isRequestedSessionIdFromCookie();
256     }
257
258     /**
259      * The default behavior of this method is to return isRequestedSessionIdFromURL() on the wrapped request object.
260      */

261     @Override
262     public boolean isRequestedSessionIdFromURL() {
263         return this._getHttpServletRequest().isRequestedSessionIdFromURL();
264     }
265
266     /**
267      * The default behavior of this method is to return isRequestedSessionIdFromUrl() on the wrapped request object.
268      *
269      * @deprecated As of Version 4.0 of the Java Servlet API, use {@link #isRequestedSessionIdFromURL} instead.
270      */

271     @Deprecated
272     @Override
273     public boolean isRequestedSessionIdFromUrl() {
274         return this._getHttpServletRequest().isRequestedSessionIdFromUrl();
275     }
276
277     /**
278      * The default behavior of this method is to call authenticate on the wrapped request object.
279      *
280      * @since Servlet 3.0
281      */

282     @Override
283     public boolean authenticate(HttpServletResponse response) throws IOException, ServletException {
284         return this._getHttpServletRequest().authenticate(response);
285     }
286
287     /**
288      * The default behavior of this method is to call login on the wrapped request object.
289      *
290      * @since Servlet 3.0
291      */

292     @Override
293     public void login(String username, String password) throws ServletException {
294         this._getHttpServletRequest().login(username, password);
295     }
296
297     /**
298      * The default behavior of this method is to call login on the wrapped request object.
299      *
300      * @since Servlet 3.0
301      */

302     @Override
303     public void logout() throws ServletException {
304         this._getHttpServletRequest().logout();
305     }
306
307     /**
308      * The default behavior of this method is to call getParts on the wrapped request object.
309      *
310      * <p>
311      * Any changes to the returned <code>Collection</code> must not affect this <code>HttpServletRequestWrapper</code>.
312      *
313      * @since Servlet 3.0
314      */

315     @Override
316     public Collection<Part> getParts() throws IOException, ServletException {
317         return this._getHttpServletRequest().getParts();
318     }
319
320     /**
321      * The default behavior of this method is to call getPart on the wrapped request object.
322      *
323      * @since Servlet 3.0
324      */

325     @Override
326     public Part getPart(String name) throws IOException, ServletException {
327         return this._getHttpServletRequest().getPart(name);
328
329     }
330
331     /**
332      * Create an instance of <code>HttpUpgradeHandler</code> for a given class and uses it for the http protocol upgrade
333      * processing.
334      *
335      * @since Servlet 3.1
336      */

337     @Override
338     public <T extends HttpUpgradeHandler> T upgrade(Class<T> handlerClass) throws IOException, ServletException {
339         return this._getHttpServletRequest().upgrade(handlerClass);
340     }
341
342     /**
343      * The default behavior of this method is to call newPushBuilder on the wrapped request object.
344      *
345      * @since Servlet 4.0
346      */

347     @Override
348     public PushBuilder newPushBuilder() {
349         return this._getHttpServletRequest().newPushBuilder();
350     }
351
352     /**
353      * The default behavior of this method is to call getTrailerFields on the wrapped request object.
354      *
355      * @since Servlet 4.0
356      */

357     @Override
358     public Map<String, String> getTrailerFields() {
359         return this._getHttpServletRequest().getTrailerFields();
360     }
361
362     /**
363      * The default behavior of this method is to call isTrailerFieldsReady on the wrapped request object.
364      *
365      * @since Servlet 4.0
366      */

367     @Override
368     public boolean isTrailerFieldsReady() {
369         return this._getHttpServletRequest().isTrailerFieldsReady();
370     }
371 }
372