Search in sources :

Example 11 with PathPatternParser

use of org.springframework.web.util.pattern.PathPatternParser in project spring-boot by spring-projects.

the class WebMvcTagsTests method uriTagIsDataRestsEffectiveRepositoryLookupPathWhenAvailable.

@Test
void uriTagIsDataRestsEffectiveRepositoryLookupPathWhenAvailable() {
    this.request.setAttribute("org.springframework.data.rest.webmvc.RepositoryRestHandlerMapping.EFFECTIVE_REPOSITORY_RESOURCE_LOOKUP_PATH", new PathPatternParser().parse("/api/cities"));
    this.request.setAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, "/api/{repository}");
    Tag tag = WebMvcTags.uri(this.request, this.response);
    assertThat(tag.getValue()).isEqualTo("/api/cities");
}
Also used : PathPatternParser(org.springframework.web.util.pattern.PathPatternParser) Tag(io.micrometer.core.instrument.Tag) Test(org.junit.jupiter.api.Test)

Example 12 with PathPatternParser

use of org.springframework.web.util.pattern.PathPatternParser 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 13 with PathPatternParser

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

Example 14 with PathPatternParser

use of org.springframework.web.util.pattern.PathPatternParser in project spring-boot by spring-projects.

the class MetricsWebFilterTests method createExchange.

private MockServerWebExchange createExchange(String path, String pathPattern) {
    PathPatternParser parser = new PathPatternParser();
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get(path).build());
    exchange.getAttributes().put(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, parser.parse(pathPattern));
    return exchange;
}
Also used : PathPatternParser(org.springframework.web.util.pattern.PathPatternParser) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange)

Example 15 with PathPatternParser

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

the class WebFluxConfigurationSupportTests method customPathMatchConfig.

@Test
public void customPathMatchConfig() {
    ApplicationContext context = loadConfig(CustomPatchMatchConfig.class);
    final Field field = ReflectionUtils.findField(PathPatternParser.class, "matchOptionalTrailingSeparator");
    ReflectionUtils.makeAccessible(field);
    String name = "requestMappingHandlerMapping";
    RequestMappingHandlerMapping mapping = context.getBean(name, RequestMappingHandlerMapping.class);
    assertThat(mapping).isNotNull();
    PathPatternParser patternParser = mapping.getPathPatternParser();
    assertThat(patternParser).isNotNull();
    boolean matchOptionalTrailingSlash = (boolean) ReflectionUtils.getField(field, patternParser);
    assertThat(matchOptionalTrailingSlash).isFalse();
    Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();
    assertThat(map.size()).isEqualTo(1);
    assertThat(map.keySet().iterator().next().getPatternsCondition().getPatterns()).isEqualTo(Collections.singleton(new PathPatternParser().parse("/api/user/{id}")));
}
Also used : Field(java.lang.reflect.Field) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) RequestMappingInfo(org.springframework.web.reactive.result.method.RequestMappingInfo) PathPatternParser(org.springframework.web.util.pattern.PathPatternParser) RequestMappingHandlerMapping(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.jupiter.api.Test)

Aggregations

PathPatternParser (org.springframework.web.util.pattern.PathPatternParser)22 Test (org.junit.jupiter.api.Test)13 RequestMappingInfo (org.springframework.web.reactive.result.method.RequestMappingInfo)4 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)3 Field (java.lang.reflect.Field)2 Collections (java.util.Collections)2 List (java.util.List)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 ApplicationContext (org.springframework.context.ApplicationContext)2 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)2 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)2 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)2 PathPattern (org.springframework.web.util.pattern.PathPattern)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Tag (io.micrometer.core.instrument.Tag)1 Method (java.lang.reflect.Method)1 Optional (java.util.Optional)1 Properties (java.util.Properties)1 BiConsumer (java.util.function.BiConsumer)1