Top Description Fields Constructors Methods
org.springframework.web.servlet.mvc.condition

public Class PatternsRequestCondition

extends AbstractRequestCondition<PatternsRequestCondition>
Class Inheritance
Imports
java.util.ArrayList, .Collection, .Collections, .Comparator, .HashSet, .Iterator, .LinkedHashSet, .List, .Set, jakarta.servlet.http.HttpServletRequest, org.springframework.lang.Nullable, org.springframework.util.AntPathMatcher, .ObjectUtils, .PathMatcher, .StringUtils, org.springframework.web.util.UrlPathHelper, org.springframework.web.util.pattern.PathPattern, .PathPatternParser

A logical disjunction (' || ') request condition that matches a request against a set of URL path patterns.

In contrast to PathPatternsRequestCondition which uses parsed PathPatterns, this condition does String pattern matching via AntPathMatcher.

Author
Rossen Stoyanchev
Since
3.1

Field Summary

Modifier and TypeField and Description
private static final Set<String>
private final List<String>
private final PathMatcher
private final Set<String>
private static final String[]
private final boolean
private final boolean

Constructor Summary

AccessConstructor and Description
public
PatternsRequestCondition(String...
0 or more URL patterns; no patterns results in an empty path "" mapping which matches all requests.
patterns
)

Constructor with URL patterns which are prepended with "/" if necessary.

public
PatternsRequestCondition(String[] patterns, boolean useTrailingSlashMatch, PathMatcher pathMatcher)

Variant of PatternsRequestCondition(String...) with a PathMatcher and flag for matching trailing slashes.

public
PatternsRequestCondition(String[] patterns, UrlPathHelper urlPathHelper, PathMatcher pathMatcher, boolean useTrailingSlashMatch)

Variant of PatternsRequestCondition(String...) with a UrlPathHelper and a PathMatcher, and whether to match trailing slashes.

public
PatternsRequestCondition(String[] patterns, UrlPathHelper urlPathHelper, PathMatcher pathMatcher, boolean useSuffixPatternMatch, boolean useTrailingSlashMatch)
Deprecated as of 5.2.4.

Variant of PatternsRequestCondition(String...) with a UrlPathHelper and a PathMatcher, and flags for matching with suffixes and trailing slashes.

public
PatternsRequestCondition(String[] patterns, UrlPathHelper urlPathHelper, PathMatcher pathMatcher, boolean useSuffixPatternMatch, boolean useTrailingSlashMatch, List<String> fileExtensions)
Deprecated as of 5.2.4.

Variant of PatternsRequestCondition(String...) with a UrlPathHelper and a PathMatcher, and flags for matching with suffixes and trailing slashes, along with specific extensions.

private
PatternsRequestCondition(Set<String> patterns, PatternsRequestCondition other)

Private constructor for use when combining and matching.

Method Summary

Modifier and TypeMethod and Description
public PatternsRequestCondition
combine(PatternsRequestCondition
the condition to combine with.
other
)

Implements org.springframework.web.servlet.mvc.condition.RequestCondition.combine.

Combine the patterns of the current and of the other instances as follows:

  • If only one instance has patterns, use those.
  • If both have patterns, combine patterns from "this" instance with patterns from the other instance via PathMatcher#combine(String, String).
  • If neither has patterns, use "" and "/" as root path patterns.
public int
compareTo(PatternsRequestCondition other, HttpServletRequest request)

Implements org.springframework.web.servlet.mvc.condition.RequestCondition.compareTo.

Compare the two conditions based on the URL patterns they contain.

protected Collection<String>
getContent()

Implements abstract org.springframework.web.servlet.mvc.condition.AbstractRequestCondition.getContent.

Return the discrete items a request condition is composed of.

public Set<String>
getDirectPaths()

Return the mapping paths that are not patterns.

public PatternsRequestCondition

Returns:

