KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > connection > RequestWrapper


1 /*
2  * Copyright (c) 1998-2003 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.server.connection;
30
31 import javax.servlet.RequestDispatcher JavaDoc;
32 import javax.servlet.ServletInputStream JavaDoc;
33 import javax.servlet.ServletRequest JavaDoc;
34 import javax.servlet.http.Cookie JavaDoc;
35 import javax.servlet.http.HttpServletRequest JavaDoc;
36 import javax.servlet.http.HttpSession JavaDoc;
37 import java.io.BufferedReader JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.io.UnsupportedEncodingException JavaDoc;
40 import java.security.Principal JavaDoc;
41 import java.util.Enumeration JavaDoc;
42 import java.util.Locale JavaDoc;
43 import java.util.Map JavaDoc;
44
45 /**
46  * Wraps a servlet request in another request. Filters may
47  * use ServletRequestWrapper to modify the headers passed to the servlet.
48  *
49  * <p/>The default methods just call the wrapped request methods.
50  *
51  * @since servlet 2.3
52  */

53 public class RequestWrapper implements ServletRequest JavaDoc {
54   // the wrapped request
55
protected HttpServletRequest JavaDoc _request;
56   
57   /**
58    * Create a new ServletRequestWrapper wrapping the enclosed request.
59    */

60   public RequestWrapper()
61   {
62   }
63   
64   /**
65    * Create a new ServletRequestWrapper wrapping the enclosed request.
66    */

67   public RequestWrapper(HttpServletRequest JavaDoc request)
68   {
69     _request = request;
70   }
71   
72   /**
73    * Sets the request object being wrapped.
74    *
75    * @exception IllegalArgumentException if the request is null
76    */

77   public void setRequest(HttpServletRequest JavaDoc request)
78   {
79     _request = request;
80   }
81   
82   /**
83    * Gets the request object being wrapped.
84    *
85    * @return the wrapped response
86    */

87   public HttpServletRequest JavaDoc getRequest()
88   {
89     return _request;
90   }
91   /**
92    * Returns the prococol, e.g. "HTTP/1.1"
93    */

94   public String JavaDoc getProtocol()
95   {
96     return _request.getProtocol();
97   }
98   /**
99    * Returns the request scheme, e.g. "http"
100    */

101   public String JavaDoc getScheme()
102   {
103     return _request.getScheme();
104   }
105   /**
106    * Returns the server name handling the request. When using virtual hosts,
107    * this returns the virtual host name, e.g. "vhost1.caucho.com".
108    */

109   public String JavaDoc getServerName()
110   {
111     return _request.getServerName();
112   }
113   /**
114    * Returns the server port handling the request, e.g. 80.
115    */

116   public int getServerPort()
117   {
118     return _request.getServerPort();
119   }
120   /**
121    * Returns the IP address of the remote host, i.e. the client browser.
122    */

123   public String JavaDoc getRemoteAddr()
124   {
125     return _request.getRemoteAddr();
126   }
127   
128   /**
129    * Returns the DNS hostname of the remote host, i.e. the client browser.
130    */

131   public String JavaDoc getRemoteHost()
132   {
133     return _request.getRemoteHost();
134   }
135   
136   /**
137    * Returns the remote port
138    *
139    * @since 2.4
140    */

141   public int getRemotePort()
142   {
143     return _request.getRemotePort();
144   }
145   
146   /**
147    * Returns the IP address of the local host, i.e. the server.
148    */

149   public String JavaDoc getLocalAddr()
150   {
151     return _request.getLocalAddr();
152   }
153   
154   /**
155    * Returns the local host name.
156    */

157   public String JavaDoc getLocalName()
158   {
159     return _request.getLocalName();
160   }
161   
162   /**
163    * Returns the local port
164    */

165   public int getLocalPort()
166   {
167     return _request.getLocalPort();
168   }
169   
170   /**
171    * Returns a form parameter. When the form contains several parameters
172    * of the same name, <code>getParameter</code> returns the first.
173    *
174    * <p>For example, calling <code>getParameter("a")</code> with the
175    * the query string <code>a=1&a=2</code> will return "1".
176    *
177    * @param name the form parameter to return
178    * @return the form value or null if none matches.
179    */

180   public String JavaDoc getParameter(String JavaDoc name)
181   {
182     return _request.getParameter(name);
183   }
184   /**
185    * Returns the parameter map request parameters. By default, returns
186    * the underlying request's map.
187    */

188   public Map JavaDoc getParameterMap()
189   {
190     return _request.getParameterMap();
191   }
192   /**
193    * Returns all values of a form parameter.
194    *
195    * <p>For example, calling <code>getParameterValues("a")</code>
196    * with the the query string <code>a=1&a=2</code> will
197    * return ["1", "2"].
198    *
199    * @param name the form parameter to return
200    * @return an array of matching form values or null if none matches.
201    */

202   public String JavaDoc []getParameterValues(String JavaDoc name)
203   {
204     return _request.getParameterValues(name);
205   }
206   /**
207    * Returns an enumeration of all form parameter names.
208    *
209    * <code><pre>
210    * Enumeration e = _request.getParameterNames();
211    * while (e.hasMoreElements()) {
212    * String name = (String) e.nextElement();
213    * out.println(name + ": " + request.getParameter(name));
214    * }
215    * </pre></code>
216    */

217   public Enumeration JavaDoc getParameterNames()
218   {
219     return _request.getParameterNames();
220   }
221   /**
222    * Returns an InputStream to retrieve POST data from the request.
223    * The stream will automatically end when the end of the POST data
224    * is complete.
225    */

226   public ServletInputStream JavaDoc getInputStream()
227     throws IOException JavaDoc
228   {
229     return _request.getInputStream();
230   }
231   /**
232    * Returns a reader to read POSTed data. Character encoding is
233    * based on the request data and is the same as
234    * <code>getCharacterEncoding()</code>
235    */

236   public BufferedReader JavaDoc getReader()
237     throws IOException JavaDoc, IllegalStateException JavaDoc
238   {
239     return _request.getReader();
240   }
241   /**
242    * Returns the character encoding of the POSTed data.
243    */

244   public String JavaDoc getCharacterEncoding()
245   {
246     return _request.getCharacterEncoding();
247   }
248   /**
249    * Sets the character encoding to be used for forms and getReader.
250    */

251   public void setCharacterEncoding(String JavaDoc encoding)
252     throws UnsupportedEncodingException JavaDoc
253   {
254     _request.setCharacterEncoding(encoding);
255   }
256   /**
257    * Returns the content length of the data. This value may differ from
258    * the actual length of the data. For newer browsers, i.e.
259    * those supporting HTTP/1.1, can support "chunked" encoding which does
260    * not make the content length available.
261    *
262    * <p>The upshot is, rely on the input stream to end when the data
263    * completes.
264    */

265   public int getContentLength()
266   {
267     return _request.getContentLength();
268   }
269   /**
270    * Returns the request's mime-type.
271    */

272   public String JavaDoc getContentType()
273   {
274     return _request.getContentType();
275   }
276   /**
277    * Returns the request's preferred locale.
278    */

279   public Locale JavaDoc getLocale()
280   {
281     return _request.getLocale();
282   }
283   /**
284    * Returns an enumeration of all locales acceptable by the client.
285    */

286   public Enumeration JavaDoc getLocales()
287   {
288     return _request.getLocales();
289   }
290   /**
291    * Returns true if the connection is secure, e.g. it uses SSL.
292    */

293   public boolean isSecure()
294   {
295     return _request.isSecure();
296   }
297   /**
298    * Returns an attribute value.
299    *
300    * @param name the attribute name
301    * @return the attribute value
302    */

303   public Object JavaDoc getAttribute(String JavaDoc name)
304   {
305     return _request.getAttribute(name);
306   }
307   /**
308    * Sets an attribute value.
309    *
310    * @param name the attribute name
311    * @param o the attribute value
312    */

313   public void setAttribute(String JavaDoc name, Object JavaDoc o)
314   {
315     _request.setAttribute(name, o);
316   }
317   /**
318    * Enumerates all attribute names in the request.
319    */

320   public Enumeration JavaDoc getAttributeNames()
321   {
322     return _request.getAttributeNames();
323   }
324   /**
325    * Removes the given attribute.
326    *
327    * @param name the attribute name
328    */

329   public void removeAttribute(String JavaDoc name)
330   {
331     _request.removeAttribute(name);
332   }
333   /**
334    * Returns a request dispatcher for later inclusion or forwarding. This
335    * is the servlet API equivalent to SSI includes. <code>uri</code>
336    * is relative to the request URI. Absolute URIs are relative to
337    * the application prefix (<code>getContextPath()</code>).
338    *
339    * <p>If <code>getRequestURI()</code> is /myapp/dir/test.jsp and the
340    * <code>uri</code> is "inc.jsp", the resulting page is
341    * /myapp/dir/inc.jsp.
342
343    * <code><pre>
344    * RequestDispatcher disp;
345    * disp = getRequestDispatcher("inc.jsp?a=b");
346    * disp.include(request, response);
347    * </pre></code>
348    *
349    * @param uri path relative to <code>getRequestURI()</code>
350    * (including query string) for the included file.
351    * @return RequestDispatcher for later inclusion or forwarding.
352    */

353   public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc uri)
354   {
355     return _request.getRequestDispatcher(uri);
356   }
357   
358   /**
359    * Returns the real path.
360    */

