Top Description Inners Fields Constructors Methods
org.springframework.web.util.pattern

public Class PathPattern

extends Object
implements Comparable<PathPattern>
Class Inheritance
All Implemented Interfaces
java.lang.Comparable
Imports
java.util.Collections, .Comparator, .HashMap, .List, .Map, .StringJoiner, org.springframework.http.server.PathContainer, .PathContainer.Element, .PathContainer.PathSegment, .PathContainer.Separator, org.springframework.lang.Nullable, org.springframework.util.CollectionUtils, .MultiValueMap, .StringUtils

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:

Note

In contrast to org.springframework.util.AntPathMatcher, ** is supported only at the end of a pattern. For example /pages/{**} is valid but /pages/{**}/details is not. The same applies also to the capturing variant {*spring}. The aim is to eliminate ambiguity when comparing patterns for specificity.

Examples

Authors
Andy Clement, Rossen Stoyanchev
Since
5.0
See Also
PathContainer

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv class
PathPattern.MatchingContext

Encapsulates context when attempting a match.

public static class
PathPattern.PathMatchInfo

Holder for URI variables and path parameters (matrix variables) extracted based on the pattern for a given matched path.

public static class
PathPattern.PathRemainingMatchInfo

Holder for the result of a match on the start of a pattern.

Field Summary

Modifier and TypeField and Description
private int
capturedVariableCount

How many variables are captured in this pattern.

private final boolean
caseSensitive

Will this match candidates in a case-sensitive way?

private boolean
catchAll

Does the pattern end with {*...}.

private static final PathContainer
private boolean
endsWithSeparatorWildcard

Does the pattern end with '<separator>'.

private final PathElement
head

First path element in the parsed chain of path elements for this pattern.

private final boolean
matchOptionalTrailingSeparator

If this pattern has no trailing slash, allow candidates to include one and still match successfully.

private int
normalizedLength

The normalized length is trying to measure the 'active' part of the pattern.

private final PathPatternParser
parser

The parser used to construct this pattern.

private final PathContainer.Options
pathOptions

The options to use to parse a pattern.

private final String
patternString

The text of the parsed pattern.

private int
score

Score is used to quickly compare patterns.

public static final 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)

Constructor Summary

AccessConstructor and Description
pack-priv
PathPattern(String patternText, PathPatternParser parser, PathElement head)

Method Summary

Modifier and TypeMethod and Description
public PathPattern
combine(PathPattern pattern2string)

Combine this pattern with another.

public int
compareTo(PathPattern
the object to be compared.
otherPattern
)

Implements java.lang.Comparable.compareTo.

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.
pack-priv String

Returns:

the string form of the pattern
computePatternString
()

Return the string form of the pattern built from walking the path element chain.

private String

Returns:

joined path that may include separator if necessary
concat
(String
first path
path1
,
String
second path
path2
)

Join two paths together including a separator if necessary.

