Search in sources :

Example 11 with PathContainer

use of org.springframework.http.server.PathContainer in project spring-framework by spring-projects.

the class RequestMappingInfoHandlerMapping method handleMatch.

/**
 * Expose URI template variables, matrix variables, and producible media types in the request.
 * @see HandlerMapping#URI_TEMPLATE_VARIABLES_ATTRIBUTE
 * @see HandlerMapping#MATRIX_VARIABLES_ATTRIBUTE
 * @see HandlerMapping#PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE
 */
@Override
protected void handleMatch(RequestMappingInfo info, HandlerMethod handlerMethod, ServerWebExchange exchange) {
    super.handleMatch(info, handlerMethod, exchange);
    PathContainer lookupPath = exchange.getRequest().getPath().pathWithinApplication();
    PathPattern bestPattern;
    Map<String, String> uriVariables;
    Map<String, MultiValueMap<String, String>> matrixVariables;
    Set<PathPattern> patterns = info.getPatternsCondition().getPatterns();
    if (patterns.isEmpty()) {
        bestPattern = getPathPatternParser().parse(lookupPath.value());
        uriVariables = Collections.emptyMap();
        matrixVariables = Collections.emptyMap();
    } else {
        bestPattern = patterns.iterator().next();
        PathPattern.PathMatchInfo result = bestPattern.matchAndExtract(lookupPath);
        Assert.notNull(result, () -> "Expected bestPattern: " + bestPattern + " to match lookupPath " + lookupPath);
        uriVariables = result.getUriVariables();
        matrixVariables = result.getMatrixVariables();
    }
    exchange.getAttributes().put(BEST_MATCHING_HANDLER_ATTRIBUTE, handlerMethod);
    exchange.getAttributes().put(BEST_MATCHING_PATTERN_ATTRIBUTE, bestPattern);
    exchange.getAttributes().put(URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriVariables);
    exchange.getAttributes().put(MATRIX_VARIABLES_ATTRIBUTE, matrixVariables);
    if (!info.getProducesCondition().getProducibleMediaTypes().isEmpty()) {
        Set<MediaType> mediaTypes = info.getProducesCondition().getProducibleMediaTypes();
        exchange.getAttributes().put(PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, mediaTypes);
    }
}
Also used : PathContainer(org.springframework.http.server.PathContainer) PathPattern(org.springframework.web.util.pattern.PathPattern) MediaType(org.springframework.http.MediaType) MultiValueMap(org.springframework.util.MultiValueMap)

Example 12 with PathContainer

use of org.springframework.http.server.PathContainer in project spring-framework by spring-projects.

the class PatternsRequestCondition method getMatchingPatterns.

@Nullable
private SortedSet<PathPattern> getMatchingPatterns(ServerWebExchange exchange) {
    PathContainer lookupPath = exchange.getRequest().getPath().pathWithinApplication();
    TreeSet<PathPattern> result = null;
    for (PathPattern pattern : this.patterns) {
        if (pattern.matches(lookupPath)) {
            result = (result != null ? result : new TreeSet<>());
            result.add(pattern);
        }
    }
    return result;
}
Also used : PathContainer(org.springframework.http.server.PathContainer) PathPattern(org.springframework.web.util.pattern.PathPattern) Nullable(org.springframework.lang.Nullable)

Example 13 with PathContainer

use of org.springframework.http.server.PathContainer in project spring-framework by spring-projects.

the class PathMatchingBenchmark method matchAndSortAllRoutesWithPathPatternParser.

@Benchmark
public void matchAndSortAllRoutesWithPathPatternParser(AllRoutesPatternParser data, Blackhole bh) {
    for (PathContainer path : data.requestPaths) {
        List<PathPattern> matches = new ArrayList<>();
        for (PathPattern pattern : data.patterns) {
            if (pattern.matches(path)) {
                matches.add(pattern);
            }
        }
        Collections.sort(matches);
        bh.consume(matches);
    }
}
Also used : PathContainer(org.springframework.http.server.PathContainer) ArrayList(java.util.ArrayList) Benchmark(org.openjdk.jmh.annotations.Benchmark)

Example 14 with PathContainer

use of org.springframework.http.server.PathContainer in project spring-framework by spring-projects.

the class PathResourceLookupFunction method apply.

@Override
public Mono<Resource> apply(ServerRequest request) {
    PathContainer pathContainer = request.requestPath().pathWithinApplication();
    if (!this.pattern.matches(pathContainer)) {
        return Mono.empty();
    }
    pathContainer = this.pattern.extractPathWithinPattern(pathContainer);
    String path = processPath(pathContainer.value());
    if (path.contains("%")) {
        path = StringUtils.uriDecode(path, StandardCharsets.UTF_8);
    }
    if (!StringUtils.hasLength(path) || isInvalidPath(path)) {
        return Mono.empty();
    }
    try {
        Resource resource = this.location.createRelative(path);
        if (resource.isReadable() && isResourceUnderLocation(resource)) {
            return Mono.just(resource);
        } else {
            return Mono.empty();
        }
    } catch (IOException ex) {
        throw new UncheckedIOException(ex);
    }
}
Also used : PathContainer(org.springframework.http.server.PathContainer) UrlResource(org.springframework.core.io.UrlResource) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Aggregations

PathContainer (org.springframework.http.server.PathContainer)14 PathPattern (org.springframework.web.util.pattern.PathPattern)6 Nullable (org.springframework.lang.Nullable)3 IOException (java.io.IOException)2 UncheckedIOException (java.io.UncheckedIOException)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 Resource (org.springframework.core.io.Resource)2 UrlResource (org.springframework.core.io.UrlResource)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Test (org.junit.jupiter.api.Test)1 Benchmark (org.openjdk.jmh.annotations.Benchmark)1 MediaType (org.springframework.http.MediaType)1 RequestPath (org.springframework.http.server.RequestPath)1 ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)1 MultiValueMap (org.springframework.util.MultiValueMap)1 ServerWebExchange (org.springframework.web.server.ServerWebExchange)1 MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)1 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)1 PathMatchInfo (org.springframework.web.util.pattern.PathPattern.PathMatchInfo)1