001: /*
002: * Copyright (c) 1998-2001 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: *
028: * $Id: HttpServletRequestWrapper.java,v 1.2 2004/09/29 00:12:47 cvs Exp $
029: */
030:
031: package javax.servlet.http;
032:
033: import javax.servlet.ServletRequest;
034: import javax.servlet.ServletRequestWrapper;
035: import java.security.Principal;
036: import java.util.Enumeration;
037:
038: /**
039: * Wraps a servlet request in another request. Filters may
040: * use ServletRequestWrapper to modify the headers passed to the servlet.
041: *
042: * <p/>The default methods just call the wrapped request methods.
043: *
044: * @since servlet 2.3
045: */
046: public class HttpServletRequestWrapper extends ServletRequestWrapper
047: implements HttpServletRequest {
048: // the wrapped request
049: private HttpServletRequest request;
050:
051: /**
052: * Creates a new request wrapper
053: *
054: * @param request the wrapped request
055: */
056: public HttpServletRequestWrapper(HttpServletRequest request) {
057: super (request);
058:
059: this .request = request;
060: }
061:
062: /**
063: * Sets the request object for the wrapper.
064: *
065: * @param request the wrapped request
066: */
067: public void setRequest(ServletRequest request) {
068: super .setRequest(request);
069:
070: this .request = (HttpServletRequest) request;
071: }
072:
073: /**
074: * Returns the HTTP method, e.g. "GET" or "POST"
075: *
076: * <p/>Equivalent to CGI's <code>REQUEST_METHOD</code>
077: */
078: public String getMethod() {
079: return request.getMethod();
080: }
081:
082: /**
083: * Returns the entire request URI
084: */
085: public String getRequestURI() {
086: return request.getRequestURI();
087: }
088:
089: /**
090: * Reconstructs the URL the client used for the request.
091: *
092: * @since Servlet 2.3
093: */
094: public StringBuffer getRequestURL() {
095: return request.getRequestURL();
096: }
097:
098: /**
099: * Returns the part of the URI corresponding to the application's
100: * prefix. The first part of the URI selects applications
101: * (ServletContexts).
102: *
103: * <p><code>getContextPath()</code> is /myapp for the uri
104: * /myapp/servlet/Hello,
105: */
106: public String getContextPath() {
107: return request.getContextPath();
108: }
109:
110: /**
111: * Returns the URI part corresponding to the selected servlet.
112: * The URI is relative to the application.
113: *
114: * <p/>Corresponds to CGI's <code>SCRIPT_NAME</code>
115: *
116: * <code>getServletPath()</code> is /servlet/Hello for the uri
117: * /myapp/servlet/Hello/foo.
118: *
119: * <code>getServletPath()</code> is /dir/hello.jsp
120: * for the uri /myapp/dir/hello.jsp/foo,
121: */
122: public String getServletPath() {
123: return request.getServletPath();
124: }
125:
126: /**
127: * Returns the URI part after the selected servlet and null if there
128: * is no suffix.
129: *
130: * <p/>Corresponds to CGI's <code>PATH_INFO</code>
131: *
132: * <p><code>getPathInfo()</code> is /foo for
133: * the uri /myapp/servlet/Hello/foo.
134: *
135: * <code>getPathInfo()</code> is /hello.jsp for for the uri
136: * /myapp/dir/hello.jsp/foo.
137: */
138: public String getPathInfo() {
139: return request.getPathInfo();
140: }
141:
142: /**
143: * Returns the physical path name for the path info.
144: *
145: * <p/>Corresponds to CGI's <code>PATH_TRANSLATED</code>
146: *
147: * @return null if there is no path info.
148: */
149: public String getPathTranslated() {
150: return request.getPathTranslated();
151: }
152:
153: /**
154: * Returns the request's query string. Form based servlets will use
155: * <code>ServletRequest.getParameter()</code> to decode the form values.
156: *
157: * <p/>Corresponds to CGI's <code>PATH_TRANSLATED</code>
158: */
159: public String getQueryString() {
160: return request.getQueryString();
161: }
162:
163: /**
164: * Returns the first value for a request header.
165: *
166: * <p/>Corresponds to CGI's <code>HTTP_*</code>
167: *
168: * <code><pre>
169: * String userAgent = request.getHeader("User-Agent");
170: * </pre></code>
171: *
172: * @param name the header name
173: * @return the header value
174: */
175: public String getHeader(String name) {
176: return request.getHeader(name);
177: }
178:
179: /**
180: * Returns all the values for a request header. In some rare cases,
181: * like cookies, browsers may return multiple headers.
182: *
183: * @param name the header name
184: * @return an enumeration of the header values.
185: */
186: public Enumeration getHeaders(String name) {
187: return request.getHeaders(name);
188: }
189:
190: /**
191: * Returns an enumeration of all headers sent by the client.
192: */
193: public Enumeration getHeaderNames() {
194: return request.getHeaderNames();
195: }
196:
197: /**
198: * Converts a header value to an integer.
199: *
200: * @param name the header name
201: * @return the header value converted to an integer
202: */
203: public int getIntHeader(String name) {
204: return request.getIntHeader(name);
205: }
206:
207: /**
208: * Converts a date header to milliseconds since the epoch.
209: *
210: * <pre><code>
211: * long mod = request.getDateHeader("If-Modified-Since");
212: * </code></pre>
213: *
214: * @param name the header name
215: * @return the header value converted to an date
216: */
217: public long getDateHeader(String name) {
218: return request.getDateHeader(name);
219: }
220:
221: /**
222: * Returns an array of all cookies sent by the client.
223: */
224: public Cookie[] getCookies() {
225: return request.getCookies();
226: }
227:
228: /**
229: * Returns a session. If no session exists and create is true, then
230: * create a new session, otherwise return null.
231: *
232: * @param create If true, then create a new session if none exists.
233: */
234: public HttpSession getSession(boolean create) {
235: return request.getSession(create);
236: }
237:
238: /**
239: * Returns the current session, creating one if necessary.
240: * Sessions are a convenience for keeping user state
241: * across requests.
242: */
243: public HttpSession getSession() {
244: return getSession(true);
245: }
246:
247: /**
248: * Returns the session id. Sessions are a convenience for keeping
249: * user state across requests.
250: *
251: * <p/>The session id is the value of the JSESSION cookie.
252: */
253: public String getRequestedSessionId() {
254: return request.getRequestedSessionId();
255: }
256:
257: /**
258: * Returns true if the session is valid.
259: */
260: public boolean isRequestedSessionIdValid() {
261: return request.isRequestedSessionIdValid();
262: }
263:
264: /**
265: * Returns true if the session came from a cookie.
266: */
267: public boolean isRequestedSessionIdFromCookie() {
268: return request.isRequestedSessionIdFromCookie();
269: }
270:
271: /**
272: * Returns true if the session came URL-encoding.
273: */
274: public boolean isRequestedSessionIdFromURL() {
275: return request.isRequestedSessionIdFromURL();
276: }
277:
278: /**
279: * Returns the auth type, e.g. basic.
280: */
281: public String getAuthType() {
282: return request.getAuthType();
283: }
284:
285: /**
286: * Returns the remote user if authenticated.
287: */
288: public String getRemoteUser() {
289: return request.getRemoteUser();
290: }
291:
292: /**
293: * Returns true if the user is in the given role.
294: */
295: public boolean isUserInRole(String role) {
296: return request.isUserInRole(role);
297: }
298:
299: /**
300: * Returns the equivalent principal object for the authenticated user.
301: */
302: public Principal getUserPrincipal() {
303: return request.getUserPrincipal();
304: }
305:
306: /**
307: * @deprecated
308: */
309: public boolean isRequestedSessionIdFromUrl() {
310: return request.isRequestedSessionIdFromUrl();
311: }
312: }
|