org.springframework.web.servlet.mvc.condition

## 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.

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

  Since:
    3.1
  Author:
    Rossen Stoyanchev

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `PatternsRequestCondition(String... patterns)` Constructor with URL patterns which are prepended with "/" if necessary.
    `PatternsRequestCondition(String[] patterns, boolean useTrailingSlashMatch, PathMatcher pathMatcher)` Variant of `PatternsRequestCondition(String...)` with a `PathMatcher` and flag for matching trailing slashes.
    `PatternsRequestCondition(String[] patterns, UrlPathHelper urlPathHelper, PathMatcher pathMatcher, boolean useTrailingSlashMatch)` Deprecated. as of 5.3 in favor of `PatternsRequestCondition(String[], boolean, PathMatcher)`.
    `PatternsRequestCondition(String[] patterns, UrlPathHelper urlPathHelper, PathMatcher pathMatcher, boolean useSuffixPatternMatch, boolean useTrailingSlashMatch)` Deprecated. as of 5.2.4. See class-level note in `RequestMappingHandlerMapping` on the deprecation of path extension config options.
    `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 `RequestMappingHandlerMapping` on the deprecation of path extension config options.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and 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" using `PathMatcher.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.
    `Set<String>` | `getDirectPaths()` Return the mapping paths that are not patterns.
    `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 via `PathMatcher.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.
    `boolean` | `isEmptyPathMapping()` Whether the condition is the "" (empty path) mapping.

    - ### Methods inherited from class org.springframework.web.servlet.mvc.condition.AbstractRequestCondition

      `equals, hashCode, isEmpty, toString`

    - ### Methods inherited from class java.lang.Object

      `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### PatternsRequestCondition

          public PatternsRequestCondition(String... patterns)

      Constructor with URL patterns which are prepended with "/" if necessary.
      Parameters:
        `patterns` - 0 or more URL patterns; no patterns results in an empty path `""` mapping which matches all requests.

    - #### PatternsRequestCondition

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

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

    - #### PatternsRequestCondition

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

      Deprecated. as of 5.3 in favor of `PatternsRequestCondition(String[], boolean, PathMatcher)`.
      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(javax.servlet.ServletRequest)` and a `UrlPathHelper` does not need to be passed in.

      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 in `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(javax.servlet.ServletRequest)` and a `UrlPathHelper` does not need to be passed in.

    - #### 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 in `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(javax.servlet.ServletRequest)` and a `UrlPathHelper` does not need to be passed in.

  - ### 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 class `AbstractRequestCondition<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 class `AbstractRequestCondition<PatternsRequestCondition>`

    - #### isEmptyPathMapping

          public boolean isEmptyPathMapping()

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

    - #### getDirectPaths

          public Set<String> getDirectPaths()

      Return the mapping paths that are not patterns.
      Since:
        5.3

    - #### combine

          public 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" 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 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` - 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 calling `getMatchingCondition(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 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.