the same instance if the condition contains no patterns; or a new condition with sorted matching patterns; or null if no patterns match.
getMatchingCondition
(HttpServletRequest
the current request
request
)

Implements org.springframework.web.servlet.mvc.condition.RequestCondition.getMatchingCondition.

Checks if any of the patterns match the given request and returns an instance that is guaranteed to contain matching patterns, sorted via PathMatcher#getPatternComparator(String).

private String
getMatchingPattern(String pattern, String lookupPath)

public List<String>

Returns:

a collection of matching patterns sorted with the closest match at the top
getMatchingPatterns
(String
the lookup path to match to existing patterns
lookupPath
)

Find the patterns matching the given lookup path.

public Set<String>
protected String
getToStringInfix()

Implements abstract org.springframework.web.servlet.mvc.condition.AbstractRequestCondition.getToStringInfix.

The notation to use when printing discrete items of content.

private static boolean
hasPattern(String[] patterns)

private static Set<String>
initPatterns(String[] patterns)

public boolean
isEmptyPathMapping()

Whether the condition is the "" (empty path) mapping.

Inherited from org.springframework.web.servlet.mvc.condition.AbstractRequestCondition:
equalshashCodeisEmptytoString

Field Detail

EMPTY_PATH_PATTERNback to summary
private static final Set<String> EMPTY_PATH_PATTERN
fileExtensionsback to summary
private final List<String> fileExtensions
pathMatcherback to summary
private final PathMatcher pathMatcher
patternsback to summary
private final Set<String> patterns
ROOT_PATH_PATTERNSback to summary
private static final String[] ROOT_PATH_PATTERNS
useSuffixPatternMatchback to summary
private final boolean useSuffixPatternMatch
useTrailingSlashMatchback to summary
private final boolean useTrailingSlashMatch

Constructor Detail

PatternsRequestConditionback to summary
public PatternsRequestCondition(String... patterns)

Constructor with URL patterns which are prepended with "/" if necessary.

Parameters
patterns:String[]

0 or more URL patterns; no patterns results in an empty path "" mapping which matches all requests.

PatternsRequestConditionback to summary
public PatternsRequestCondition(String[] patterns, boolean useTrailingSlashMatch, PathMatcher pathMatcher)

Variant of PatternsRequestCondition(String...) with a PathMatcher and flag for matching trailing slashes.

Since
5.3
PatternsRequestConditionback to summary
public PatternsRequestCondition(String[] patterns, UrlPathHelper urlPathHelper, PathMatcher pathMatcher, boolean useTrailingSlashMatch)

Variant of PatternsRequestCondition(String...) with a UrlPathHelper and a PathMatcher, and whether to match trailing slashes.

As of 5.3 the path is obtained through the static method UrlPathHelper#getResolvedLookupPath and a UrlPathHelper does not need to be passed in.

Annotations
@Deprecated
Since
5.2.4
PatternsRequestConditionback to summary
public PatternsRequestCondition(String[] patterns, UrlPathHelper urlPathHelper, PathMatcher pathMatcher, boolean useSuffixPatternMatch, boolean useTrailingSlashMatch)

Deprecated

as of 5.2.4. See class-level note in org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping on the deprecation of path extension config options.

Variant of PatternsRequestCondition(String...) with a UrlPathHelper and a PathMatcher, and flags for matching with suffixes and trailing slashes.

As of 5.3 the path is obtained through the static method UrlPathHelper#getResolvedLookupPath and a UrlPathHelper does not need to be passed in.

Annotations
@Deprecated
PatternsRequestConditionback to summary
public PatternsRequestCondition(String[] patterns, UrlPathHelper urlPathHelper, PathMatcher pathMatcher, boolean useSuffixPatternMatch, boolean useTrailingSlashMatch, List<String> fileExtensions)

Deprecated

as of 5.2.4. See class-level note in org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping on the deprecation of path extension config options.

Variant of PatternsRequestCondition(String...) with a UrlPathHelper and a PathMatcher, and flags for matching with suffixes and trailing slashes, along with specific extensions.

