Class ServletRequestPathUtils

java.lang.Object
org.springframework.web.util.ServletRequestPathUtils

public abstract class ServletRequestPathUtils extends Object
Utility class to assist with preparation and access to the lookup path for request mapping purposes. This can be the parsed RequestPath representation of the path when use of parsed patterns is enabled or a String path for use with a PathMatcher otherwise.
Since:
5.3
Author:
Rossen Stoyanchev
  • Field Details

    • PATH_ATTRIBUTE

      public static final String PATH_ATTRIBUTE
      Name of Servlet request attribute that holds the parsed RequestPath.
  • Constructor Details

    • ServletRequestPathUtils

      public ServletRequestPathUtils()
  • Method Details

    • parseAndCache

      public static RequestPath parseAndCache(HttpServletRequest request)
      Parse the requestURI to a RequestPath and save it in the request attribute PATH_ATTRIBUTE for subsequent use with parsed patterns.

      The returned RequestPath will have both the contextPath and any servletPath prefix omitted from the pathWithinApplication it exposes.

      This method is typically called by the DispatcherServlet to determine if any HandlerMapping indicates that it uses parsed patterns. After that the pre-parsed and cached RequestPath can be accessed through getParsedRequestPath(ServletRequest).

    • getParsedRequestPath

      public static RequestPath getParsedRequestPath(ServletRequest request)
      Return a previously parsed and cached RequestPath.
      Throws:
      IllegalArgumentException - if not found
    • setParsedRequestPath

      public static void setParsedRequestPath(@Nullable RequestPath requestPath, ServletRequest request)
      Set the cached, parsed RequestPath to the given value.
      Parameters:
      requestPath - the value to set to, or if null the cache value is cleared.
      request - the current request
      Since:
      5.3.3
    • hasParsedRequestPath

      public static boolean hasParsedRequestPath(ServletRequest request)
      Check for a previously parsed and cached RequestPath.
    • clearParsedRequestPath

      public static void clearParsedRequestPath(ServletRequest request)
      Remove the request attribute PATH_ATTRIBUTE that holds a previously parsed and cached RequestPath.
    • getCachedPath

      public static Object getCachedPath(ServletRequest request)
      Return either a pre-resolved String lookupPath or a pre-parsed RequestPath.

      In Spring MVC, when at least one HandlerMapping has parsed PathPatterns enabled, the DispatcherServlet parses and caches the RequestPath which can be also done even earlier with ServletRequestPathFilter. In other cases where HandlerMappings use String pattern matching with PathMatcher, the String lookupPath is resolved separately in each HandlerMapping.

      Parameters:
      request - the current request
      Returns:
      a String lookupPath or a RequestPath
      Throws:
      IllegalArgumentException - if neither is available
    • getCachedPathValue

      public static String getCachedPathValue(ServletRequest request)
      Variant of getCachedPath(ServletRequest) that returns the path for request mapping as a String.

      If the cached path is a pre-parsed RequestPath then the returned String path value is encoded and with path parameters removed.

      If the cached path is a pre-resolved String lookupPath, then the returned String path value depends on how UrlPathHelper that resolved is configured.

      Parameters:
      request - the current request
      Returns:
      the full request mapping path as a String
    • hasCachedPath

      public static boolean hasCachedPath(ServletRequest request)
      Check for a previously resolved String lookupPath or a previously parsed RequestPath.
      Parameters:
      request - the current request
      Returns:
      whether a pre-resolved or pre-parsed path is available