com.caucho.server.http
Class RequestWrapper

java.lang.Object
  extended by com.caucho.server.http.RequestWrapper
All Implemented Interfaces:
ServletRequest
Direct Known Subclasses:
RequestAdapter

public class RequestWrapper
extends java.lang.Object
implements ServletRequest

Wraps a servlet request in another request. Filters may use ServletRequestWrapper to modify the headers passed to the servlet.

The default methods just call the wrapped request methods.

Since:
servlet 2.3

Field Summary
protected  HttpServletRequest _request
           
 
Constructor Summary
RequestWrapper()
          Create a new ServletRequestWrapper wrapping the enclosed request.
RequestWrapper(HttpServletRequest request)
          Create a new ServletRequestWrapper wrapping the enclosed request.
 
Method Summary
 void addAsyncListener(AsyncListener listener)
          Adds an async listener for this request
 void addAsyncListener(AsyncListener listener, ServletRequest request, ServletResponse response)
          Adds an async listener for this request
 void complete()
          Complete the request
protected  void free()
          Clears the wrapper.
 AsyncContext getAsyncContext()
          Returns the async context for the request
 java.lang.Object getAttribute(java.lang.String name)
          Returns an attribute value.
 java.util.Enumeration getAttributeNames()
          Enumerates all attribute names in the request.
 java.lang.String getAuthType()
          Returns the auth type, e.g.
 java.lang.String getCharacterEncoding()
          Returns the character encoding of the POSTed data.
 int getContentLength()
          Returns the content length of the data.
 java.lang.String getContentType()
          Returns the request's mime-type.
 java.lang.String getContextPath()
          Returns the part of the URI corresponding to the application's prefix.
 Cookie[] getCookies()
          Returns an array of all cookies sent by the client.
 long getDateHeader(java.lang.String name)
          Converts a date header to milliseconds since the epoch.
 DispatcherType getDispatcherType()
          Returns DispatcherType
 java.lang.String getHeader(java.lang.String name)
          Returns the first value for a request header.
 java.util.Enumeration getHeaderNames()
          Returns an enumeration of all headers sent by the client.
 java.util.Enumeration getHeaders(java.lang.String name)
          Returns all the values for a request header.
 ServletInputStream getInputStream()
          Returns an InputStream to retrieve POST data from the request.
 int getIntHeader(java.lang.String name)
          Converts a header value to an integer.
 java.lang.String getLocalAddr()
          Returns the IP address of the local host, i.e.
 java.util.Locale getLocale()
          Returns the request's preferred locale.
 java.util.Enumeration getLocales()
          Returns an enumeration of all locales acceptable by the client.
 java.lang.String getLocalName()
          Returns the local host name.
 int getLocalPort()
          Returns the local port
 java.lang.String getMethod()
          Returns the HTTP method, e.g.
 java.lang.String getParameter(java.lang.String name)
          Returns a form parameter.
 java.util.Map getParameterMap()
          Returns the parameter map request parameters.
 java.util.Enumeration getParameterNames()
          Returns an enumeration of all form parameter names.
 java.lang.String[] getParameterValues(java.lang.String name)
          Returns all values of a form parameter.
 java.lang.String getPathInfo()
          Returns the URI part after the selected servlet and null if there is no suffix.
 java.lang.String getPathTranslated()
          Returns the physical path name for the path info.
 java.lang.String getProtocol()
          Returns the prococol, e.g.
 java.lang.String getQueryString()
          Returns the request's query string.
 java.io.BufferedReader getReader()
          Returns a reader to read POSTed data.
 java.lang.String getRealPath(java.lang.String uri)
          Returns the real path.
 java.lang.String getRemoteAddr()
          Returns the IP address of the remote host, i.e.
 java.lang.String getRemoteHost()
          Returns the DNS hostname of the remote host, i.e.
 int getRemotePort()
          Returns the remote port
 java.lang.String getRemoteUser()
          Returns the remote user if authenticated.
 HttpServletRequest getRequest()
          Gets the request object being wrapped.
 RequestDispatcher getRequestDispatcher(java.lang.String uri)
          Returns a request dispatcher for later inclusion or forwarding.
 java.lang.String getRequestedSessionId()
          Returns the session id.
 java.lang.String getRequestURI()
          Returns the entire request URI
 java.lang.StringBuffer getRequestURL()
          Reconstructs the URL the client used for the request.
 java.lang.String getScheme()
          Returns the request scheme, e.g.
 java.lang.String getServerName()
          Returns the server name handling the request.
 int getServerPort()
          Returns the server port handling the request, e.g.
 ServletContext getServletContext()
          Returns the servlet context for the request
 java.lang.String getServletPath()
          Returns the URI part corresponding to the selected servlet.
 ServletResponse getServletResponse()
          Returns the servlet response for the request
 HttpSession getSession()
          Returns the current session, creating one if necessary.
 HttpSession getSession(boolean create)
          Returns a session.
 java.security.Principal getUserPrincipal()
          Returns the equivalent principal object for the authenticated user.
 boolean isAsyncStarted()
          Returns true if the request is in async.
 boolean isAsyncSupported()
          Returns true if the request supports async
 boolean isInitial()
          Returns true for the initial dispatch
 boolean isRequestedSessionIdFromCookie()
          Returns true if the session came from a cookie.
 boolean isRequestedSessionIdFromUrl()
          Deprecated.  
 boolean isRequestedSessionIdFromURL()
          Returns true if the session came URL-encoding.
 boolean isRequestedSessionIdValid()
          Returns true if the session is valid.
 boolean isResumed()
          Returns true if the servlet is resumed
 boolean isSecure()
          Returns true if the connection is secure, e.g.
 boolean isSuspended()
          Returns true if the servlet is suspended
 boolean isTimeout()
          Returns true if the servlet timed out
 boolean isUserInRole(java.lang.String role)
          Returns true if the user is in the given role.
 void removeAttribute(java.lang.String name)
          Removes the given attribute.
 void resume()
          Resume the request
 void setAttribute(java.lang.String name, java.lang.Object o)
          Sets an attribute value.
 void setCharacterEncoding(java.lang.String encoding)
          Sets the character encoding to be used for forms and getReader.
 void setRequest(HttpServletRequest request)
          Sets the request object being wrapped.
 AsyncContext startAsync()
          Starts an async mode
 AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse)
          Starts an async mode
 void suspend()
          Suspend the request
 void suspend(long timeout)
          Suspend the request
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_request