As of 5.3 the path is obtained through the static method UrlPathHelper#getResolvedLookupPath and a UrlPathHelper does not need to be passed in.

Annotations
@Deprecated
PatternsRequestConditionback to summary
private PatternsRequestCondition(Set<String> patterns, PatternsRequestCondition other)

Private constructor for use when combining and matching.

Method Detail

combineback to summary
public PatternsRequestCondition combine(PatternsRequestCondition other)

Implements org.springframework.web.servlet.mvc.condition.RequestCondition.combine.

Combine the patterns of the current and of the other instances as follows:

  • If only one instance has patterns, use those.
  • If both have patterns, combine patterns from "this" instance with patterns from the other instance via PathMatcher#combine(String, String).
  • If neither has patterns, use "" and "/" as root path patterns.
Parameters
other:PatternsRequestCondition

Doc from org.springframework.web.servlet.mvc.condition.RequestCondition.combine.

the condition to combine with.

Returns:PatternsRequestCondition

Doc from org.springframework.web.servlet.mvc.condition.RequestCondition.combine.

a request condition instance that is the result of combining the two condition instances.

Annotations
@Override
compareToback to summary
public int compareTo(PatternsRequestCondition other, HttpServletRequest request)

Implements org.springframework.web.servlet.mvc.condition.RequestCondition.compareTo.

Compare the two conditions based on the URL patterns they contain. Patterns are compared one at a time, from top to bottom via PathMatcher#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.

Annotations
@Override
getContentback to summary
protected Collection<String> getContent()

Implements abstract org.springframework.web.servlet.mvc.condition.AbstractRequestCondition.getContent.

Doc from org.springframework.web.servlet.mvc.condition.AbstractRequestCondition.getContent.

Return the discrete items a request condition is composed of.

For example URL patterns, HTTP request methods, param expressions, etc.

Returns:Collection<String>

a collection of objects (never null)

Annotations
@Override
getDirectPathsback to summary
public Set<String> getDirectPaths()

Return the mapping paths that are not patterns.

Since
5.3
getMatchingConditionback to summary
public PatternsRequestCondition getMatchingCondition(HttpServletRequest request)

Implements org.springframework.web.servlet.mvc.condition.RequestCondition.getMatchingCondition.

Checks if any of the patterns match the given request and returns an instance that is guaranteed to contain matching patterns, sorted via PathMatcher#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:HttpServletRequest

the current request

Returns:PatternsRequestCondition

the same instance if the condition contains no patterns; or a new condition with sorted matching patterns; or null if no patterns match.

Annotations
@Override
@Nullable
getMatchingPatternback to summary
private String getMatchingPattern(String pattern, String lookupPath)
Annotations
@Nullable
getMatchingPatternsback to summary
public List<String> getMatchingPatterns(String lookupPath)

Find the patterns matching the given lookup path. Invoking this method should yield results equivalent to those of calling getMatchingCondition. This method is provided as an alternative to be used if no request is available (e.g. introspection, tooling, etc).

Parameters
lookupPath:String

the lookup path to match to existing patterns

Returns:List<String>

a collection of matching patterns sorted with the closest match at the top

getPatternsback to summary
public Set<String> getPatterns()
getToStringInfixback to summary
protected String getToStringInfix()

Implements abstract org.springframework.web.servlet.mvc.condition.AbstractRequestCondition.getToStringInfix.

Doc from org.springframework.web.servlet.mvc.condition.AbstractRequestCondition.getToStringInfix.

The notation to use when printing discrete items of content.

For example " || " for URL patterns or " && " for param expressions.

Annotations
@Override
hasPatternback to summary
private static boolean hasPattern(String[] patterns)
initPatternsback to summary
private static Set<String> initPatterns(String[] patterns)
isEmptyPathMappingback to summary
public boolean isEmptyPathMapping()

Whether the condition is the "" (empty path) mapping.