Class 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.
Rossen Stoyanchev
  • Field Details


      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.
      IllegalArgumentException - if not found
    • setParsedRequestPath

      public static void setParsedRequestPath(@Nullable RequestPath requestPath, ServletRequest request)
      Set the cached, parsed RequestPath to the given value.
      requestPath - the value to set to, or if null the cache value is cleared.
      request - the current request
    • 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.

      request - the current request
      a String lookupPath or a RequestPath
      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.

      request - the current request
      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.
      request - the current request
      whether a pre-resolved or pre-parsed path is available