protected HttpServletRequest _request
Constructor Detail

RequestWrapper

public RequestWrapper()
Create a new ServletRequestWrapper wrapping the enclosed request.


RequestWrapper

public RequestWrapper(HttpServletRequest request)
Create a new ServletRequestWrapper wrapping the enclosed request.

Method Detail

setRequest

public void setRequest(HttpServletRequest request)
Sets the request object being wrapped.

Throws:
java.lang.IllegalArgumentException - if the request is null

getRequest

public HttpServletRequest getRequest()
Gets the request object being wrapped.

Returns:
the wrapped response

getProtocol

public java.lang.String getProtocol()
Returns the prococol, e.g. "HTTP/1.1"

Specified by:
getProtocol in interface ServletRequest

getScheme

public java.lang.String getScheme()
Returns the request scheme, e.g. "http"

Specified by:
getScheme in interface ServletRequest

getServerName

public java.lang.String getServerName()
Returns the server name handling the request. When using virtual hosts, this returns the virtual host name, e.g. "vhost1.caucho.com".

Specified by:
getServerName in interface ServletRequest

getServerPort

public int getServerPort()
Returns the server port handling the request, e.g. 80.

Specified by:
getServerPort in interface ServletRequest

getRemoteAddr

public java.lang.String getRemoteAddr()
Returns the IP address of the remote host, i.e. the client browser.

Specified by:
getRemoteAddr in interface ServletRequest

getRemoteHost

public java.lang.String getRemoteHost()
Returns the DNS hostname of the remote host, i.e. the client browser.

Specified by:
getRemoteHost in interface ServletRequest

