Class ServletWebRequest

All Implemented Interfaces:
NativeWebRequest, RequestAttributes, WebRequest
Direct Known Subclasses:
DispatcherServletWebRequest, StandardServletAsyncWebRequest

public class ServletWebRequest extends ServletRequestAttributes implements NativeWebRequest
Since:
2.0
Author:
Juergen Hoeller, Brian Clozel, Markus Malkusch
  • Constructor Details

    • ServletWebRequest

      public ServletWebRequest(HttpServletRequest request)
      Create a new ServletWebRequest instance for the given request.
      Parameters:
      request - current HTTP request
    • ServletWebRequest

      public ServletWebRequest(HttpServletRequest request, @Nullable HttpServletResponse response)
      Create a new ServletWebRequest instance for the given request/response pair.
      Parameters:
      request - current HTTP request
      response - current HTTP response (for automatic last-modified handling)
  • Method Details

    • getNativeRequest

      public Object getNativeRequest()
      Description copied from interface: NativeWebRequest
      Return the underlying native request object.
      Specified by:
      getNativeRequest in interface NativeWebRequest
      See Also:
    • getNativeResponse

      public Object getNativeResponse()
      Description copied from interface: NativeWebRequest
      Return the underlying native response object, if any.
      Specified by:
      getNativeResponse in interface NativeWebRequest
      See Also:
    • getNativeRequest

      public <T> T getNativeRequest(@Nullable Class<T> requiredType)
      Description copied from interface: NativeWebRequest
      Return the underlying native request object, if available.
      Specified by:
      getNativeRequest in interface NativeWebRequest
      Parameters:
      requiredType - the desired type of request object
      Returns:
      the matching request object, or null if none of that type is available
      See Also:
    • getNativeResponse

      public <T> T getNativeResponse(@Nullable Class<T> requiredType)
      Description copied from interface: NativeWebRequest
      Return the underlying native response object, if available.
      Specified by:
      getNativeResponse in interface NativeWebRequest
      Parameters:
      requiredType - the desired type of response object
      Returns:
      the matching response object, or null if none of that type is available
      See Also:
    • getHttpMethod

      public HttpMethod getHttpMethod()
      Return the HTTP method of the request.
      Since:
      4.0.2
    • getHeader

      @Nullable public String getHeader(String headerName)
      Description copied from interface: WebRequest
      Return the request header of the given name, or null if none.

      Retrieves the first header value in case of a multi-value header.

      Specified by:
      getHeader in interface WebRequest
      See Also:
    • getHeaderValues

      @Nullable public String[] getHeaderValues(String headerName)
      Description copied from interface: WebRequest
      Return the request header values for the given header name, or null if none.

      A single-value header will be exposed as an array with a single element.

      Specified by:
      getHeaderValues in interface WebRequest
      See Also:
    • getHeaderNames

      public Iterator<String> getHeaderNames()
      Description copied from interface: WebRequest
      Return an Iterator over request header names.
      Specified by:
      getHeaderNames in interface WebRequest
      See Also:
    • getParameter

      @Nullable public String getParameter(String paramName)
      Description copied from interface: WebRequest
      Return the request parameter of the given name, or null if none.

      Retrieves the first parameter value in case of a multi-value parameter.

      Specified by:
      getParameter in interface WebRequest
      See Also:
    • getParameterValues

      @Nullable public String[] getParameterValues(String paramName)
      Description copied from interface: WebRequest
      Return the request parameter values for the given parameter name, or null if none.

      A single-value parameter will be exposed as an array with a single element.

      Specified by:
      getParameterValues in interface WebRequest
      See Also:
    • getParameterNames

      public Iterator<String> getParameterNames()
      Description copied from interface: WebRequest
      Return an Iterator over request parameter names.
      Specified by:
      getParameterNames in interface WebRequest
      See Also:
    • getParameterMap

      public Map<String,String[]> getParameterMap()
      Description copied from interface: WebRequest
      Return an immutable Map of the request parameters, with parameter names as map keys and parameter values as map values. The map values will be of type String array.

      A single-value parameter will be exposed as an array with a single element.

      Specified by:
      getParameterMap in interface WebRequest
      See Also:
    • getLocale

      public Locale getLocale()
      Description copied from interface: WebRequest
      Return the primary Locale for this request.
      Specified by:
      getLocale in interface WebRequest
      See Also:
    • getContextPath

      public String getContextPath()
      Description copied from interface: WebRequest
      Return the context path for this request (usually the base path that the current web application is mapped to).
      Specified by:
      getContextPath in interface WebRequest
      See Also:
    • getRemoteUser

      @Nullable public String getRemoteUser()
      Description copied from interface: WebRequest
      Return the remote user for this request, if any.
      Specified by:
      getRemoteUser in interface WebRequest
      See Also:
    • getUserPrincipal

      @Nullable public Principal getUserPrincipal()
      Description copied from interface: WebRequest
      Return the user principal for this request, if any.
      Specified by:
      getUserPrincipal in interface WebRequest
      See Also:
    • isUserInRole

      public boolean isUserInRole(String role)
      Description copied from interface: WebRequest
      Determine whether the user is in the given role for this request.
      Specified by:
      isUserInRole in interface WebRequest
      See Also:
    • isSecure

      public boolean isSecure()
      Description copied from interface: WebRequest
      Return whether this request has been sent over a secure transport mechanism (such as SSL).
      Specified by:
      isSecure in interface WebRequest
      See Also:
    • checkNotModified

      public boolean checkNotModified(long lastModifiedTimestamp)
      Description copied from interface: WebRequest
      Check whether the requested resource has been modified given the supplied last-modified timestamp (as determined by the application).

      This will also transparently set the "Last-Modified" response header and HTTP status when applicable.

      Typical usage:

       public String myHandleMethod(WebRequest request, Model model) {
         long lastModified = // application-specific calculation
         if (request.checkNotModified(lastModified)) {
           // shortcut exit - no further processing necessary
           return null;
         }
         // further request processing, actually building content
         model.addAttribute(...);
         return "myViewName";
       }

      This method works with conditional GET/HEAD requests, but also with conditional POST/PUT/DELETE requests.

      Note: you can use either this #checkNotModified(long) method; or WebRequest.checkNotModified(String). If you want to enforce both a strong entity tag and a Last-Modified value, as recommended by the HTTP specification, then you should use WebRequest.checkNotModified(String, long).

      If the "If-Modified-Since" header is set but cannot be parsed to a date value, this method will ignore the header and proceed with setting the last-modified timestamp on the response.

      Specified by:
      checkNotModified in interface WebRequest
      Parameters:
      lastModifiedTimestamp - the last-modified timestamp in milliseconds that the application determined for the underlying resource
      Returns:
      whether the request qualifies as not modified, allowing to abort request processing and relying on the response telling the client that the content has not been modified
    • checkNotModified

      public boolean checkNotModified(String etag)
      Description copied from interface: WebRequest
      Check whether the requested resource has been modified given the supplied ETag (entity tag), as determined by the application.

      This will also transparently set the "ETag" response header and HTTP status when applicable.

      Typical usage:

       public String myHandleMethod(WebRequest request, Model model) {
         String eTag = // application-specific calculation
         if (request.checkNotModified(eTag)) {
           // shortcut exit - no further processing necessary
           return null;
         }
         // further request processing, actually building content
         model.addAttribute(...);
         return "myViewName";
       }

      Note: you can use either this #checkNotModified(String) method; or WebRequest.checkNotModified(long). If you want to enforce both a strong entity tag and a Last-Modified value, as recommended by the HTTP specification, then you should use WebRequest.checkNotModified(String, long).

      Specified by:
      checkNotModified in interface WebRequest
      Parameters:
      etag - the entity tag that the application determined for the underlying resource. This parameter will be padded with quotes (") if necessary.
      Returns:
      true if the request does not require further processing.
    • checkNotModified

      public boolean checkNotModified(@Nullable String eTag, long lastModifiedTimestamp)
      Description copied from interface: WebRequest
      Check whether the requested resource has been modified given the supplied ETag (entity tag) and last-modified timestamp, as determined by the application.

      This will also transparently set the "ETag" and "Last-Modified" response headers, and HTTP status when applicable.

      Typical usage:

       public String myHandleMethod(WebRequest request, Model model) {
         String eTag = // application-specific calculation
         long lastModified = // application-specific calculation
         if (request.checkNotModified(eTag, lastModified)) {
           // shortcut exit - no further processing necessary
           return null;
         }
         // further request processing, actually building content
         model.addAttribute(...);
         return "myViewName";
       }

      This method works with conditional GET/HEAD requests, but also with conditional POST/PUT/DELETE requests.

      Note: The HTTP specification recommends setting both ETag and Last-Modified values, but you can also use #checkNotModified(String) or WebRequest.checkNotModified(long).

      Specified by:
      checkNotModified in interface WebRequest
      Parameters:
      eTag - the entity tag that the application determined for the underlying resource. This parameter will be padded with quotes (") if necessary.
      lastModifiedTimestamp - the last-modified timestamp in milliseconds that the application determined for the underlying resource
      Returns:
      true if the request does not require further processing.
    • isNotModified

      public boolean isNotModified()
    • getDescription

      public String getDescription(boolean includeClientInfo)
      Description copied from interface: WebRequest
      Get a short description of this request, typically containing request URI and session id.
      Specified by:
      getDescription in interface WebRequest
      Parameters:
      includeClientInfo - whether to include client-specific information such as session id and user name
      Returns:
      the requested description as String
    • toString

      public String toString()
      Overrides:
      toString in class ServletRequestAttributes