public boolean
equals(Object
the reference object with which to compare.
other
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public PathContainer

Returns:

the subset of the path that is matched by pattern or "" if none of it is matched by pattern elements
extractPathWithinPattern
(PathContainer
a path that matches this pattern
path
)

Determine the pattern-mapped part for the given path.

pack-priv int
pack-priv PathElement
pack-priv int
getNormalizedLength()

The normalized length is trying to measure the 'active' part of the pattern.

public String
getPatternString()

Return the original String that was parsed to create this PathPattern.

pack-priv int
pack-priv char
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.
private boolean

Returns:

true has more than zero elements
hasLength
(PathContainer
a path container
container
)

Return if the container is not null and has more than zero elements.

public boolean
hasPatternSyntax()

Whether the pattern string contains pattern syntax that would require use of matches(PathContainer), or if it is a regular String that could be compared directly to others.

pack-priv boolean
public PathPattern.PathMatchInfo

Returns:

info object with the extracted variables, or null for no match
matchAndExtract
(PathContainer
the candidate path to attempt to match against
pathContainer
)

Match this pattern to the given URI path and return extracted URI template variables as well as path parameters (matrix variables).

public boolean

Returns:

true if the path matches this pattern
matches
(PathContainer
the candidate path to attempt to match against
pathContainer
)

Whether this pattern matches the given path.

public PathPattern.PathRemainingMatchInfo

Returns:

info object with the match result or null for no match
matchStartOfPath
(PathContainer
the candidate path to attempt to match against
pathContainer
)

Match the beginning of the given path and return the remaining portion not covered by this pattern.

private boolean
private static int
pack-priv String
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

capturedVariableCountback to summary
private int capturedVariableCount

How many variables are captured in this pattern.

caseSensitiveback to summary
private final boolean caseSensitive

Will this match candidates in a case-sensitive way? (case sensitivity at parse time).

catchAllback to summary
private boolean catchAll

Does the pattern end with {*...}.

EMPTY_PATHback to summary
private static final PathContainer EMPTY_PATH
endsWithSeparatorWildcardback to summary
private boolean endsWithSeparatorWildcard

Does the pattern end with '<separator>'.

matchOptionalTrailingSeparatorback to summary
private final boolean matchOptionalTrailingSeparator

If this pattern has no trailing slash, allow candidates to include one and still match successfully.

normalizedLengthback to summary
private int normalizedLength

The normalized length is trying to measure the 'active' part of the pattern. It is computed by assuming all captured variables have a normalized length of 1. Effectively this means changing your variable name lengths isn't going to change the length of the active part of the pattern. Useful when comparing two patterns.

parserback to summary
private final PathPatternParser parser

The parser used to construct this pattern.

pathOptionsback to summary
private final PathContainer.Options pathOptions

The options to use to parse a pattern.

patternStringback to summary
private final String patternString

The text of the parsed pattern.

scoreback to summary
private int score

Score is used to quickly compare patterns. Different pattern components are given different weights. A 'lower score' is more specific. Current weights:

  • Captured variables are worth 1
  • Wildcard is worth 100
SPECIFICITY_COMPARATORback to summary
public static final 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)

Constructor Detail

PathPatternback to summary
pack-priv PathPattern(String patternText, PathPatternParser parser, PathElement head)
Annotations
@SuppressWarnings:deprecation

Method Detail

combineback to summary
public PathPattern combine(PathPattern pattern2string)

Combine this pattern with another.

compareToback to summary
public int compareTo(PathPattern otherPattern)

Implements java.lang.Comparable.compareTo.

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.

Parameters
otherPattern:PathPattern

Doc from java.lang.Comparable.compareTo.

the object to be compared.

Returns:int

Doc from java.lang.Comparable.compareTo.

a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

Annotations
@Override
computePatternStringback to summary
pack-priv String computePatternString()

Return the string form of the pattern built from walking the path element chain.

Returns:String

the string form of the pattern

concatback to summary
private String concat(String path1, String path2)

Join two paths together including a separator if necessary. Extraneous separators are removed (if the first path ends with one and the second path starts with one).

Parameters
path1:String

first path

path2:String

second path

Returns:String

joined path that may include separator if necessary

equalsback to summary
public boolean equals(Object other)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
other:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
extractPathWithinPatternback to summary
public PathContainer extractPathWithinPattern(PathContainer path)

Determine the pattern-mapped part for the given path.

For example:

  • '/docs/cvs/commit.html' and '/docs/cvs/commit.html → ''
  • '/docs/*' and '/docs/cvs/commit' → 'cvs/commit'
  • '/docs/cvs/*.html' and '/docs/cvs/commit.html → 'commit.html'
  • '/docs/**' and '/docs/cvs/commit → 'cvs/commit'

Notes:

  • Assumes that matches 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:PathContainer

a path that matches this pattern

Returns:PathContainer

the subset of the path that is matched by pattern or "" if none of it is matched by pattern elements

getCapturedVariableCountback to summary
pack-priv int getCapturedVariableCount()
getHeadSectionback to summary
pack-priv PathElement getHeadSection()
Annotations
@Nullable
getNormalizedLengthback to summary
pack-priv int getNormalizedLength()

