Top Description Inners Fields Constructors Methods
org.springframework.util

public Class AntPathMatcher

extends Object
implements PathMatcher
Class Inheritance
All Implemented Interfaces
org.springframework.util.PathMatcher
Imports
java.util.ArrayList, .Comparator, .LinkedHashMap, .List, .Map, java.util.concurrent.ConcurrentHashMap, java.util.regex.Matcher, .Pattern, org.springframework.lang.Nullable

PathMatcher implementation for Ant-style path patterns.

Part of this mapping code has been kindly borrowed from Apache Ant.

The mapping matches URLs using the following rules:

Examples

Note

a pattern and a path must both be absolute or must both be relative in order for the two to match. Therefore, it is recommended that users of this implementation to sanitize patterns in order to prefix them with "/" as it makes sense in the context in which they're used.

Authors
Alef Arendsen, Juergen Hoeller, Rob Harrop, Arjen Poutsma, Rossen Stoyanchev, Sam Brannen, Vladislav Kisel
Since
16.07.2003

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected static class
AntPathMatcher.AntPathStringMatcher

Tests whether a string matches against a pattern via a Pattern.

protected static class
private static class
AntPathMatcher.PathSeparatorPatternCache

A simple cache for patterns that depend on the configured path separator.

Field Summary

Modifier and TypeField and Description
private static final int
private volatile Boolean
private boolean
public static final String
DEFAULT_PATH_SEPARATOR

Default path separator: "/".

private String
private AntPathMatcher.PathSeparatorPatternCache
pack-priv final Map<String, AntPathMatcher.AntPathStringMatcher>
private final Map<String, String[]>
private boolean
private static final Pattern
private static final char[]

Constructor Summary

AccessConstructor and Description
public
AntPathMatcher()

Create a new instance with the DEFAULT_PATH_SEPARATOR.

public
AntPathMatcher(String
the path separator to use, must not be null.
pathSeparator
)

A convenient, alternative constructor to use with a custom path separator.

Method Summary

Modifier and TypeMethod and Description
public String

Returns:

the combination of the two patterns
combine
(String
the first pattern
pattern1
,
String
the second pattern
pattern2
)

Implements org.springframework.util.PathMatcher.combine.

Combine two patterns into a new pattern.
private String
concat(String path1, String path2)

private void
protected boolean

Returns:

true if the supplied path matched, false if it didn't
doMatch
(String
the pattern to match against
pattern
,
String
the path to test
path
,
boolean
whether a full pattern match is required (else a pattern match as far as the given base path goes is sufficient)
fullMatch
,
Map<String, String> uriTemplateVariables)

Actually match the given path against the given pattern.

public String
extractPathWithinPattern(String
the path pattern
pattern
,
String
the full path to introspect
path
)

Implements org.springframework.util.PathMatcher.extractPathWithinPattern.

Given a pattern and a full path, determine the pattern-mapped part.
public Map<String, String>
extractUriTemplateVariables(String
the path pattern, possibly containing URI templates
pattern
,
String
the full path to extract template variables from
path
)

Implements org.springframework.util.PathMatcher.extractUriTemplateVariables.

Given a pattern and a full path, extract the URI template variables.
public Comparator<String>

Returns:

a comparator capable of sorting patterns in order of explicitness
getPatternComparator
(String
the full path to use for comparison
path
)

Implements org.springframework.util.PathMatcher.getPatternComparator.

Given a full path, returns a Comparator suitable for sorting patterns in order of explicitness.
protected AntPathMatcher.AntPathStringMatcher

Returns:

a corresponding AntPathStringMatcher (never null)
getStringMatcher
(String
the pattern to match against (never null)
pattern
)

Build or retrieve an AntPathStringMatcher for the given pattern.

public boolean
isPattern(String
the path to check
path
)

Implements org.springframework.util.PathMatcher.isPattern.

Does the given path represent a pattern that can be matched by an implementation of this interface?
private boolean
isPotentialMatch(String path, String[] pattDirs)

private boolean
public boolean
match(String
the pattern to match against
pattern
,
String
the path to test
path
)

Implements org.springframework.util.PathMatcher.match.

Match the given path against the given pattern, according to this PathMatcher's matching strategy.
public boolean
matchStart(String
the pattern to match against
pattern
,
String
the path to test
path
)

