Search in sources :

Example 6 with PathPattern

use of org.springframework.web.util.pattern.PathPattern 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)

Example 7 with PathPattern

use of org.springframework.web.util.pattern.PathPattern in project spring-framework by spring-projects.

the class PathPatternsRequestCondition method getMatchingPatterns.

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

Example 8 with PathPattern

use of org.springframework.web.util.pattern.PathPattern in project spring-framework by spring-projects.

the class RequestMappingInfoHandlerMapping method extractMatchDetails.

private void extractMatchDetails(PathPatternsRequestCondition condition, String lookupPath, HttpServletRequest request) {
    PathPattern bestPattern;
    Map<String, String> uriVariables;
    if (condition.isEmptyPathMapping()) {
        bestPattern = condition.getFirstPattern();
        uriVariables = Collections.emptyMap();
    } else {
        PathContainer path = ServletRequestPathUtils.getParsedRequestPath(request).pathWithinApplication();
        bestPattern = condition.getFirstPattern();
        PathPattern.PathMatchInfo result = bestPattern.matchAndExtract(path);
        Assert.notNull(result, () -> "Expected bestPattern: " + bestPattern + " to match lookupPath " + path);
        uriVariables = result.getUriVariables();
        request.setAttribute(MATRIX_VARIABLES_ATTRIBUTE, result.getMatrixVariables());
    }
    request.setAttribute(BEST_MATCHING_PATTERN_ATTRIBUTE, bestPattern.getPatternString());
    request.setAttribute(URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriVariables);
}
Also used : PathContainer(org.springframework.http.server.PathContainer) PathPattern(org.springframework.web.util.pattern.PathPattern)

Example 9 with PathPattern

use of org.springframework.web.util.pattern.PathPattern in project flow by vaadin.

the class EndpointUtil method getEndpoint.

private Optional<Method> getEndpoint(HttpServletRequest request) {
    PathPatternParser pathParser = new PathPatternParser();
    PathPattern pathPattern = pathParser.parse(endpointProperties.getEndpointPrefix() + EndpointController.ENDPOINT_METHODS);
    RequestPath requestPath = ServletRequestPathUtils.parseAndCache(request);
    PathContainer pathWithinApplication = requestPath.pathWithinApplication();
    PathMatchInfo matchInfo = pathPattern.matchAndExtract(pathWithinApplication);
    if (matchInfo == null) {
        return Optional.empty();
    }
    Map<String, String> uriVariables = matchInfo.getUriVariables();
    String endpointName = uriVariables.get("endpoint");
    String endpointMethod = uriVariables.get("method");
    EndpointRegistry.VaadinEndpointData data = registry.get(endpointName);
    if (data == null) {
        return Optional.empty();
    }
    return data.getMethod(endpointMethod);
}
Also used : RequestPath(org.springframework.http.server.RequestPath) PathContainer(org.springframework.http.server.PathContainer) PathMatchInfo(org.springframework.web.util.pattern.PathPattern.PathMatchInfo) PathPattern(org.springframework.web.util.pattern.PathPattern) PathPatternParser(org.springframework.web.util.pattern.PathPatternParser)

Example 10 with PathPattern

use of org.springframework.web.util.pattern.PathPattern in project spring-cloud-open-service-broker by spring-cloud.

the class ApiVersionWebFilter method filter.

/**
 * Process the web request and validate the API version in the header. If the API version
 * does not match, then set an HTTP 412 status and write the error message to the response.
 *
 * @param exchange {@inheritDoc}
 * @param chain {@inheritDoc}
 * @return {@inheritDoc}
 */
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
    PathPattern p = new PathPatternParser().parse(V2_API_PATH_PATTERN);
    if (p.matches(exchange.getRequest().getPath()) && version != null && !anyVersionAllowed()) {
        String apiVersion = exchange.getRequest().getHeaders().getFirst(version.getBrokerApiVersionHeader());
        if (!version.getApiVersion().equals(apiVersion)) {
            String message = ServiceBrokerApiVersionErrorMessage.from(version.getApiVersion(), apiVersion).toString();
            ServerHttpResponse response = exchange.getResponse();
            String json;
            try {
                json = new ObjectMapper().writeValueAsString(new ErrorMessage(message));
            } catch (JsonProcessingException e) {
                json = "{}";
            }
            response.setStatusCode(HttpStatus.PRECONDITION_FAILED);
            Flux<DataBuffer> responseBody = Flux.just(json).map(s -> toDataBuffer(s, response.bufferFactory()));
            return response.writeWith(responseBody);
        }
    }
    return chain.filter(exchange);
}
Also used : PathPattern(org.springframework.web.util.pattern.PathPattern) PathPatternParser(org.springframework.web.util.pattern.PathPatternParser) ErrorMessage(org.springframework.cloud.servicebroker.model.error.ErrorMessage) ServiceBrokerApiVersionErrorMessage(org.springframework.cloud.servicebroker.exception.ServiceBrokerApiVersionErrorMessage) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Aggregations

PathPattern (org.springframework.web.util.pattern.PathPattern)19 PathContainer (org.springframework.http.server.PathContainer)6 Test (org.junit.jupiter.api.Test)5 Nullable (org.springframework.lang.Nullable)4 RequestMappingInfo (org.springframework.web.reactive.result.method.RequestMappingInfo)3 ServerWebExchange (org.springframework.web.server.ServerWebExchange)3 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)3 PathPatternParser (org.springframework.web.util.pattern.PathPatternParser)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 ServiceBrokerApiVersionErrorMessage (org.springframework.cloud.servicebroker.exception.ServiceBrokerApiVersionErrorMessage)1 ErrorMessage (org.springframework.cloud.servicebroker.model.error.ErrorMessage)1 DataBuffer (org.springframework.core.io.buffer.DataBuffer)1 HttpStatus (org.springframework.http.HttpStatus)1 MediaType (org.springframework.http.MediaType)1 ServerCodecConfigurer (org.springframework.http.codec.ServerCodecConfigurer)1