javax.servlet.http
Class HttpServletRequestWrapper

java.lang.Object
  extended by javax.servlet.ServletRequestWrapper
      extended by javax.servlet.http.HttpServletRequestWrapper
All Implemented Interfaces:
HttpServletRequest, ServletRequest
Direct Known Subclasses:
FastCgiProxy.ProxyRequest, HttpProxy.ProxyRequest, LoadBalance.LoadBalanceRequest, ProxyRule.ProxyRequest

public class HttpServletRequestWrapper
extends ServletRequestWrapper
implements HttpServletRequest

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
 
Fields inherited from interface javax.servlet.http.HttpServletRequest
BASIC_AUTH, CLIENT_CERT_AUTH, DIGEST_AUTH, FORM_AUTH
 
Constructor Summary
HttpServletRequestWrapper(HttpServletRequest request)
          Creates a new request wrapper
 
Method Summary
 boolean authenticate(HttpServletResponse response)
           
 java.lang.String getAuthType()
          Returns the auth type, e.g.
 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.
 java.lang.String getHeader(java.lang.String name)
          Returns the first value for a request header.
 java.util.Enumeration<java.lang.String> getHeaderNames()
          Returns an enumeration of all headers sent by the client.
 java.util.Enumeration<java.lang.String> getHeaders(java.lang.String name)
          Returns all the values for a request header.
 int getIntHeader(java.lang.String name)
          Converts a header value to an integer.
 java.lang.String getMethod()
          Returns the HTTP method, e.g.
 Part getPart(java.lang.String name)
           
 java.util.Collection<Part> getParts()
           
 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 getQueryString()
          Returns the request's query string.
 java.lang.String getRemoteUser()
          Returns the remote user if authenticated.
 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 getServletPath()
          Returns the URI part corresponding to the selected servlet.
 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 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 isUserInRole(java.lang.String role)
          Returns true if the user is in the given role.
 void login(java.lang.String username, java.lang.String password)
           
 void logout()
           
 void setRequest(ServletRequest request)
          Sets the request object for the wrapper.
 
Methods inherited from class javax.servlet.ServletRequestWrapper
getAsyncContext, getAttribute, getAttributeNames, getCharacterEncoding, getContentLength, getContentType, getDispatcherType, getInputStream, getLocalAddr, getLocale, getLocales, getLocalName, getLocalPort, getParameter, getParameterMap, getParameterNames, getParameterValues, getProtocol, getReader, getRealPath, getRemoteAddr, getRemoteHost, getRemotePort, getRequest, getRequestDispatcher, getScheme, getServerName, getServerPort, getServletContext, isAsyncStarted, isAsyncSupported, isSecure, isWrapperFor, isWrapperFor, removeAttribute, setAttribute, setCharacterEncoding, startAsync, startAsync
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface javax.servlet.ServletRequest
getAsyncContext, getAttribute, getAttributeNames, getCharacterEncoding, getContentLength, getContentType, getDispatcherType, getInputStream, getLocalAddr, getLocale, getLocales, getLocalName, getLocalPort, getParameter, getParameterMap, getParameterNames, getParameterValues, getProtocol, getReader, getRealPath, getRemoteAddr, getRemoteHost, getRemotePort, getRequestDispatcher, getScheme, getServerName, getServerPort, getServletContext, isAsyncStarted, isAsyncSupported, isSecure, removeAttribute, setAttribute, setCharacterEncoding, startAsync, startAsync
 

Constructor Detail

HttpServletRequestWrapper

public HttpServletRequestWrapper(HttpServletRequest request)
Creates a new request wrapper

Parameters:
request - the wrapped request
Method Detail

setRequest

public void setRequest(ServletRequest request)
Sets the request object for the wrapper.

Overrides:
setRequest in class ServletRequestWrapper
Parameters:
request - the wrapped request

getMethod

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

Equivalent to CGI's REQUEST_METHOD

Specified by:
getMethod in interface HttpServletRequest

getRequestURI

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

Specified by:
getRequestURI in interface HttpServletRequest

getRequestURL

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

Specified by:
getRequestURL in interface HttpServletRequest
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,

Specified by:
getContextPath in interface HttpServletRequest

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,

Specified by:
getServletPath in interface HttpServletRequest

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.

Specified by:
getPathInfo in interface HttpServletRequest

getPathTranslated

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

Corresponds to CGI's PATH_TRANSLATED

Specified by:
getPathTranslated in interface HttpServletRequest
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

Specified by:
getQueryString in interface HttpServletRequest

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");
 

Specified by:
getHeader in interface HttpServletRequest
Parameters:
name - the header name
Returns:
the header value

getHeaders

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

Specified by:
getHeaders in interface HttpServletRequest
Parameters:
name - the header name
Returns:
an enumeration of the header values.

getHeaderNames

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

Specified by:
getHeaderNames in interface HttpServletRequest

getIntHeader

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

Specified by:
getIntHeader in interface HttpServletRequest
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 = request.getDateHeader("If-Modified-Since");
 

Specified by:
getDateHeader in interface HttpServletRequest
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.

Specified by:
getCookies in interface HttpServletRequest

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.

Specified by:
getSession in interface HttpServletRequest
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.

Specified by:
getSession in interface HttpServletRequest

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.

Specified by:
getRequestedSessionId in interface HttpServletRequest

isRequestedSessionIdValid

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

Specified by:
isRequestedSessionIdValid in interface HttpServletRequest

isRequestedSessionIdFromCookie

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

Specified by:
isRequestedSessionIdFromCookie in interface HttpServletRequest

isRequestedSessionIdFromURL

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

Specified by:
isRequestedSessionIdFromURL in interface HttpServletRequest

getAuthType

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

Specified by:
getAuthType in interface HttpServletRequest

getRemoteUser

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

Specified by:
getRemoteUser in interface HttpServletRequest

isUserInRole

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

Specified by:
isUserInRole in interface HttpServletRequest

getUserPrincipal

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

Specified by:
getUserPrincipal in interface HttpServletRequest

isRequestedSessionIdFromUrl

public boolean isRequestedSessionIdFromUrl()
Deprecated. 

Specified by:
isRequestedSessionIdFromUrl in interface HttpServletRequest

authenticate

public boolean authenticate(HttpServletResponse response)
                     throws java.io.IOException,
                            ServletException
Specified by:
authenticate in interface HttpServletRequest
Throws:
java.io.IOException
ServletException
Since:
Servlet 3.0

getPart

public Part getPart(java.lang.String name)
             throws java.io.IOException,
                    ServletException
Specified by:
getPart in interface HttpServletRequest
Throws:
java.io.IOException
ServletException
Since:
Servlet 3.0

getParts

public java.util.Collection<Part> getParts()
                                    throws java.io.IOException,
                                           ServletException
Specified by:
getParts in interface HttpServletRequest
Throws:
java.io.IOException
ServletException
Since:
Servlet 3.0

login

public void login(java.lang.String username,
                  java.lang.String password)
           throws ServletException
Specified by:
login in interface HttpServletRequest
Throws:
ServletException
Since:
Servlet 3.0

logout

public void logout()
            throws ServletException
Specified by:
logout in interface HttpServletRequest
Throws:
ServletException
Since:
Servlet 3.0