Class WebRequestUtils


  • public abstract class WebRequestUtils
    extends java.lang.Object
    WebRequest Utilities class
    Since:
    1.0.0
    Author:
    Mercy
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String METHOD_HEADER_NAME
      The header name for HTTP method
      static java.lang.String PATH_ATTRIBUTE
      Name of Servlet request attribute that holds a resolved lookupPath.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void addCookie​(org.springframework.web.context.request.NativeWebRequest request, java.lang.String cookieName, java.lang.String cookieValue)
      Adds the specified cookie to the response.
      static void addHeader​(org.springframework.web.context.request.NativeWebRequest request, java.lang.String headerName, java.lang.String... headerValues)
      Adds a response header with the given name and value.
      static java.lang.Object getBestMatchingHandler​(org.springframework.web.context.request.NativeWebRequest request)
      Get the mapped handler for the best matching pattern.
      static java.lang.String getBestMatchingPattern​(org.springframework.web.context.request.NativeWebRequest request)
      Get the best matching pattern within the handler mapping.
      static java.lang.String getContentType​(org.springframework.web.context.request.NativeWebRequest request)  
      static java.lang.String getCookieValue​(org.springframework.web.context.request.NativeWebRequest request, java.lang.String cookieName)
      Get the cookie value for the given cookie name.
      static java.lang.String getHeader​(org.springframework.web.context.request.NativeWebRequest request, java.lang.String headerName)
      Get the header value from the request by the given header name.
      static java.lang.String[] getHeaderValues​(org.springframework.web.context.request.NativeWebRequest request, java.lang.String headerName)
      Get the header values from the request by the given header name.
      static java.util.Map<java.lang.String,​org.springframework.util.MultiValueMap<java.lang.String,​java.lang.String>> getMatrixVariables​(org.springframework.web.context.request.NativeWebRequest request)
      Get a map with URI variable names and a corresponding MultiValueMap of URI matrix variables for each.
      static java.lang.String getMethod​(org.springframework.web.context.request.NativeWebRequest request)
      Get the HTTP method of the current request.
      static java.lang.String getPathWithinHandlerMapping​(org.springframework.web.context.request.NativeWebRequest request)
      Get the path within the handler mapping, in case of a pattern match, or the full relevant URI (typically within the DispatcherServlet's mapping) else.
      static java.util.Set<org.springframework.http.MediaType> getProducibleMediaTypes​(org.springframework.web.context.request.NativeWebRequest request)
      Get the set of producible MediaTypes applicable to the mapped handler.
      static <T> T getRequestBody​(org.springframework.web.context.request.NativeWebRequest request, java.lang.Class<T> requestBodyType)  
      static java.lang.String getResolvedLookupPath​(org.springframework.web.context.request.NativeWebRequest request)
      Return a previously resolved lookupPath.
      protected static SpringWebHelper getSpringWebHelper​(org.springframework.web.context.request.NativeWebRequest request)  
      static java.util.Map<java.lang.String,​java.lang.String> getUriTemplateVariables​(org.springframework.web.context.request.NativeWebRequest request)
      Get the URI templates map, mapping variable names to values.
      static boolean hasBody​(org.springframework.web.context.request.NativeWebRequest request)  
      static boolean isPreFlightRequest​(org.springframework.web.context.request.NativeWebRequest request)
      Returns true if the request is a valid CORS pre-flight one by checking OPTIONS method with Origin and Access-Control-Request-Method headers presence.
      static org.springframework.http.MediaType parseContentType​(org.springframework.web.context.request.NativeWebRequest request)  
      static void setHeader​(org.springframework.web.context.request.NativeWebRequest request, java.lang.String headerName, java.lang.String headerValue)
      Sets a response header with the given name and value.
      static void writeResponseBody​(org.springframework.web.context.request.NativeWebRequest request, java.lang.String name, java.lang.String value)
      Write value to the response body.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • METHOD_HEADER_NAME

        public static final java.lang.String METHOD_HEADER_NAME
        The header name for HTTP method
        See Also:
        Constant Field Values
      • PATH_ATTRIBUTE

        public static final java.lang.String PATH_ATTRIBUTE
        Name of Servlet request attribute that holds a resolved lookupPath.
        Since:
        Spring Framework 5.3
    • Method Detail

      • getMethod

        @Nullable
        public static java.lang.String getMethod​(@Nonnull
                                                 org.springframework.web.context.request.NativeWebRequest request)
        Get the HTTP method of the current request.
        Parameters:
        request - the NativeWebRequest
        Returns:
        the HTTP method if found, otherwise null
        See Also:
        HttpServletRequest#getMethod(), HttpRequest.getMethod()
      • isPreFlightRequest

        public static boolean isPreFlightRequest​(@Nonnull
                                                 org.springframework.web.context.request.NativeWebRequest request)
        Returns true if the request is a valid CORS pre-flight one by checking OPTIONS method with Origin and Access-Control-Request-Method headers presence.
      • getContentType

        public static java.lang.String getContentType​(@Nonnull
                                                      org.springframework.web.context.request.NativeWebRequest request)
      • parseContentType

        @Nullable
        public static org.springframework.http.MediaType parseContentType​(@Nonnull
                                                                          org.springframework.web.context.request.NativeWebRequest request)
      • hasBody

        public static boolean hasBody​(@Nonnull
                                      org.springframework.web.context.request.NativeWebRequest request)
      • getResolvedLookupPath

        public static java.lang.String getResolvedLookupPath​(@Nonnull
                                                             org.springframework.web.context.request.NativeWebRequest request)
        Return a previously resolved lookupPath.
        Parameters:
        request - the current request
        Returns:
        the previously resolved lookupPath
        Throws:
        java.lang.IllegalArgumentException - if the lookup path is not found
      • getHeader

        @Nullable
        public static java.lang.String getHeader​(@Nonnull
                                                 org.springframework.web.context.request.NativeWebRequest request,
                                                 java.lang.String headerName)
        Get the header value from the request by the given header name.
        Parameters:
        request - the NativeWebRequest
        headerName - the name of header
        Returns:
        the header value if found, otherwise null
        See Also:
        HttpServletRequest.getHeader(String), HttpMessage.getHeaders(), HttpHeaders.getFirst(String)
      • getHeaderValues

        @Nullable
        public static java.lang.String[] getHeaderValues​(@Nonnull
                                                         org.springframework.web.context.request.NativeWebRequest request,
                                                         java.lang.String headerName)
        Get the header values from the request by the given header name.
        Parameters:
        request - the NativeWebRequest
        headerName - the name of header
        Returns:
        the header values if found, otherwise null
        See Also:
        HttpServletRequest.getHeaders(String), HttpMessage.getHeaders(), HttpHeaders.get(Object)
      • setHeader

        public static void setHeader​(@Nonnull
                                     org.springframework.web.context.request.NativeWebRequest request,
                                     @Nullable
                                     java.lang.String headerName,
                                     @Nullable
                                     java.lang.String headerValue)
        Sets a response header with the given name and value. If the header had already been set, the new value overwrites all previous values.
        Parameters:
        request - the NativeWebRequest
        headerName - the name of header
        headerValue - the header value
        See Also:
        HttpServletResponse.setHeader(String, String), HttpHeaders.set(String, String)
      • addHeader

        public static void addHeader​(@Nonnull
                                     org.springframework.web.context.request.NativeWebRequest request,
                                     @Nullable
                                     java.lang.String headerName,
                                     @Nullable
                                     java.lang.String... headerValues)
        Adds a response header with the given name and value. This method allows response headers to have multiple values.
        Parameters:
        request - the NativeWebRequest
        headerName - the name of header
        headerValues - the header values
        See Also:
        HttpServletResponse.addHeader(String, String), HttpHeaders.add(String, String)
      • getCookieValue

        @Nullable
        public static java.lang.String getCookieValue​(@Nonnull
                                                      org.springframework.web.context.request.NativeWebRequest request,
                                                      java.lang.String cookieName)
        Get the cookie value for the given cookie name.
        Parameters:
        request - the NativeWebRequest
        cookieName - the name of Cookie
        Returns:
        the cookie value if found, otherwise null
      • addCookie

        public static void addCookie​(@Nonnull
                                     org.springframework.web.context.request.NativeWebRequest request,
                                     java.lang.String cookieName,
                                     java.lang.String cookieValue)
        Adds the specified cookie to the response. This method can be called multiple times to set more than one cookie.
        Parameters:
        request - the NativeWebRequest
        cookieName - the name of Cookie
        cookieValue - the cookie value
        See Also:
        HttpServletResponse.addCookie(Cookie), ServerHttpResponse.addCookie(ResponseCookie)
      • getRequestBody

        @Nullable
        public static <T> T getRequestBody​(@Nonnull
                                           org.springframework.web.context.request.NativeWebRequest request,
                                           java.lang.Class<T> requestBodyType)
      • writeResponseBody

        public static void writeResponseBody​(@Nonnull
                                             org.springframework.web.context.request.NativeWebRequest request,
                                             java.lang.String name,
                                             java.lang.String value)
        Write value to the response body.
        Parameters:
        request - the NativeWebRequest
        name - the name
        value - the value
      • getBestMatchingHandler

        @Nullable
        public static java.lang.Object getBestMatchingHandler​(@Nonnull
                                                              org.springframework.web.context.request.NativeWebRequest request)
        Get the mapped handler for the best matching pattern.
        Parameters:
        request - the NativeWebRequest
        Returns:
        the mapped handler for the best matching pattern if found, otherwise null
        See Also:
        HandlerMethod, org.springframework.web.servlet.function.RouterFunction, org.springframework.web.reactive.function.server.RouterFunction, org.springframework.web.servlet.HandlerMapping#getHandler, org.springframework.web.reactive.HandlerMapping#getHandler, org.springframework.web.servlet.HandlerMapping#BEST_MATCHING_HANDLER_ATTRIBUTE, org.springframework.web.reactive.HandlerMapping#BEST_MATCHING_HANDLER_ATTRIBUTE
      • getPathWithinHandlerMapping

        @Nullable
        public static java.lang.String getPathWithinHandlerMapping​(@Nonnull
                                                                   org.springframework.web.context.request.NativeWebRequest request)
        Get the path within the handler mapping, in case of a pattern match, or the full relevant URI (typically within the DispatcherServlet's mapping) else.
        Parameters:
        request - the NativeWebRequest
        Returns:
        the path within handler mapping if found, otherwise null
        See Also:
        org.springframework.web.servlet.handler.AbstractUrlHandlerMapping#exposePathWithinMapping, org.springframework.web.reactive.handler.AbstractUrlHandlerMapping#lookupHandler, org.springframework.web.servlet.HandlerMapping#PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, org.springframework.web.reactive.HandlerMapping#PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE
      • getBestMatchingPattern

        @Nullable
        public static java.lang.String getBestMatchingPattern​(@Nonnull
                                                              org.springframework.web.context.request.NativeWebRequest request)
        Get the best matching pattern within the handler mapping.
        Parameters:
        request - the NativeWebRequest
        Returns:
        the best matching pattern within the handler mapping if found, otherwise null
        See Also:
        RequestMapping.path(), org.springframework.web.servlet.handler.AbstractUrlHandlerMapping#exposePathWithinMapping, org.springframework.web.reactive.handler.AbstractUrlHandlerMapping#lookupHandler, org.springframework.web.servlet.HandlerMapping#BEST_MATCHING_PATTERN_ATTRIBUTE, org.springframework.web.reactive.HandlerMapping#BEST_MATCHING_PATTERN_ATTRIBUTE
      • getUriTemplateVariables

        @Nullable
        public static java.util.Map<java.lang.String,​java.lang.String> getUriTemplateVariables​(@Nonnull
                                                                                                     org.springframework.web.context.request.NativeWebRequest request)
        Get the URI templates map, mapping variable names to values.
        Parameters:
        request - the NativeWebRequest
        Returns:
        the URI template variables if found, otherwise null
        See Also:
        PathVariable, org.springframework.web.servlet.handler.AbstractUrlHandlerMapping#exposeUriTemplateVariables, org.springframework.web.reactive.handler.AbstractUrlHandlerMapping#lookupHandler, org.springframework.web.servlet.HandlerMapping#URI_TEMPLATE_VARIABLES_ATTRIBUTE, org.springframework.web.reactive.HandlerMapping#URI_TEMPLATE_VARIABLES_ATTRIBUTE
      • getMatrixVariables

        @Nullable
        public static java.util.Map<java.lang.String,​org.springframework.util.MultiValueMap<java.lang.String,​java.lang.String>> getMatrixVariables​(org.springframework.web.context.request.NativeWebRequest request)
        Get a map with URI variable names and a corresponding MultiValueMap of URI matrix variables for each.
        Parameters:
        request - the NativeWebRequest
        Returns:
        the matrix variables if found, otherwise null
        See Also:
        MatrixVariable, org.springframework.web.servlet.mvc.method.annotation.MatrixVariableMapMethodArgumentResolver#resolveArgument, org.springframework.web.reactive.result.method.annotation.MatrixVariableMapMethodArgumentResolver#resolveArgumentValue, org.springframework.web.servlet.HandlerMapping#MATRIX_VARIABLES_ATTRIBUTE, org.springframework.web.reactive.HandlerMapping#MATRIX_VARIABLES_ATTRIBUTE
      • getProducibleMediaTypes

        @Nullable
        public static java.util.Set<org.springframework.http.MediaType> getProducibleMediaTypes​(@Nonnull
                                                                                                org.springframework.web.context.request.NativeWebRequest request)
        Get the set of producible MediaTypes applicable to the mapped handler.
        Parameters:
        request - the NativeWebRequest
        Returns:
        the producible media types if found, otherwise null
        See Also:
        RequestMapping.produces(), org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping#handleMatch, org.springframework.web.reactive.result.method.RequestMappingInfoHandlerMapping#handleMatch, org.springframework.web.servlet.HandlerMapping#PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, org.springframework.web.reactive.HandlerMapping#PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE
      • getSpringWebHelper

        protected static SpringWebHelper getSpringWebHelper​(org.springframework.web.context.request.NativeWebRequest request)