getRemotePort

public int getRemotePort()
Returns the remote port

Specified by:
getRemotePort in interface ServletRequest
Since:
2.4

getLocalAddr

public java.lang.String getLocalAddr()
Returns the IP address of the local host, i.e. the server.

Specified by:
getLocalAddr in interface ServletRequest

getLocalName

public java.lang.String getLocalName()
Returns the local host name.

Specified by:
getLocalName in interface ServletRequest

getLocalPort

public int getLocalPort()
Returns the local port

Specified by:
getLocalPort in interface ServletRequest

getParameter

public java.lang.String getParameter(java.lang.String name)
Returns a form parameter. When the form contains several parameters of the same name, getParameter returns the first.

For example, calling getParameter("a") with the the query string a=1&a=2 will return "1".

Specified by:
getParameter in interface ServletRequest
Parameters:
name - the form parameter to return
Returns:
the form value or null if none matches.

getParameterMap

public java.util.Map getParameterMap()
Returns the parameter map request parameters. By default, returns the underlying request's map.

Specified by:
getParameterMap in interface ServletRequest

getParameterValues

public java.lang.String[] getParameterValues(java.lang.String name)
Returns all values of a form parameter.

For example, calling getParameterValues("a") with the the query string a=1&a=2 will return ["1", "2"].

Specified by:
getParameterValues in interface ServletRequest
Parameters:
name - the form parameter to return
Returns:
an array of matching form values or null if none matches.

getParameterNames

public java.util.Enumeration getParameterNames()
Returns an enumeration of all form parameter names.
 Enumeration e = getRequest().getParameterNames();
 while (e.hasMoreElements()) {
   String name = (String) e.nextElement();
   out.println(name + ": " + request.getParameter(name));
 }
 

Specified by:
getParameterNames in interface ServletRequest

getInputStream

public ServletInputStream getInputStream()
                                  throws java.io.IOException
Returns an InputStream to retrieve POST data from the request. The stream will automatically end when the end of the POST data is complete.

Specified by:
getInputStream in interface ServletRequest
Throws:
java.io.IOException

getReader

public java.io.BufferedReader getReader()
                                 throws java.io.IOException,
                                        java.lang.IllegalStateException
Returns a reader to read POSTed data. Character encoding is based on the request data and is the same as getCharacterEncoding()

Specified by:
getReader in interface ServletRequest
Throws:
java.io.IOException
java.lang.IllegalStateException

getCharacterEncoding

public java.lang.String getCharacterEncoding()
Returns the character encoding of the POSTed data.

Specified by:
getCharacterEncoding in interface ServletRequest

setCharacterEncoding

public void setCharacterEncoding(java.lang.String encoding)
                          throws java.io.UnsupportedEncodingException
Sets the character encoding to be used for forms and getReader.

Specified by:
setCharacterEncoding in interface ServletRequest
Throws:
java.io.UnsupportedEncodingException

getContentLength

public int getContentLength()
Returns the content length of the data. This value may differ from the actual length of the data. For newer browsers, i.e. those supporting HTTP/1.1, can support "chunked" encoding which does not make the content length available.

The upshot is, rely on the input stream to end when the data completes.

Specified by:
getContentLength in interface ServletRequest

getContentType

public java.lang.String getContentType()
Returns the request's mime-type.

Specified by:
getContentType in interface ServletRequest

getLocale

public java.util.Locale getLocale()
Returns the request's preferred locale.

Specified by:
getLocale in interface ServletRequest

getLocales

public java.util.Enumeration getLocales()
Returns an enumeration of all locales acceptable by the client.

Specified by:
getLocales in interface ServletRequest

isSecure

public boolean isSecure()
Returns true if the connection is secure, e.g. it uses SSL.

Specified by:
isSecure in interface ServletRequest

getAttribute

public java.lang.Object getAttribute(java.lang.String name)
Returns an attribute value.

Specified by:
getAttribute in interface ServletRequest
Parameters:
name - the attribute name
Returns:
the attribute value

setAttribute