361   public String JavaDoc getRealPath(String JavaDoc uri)
362   {
363     return _request.getRealPath(uri);
364   }
365   
366   /**
367    * Returns the HTTP method, e.g. "GET" or "POST"
368    *
369    * <p/>Equivalent to CGI's <code>REQUEST_METHOD</code>
370    */

371   public String JavaDoc getMethod()
372   {
373     return _request.getMethod();
374   }
375   /**
376    * Returns the entire request URI
377    */

378   public String JavaDoc getRequestURI()
379   {
380     return _request.getRequestURI();
381   }
382   /**
383    * Reconstructs the URL the client used for the request.
384    *
385    * @since Servlet 2.3
386    */

387   public StringBuffer JavaDoc getRequestURL()
388   {
389     return _request.getRequestURL();
390   }
391   /**
392    * Returns the part of the URI corresponding to the application's
393    * prefix. The first part of the URI selects applications
394    * (ServletContexts).
395    *
396    * <p><code>getContextPath()</code> is /myapp for the uri
397    * /myapp/servlet/Hello,
398    */

399   public String JavaDoc getContextPath()
400   {
401     return _request.getContextPath();
402   }
403   /**
404    * Returns the URI part corresponding to the selected servlet.
405    * The URI is relative to the application.
406    *
407    * <p/>Corresponds to CGI's <code>SCRIPT_NAME</code>
408    *
409    * <code>getServletPath()</code> is /servlet/Hello for the uri
410    * /myapp/servlet/Hello/foo.
411    *
412    * <code>getServletPath()</code> is /dir/hello.jsp
413    * for the uri /myapp/dir/hello.jsp/foo,
414    */

