org.springframework.web.util.pattern

Class PathPattern

  • java.lang.Object
    • org.springframework.web.util.pattern.PathPattern
  • All Implemented Interfaces:
    java.lang.Comparable<PathPattern>


    public class PathPattern
    extends java.lang.Object
    implements java.lang.Comparable<PathPattern>
    Representation of a parsed path pattern. Includes a chain of path elements for fast matching and accumulates computed state for quick comparison of patterns.

    PathPattern matches URL paths using the following rules:

    • ? matches one character
    • * matches zero or more characters within a path segment
    • ** matches zero or more path segments until the end of the path
    • {spring} matches a path segment and captures it as a variable named "spring"
    • {spring:[a-z]+} matches the regexp [a-z]+ as a path variable named "spring"
    • {*spring} matches zero or more path segments until the end of the path and captures it as a variable named "spring"

    Examples

    • /pages/t?st.php — matches /pages/test.php but also /pages/tast.php but not /pages/toast.php
    • /resources/*.png — matches all .png files in the resources directory
    • /resources/** — matches all files underneath the /resources/ path, including /resources/image.png and /resources/css/spring.css
    • /resources/{*path} — matches all files underneath the /resources/ path and captures their relative path in a variable named "path"; /resources/image.png will match with "spring" -> "/image.png", and /resources/css/spring.css will match with "spring" -> "/css/spring.css"
    • /resources/{filename:\\w+}.dat will match /resources/spring.dat and assign the value "spring" to the filename variable
    Since:
    5.0
    Author:
    Andy Clement, Rossen Stoyanchev
    See Also:
    PathContainer
    • Field Detail

      • SPECIFICITY_COMPARATOR

        public static final java.util.Comparator<PathPattern> SPECIFICITY_COMPARATOR
        Comparator that sorts patterns by specificity as follows:
        1. Null instances are last.
        2. Catch-all patterns are last.
        3. If both patterns are catch-all, consider the length (longer wins).
        4. Compare wildcard and captured variable count (lower wins).
        5. Consider length (longer wins)
    • Method Detail

      • getPatternString

        public java.lang.String getPatternString()
        Return the original String that was parsed to create this PathPattern.
      • matches

        public boolean matches(PathContainer pathContainer)
        Whether this pattern matches the given path.
        Parameters:
        pathContainer - the candidate path to attempt to match against
        Returns:
        true if the path matches this pattern
      • matchAndExtract

        @Nullable
        public PathPattern.PathMatchInfo matchAndExtract(PathContainer pathContainer)
        Match this pattern to the given URI path and return extracted URI template variables as well as path parameters (matrix variables).
        Parameters:
        pathContainer - the candidate path to attempt to match against
        Returns:
        info object with the extracted variables, or null for no match
      • matchStartOfPath

        @Nullable
        public PathPattern.PathRemainingMatchInfo matchStartOfPath(PathContainer pathContainer)
        Match the beginning of the given path and return the remaining portion not covered by this pattern. This is useful for matching nested routes where the path is matched incrementally at each level.
        Parameters:
        pathContainer - the candidate path to attempt to match against
        Returns:
        info object with the match result or null for no match
      • extractPathWithinPattern

        public PathContainer extractPathWithinPattern(PathContainer path)
        Determine the pattern-mapped part for the given path.

        For example:

        • '/docs/cvs/commit.php' and '/docs/cvs/commit.php -> ''
        • '/docs/*' and '/docs/cvs/commit' -> 'cvs/commit'
        • '/docs/cvs/*.php' and '/docs/cvs/commit.php -> 'commit.php'
        • '/docs/**' and '/docs/cvs/commit -> 'cvs/commit'

        Notes:

        • Assumes that matches(org.springframework.http.server.PathContainer) returns true for the same path but does not enforce this.
        • Duplicate occurrences of separators within the returned result are removed
        • Leading and trailing separators are removed from the returned result
        Parameters:
        path - a path that matches this pattern
        Returns:
        the subset of the path that is matched by pattern or "" if none of it is matched by pattern elements
      • compareTo

        public int compareTo(@Nullable
                             PathPattern otherPattern)
        Compare this pattern with a supplied pattern: return -1,0,+1 if this pattern is more specific, the same or less specific than the supplied pattern. The aim is to sort more specific patterns first.
        Specified by:
        compareTo in interface java.lang.Comparable<PathPattern>
      • combine

        public PathPattern combine(PathPattern pattern2string)
        Combine this pattern with another. Currently does not produce a new PathPattern, just produces a new string.
      • equals

        public boolean equals(java.lang.Object other)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object