Implements org.springframework.util.PathMatcher.matchStart.

Match the given path against the corresponding part of the given pattern, according to this PathMatcher's matching strategy.
private boolean

Returns:

true if the string matches against the pattern, or false otherwise
matchStrings
(String
the pattern to match against (never null)
pattern
,
String
the String which must be matched against the pattern (never null)
str
,
Map<String, String> uriTemplateVariables)

Test whether a string matches against a pattern.

public void
setCachePatterns(boolean cachePatterns)

Specify whether to cache parsed pattern metadata for patterns passed into this matcher's match method.

public void
setCaseSensitive(boolean caseSensitive)

Specify whether to perform pattern matching in a case-sensitive fashion.

public void
setPathSeparator(String pathSeparator)

Set the path separator to use for pattern parsing.

public void
setTrimTokens(boolean trimTokens)

Specify whether to trim tokenized paths and patterns.

private int
skipSegment(String path, int pos, String prefix)

private int
skipSeparator(String path, int pos, String separator)

protected String[]

Returns:

the tokenized path parts
tokenizePath
(String
the path to tokenize
path
)

Tokenize the given path into parts, based on this matcher's settings.

protected String[]

Returns:

the tokenized pattern parts
tokenizePattern
(String
the pattern to tokenize
pattern
)

Tokenize the given path pattern into parts, based on this matcher's settings.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CACHE_TURNOFF_THRESHOLDback to summary
private static final int CACHE_TURNOFF_THRESHOLD
cachePatternsback to summary
private volatile Boolean cachePatterns
Annotations
@Nullable
caseSensitiveback to summary
private boolean caseSensitive
DEFAULT_PATH_SEPARATORback to summary
public static final String DEFAULT_PATH_SEPARATOR

Default path separator: "/".

pathSeparatorback to summary
private String pathSeparator
pathSeparatorPatternCacheback to summary
private AntPathMatcher.PathSeparatorPatternCache pathSeparatorPatternCache
stringMatcherCacheback to summary
pack-priv final Map<String, AntPathMatcher.AntPathStringMatcher> stringMatcherCache
tokenizedPatternCacheback to summary
private final Map<String, String[]> tokenizedPatternCache
trimTokensback to summary
private boolean trimTokens
VARIABLE_PATTERNback to summary
private static final Pattern VARIABLE_PATTERN
WILDCARD_CHARSback to summary
private static final char[] WILDCARD_CHARS

Constructor Detail

AntPathMatcherback to summary
public AntPathMatcher()

Create a new instance with the DEFAULT_PATH_SEPARATOR.

AntPathMatcherback to summary
public AntPathMatcher(String pathSeparator)

A convenient, alternative constructor to use with a custom path separator.

Parameters
pathSeparator:String

the path separator to use, must not be null.

Since
4.1

Method Detail

combineback to summary
public String combine(String pattern1, String pattern2)

Implements org.springframework.util.PathMatcher.combine.

Combine two patterns into a new pattern.

This implementation simply concatenates the two patterns, unless the first pattern contains a file extension match (e.g., *.html). In that case, the second pattern will be merged into the first. Otherwise, an IllegalArgumentException will be thrown.

Examples