public void setAttribute(java.lang.String name,
                         java.lang.Object o)
Sets an attribute value.

Specified by:
setAttribute in interface ServletRequest
Parameters:
name - the attribute name
o - the attribute value

getAttributeNames

public java.util.Enumeration getAttributeNames()
Enumerates all attribute names in the request.

Specified by:
getAttributeNames in interface ServletRequest

removeAttribute

public void removeAttribute(java.lang.String name)
Removes the given attribute.

Specified by:
removeAttribute in interface ServletRequest
Parameters:
name - the attribute name

getRequestDispatcher

public RequestDispatcher getRequestDispatcher(java.lang.String uri)
Returns a request dispatcher for later inclusion or forwarding. This is the servlet API equivalent to SSI includes. uri is relative to the request URI. Absolute URIs are relative to the application prefix (getContextPath()).

If getRequestURI() is /myapp/dir/test.jsp and the uri is "inc.jsp", the resulting page is /myapp/dir/inc.jsp.

   RequestDispatcher disp;
   disp = getRequestDispatcher("inc.jsp?a=b");
   disp.include(request, response);
 

Specified by:
getRequestDispatcher in interface ServletRequest
Parameters:
uri - path relative to getRequestURI() (including query string) for the included file.
Returns:
RequestDispatcher for later inclusion or forwarding.

getRealPath

public java.lang.String getRealPath(java.lang.String uri)
Returns the real path.

Specified by:
getRealPath in interface ServletRequest

getMethod

public java.lang.String getMethod()
Returns the HTTP method, e.g. "GET" or "POST"

Equivalent to CGI's REQUEST_METHOD


getRequestURI

public java.lang.String getRequestURI()
Returns the entire request URI


getRequestURL

public java.lang.StringBuffer getRequestURL()
Reconstructs the URL the client used for the request.

Since:
Servlet 2.3

getContextPath

public java.lang.String getContextPath()
Returns the part of the URI corresponding to the application's prefix. The first part of the URI selects applications (ServletContexts).

getContextPath() is /myapp for the uri /myapp/servlet/Hello,


getServletPath

public java.lang.String getServletPath()
Returns the URI part corresponding to the selected servlet. The URI is relative to the application.

Corresponds to CGI's SCRIPT_NAME getServletPath() is /servlet/Hello for the uri /myapp/servlet/Hello/foo. getServletPath() is /dir/hello.jsp for the uri /myapp/dir/hello.jsp/foo,


getPathInfo

public java.lang.String getPathInfo()
Returns the URI part after the selected servlet and null if there is no suffix.

Corresponds to CGI's PATH_INFO

getPathInfo() is /foo for the uri /myapp/servlet/Hello/foo. getPathInfo() is /hello.jsp for for the uri /myapp/dir/hello.jsp/foo.


getPathTranslated

public java.lang.String getPathTranslated()
Returns the physical path name for the path info.

Corresponds to CGI's PATH_TRANSLATED

Returns:
null if there is no path info.

getQueryString

public java.lang.String getQueryString()
Returns the request's query string. Form based servlets will use ServletRequest.getParameter() to decode the form values.

Corresponds to CGI's PATH_TRANSLATED


getHeader

public java.lang.String getHeader(java.lang.String name)
Returns the first value for a request header.

Corresponds to CGI's HTTP_*

 String userAgent = request.getHeader("User-Agent");
 

Parameters:
name - the header name
Returns:
the header value

getHeaders

public java.util.Enumeration getHeaders(java.lang.String name)
Returns all the values for a request header. In some rare cases, like cookies, browsers may return multiple headers.

Parameters:
name - the header name
Returns:
an enumeration of the header values.

getHeaderNames

public java.util.Enumeration getHeaderNames()
Returns an enumeration of all headers sent by the client.


getIntHeader

public int getIntHeader(java.lang.String name)
Converts a header value to an integer.

Parameters:
name - the header name
Returns:
the header value converted to an integer

getDateHeader

public long getDateHeader(java.lang.String name)
Converts a date header to milliseconds since the epoch.

 long mod = getRequest().getDateHeader("If-Modified-Since");
 