415   public String JavaDoc getServletPath()
416   {
417     return _request.getServletPath();
418   }
419   /**
420    * Returns the URI part after the selected servlet and null if there
421    * is no suffix.
422    *
423    * <p/>Corresponds to CGI's <code>PATH_INFO</code>
424    *
425    * <p><code>getPathInfo()</code> is /foo for
426    * the uri /myapp/servlet/Hello/foo.
427    *
428    * <code>getPathInfo()</code> is /hello.jsp for for the uri
429    * /myapp/dir/hello.jsp/foo.
430    */

431   public String JavaDoc getPathInfo()
432   {
433     return _request.getPathInfo();
434   }
435   /**
436    * Returns the physical path name for the path info.
437    *
438    * <p/>Corresponds to CGI's <code>PATH_TRANSLATED</code>
439    *
440    * @return null if there is no path info.
441    */

442   public String JavaDoc getPathTranslated()
443   {
444     return _request.getPathTranslated();
445   }
446   /**
447    * Returns the request's query string. Form based servlets will use
448    * <code>ServletRequest.getParameter()</code> to decode the form values.
449    *
450    * <p/>Corresponds to CGI's <code>PATH_TRANSLATED</code>
451    */

452   public String JavaDoc getQueryString()
453   {
454     return _request.getQueryString();
455   }
456   /**
457    * Returns the first value for a request header.
458    *
459    * <p/>Corresponds to CGI's <code>HTTP_*</code>
460    *
461    * <code><pre>
462    * String userAgent = request.getHeader("User-Agent");
463    * </pre></code>
464    *
465    * @param name the header name
466    * @return the header value
467    */

