Example usage for org.springframework.web.util.pattern PathPattern matchAndExtract

List of usage examples for org.springframework.web.util.pattern PathPattern matchAndExtract

Introduction

In this page you can find the example usage for org.springframework.web.util.pattern PathPattern matchAndExtract.

Prototype

@Nullable
public PathMatchInfo matchAndExtract(PathContainer pathContainer) 

Source Link

Document

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

Usage

From source file:org.springframework.cloud.gateway.handler.predicate.PathRoutePredicateFactory.java

@Override
public Predicate<ServerWebExchange> apply(Config config) {
    final ArrayList<PathPattern> pathPatterns = new ArrayList<>();
    synchronized (this.pathPatternParser) {
        pathPatternParser.setMatchOptionalTrailingSeparator(config.isMatchOptionalTrailingSeparator());
        config.getPatterns().forEach(pattern -> {
            PathPattern pathPattern = this.pathPatternParser.parse(pattern);
            pathPatterns.add(pathPattern);
        });/*  w  w w.  ja v a2s. com*/
    }
    return exchange -> {
        PathContainer path = parsePath(exchange.getRequest().getURI().getRawPath());

        Optional<PathPattern> optionalPathPattern = pathPatterns.stream()
                .filter(pattern -> pattern.matches(path)).findFirst();

        if (optionalPathPattern.isPresent()) {
            PathPattern pathPattern = optionalPathPattern.get();
            traceMatch("Pattern", pathPattern.getPatternString(), path, true);
            PathMatchInfo pathMatchInfo = pathPattern.matchAndExtract(path);
            putUriTemplateVariables(exchange, pathMatchInfo.getUriVariables());
            return true;
        } else {
            traceMatch("Pattern", config.getPatterns(), path, false);
            return false;
        }
    };
}