Class PatternsRequestCondition
- java.lang.Object
- org.springframework.web.servlet.mvc.condition.AbstractRequestCondition<PatternsRequestCondition>
- org.springframework.web.servlet.mvc.condition.PatternsRequestCondition
- All Implemented Interfaces:
RequestCondition<PatternsRequestCondition>
public class PatternsRequestCondition extends AbstractRequestCondition<PatternsRequestCondition>
A logical disjunction (' || ') request condition that matches a request against a set of URL path patterns.- Since:
- 3.1
- Author:
- Rossen Stoyanchev
Constructor Summary
Constructors Constructor Description PatternsRequestCondition(String... patterns)
Creates a new instance with the given URL patterns.PatternsRequestCondition(String[] patterns, UrlPathHelper urlPathHelper, PathMatcher pathMatcher, boolean useTrailingSlashMatch)
Alternative constructor with additional, optionalUrlPathHelper
,PathMatcher
, and whether to automatically match trailing slashes.PatternsRequestCondition(String[] patterns, UrlPathHelper urlPathHelper, PathMatcher pathMatcher, boolean useSuffixPatternMatch, boolean useTrailingSlashMatch)
Deprecated.as of 5.2.4.PatternsRequestCondition(String[] patterns, UrlPathHelper urlPathHelper, PathMatcher pathMatcher, boolean useSuffixPatternMatch, boolean useTrailingSlashMatch, List<String> fileExtensions)
Deprecated.as of 5.2.4.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description PatternsRequestCondition
combine(PatternsRequestCondition other)
Returns a new instance with URL patterns from the current instance ("this") and the "other" instance as follows: If there are patterns in both instances, combine the patterns in "this" with the patterns in "other" usingPathMatcher.combine(String, String)
.int
compareTo(PatternsRequestCondition other, HttpServletRequest request)
Compare the two conditions based on the URL patterns they contain.protected Collection<String>
getContent()
Return the discrete items a request condition is composed of.PatternsRequestCondition
getMatchingCondition(HttpServletRequest request)
Checks if any of the patterns match the given request and returns an instance that is guaranteed to contain matching patterns, sorted viaPathMatcher.getPatternComparator(String)
.List<String>
getMatchingPatterns(String lookupPath)
Find the patterns matching the given lookup path.Set<String>
getPatterns()
protected String
getToStringInfix()
The notation to use when printing discrete items of content.Methods inherited from class org.springframework.web.servlet.mvc.condition.AbstractRequestCondition
equals, hashCode, isEmpty, toString
Constructor Detail
PatternsRequestCondition
public PatternsRequestCondition(String... patterns)
Creates a new instance with the given URL patterns. Each pattern that is not empty and does not start with "/" is prepended with "/".- Parameters:
patterns
- 0 or more URL patterns; if 0 the condition will match to every request.
PatternsRequestCondition
public PatternsRequestCondition(String[] patterns, @Nullable UrlPathHelper urlPathHelper, @Nullable PathMatcher pathMatcher, boolean useTrailingSlashMatch)
Alternative constructor with additional, optionalUrlPathHelper
,PathMatcher
, and whether to automatically match trailing slashes.- Parameters:
patterns
- the URL patterns to use; if 0, the condition will match to every request.urlPathHelper
- aUrlPathHelper
for determining the lookup path for a requestpathMatcher
- aPathMatcher
for pattern path matchinguseTrailingSlashMatch
- whether to match irrespective of a trailing slash- Since:
- 5.2.4
PatternsRequestCondition
@Deprecated public PatternsRequestCondition(String[] patterns, @Nullable UrlPathHelper urlPathHelper, @Nullable PathMatcher pathMatcher, boolean useSuffixPatternMatch, boolean useTrailingSlashMatch)
Deprecated.as of 5.2.4. See class-level note inRequestMappingHandlerMapping
on the deprecation of path extension config options.Alternative constructor with additional optional parameters.- Parameters:
patterns
- the URL patterns to use; if 0, the condition will match to every request.urlPathHelper
- for determining the lookup path of a requestpathMatcher
- for path matching with patternsuseSuffixPatternMatch
- whether to enable matching by suffix (".*")useTrailingSlashMatch
- whether to match irrespective of a trailing slash
PatternsRequestCondition
@Deprecated public PatternsRequestCondition(String[] patterns, @Nullable UrlPathHelper urlPathHelper, @Nullable PathMatcher pathMatcher, boolean useSuffixPatternMatch, boolean useTrailingSlashMatch, @Nullable List<String> fileExtensions)
Deprecated.as of 5.2.4. See class-level note inRequestMappingHandlerMapping
on the deprecation of path extension config options.Alternative constructor with additional optional parameters.- Parameters:
patterns
- the URL patterns to use; if 0, the condition will match to every request.urlPathHelper
- aUrlPathHelper
for determining the lookup path for a requestpathMatcher
- aPathMatcher
for pattern path matchinguseSuffixPatternMatch
- whether to enable matching by suffix (".*")useTrailingSlashMatch
- whether to match irrespective of a trailing slashfileExtensions
- a list of file extensions to consider for path matching
Method Detail
getPatterns
public Set<String> getPatterns()
getContent
protected Collection<String> getContent()
Description copied from class:AbstractRequestCondition
Return the discrete items a request condition is composed of.For example URL patterns, HTTP request methods, param expressions, etc.
- Specified by:
getContent
in classAbstractRequestCondition<PatternsRequestCondition>
- Returns:
- a collection of objects (never
null
)
getToStringInfix
protected String getToStringInfix()
Description copied from class:AbstractRequestCondition
The notation to use when printing discrete items of content.For example
" || "
for URL patterns or" && "
for param expressions.- Specified by:
getToStringInfix
in classAbstractRequestCondition<PatternsRequestCondition>
- If there are patterns in both instances, combine the patterns in "this" with the patterns in "other" using
PathMatcher.combine(String, String)
. - If only one instance has patterns, use them.
- If neither instance has patterns, use an empty String (i.e. "").
- Parameters:
other
- the condition to combine with.- Returns:
- a request condition instance that is the result of combining the two condition instances.
getMatchingCondition
@Nullable public PatternsRequestCondition getMatchingCondition(HttpServletRequest request)
Checks if any of the patterns match the given request and returns an instance that is guaranteed to contain matching patterns, sorted viaPathMatcher.getPatternComparator(String)
.A matching pattern is obtained by making checks in the following order:
- Direct match
- Pattern match with ".*" appended if the pattern doesn't already contain a "."
- Pattern match
- Pattern match with "/" appended if the pattern doesn't already end in "/"
- Parameters:
request
- the current request- Returns:
- the same instance if the condition contains no patterns; or a new condition with sorted matching patterns; or
null
if no patterns match.
getMatchingPatterns
public List<String> getMatchingPatterns(String lookupPath)
Find the patterns matching the given lookup path. Invoking this method should yield results equivalent to those of callinggetMatchingCondition(javax.servlet.http.HttpServletRequest)
. This method is provided as an alternative to be used if no request is available (e.g. introspection, tooling, etc).- Parameters:
lookupPath
- the lookup path to match to existing patterns- Returns:
- a collection of matching patterns sorted with the closest match at the top
compareTo
public int compareTo(PatternsRequestCondition other, HttpServletRequest request)
Compare the two conditions based on the URL patterns they contain. Patterns are compared one at a time, from top to bottom viaPathMatcher.getPatternComparator(String)
. If all compared patterns match equally, but one instance has more patterns, it is considered a closer match.It is assumed that both instances have been obtained via
getMatchingCondition(HttpServletRequest)
to ensure they contain only patterns that match the request and are sorted with the best matches on top.