468   public String JavaDoc getHeader(String JavaDoc name)
469   {
470     return _request.getHeader(name);
471   }
472   /**
473    * Returns all the values for a request header. In some rare cases,
474    * like cookies, browsers may return multiple headers.
475    *
476    * @param name the header name
477    * @return an enumeration of the header values.
478    */

479   public Enumeration JavaDoc getHeaders(String JavaDoc name)
480   {
481     return _request.getHeaders(name);
482   }
483   /**
484    * Returns an enumeration of all headers sent by the client.
485    */

486   public Enumeration JavaDoc getHeaderNames()
487   {
488     return _request.getHeaderNames();
489   }
490   /**
491    * Converts a header value to an integer.
492    *
493    * @param name the header name
494    * @return the header value converted to an integer
495    */

496   public int getIntHeader(String JavaDoc name)
497   {
498     return _request.getIntHeader(name);
499   }
500   /**
501    * Converts a date header to milliseconds since the epoch.
502    *
503    * <pre><code>
504    * long mod = _request.getDateHeader("If-Modified-Since");
505    * </code></pre>
506    *
507    * @param name the header name
508    * @return the header value converted to an date
509    */

510   public long getDateHeader(String JavaDoc name)
511   {
512     return _request.getDateHeader(name);
513   }
514   /**
515    * Returns an array of all cookies sent by the client.
516    */

517   public Cookie JavaDoc []getCookies()
518   {
519     return _request.getCookies();
520   }
521   /**
522    * Returns a session. If no session exists and create is true, then
523    * create a new session, otherwise return null.
524    *
525    * @param create If true, then create a new session if none exists.
526    */

527   public HttpSession JavaDoc getSession(boolean create)
528   {
529     return _request.getSession(create);
530   }
531   /**
532    * Returns the current session, creating one if necessary.
533    * Sessions are a convenience for keeping user state
534    * across requests.
535    */

536   public HttpSession JavaDoc getSession()
537   {
538     return getSession(true);
539   }
540   /**
541    * Returns the session id. Sessions are a convenience for keeping
542    * user state across requests.
543    *
544    * <p/>The session id is the value of the JSESSION cookie.
545    */

546   public String JavaDoc getRequestedSessionId()
547   {
548     return _request.getRequestedSessionId();
549   }
550   /**
551    * Returns true if the session is valid.
552    */

553   public boolean isRequestedSessionIdValid()
554   {
555     return _request.isRequestedSessionIdValid();
556   }
557   /**
558    * Returns true if the session came from a cookie.
559    */

560   public boolean isRequestedSessionIdFromCookie()
561   {
562     return _request.isRequestedSessionIdFromCookie();
563   }
564   /**
565    * Returns true if the session came URL-encoding.
566    */

567   public boolean isRequestedSessionIdFromURL()
568   {
569     return _request.isRequestedSessionIdFromURL();
570   }
571   /**
572    * Returns the auth type, e.g. basic.
573    */

574   public String JavaDoc getAuthType()
575   {
576     return _request.getAuthType();
577   }
578   /**
579    * Returns the remote user if authenticated.
580    */

581   public String JavaDoc getRemoteUser()
582   {
583     return _request.getRemoteUser();
584   }
585   /**
586    * Returns true if the user is in the given role.
587    */

588   public boolean isUserInRole(String JavaDoc role)
589   {
590     return _request.isUserInRole(role);
591   }
592   
593   /**
594    * Returns the equivalent principal object for the authenticated user.
595    */

596   public Principal JavaDoc getUserPrincipal()
597   {
598     return _request.getUserPrincipal();
599   }
600   
601   /**
602    * @deprecated
603    */

604   public boolean isRequestedSessionIdFromUrl()
605   {
606     return _request.isRequestedSessionIdFromUrl();
607   }
608
609   /**
610    * Clears the wrapper.
611    */

612   protected void free()
613   {
614     _request = null;
615   }
616 }
617
Popular Tags