Pattern 1Pattern 2Result
nullnull 
/hotelsnull/hotels
null/hotels/hotels
/hotels/bookings/hotels/bookings
/hotelsbookings/hotels/bookings
/hotels/*/bookings/hotels/bookings
/hotels/**/bookings/hotels/**/bookings
/hotels{hotel}/hotels/{hotel}
/hotels/*{hotel}/hotels/{hotel}
/hotels/**{hotel}/hotels/**/{hotel}
/*.html/hotels.html/hotels.html
/*.html/hotels/hotels.html
/*.html/*.txtIllegalArgumentException
Parameters
pattern1:String

the first pattern

pattern2:String

the second pattern

Returns:String

the combination of the two patterns

Annotations
@Override
Exceptions
IllegalArgumentException:
if the two patterns cannot be combined
concatback to summary
private String concat(String path1, String path2)
deactivatePatternCacheback to summary
private void deactivatePatternCache()
doMatchback to summary
protected boolean doMatch(String pattern, String path, boolean fullMatch, Map<String, String> uriTemplateVariables)

Actually match the given path against the given pattern.

Parameters
pattern:String

the pattern to match against

path:String

the path to test

fullMatch:boolean

whether a full pattern match is required (else a pattern match as far as the given base path goes is sufficient)

Returns:boolean

true if the supplied path matched, false if it didn't

extractPathWithinPatternback to summary
public String extractPathWithinPattern(String pattern, String path)

Implements org.springframework.util.PathMatcher.extractPathWithinPattern.

Given a pattern and a full path, determine the pattern-mapped part.

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'
  • '/docs/**\/*.html' and '/docs/cvs/commit.html → 'cvs/commit.html'
  • '/*.html' and '/docs/cvs/commit.html → 'docs/cvs/commit.html'
  • '*.html' and '/docs/cvs/commit.html → '/docs/cvs/commit.html'
  • '*' and '/docs/cvs/commit.html → '/docs/cvs/commit.html'

Assumes that match returns true for 'pattern' and 'path', but does not enforce this.

Parameters
pattern:String

Doc from org.springframework.util.PathMatcher.extractPathWithinPattern.

the path pattern

path:String

Doc from org.springframework.util.PathMatcher.extractPathWithinPattern.

the full path to introspect

Returns:String

Doc from org.springframework.util.PathMatcher.extractPathWithinPattern.

the pattern-mapped part of the given path (never null)

Annotations
@Override
extractUriTemplateVariablesback to summary
public Map<String, String> extractUriTemplateVariables(String pattern, String path)

Implements org.springframework.util.PathMatcher.extractUriTemplateVariables.

Doc from org.springframework.util.PathMatcher.extractUriTemplateVariables.

Given a pattern and a full path, extract the URI template variables. URI template variables are expressed through curly brackets ('{' and '}').

For example: For pattern "/hotels/{hotel}" and path "/hotels/1", this method will return a map containing "hotel" → "1".

Parameters
pattern:String

the path pattern, possibly containing URI templates

path:String

the full path to extract template variables from

Returns:Map<String, String>

a map, containing variable names as keys; variables values as values

Annotations
@Override
getPatternComparatorback to summary
public Comparator<String> getPatternComparator(String path)

Implements org.springframework.util.PathMatcher.getPatternComparator.

Given a full path, returns a Comparator suitable for sorting patterns in order of explicitness.

This Comparator will sort a list so that more specific patterns (without URI templates or wild cards) come before generic patterns. So given a list with the following patterns, the returned comparator will sort this list so that the order will be as indicated.

  1. /hotels/new
  2. /hotels/{hotel}
  3. /hotels/*

The full path given as parameter is used to test for exact matches. So when the given path is /hotels/2, the pattern /hotels/2 will be sorted before /hotels/1.

Parameters
path:String

the full path to use for comparison

Returns:Comparator<String>

a comparator capable of sorting patterns in order of explicitness

Annotations
@Override
getStringMatcherback to summary
protected AntPathMatcher.AntPathStringMatcher getStringMatcher(String pattern)

Build or retrieve an AntPathStringMatcher for the given pattern.

The default implementation checks this AntPathMatcher's internal cache (see setCachePatterns), creating a new AntPathStringMatcher instance if no cached copy is found.

When encountering too many patterns to cache at runtime (the threshold is 65536), it turns the default cache off, assuming that arbitrary permutations of patterns are coming in, with little chance for encountering a recurring pattern.

This method may be overridden to implement a custom cache strategy.

Parameters
pattern:String

the pattern to match against (never null)

Returns:AntPathMatcher.AntPathStringMatcher

a corresponding AntPathStringMatcher (never null)

See Also
setCachePatterns
isPatternback to summary
public boolean isPattern(String path)

Implements org.springframework.util.PathMatcher.isPattern.

Doc from org.springframework.util.PathMatcher.isPattern.

Does the given path represent a pattern that can be matched by an implementation of this interface?

If the return value is false, then the match method does not have to be used because direct equality comparisons on the static path Strings will lead to the same result.

Parameters
path:String

the path to check

Returns:boolean

true if the given path represents a pattern

Annotations
@Override
isPotentialMatchback to summary
private boolean isPotentialMatch(String path, String[] pattDirs)
isWildcardCharback to summary
private boolean isWildcardChar(char c)
matchback to summary
public boolean match(String pattern, String path)

Implements org.springframework.util.PathMatcher.match.

Doc from org.springframework.util.PathMatcher.match.

Match the given path against the given pattern, according to this PathMatcher's matching strategy.

Parameters
pattern:String

the pattern to match against

path:String

the path to test

Returns:boolean

true if the supplied path matched, false if it didn't

Annotations
@Override
matchStartback to summary
public boolean matchStart(String pattern, String path)

Implements org.springframework.util.PathMatcher.matchStart.

Doc from org.springframework.util.PathMatcher.matchStart.

Match the given path against the corresponding part of the given pattern, according to this PathMatcher's matching strategy.

Determines whether the pattern at least matches as far as the given base path goes, assuming that a full path may then match as well.

Parameters
pattern:String

the pattern to match against

path:String

the path to test

Returns:boolean

true if the supplied path matched, false if it didn't

Annotations
@Override
matchStringsback to summary
private boolean matchStrings(String pattern, String str, Map<String, String> uriTemplateVariables)

Test whether a string matches against a pattern.

Parameters
pattern:String

the pattern to match against (never null)

str:String

the String which must be matched against the pattern (never null)

Returns:boolean

true if the string matches against the pattern, or false otherwise

setCachePatternsback to summary
public void setCachePatterns(boolean cachePatterns)

Specify whether to cache parsed pattern metadata for patterns passed into this matcher's match method. A value of true activates an unlimited pattern cache; a value of false turns the pattern cache off completely.

Default is for the cache to be on, but with the variant to automatically turn it off when encountering too many patterns to cache at runtime (the threshold is 65536), assuming that arbitrary permutations of patterns are coming in, with little chance for encountering a recurring pattern.

Since
4.0.1
See Also
getStringMatcher(String)
setCaseSensitiveback to summary
public void setCaseSensitive(boolean caseSensitive)

Specify whether to perform pattern matching in a case-sensitive fashion.

Default is true. Switch this to false for case-insensitive matching.

Since
4.2
setPathSeparatorback to summary
public void setPathSeparator(String pathSeparator)

Set the path separator to use for pattern parsing.

Default is "/", as in Ant.

setTrimTokensback to summary
public void setTrimTokens(boolean trimTokens)

Specify whether to trim tokenized paths and patterns.

Default is false.

skipSegmentback to summary
private int skipSegment(String path, int pos, String prefix)
skipSeparatorback to summary
private int skipSeparator(String path, int pos, String separator)
tokenizePathback to summary
protected String[] tokenizePath(String path)

Tokenize the given path into parts, based on this matcher's settings.

Parameters
path:String

the path to tokenize

Returns:String[]

the tokenized path parts

tokenizePatternback to summary
protected String[] tokenizePattern(String pattern)

Tokenize the given path pattern into parts, based on this matcher's settings.

Performs caching based on setCachePatterns, delegating to tokenizePath(String) for the actual tokenization algorithm.

Parameters
pattern:String

the pattern to tokenize

Returns:String[]

the tokenized pattern parts

org.springframework.util back to summary

protected Class AntPathMatcher.AntPathStringMatcher

extends Object
Class Inheritance

Tests whether a string matches against a pattern via a Pattern.

The pattern may contain special characters: '*' means zero or more characters; '?' means one and only one character; '{' and '}' indicate a URI template pattern. For example /users/{user}.

Field Summary

Modifier and TypeField and Description
private final boolean
private static final String
private final boolean
private static final Pattern
private final Pattern
private final String
private final List<String>

Constructor Summary

AccessConstructor and Description
public
public
AntPathStringMatcher(String pattern, boolean caseSensitive)

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if the string matches against the pattern, or false otherwise.
matchStrings
(String str, Map<String, String> uriTemplateVariables)

Main entry point.

private String
quote(String s, int start, int end)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

caseSensitiveback to summary
private final boolean caseSensitive
DEFAULT_VARIABLE_PATTERNback to summary
private static final String DEFAULT_VARIABLE_PATTERN
exactMatchback to summary
private final boolean exactMatch
GLOB_PATTERNback to summary
private static final Pattern GLOB_PATTERN
patternback to summary
private final Pattern pattern
Annotations
@Nullable
rawPatternback to summary
private final String rawPattern
variableNamesback to summary
private final List<String> variableNames

Constructor Detail

AntPathStringMatcherback to summary
public AntPathStringMatcher(String pattern)
AntPathStringMatcherback to summary
public AntPathStringMatcher(String pattern, boolean caseSensitive)

Method Detail

matchStringsback to summary
public boolean matchStrings(String str, Map<String, String> uriTemplateVariables)

Main entry point.

Returns:boolean

true if the string matches against the pattern, or false otherwise.

quoteback to summary
private String quote(String s, int start, int end)
org.springframework.util back to summary

protected Class AntPathMatcher.AntPatternComparator

extends Object
implements Comparator<String>
Class Inheritance
All Implemented Interfaces
java.util.Comparator

The default Comparator implementation returned by getPatternComparator(String).

In order, the most "generic" pattern is determined by the following:

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
AntPathMatcher.AntPatternComparator.PatternInfo

Value class that holds information about the pattern, e.g. number of occurrences of "*", "**", and "{" pattern elements.

Field Summary

Modifier and TypeField and Description
private final String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

a negative integer, zero, or a positive integer as pattern1 is more specific, equally specific, or less specific than pattern2.
compare
(String
the first object to be compared.
pattern1
,
String
the second object to be compared.
pattern2
)

Implements java.util.Comparator.compare.

Compare two patterns to determine which should match first, i.e. which is the most specific regarding the current path.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

pathback to summary
private final String path

Constructor Detail

AntPatternComparatorback to summary
public AntPatternComparator(String path)

Method Detail

compareback to summary
public int compare(String pattern1, String pattern2)

Implements java.util.Comparator.compare.

Compare two patterns to determine which should match first, i.e. which is the most specific regarding the current path.

Parameters
pattern1:String

Doc from java.util.Comparator.compare.

the first object to be compared.

pattern2:String

Doc from java.util.Comparator.compare.

the second object to be compared.

Returns:int

a negative integer, zero, or a positive integer as pattern1 is more specific, equally specific, or less specific than pattern2.

Annotations
@Override
org.springframework.util back to summary

private Class AntPathMatcher.AntPatternComparator.PatternInfo

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.util.AntPathMatcher.AntPatternComparator.PatternInfo

Value class that holds information about the pattern, e.g. number of occurrences of "*", "**", and "{" pattern elements.

Field Summary

Modifier and TypeField and Description
private boolean
private int
private Integer
private final String
private boolean
private int
private int

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public int
public int
getLength()

Returns the length of the given pattern, where template variables are considered to be 1 long.

public int
public int
public int
protected void
public boolean
public boolean
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

catchAllPatternback to summary
private boolean catchAllPattern
doubleWildcardsback to summary
private int doubleWildcards
lengthback to summary
private Integer length
Annotations
@Nullable
patternback to summary
private final String pattern
Annotations
@Nullable
prefixPatternback to summary
private boolean prefixPattern
singleWildcardsback to summary
private int singleWildcards
uriVarsback to summary
private int uriVars

Constructor Detail

PatternInfoback to summary
public PatternInfo(String pattern)

Method Detail

getDoubleWildcardsback to summary
public int getDoubleWildcards()
getLengthback to summary
public int getLength()

Returns the length of the given pattern, where template variables are considered to be 1 long.

getSingleWildcardsback to summary
public int getSingleWildcards()
getTotalCountback to summary
public int getTotalCount()
getUriVarsback to summary
public int getUriVars()
initCountersback to summary
protected void initCounters()
isLeastSpecificback to summary
public boolean isLeastSpecific()
isPrefixPatternback to summary
public boolean isPrefixPattern()
org.springframework.util back to summary

private Class AntPathMatcher.PathSeparatorPatternCache

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.util.AntPathMatcher.PathSeparatorPatternCache

A simple cache for patterns that depend on the configured path separator.

Field Summary

Modifier and TypeField and Description
private final String
private final String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public String
public String
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

endsOnDoubleWildCardback to summary
private final String endsOnDoubleWildCard
endsOnWildCardback to summary
private final String endsOnWildCard

Constructor Detail

PathSeparatorPatternCacheback to summary
public PathSeparatorPatternCache(String pathSeparator)

Method Detail

getEndsOnDoubleWildCardback to summary
public String getEndsOnDoubleWildCard()
getEndsOnWildCardback to summary
public String getEndsOnWildCard()