001: /*
002: * Copyright (c) 1998-2008 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: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.server.connection;
031:
032: import javax.servlet.RequestDispatcher;
033: import javax.servlet.ServletInputStream;
034: import javax.servlet.ServletRequest;
035: import javax.servlet.http.Cookie;
036: import javax.servlet.http.HttpServletRequest;
037: import javax.servlet.http.HttpSession;
038: import java.io.BufferedReader;
039: import java.io.IOException;
040: import java.io.UnsupportedEncodingException;
041: import java.security.Principal;
042: import java.util.Enumeration;
043: import java.util.Locale;
044: import java.util.Map;
045:
046: /**
047: * Wraps a servlet request in another request. Filters may
048: * use ServletRequestWrapper to modify the headers passed to the servlet.
049: *
050: * <p/>The default methods just call the wrapped request methods.
051: *
052: * @since servlet 2.3
053: */
054: public class RequestWrapper implements ServletRequest {
055: // the wrapped request
056: protected HttpServletRequest _request;
057:
058: /**
059: * Create a new ServletRequestWrapper wrapping the enclosed request.
060: */
061: public RequestWrapper() {
062: }
063:
064: /**
065: * Create a new ServletRequestWrapper wrapping the enclosed request.
066: */
067: public RequestWrapper(HttpServletRequest request) {
068: setRequest(request);
069: }
070:
071: /**
072: * Sets the request object being wrapped.
073: *
074: * @exception IllegalArgumentException if the request is null
075: */
076: public void setRequest(HttpServletRequest request) {
077: _request = request;
078: }
079:
080: /**
081: * Gets the request object being wrapped.
082: *
083: * @return the wrapped response
084: */
085: public HttpServletRequest getRequest() {
086: return _request;
087: }
088:
089: /**
090: * Returns the prococol, e.g. "HTTP/1.1"
091: */
092: public String getProtocol() {
093: return _request.getProtocol();
094: }
095:
096: /**
097: * Returns the request scheme, e.g. "http"
098: */
099: public String getScheme() {
100: return _request.getScheme();
101: }
102:
103: /**
104: * Returns the server name handling the request. When using virtual hosts,
105: * this returns the virtual host name, e.g. "vhost1.caucho.com".
106: */
107: public String getServerName() {
108: return _request.getServerName();
109: }
110:
111: /**
112: * Returns the server port handling the request, e.g. 80.
113: */
114: public int getServerPort() {
115: return _request.getServerPort();
116: }
117:
118: /**
119: * Returns the IP address of the remote host, i.e. the client browser.
120: */
121: public String getRemoteAddr() {
122: return _request.getRemoteAddr();
123: }
124:
125: /**
126: * Returns the DNS hostname of the remote host, i.e. the client browser.
127: */
128: public String getRemoteHost() {
129: return _request.getRemoteHost();
130: }
131:
132: /**
133: * Returns the remote port
134: *
135: * @since 2.4
136: */
137: public int getRemotePort() {
138: return _request.getRemotePort();
139: }
140:
141: /**
142: * Returns the IP address of the local host, i.e. the server.
143: */
144: public String getLocalAddr() {
145: return _request.getLocalAddr();
146: }
147:
148: /**
149: * Returns the local host name.
150: */
151: public String getLocalName() {
152: return _request.getLocalName();
153: }
154:
155: /**
156: * Returns the local port
157: */
158: public int getLocalPort() {
159: return _request.getLocalPort();
160: }
161:
162: /**
163: * Returns a form parameter. When the form contains several parameters
164: * of the same name, <code>getParameter</code> returns the first.
165: *
166: * <p>For example, calling <code>getParameter("a")</code> with the
167: * the query string <code>a=1&a=2</code> will return "1".
168: *
169: * @param name the form parameter to return
170: * @return the form value or null if none matches.
171: */
172: public String getParameter(String name) {
173: return _request.getParameter(name);
174: }
175:
176: /**
177: * Returns the parameter map request parameters. By default, returns
178: * the underlying request's map.
179: */
180: public Map getParameterMap() {
181: return _request.getParameterMap();
182: }
183:
184: /**
185: * Returns all values of a form parameter.
186: *
187: * <p>For example, calling <code>getParameterValues("a")</code>
188: * with the the query string <code>a=1&a=2</code> will
189: * return ["1", "2"].
190: *
191: * @param name the form parameter to return
192: * @return an array of matching form values or null if none matches.
193: */
194: public String[] getParameterValues(String name) {
195: return _request.getParameterValues(name);
196: }
197:
198: /**
199: * Returns an enumeration of all form parameter names.
200: *
201: * <code><pre>
202: * Enumeration e = _request.getParameterNames();
203: * while (e.hasMoreElements()) {
204: * String name = (String) e.nextElement();
205: * out.println(name + ": " + request.getParameter(name));
206: * }
207: * </pre></code>
208: */
209: public Enumeration getParameterNames() {
210: return _request.getParameterNames();
211: }
212:
213: /**
214: * Returns an InputStream to retrieve POST data from the request.
215: * The stream will automatically end when the end of the POST data
216: * is complete.
217: */
218: public ServletInputStream getInputStream() throws IOException {
219: return _request.getInputStream();
220: }
221:
222: /**
223: * Returns a reader to read POSTed data. Character encoding is
224: * based on the request data and is the same as
225: * <code>getCharacterEncoding()</code>
226: */
227: public BufferedReader getReader() throws IOException,
228: IllegalStateException {
229: return _request.getReader();
230: }
231:
232: /**
233: * Returns the character encoding of the POSTed data.
234: */
235: public String getCharacterEncoding() {
236: return _request.getCharacterEncoding();
237: }
238:
239: /**
240: * Sets the character encoding to be used for forms and getReader.
241: */
242: public void setCharacterEncoding(String encoding)
243: throws UnsupportedEncodingException {
244: _request.setCharacterEncoding(encoding);
245: }
246:
247: /**
248: * Returns the content length of the data. This value may differ from
249: * the actual length of the data. For newer browsers, i.e.
250: * those supporting HTTP/1.1, can support "chunked" encoding which does
251: * not make the content length available.
252: *
253: * <p>The upshot is, rely on the input stream to end when the data
254: * completes.
255: */
256: public int getContentLength() {
257: return _request.getContentLength();
258: }
259:
260: /**
261: * Returns the request's mime-type.
262: */
263: public String getContentType() {
264: return _request.getContentType();
265: }
266:
267: /**
268: * Returns the request's preferred locale.
269: */
270: public Locale getLocale() {
271: return _request.getLocale();
272: }
273:
274: /**
275: * Returns an enumeration of all locales acceptable by the client.
276: */
277: public Enumeration getLocales() {
278: return _request.getLocales();
279: }
280:
281: /**
282: * Returns true if the connection is secure, e.g. it uses SSL.
283: */
284: public boolean isSecure() {
285: return _request.isSecure();
286: }
287:
288: /**
289: * Returns an attribute value.
290: *
291: * @param name the attribute name
292: * @return the attribute value
293: */
294: public Object getAttribute(String name) {
295: return _request.getAttribute(name);
296: }
297:
298: /**
299: * Sets an attribute value.
300: *
301: * @param name the attribute name
302: * @param o the attribute value
303: */
304: public void setAttribute(String name, Object o) {
305: _request.setAttribute(name, o);
306: }
307:
308: /**
309: * Enumerates all attribute names in the request.
310: */
311: public Enumeration getAttributeNames() {
312: return _request.getAttributeNames();
313: }
314:
315: /**
316: * Removes the given attribute.
317: *
318: * @param name the attribute name
319: */
320: public void removeAttribute(String name) {
321: _request.removeAttribute(name);
322: }
323:
324: /**
325: * Returns a request dispatcher for later inclusion or forwarding. This
326: * is the servlet API equivalent to SSI includes. <code>uri</code>
327: * is relative to the request URI. Absolute URIs are relative to
328: * the application prefix (<code>getContextPath()</code>).
329: *
330: * <p>If <code>getRequestURI()</code> is /myapp/dir/test.jsp and the
331: * <code>uri</code> is "inc.jsp", the resulting page is
332: * /myapp/dir/inc.jsp.
333:
334: * <code><pre>
335: * RequestDispatcher disp;
336: * disp = getRequestDispatcher("inc.jsp?a=b");
337: * disp.include(request, response);
338: * </pre></code>
339: *
340: * @param uri path relative to <code>getRequestURI()</code>
341: * (including query string) for the included file.
342: * @return RequestDispatcher for later inclusion or forwarding.
343: */
344: public RequestDispatcher getRequestDispatcher(String uri) {
345: return _request.getRequestDispatcher(uri);
346: }
347:
348: /**
349: * Returns the real path.
350: */
351: public String getRealPath(String uri) {
352: return _request.getRealPath(uri);
353: }
354:
355: /**
356: * Returns the HTTP method, e.g. "GET" or "POST"
357: *
358: * <p/>Equivalent to CGI's <code>REQUEST_METHOD</code>
359: */
360: public String getMethod() {
361: return _request.getMethod();
362: }
363:
364: /**
365: * Returns the entire request URI
366: */
367: public String getRequestURI() {
368: return _request.getRequestURI();
369: }
370:
371: /**
372: * Reconstructs the URL the client used for the request.
373: *
374: * @since Servlet 2.3
375: */
376: public StringBuffer getRequestURL() {
377: return _request.getRequestURL();
378: }
379:
380: /**
381: * Returns the part of the URI corresponding to the application's
382: * prefix. The first part of the URI selects applications
383: * (ServletContexts).
384: *
385: * <p><code>getContextPath()</code> is /myapp for the uri
386: * /myapp/servlet/Hello,
387: */
388: public String getContextPath() {
389: return _request.getContextPath();
390: }
391:
392: /**
393: * Returns the URI part corresponding to the selected servlet.
394: * The URI is relative to the application.
395: *
396: * <p/>Corresponds to CGI's <code>SCRIPT_NAME</code>
397: *
398: * <code>getServletPath()</code> is /servlet/Hello for the uri
399: * /myapp/servlet/Hello/foo.
400: *
401: * <code>getServletPath()</code> is /dir/hello.jsp
402: * for the uri /myapp/dir/hello.jsp/foo,
403: */
404: public String getServletPath() {
405: return _request.getServletPath();
406: }
407:
408: /**
409: * Returns the URI part after the selected servlet and null if there
410: * is no suffix.
411: *
412: * <p/>Corresponds to CGI's <code>PATH_INFO</code>
413: *
414: * <p><code>getPathInfo()</code> is /foo for
415: * the uri /myapp/servlet/Hello/foo.
416: *
417: * <code>getPathInfo()</code> is /hello.jsp for for the uri
418: * /myapp/dir/hello.jsp/foo.
419: */
420: public String getPathInfo() {
421: return _request.getPathInfo();
422: }
423:
424: /**
425: * Returns the physical path name for the path info.
426: *
427: * <p/>Corresponds to CGI's <code>PATH_TRANSLATED</code>
428: *
429: * @return null if there is no path info.
430: */
431: public String getPathTranslated() {
432: return _request.getPathTranslated();
433: }
434:
435: /**
436: * Returns the request's query string. Form based servlets will use
437: * <code>ServletRequest.getParameter()</code> to decode the form values.
438: *
439: * <p/>Corresponds to CGI's <code>PATH_TRANSLATED</code>
440: */
441: public String getQueryString() {
442: return _request.getQueryString();
443: }
444:
445: /**
446: * Returns the first value for a request header.
447: *
448: * <p/>Corresponds to CGI's <code>HTTP_*</code>
449: *
450: * <code><pre>
451: * String userAgent = request.getHeader("User-Agent");
452: * </pre></code>
453: *
454: * @param name the header name
455: * @return the header value
456: */
457: public String getHeader(String name) {
458: return _request.getHeader(name);
459: }
460:
461: /**
462: * Returns all the values for a request header. In some rare cases,
463: * like cookies, browsers may return multiple headers.
464: *
465: * @param name the header name
466: * @return an enumeration of the header values.
467: */
468: public Enumeration getHeaders(String name) {
469: return _request.getHeaders(name);
470: }
471:
472: /**
473: * Returns an enumeration of all headers sent by the client.
474: */
475: public Enumeration getHeaderNames() {
476: return _request.getHeaderNames();
477: }
478:
479: /**
480: * Converts a header value to an integer.
481: *
482: * @param name the header name
483: * @return the header value converted to an integer
484: */
485: public int getIntHeader(String name) {
486: return _request.getIntHeader(name);
487: }
488:
489: /**
490: * Converts a date header to milliseconds since the epoch.
491: *
492: * <pre><code>
493: * long mod = _request.getDateHeader("If-Modified-Since");
494: * </code></pre>
495: *
496: * @param name the header name
497: * @return the header value converted to an date
498: */
499: public long getDateHeader(String name) {
500: return _request.getDateHeader(name);
501: }
502:
503: /**
504: * Returns an array of all cookies sent by the client.
505: */
506: public Cookie[] getCookies() {
507: return _request.getCookies();
508: }
509:
510: /**
511: * Returns a session. If no session exists and create is true, then
512: * create a new session, otherwise return null.
513: *
514: * @param create If true, then create a new session if none exists.
515: */
516: public HttpSession getSession(boolean create) {
517: return _request.getSession(create);
518: }
519:
520: /**
521: * Returns the current session, creating one if necessary.
522: * Sessions are a convenience for keeping user state
523: * across requests.
524: */
525: public HttpSession getSession() {
526: return getSession(true);
527: }
528:
529: /**
530: * Returns the session id. Sessions are a convenience for keeping
531: * user state across requests.
532: *
533: * <p/>The session id is the value of the JSESSION cookie.
534: */
535: public String getRequestedSessionId() {
536: return _request.getRequestedSessionId();
537: }
538:
539: /**
540: * Returns true if the session is valid.
541: */
542: public boolean isRequestedSessionIdValid() {
543: return _request.isRequestedSessionIdValid();
544: }
545:
546: /**
547: * Returns true if the session came from a cookie.
548: */
549: public boolean isRequestedSessionIdFromCookie() {
550: return _request.isRequestedSessionIdFromCookie();
551: }
552:
553: /**
554: * Returns true if the session came URL-encoding.
555: */
556: public boolean isRequestedSessionIdFromURL() {
557: return _request.isRequestedSessionIdFromURL();
558: }
559:
560: /**
561: * Returns the auth type, e.g. basic.
562: */
563: public String getAuthType() {
564: return _request.getAuthType();
565: }
566:
567: /**
568: * Returns the remote user if authenticated.
569: */
570: public String getRemoteUser() {
571: return _request.getRemoteUser();
572: }
573:
574: /**
575: * Returns true if the user is in the given role.
576: */
577: public boolean isUserInRole(String role) {
578: return _request.isUserInRole(role);
579: }
580:
581: /**
582: * Returns the equivalent principal object for the authenticated user.
583: */
584: public Principal getUserPrincipal() {
585: return _request.getUserPrincipal();
586: }
587:
588: /**
589: * @deprecated
590: */
591: public boolean isRequestedSessionIdFromUrl() {
592: return _request.isRequestedSessionIdFromUrl();
593: }
594:
595: /**
596: * Clears the wrapper.
597: */
598: protected void free() {
599: _request = null;
600: }
601: }
|