Parameters:
name - the header name
Returns:
the header value converted to an date

getCookies

public Cookie[] getCookies()
Returns an array of all cookies sent by the client.


getSession

public HttpSession getSession(boolean create)
Returns a session. If no session exists and create is true, then create a new session, otherwise return null.

Parameters:
create - If true, then create a new session if none exists.

getSession

public HttpSession getSession()
Returns the current session, creating one if necessary. Sessions are a convenience for keeping user state across requests.


getRequestedSessionId

public java.lang.String getRequestedSessionId()
Returns the session id. Sessions are a convenience for keeping user state across requests.

The session id is the value of the JSESSION cookie.


isRequestedSessionIdValid

public boolean isRequestedSessionIdValid()
Returns true if the session is valid.


isRequestedSessionIdFromCookie

public boolean isRequestedSessionIdFromCookie()
Returns true if the session came from a cookie.


isRequestedSessionIdFromURL

public boolean isRequestedSessionIdFromURL()
Returns true if the session came URL-encoding.


getAuthType

public java.lang.String getAuthType()
Returns the auth type, e.g. basic.


getRemoteUser

public java.lang.String getRemoteUser()
Returns the remote user if authenticated.


isUserInRole

public boolean isUserInRole(java.lang.String role)
Returns true if the user is in the given role.


getUserPrincipal

public java.security.Principal getUserPrincipal()
Returns the equivalent principal object for the authenticated user.


isRequestedSessionIdFromUrl

public boolean isRequestedSessionIdFromUrl()
Deprecated. 


getServletContext

public ServletContext getServletContext()
Returns the servlet context for the request

Specified by:
getServletContext in interface ServletRequest
Since:
Servlet 3.0

getServletResponse

public ServletResponse getServletResponse()
Returns the servlet response for the request

Since:
Servlet 3.0

addAsyncListener

public void addAsyncListener(AsyncListener listener)
Adds an async listener for this request

Since:
Servlet 3.0

addAsyncListener

public void addAsyncListener(AsyncListener listener,
                             ServletRequest request,
                             ServletResponse response)
Adds an async listener for this request

Since:
Servlet 3.0

getAsyncContext

public AsyncContext getAsyncContext()
Returns the async context for the request

Specified by:
getAsyncContext in interface ServletRequest
Since:
Servlet 3.0

isAsyncStarted

public boolean isAsyncStarted()
Returns true if the request is in async.

Specified by:
isAsyncStarted in interface ServletRequest
Since:
Servlet 3.0

isAsyncSupported

public boolean isAsyncSupported()
Returns true if the request supports async

Specified by:
isAsyncSupported in interface ServletRequest
Since:
Servlet 3.0

startAsync

public AsyncContext startAsync()
Starts an async mode

Specified by:
startAsync in interface ServletRequest
Since:
Servlet 3.0

startAsync

public AsyncContext startAsync(ServletRequest servletRequest,
                               ServletResponse servletResponse)
Starts an async mode

Specified by:
startAsync in interface ServletRequest
Since:
Servlet 3.0

suspend

public void suspend(long timeout)
Suspend the request

Since:
Servlet 3.0

suspend

public void suspend()
Suspend the request

Since:
Servlet 3.0

resume

public void resume()
Resume the request

Since:
Servlet 3.0

complete

public void complete()
Complete the request

Since:
Servlet 3.0

isSuspended

public boolean isSuspended()
Returns true if the servlet is suspended

Since:
Servlet 3.0

isResumed

public boolean isResumed()
Returns true if the servlet is resumed

Since:
Servlet 3.0

isTimeout

public boolean isTimeout()
Returns true if the servlet timed out

Since:
Servlet 3.0

isInitial

public boolean isInitial()
Returns true for the initial dispatch

Since:
Servlet 3.0

getDispatcherType

public DispatcherType getDispatcherType()
Description copied from interface: ServletRequest
Returns DispatcherType

Specified by:
getDispatcherType in interface ServletRequest
Returns:

free

protected void free()
Clears the wrapper.