The normalized length is trying to measure the 'active' part of the pattern. It is computed by assuming all capture variables have a normalized length of 1. Effectively this means changing your variable name lengths isn't going to change the length of the active part of the pattern. Useful when comparing two patterns.

getPatternStringback to summary
public String getPatternString()

Return the original String that was parsed to create this PathPattern.

getScoreback to summary
pack-priv int getScore()
getSeparatorback to summary
pack-priv char getSeparator()
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object.

Annotations
@Override
hasLengthback to summary
private boolean hasLength(PathContainer container)

Return if the container is not null and has more than zero elements.

Parameters
container:PathContainer

a path container

Returns:boolean

true has more than zero elements

hasPatternSyntaxback to summary
public boolean hasPatternSyntax()

Whether the pattern string contains pattern syntax that would require use of matches(PathContainer), or if it is a regular String that could be compared directly to others.

Since
5.2
isCatchAllback to summary
pack-priv boolean isCatchAll()
matchAndExtractback to summary
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:PathContainer

the candidate path to attempt to match against

Returns:PathPattern.PathMatchInfo

info object with the extracted variables, or null for no match

Annotations
@Nullable
matchesback to summary
public boolean matches(PathContainer pathContainer)

Whether this pattern matches the given path.

Parameters
pathContainer:PathContainer

the candidate path to attempt to match against

Returns:boolean

true if the path matches this pattern

matchStartOfPathback to summary
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:PathContainer

the candidate path to attempt to match against

Returns:PathPattern.PathRemainingMatchInfo

info object with the match result or null for no match

Annotations
@Nullable
pathContainerIsJustSeparatorback to summary
private boolean pathContainerIsJustSeparator(PathContainer pathContainer)
scoreByNormalizedLengthback to summary
private static int scoreByNormalizedLength(PathPattern pattern)
toChainStringback to summary
pack-priv String toChainString()
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
org.springframework.web.util.pattern back to summary

pack-priv Class PathPattern.MatchingContext

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.web.util.pattern.PathPattern.MatchingContext

Encapsulates context when attempting a match. Includes some fixed state like the candidate currently being considered for a match but also some accumulators for extracted variables.

Field Summary

Modifier and TypeField and Description
pack-priv final PathContainer
pack-priv boolean
private Map<String, MultiValueMap<String, String>>
private Map<String, String>
pack-priv boolean
pack-priv final List<PathContainer.Element>
pack-priv final int
pack-priv int

Constructor Summary

AccessConstructor and Description
public
MatchingContext(PathContainer pathContainer, boolean extractVariables)

Method Summary

Modifier and TypeMethod and Description
public PathPattern.PathMatchInfo
public boolean
pack-priv boolean

Returns:

true if element is a separator
isSeparator
(int
possible index of a separator
pathIndex
)

Return if element at specified index is a separator.

pack-priv String

Returns:

the decoded value
pathElementValue
(int
path element index
pathIndex
)

Return the decoded value of the specified element.

public void
set(String key, String value, MultiValueMap<String, String> parameters)

public void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

candidateback to summary
pack-priv final PathContainer candidate
determineRemainingPathback to summary
pack-priv boolean determineRemainingPath
extractedMatrixVariablesback to summary
private Map<String, MultiValueMap<String, String>> extractedMatrixVariables
Annotations
@Nullable
extractedUriVariablesback to summary
private Map<String, String> extractedUriVariables
Annotations
@Nullable
extractingVariablesback to summary
pack-priv boolean extractingVariables
pathElementsback to summary
pack-priv final List<PathContainer.Element> pathElements
pathLengthback to summary
pack-priv final int pathLength
remainingPathIndexback to summary
pack-priv int remainingPathIndex

Constructor Detail

MatchingContextback to summary
public MatchingContext(PathContainer pathContainer, boolean extractVariables)

Method Detail

getPathMatchResultback to summary
public PathPattern.PathMatchInfo getPathMatchResult()
isMatchOptionalTrailingSeparatorback to summary
public boolean isMatchOptionalTrailingSeparator()
isSeparatorback to summary
pack-priv boolean isSeparator(int pathIndex)

Return if element at specified index is a separator.

