Search in sources :

Example 1 with PathContainer

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

the class PathPatternParserServerWebExchangeMatcher method matches.

@Override
public Mono<MatchResult> matches(ServerWebExchange exchange) {
    ServerHttpRequest request = exchange.getRequest();
    PathContainer path = request.getPath().pathWithinApplication();
    if (this.method != null && !this.method.equals(request.getMethod())) {
        return MatchResult.notMatch().doOnNext((result) -> {
            if (logger.isDebugEnabled()) {
                logger.debug("Request '" + request.getMethod() + " " + path + "' doesn't match '" + this.method + " " + this.pattern.getPatternString() + "'");
            }
        });
    }
    boolean match = this.pattern.matches(path);
    if (!match) {
        return MatchResult.notMatch().doOnNext((result) -> {
            if (logger.isDebugEnabled()) {
                logger.debug("Request '" + request.getMethod() + " " + path + "' doesn't match '" + this.method + " " + this.pattern.getPatternString() + "'");
            }
        });
    }
    Map<String, String> pathVariables = this.pattern.matchAndExtract(path).getUriVariables();
    Map<String, Object> variables = new HashMap<>(pathVariables);
    if (logger.isDebugEnabled()) {
        logger.debug("Checking match of request : '" + path + "'; against '" + this.pattern.getPatternString() + "'");
    }
    return MatchResult.match(variables);
}
Also used : PathContainer(org.springframework.http.server.PathContainer) HashMap(java.util.HashMap) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest)

Example 2 with PathContainer

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

the class PathPatternTests method checkMatches.

private void checkMatches(String uriTemplate, String path) {
    PathPatternParser parser = new PathPatternParser();
    parser.setMatchOptionalTrailingSeparator(true);
    PathPattern p = parser.parse(uriTemplate);
    PathContainer pc = toPathContainer(path);
    assertThat(p.matches(pc)).isTrue();
}
Also used : PathContainer(org.springframework.http.server.PathContainer)

Example 3 with PathContainer

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

the class SimpleUrlHandlerMappingTests method testUrl.

void testUrl(String url, Object bean, HandlerMapping handlerMapping, String pathWithinMapping) {
    MockServerHttpRequest request = MockServerHttpRequest.method(HttpMethod.GET, URI.create(url)).build();
    ServerWebExchange exchange = MockServerWebExchange.from(request);
    Object actual = handlerMapping.getHandler(exchange).block();
    if (bean != null) {
        assertThat(actual).isNotNull();
        assertThat(actual).isSameAs(bean);
        // noinspection OptionalGetWithoutIsPresent
        PathContainer path = exchange.getAttribute(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
        assertThat(path).isNotNull();
        assertThat(path.value()).isEqualTo(pathWithinMapping);
    } else {
        assertThat(actual).isNull();
    }
}
Also used : PathContainer(org.springframework.http.server.PathContainer) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)

Example 4 with PathContainer

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

the class PathResourceLookupFunction method apply.

@Override
public Optional<Resource> apply(ServerRequest request) {
    PathContainer pathContainer = request.requestPath().pathWithinApplication();
    if (!this.pattern.matches(pathContainer)) {
        return Optional.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 Optional.empty();
    }
    try {
        Resource resource = this.location.createRelative(path);
        if (resource.isReadable() && isResourceUnderLocation(resource)) {
            return Optional.of(resource);
        } else {
            return Optional.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)

Example 5 with PathContainer

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

the class PathPatternMatchableHandlerMapping method match.

@Nullable
@Override
public RequestMatchResult match(HttpServletRequest request, String pattern) {
    PathPattern pathPattern = this.pathPatternCache.computeIfAbsent(pattern, value -> {
        Assert.isTrue(this.pathPatternCache.size() < MAX_PATTERNS, "Max size for pattern cache exceeded.");
        return this.parser.parse(pattern);
    });
    PathContainer path = ServletRequestPathUtils.getParsedRequestPath(request).pathWithinApplication();
    return (pathPattern.matches(path) ? new RequestMatchResult(pathPattern, path) : null);
}
Also used : PathContainer(org.springframework.http.server.PathContainer) PathPattern(org.springframework.web.util.pattern.PathPattern) Nullable(org.springframework.lang.Nullable)

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