Class HttpServletRequestWrapper

java.lang.Object
javax.servlet.ServletRequestWrapper
javax.servlet.http.HttpServletRequestWrapper
All Implemented Interfaces:
HttpServletRequest, ServletRequest

public class HttpServletRequestWrapper extends ServletRequestWrapper implements HttpServletRequest
Provides a convenient implementation of the HttpServletRequest interface that can be subclassed by developers wishing to adapt the request to a Servlet. This class implements the Wrapper or Decorator pattern. Methods default to calling through to the wrapped request object.
Since:
Servlet 2.3
See Also:
  • Constructor Details

    • HttpServletRequestWrapper

      public HttpServletRequestWrapper(HttpServletRequest request)
      Constructs a request object wrapping the given request.
      Parameters:
      request - The request to wrap
      Throws:
      IllegalArgumentException - if the request is null
  • Method Details

    • getAuthType

      public String getAuthType()
      The default behavior of this method is to return getAuthType() on the wrapped request object.
      Specified by:
      getAuthType in interface HttpServletRequest
      Returns:
      one of the static members BASIC_AUTH, FORM_AUTH, CLIENT_CERT_AUTH, DIGEST_AUTH (suitable for == comparison) or the container-specific string indicating the authentication scheme, or null if the request was not authenticated.
    • getCookies

      public Cookie[] getCookies()
      The default behavior of this method is to return getCookies() on the wrapped request object.
      Specified by:
      getCookies in interface HttpServletRequest
      Returns:
      an array of all the Cookies included with this request, or null if the request has no cookies
    • getDateHeader

      public long getDateHeader(String name)
      The default behavior of this method is to return getDateHeader(String name) on the wrapped request object.
      Specified by:
      getDateHeader in interface HttpServletRequest
      Parameters:
      name - a String specifying the name of the header
      Returns:
      a long value representing the date specified in the header expressed as the number of milliseconds since January 1, 1970 GMT, or -1 if the named header was not included with the request
    • getHeader

      public String getHeader(String name)
      The default behavior of this method is to return getHeader(String name) on the wrapped request object.
      Specified by:
      getHeader in interface HttpServletRequest
      Parameters:
      name - a String specifying the header name
      Returns:
      a String containing the value of the requested header, or null if the request does not have a header of that name
    • getHeaders

      public Enumeration<String> getHeaders(String name)
      The default behavior of this method is to return getHeaders(String name) on the wrapped request object.
      Specified by:
      getHeaders in interface HttpServletRequest
      Parameters:
      name - a String specifying the header name
      Returns:
      an Enumeration containing the values of the requested header. If the request does not have any headers of that name return an empty enumeration. If the container does not allow access to header information, return null
    • getHeaderNames

      public Enumeration<String> getHeaderNames()
      The default behavior of this method is to return getHeaderNames() on the wrapped request object.
      Specified by:
      getHeaderNames in interface HttpServletRequest
      Returns:
      an enumeration of all the header names sent with this request; if the request has no headers, an empty enumeration; if the servlet container does not allow servlets to use this method, null
    • getIntHeader

      public int getIntHeader(String name)
      The default behavior of this method is to return getIntHeader(String name) on the wrapped request object.
      Specified by:
      getIntHeader in interface HttpServletRequest
      Parameters:
      name - a String specifying the name of a request header
      Returns:
      an integer expressing the value of the request header or -1 if the request doesn't have a header of this name
    • getMethod

      public String getMethod()
      The default behavior of this method is to return getMethod() on the wrapped request object.
      Specified by:
      getMethod in interface HttpServletRequest
      Returns:
      a String specifying the name of the method with which this request was made
    • getPathInfo

      public String getPathInfo()
      The default behavior of this method is to return getPathInfo() on the wrapped request object.
      Specified by:
      getPathInfo in interface HttpServletRequest
      Returns:
      a String, decoded by the web container, specifying extra path information that comes after the servlet path but before the query string in the request URL; or null if the URL does not have any extra path information
    • getPathTranslated

      public String getPathTranslated()
      The default behavior of this method is to return getPathTranslated() on the wrapped request object.
      Specified by:
      getPathTranslated in interface HttpServletRequest
      Returns:
      a String specifying the real path, or null if the URL does not have any extra path information
    • getContextPath

      public String getContextPath()
      The default behavior of this method is to return getContextPath() on the wrapped request object.
      Specified by:
      getContextPath in interface HttpServletRequest
      Returns:
      a String specifying the portion of the request URI that indicates the context of the request
    • getQueryString

      public String getQueryString()
      The default behavior of this method is to return getQueryString() on the wrapped request object.
      Specified by:
      getQueryString in interface HttpServletRequest
      Returns:
      a String containing the query string or null if the URL contains no query string. The value is not decoded by the container.
    • getRemoteUser

      public String getRemoteUser()
      The default behavior of this method is to return getRemoteUser() on the wrapped request object.
      Specified by:
      getRemoteUser in interface HttpServletRequest
      Returns:
      a String specifying the login of the user making this request, or null if the user login is not known
    • isUserInRole

      public boolean isUserInRole(String role)
      The default behavior of this method is to return isUserInRole(String role) on the wrapped request object.
      Specified by:
      isUserInRole in interface HttpServletRequest
      Parameters:
      role - a String specifying the name of the role
      Returns:
      a boolean indicating whether the user making this request belongs to a given role; false if the user has not been authenticated
    • getUserPrincipal

      public Principal getUserPrincipal()
      The default behavior of this method is to return getUserPrincipal() on the wrapped request object.
      Specified by:
      getUserPrincipal in interface HttpServletRequest
      Returns:
      a java.security.Principal containing the name of the user making this request; null if the user has not been authenticated
    • getRequestedSessionId

      public String getRequestedSessionId()
      The default behavior of this method is to return getRequestedSessionId() on the wrapped request object.
      Specified by:
      getRequestedSessionId in interface HttpServletRequest
      Returns:
      a String specifying the session ID, or null if the request did not specify a session ID
      See Also:
    • getRequestURI

      public String getRequestURI()
      The default behavior of this method is to return getRequestURI() on the wrapped request object.
      Specified by:
      getRequestURI in interface HttpServletRequest
      Returns:
      a String containing the part of the URL from the protocol name up to the query string
      See Also:
    • getRequestURL

      public StringBuffer getRequestURL()
      The default behavior of this method is to return getRequestURL() on the wrapped request object.
      Specified by:
      getRequestURL in interface HttpServletRequest
      Returns:
      a StringBuffer object containing the reconstructed URL
    • getServletPath

      public String getServletPath()
      The default behavior of this method is to return getServletPath() on the wrapped request object.
      Specified by:
      getServletPath in interface HttpServletRequest
      Returns:
      a String containing the name or path of the servlet being called, as specified in the request URL, decoded, or an empty string if the servlet used to process the request is matched using the "/*" pattern.
    • getSession

      public HttpSession getSession(boolean create)
      The default behavior of this method is to return getSession(boolean create) on the wrapped request object.
      Specified by:
      getSession in interface HttpServletRequest
      Parameters:
      create - true to create a new session for this request if necessary; false to return null if there's no current session
      Returns:
      the HttpSession associated with this request or null if create is false and the request has no valid session
      See Also:
    • getSession

      public HttpSession getSession()
      The default behavior of this method is to return getSession() on the wrapped request object.
      Specified by:
      getSession in interface HttpServletRequest
      Returns:
      the HttpSession associated with this request
      See Also:
    • changeSessionId

      public String changeSessionId()
      The default behavior of this method is to call changeSessionId() on the wrapped request object.
      Specified by:
      changeSessionId in interface HttpServletRequest
      Returns:
      the new session ID allocated to the session
      Since:
      Servlet 3.1
      See Also:
    • isRequestedSessionIdValid

      public boolean isRequestedSessionIdValid()
      The default behavior of this method is to return isRequestedSessionIdValid() on the wrapped request object.
      Specified by:
      isRequestedSessionIdValid in interface HttpServletRequest
      Returns:
      true if this request has an id for a valid session in the current session context; false otherwise
      See Also:
    • isRequestedSessionIdFromCookie

      public boolean isRequestedSessionIdFromCookie()
      The default behavior of this method is to return isRequestedSessionIdFromCookie() on the wrapped request object.
      Specified by:
      isRequestedSessionIdFromCookie in interface HttpServletRequest
      Returns:
      true if the session ID came in as a cookie; otherwise, false
      See Also:
    • isRequestedSessionIdFromURL

      public boolean isRequestedSessionIdFromURL()
      The default behavior of this method is to return isRequestedSessionIdFromURL() on the wrapped request object.
      Specified by:
      isRequestedSessionIdFromURL in interface HttpServletRequest
      Returns:
      true if the session ID came in as part of a URL; otherwise, false
      See Also:
    • isRequestedSessionIdFromUrl

      public boolean isRequestedSessionIdFromUrl()
      Deprecated.
      As of Version 3.0 of the Java Servlet API
      The default behavior of this method is to return isRequestedSessionIdFromUrl() on the wrapped request object.
      Specified by:
      isRequestedSessionIdFromUrl in interface HttpServletRequest
      Returns:
      HttpServletRequest.isRequestedSessionIdFromURL()
    • authenticate

      public boolean authenticate(HttpServletResponse response) throws IOException, ServletException
      Triggers the same authentication process as would be triggered if the request is for a resource that is protected by a security constraint.

      The default behavior of this method is to return HttpServletRequest.authenticate(HttpServletResponse) on the wrapped request object.

      Specified by:
      authenticate in interface HttpServletRequest
      Parameters:
      response - The response to use to return any authentication challenge
      Returns:
      true if the user is successfully authenticated and false if not
      Throws:
      IOException - if the authentication process attempted to read from the request or write to the response and an I/O error occurred
      ServletException - if the authentication failed and the caller is expected to handle the failure
      Since:
      Servlet 3.0
    • login

      public void login(String username, String password) throws ServletException
      Authenticate the provided user name and password and then associated the authenticated user with the request.

      The default behavior of this method is to return HttpServletRequest.login(String, String) on the wrapped request object.

      Specified by:
      login in interface HttpServletRequest
      Parameters:
      username - The user name to authenticate
      password - The password to use to authenticate the user
      Throws:
      ServletException - If any of HttpServletRequest.getRemoteUser(), HttpServletRequest.getUserPrincipal() or HttpServletRequest.getAuthType() are non-null, if the configured authenticator does not support user name and password authentication or if the authentication fails
      Since:
      Servlet 3.0
    • logout

      public void logout() throws ServletException
      Removes any authenticated user from the request.

      The default behavior of this method is to return HttpServletRequest.logout() on the wrapped request object.

      Specified by:
      logout in interface HttpServletRequest
      Throws:
      ServletException - If the logout fails
      Since:
      Servlet 3.0
    • getParts

      public Collection<Part> getParts() throws IOException, ServletException
      Return a collection of all uploaded Parts.

      The default behavior of this method is to return HttpServletRequest.getParts() on the wrapped request object.

      Specified by:
      getParts in interface HttpServletRequest
      Returns:
      A collection of all uploaded Parts.
      Throws:
      IOException - if an I/O error occurs
      ServletException - if the request is not multipart/form-data
      Since:
      Servlet 3.0
    • getPart

      public Part getPart(String name) throws IOException, ServletException
      Gets the named Part or null if the Part does not exist. Triggers upload of all Parts.

      The default behavior of this method is to return HttpServletRequest.getPart(String) on the wrapped request object.

      Specified by:
      getPart in interface HttpServletRequest
      Parameters:
      name - The name of the Part to obtain
      Returns:
      The named Part or null if the Part does not exist
      Throws:
      IOException - if an I/O error occurs
      ServletException - if the request is not multipart/form-data
      Since:
      Servlet 3.0
    • upgrade

      public <T extends HttpUpgradeHandler> T upgrade(Class<T> httpUpgradeHandlerClass) throws IOException, ServletException
      Start the HTTP upgrade process and create and instance of the provided protocol handler class. The connection will be passed this instance once the current request/response pair has completed processing. Calling this method sets the response status to HttpServletResponse.SC_SWITCHING_PROTOCOLS.

      The default behavior of this method is to return HttpServletRequest.upgrade(Class) on the wrapped request object.

      Specified by:
      upgrade in interface HttpServletRequest
      Type Parameters:
      T - The type of the upgrade handler
      Parameters:
      httpUpgradeHandlerClass - The class that implements the upgrade handler
      Returns:
      A newly created instance of the specified upgrade handler type
      Throws:
      IOException - if an I/O error occurred during the upgrade
      ServletException - if the given httpUpgradeHandlerClass fails to be instantiated
      Since:
      Servlet 3.1