Parameters
pathIndex:int

possible index of a separator

Returns:boolean

true if element is a separator

pathElementValueback to summary
pack-priv String pathElementValue(int pathIndex)

Return the decoded value of the specified element.

Parameters
pathIndex:int

path element index

Returns:String

the decoded value

setback to summary
public void set(String key, String value, MultiValueMap<String, String> parameters)
setMatchAllowExtraPathback to summary
public void setMatchAllowExtraPath()
org.springframework.web.util.pattern back to summary

public Class PathPattern.PathMatchInfo

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.web.util.pattern.PathPattern.PathMatchInfo

Holder for URI variables and path parameters (matrix variables) extracted based on the pattern for a given matched path.

Field Summary

Modifier and TypeField and Description
private static final PathPattern.PathMatchInfo
private final Map<String, MultiValueMap<String, String>>
private final Map<String, String>

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public Map<String, MultiValueMap<String, String>>
getMatrixVariables()

Return maps of matrix variables per path segment, keyed off by URI variable name.

public Map<String, String>
getUriVariables()

Return the extracted URI variables.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

EMPTYback to summary
private static final PathPattern.PathMatchInfo EMPTY
matrixVariablesback to summary
private final Map<String, MultiValueMap<String, String>> matrixVariables
uriVariablesback to summary
private final Map<String, String> uriVariables

Constructor Detail

PathMatchInfoback to summary
pack-priv PathMatchInfo(Map<String, String> uriVars, Map<String, MultiValueMap<String, String>> matrixVars)

Method Detail

getMatrixVariablesback to summary
public Map<String, MultiValueMap<String, String>> getMatrixVariables()

Return maps of matrix variables per path segment, keyed off by URI variable name.

getUriVariablesback to summary
public Map<String, String> getUriVariables()

Return the extracted URI variables.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
org.springframework.web.util.pattern back to summary

public Class PathPattern.PathRemainingMatchInfo

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.web.util.pattern.PathPattern.PathRemainingMatchInfo

Holder for the result of a match on the start of a pattern. Provides access to the remaining path not matched to the pattern as well as any variables bound in that first part that was matched.

Field Summary

Modifier and TypeField and Description
private final PathContainer
private final PathPattern.PathMatchInfo
private final PathContainer

Constructor Summary

AccessConstructor and Description
pack-priv
pack-priv
PathRemainingMatchInfo(PathContainer pathMatched, PathContainer pathRemaining, PathPattern.PathMatchInfo pathMatchInfo)

Method Summary

Modifier and TypeMethod and Description
public Map<String, MultiValueMap<String, String>>
getMatrixVariables()

Return the path parameters for each bound variable.

public PathContainer
getPathMatched()

Return the part of a path that was matched by a pattern.

public PathContainer
getPathRemaining()

Return the part of a path that was not matched by a pattern.

public Map<String, String>
getUriVariables()

Return variables that were bound in the part of the path that was successfully matched or an empty map.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

pathMatchedback to summary
private final PathContainer pathMatched
pathMatchInfoback to summary
private final PathPattern.PathMatchInfo pathMatchInfo
pathRemainingback to summary
private final PathContainer pathRemaining

Constructor Detail

PathRemainingMatchInfoback to summary
pack-priv PathRemainingMatchInfo(PathContainer pathMatched, PathContainer pathRemaining)
PathRemainingMatchInfoback to summary
pack-priv PathRemainingMatchInfo(PathContainer pathMatched, PathContainer pathRemaining, PathPattern.PathMatchInfo pathMatchInfo)

Method Detail

getMatrixVariablesback to summary
public Map<String, MultiValueMap<String, String>> getMatrixVariables()

Return the path parameters for each bound variable.

getPathMatchedback to summary
public PathContainer getPathMatched()

Return the part of a path that was matched by a pattern.

Since
5.3
getPathRemainingback to summary
public PathContainer getPathRemaining()

Return the part of a path that was not matched by a pattern.

getUriVariablesback to summary
public Map<String, String> getUriVariables()

Return variables that were bound in the part of the path that was successfully matched or an empty map.