Search in sources :

Example 11 with PathPattern

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

the class WebFluxTags method uri.

/**
 * Creates a {@code uri} tag based on the URI of the given {@code exchange}. Uses the
 * {@link HandlerMapping#BEST_MATCHING_PATTERN_ATTRIBUTE} best matching pattern if
 * available. Falling back to {@code REDIRECTION} for 3xx responses, {@code NOT_FOUND}
 * for 404 responses, {@code root} for requests with no path info, and {@code UNKNOWN}
 * for all other requests.
 * @param exchange the exchange
 * @param ignoreTrailingSlash whether to ignore the trailing slash
 * @return the uri tag derived from the exchange
 */
public static Tag uri(ServerWebExchange exchange, boolean ignoreTrailingSlash) {
    PathPattern pathPattern = exchange.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
    if (pathPattern != null) {
        String patternString = pathPattern.getPatternString();
        if (ignoreTrailingSlash && patternString.length() > 1) {
            patternString = removeTrailingSlash(patternString);
        }
        if (patternString.isEmpty()) {
            return URI_ROOT;
        }
        return Tag.of("uri", patternString);
    }
    HttpStatus status = exchange.getResponse().getStatusCode();
    if (status != null) {
        if (status.is3xxRedirection()) {
            return URI_REDIRECTION;
        }
        if (status == HttpStatus.NOT_FOUND) {
            return URI_NOT_FOUND;
        }
    }
    String path = getPathInfo(exchange);
    if (path.isEmpty()) {
        return URI_ROOT;
    }
    return URI_UNKNOWN;
}
Also used : PathPattern(org.springframework.web.util.pattern.PathPattern) HttpStatus(org.springframework.http.HttpStatus)

Example 12 with PathPattern

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

the class RequestMappingInfoTests method prependPatternWithSlash.

@Test
public void prependPatternWithSlash() {
    RequestMappingInfo actual = paths("foo").build();
    List<PathPattern> patterns = new ArrayList<>(actual.getPatternsCondition().getPatterns());
    assertThat(patterns.size()).isEqualTo(1);
    assertThat(patterns.get(0).getPatternString()).isEqualTo("/foo");
}
Also used : PathPattern(org.springframework.web.util.pattern.PathPattern) RequestMappingInfo(org.springframework.web.reactive.result.method.RequestMappingInfo) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 13 with PathPattern

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

the class PathPatternsRequestCondition method getMatchingCondition.

/**
 * Checks if any of the patterns match the given request and returns an
 * instance that is guaranteed to contain matching patterns, sorted.
 * @param request the current request
 * @return the same instance if the condition contains no patterns;
 * or a new condition with sorted matching patterns;
 * or {@code null} if no patterns match.
 */
@Override
@Nullable
public PathPatternsRequestCondition getMatchingCondition(HttpServletRequest request) {
    PathContainer path = ServletRequestPathUtils.getParsedRequestPath(request).pathWithinApplication();
    SortedSet<PathPattern> matches = getMatchingPatterns(path);
    return (matches != null ? new PathPatternsRequestCondition(matches) : null);
}
Also used : PathContainer(org.springframework.http.server.PathContainer) PathPattern(org.springframework.web.util.pattern.PathPattern) Nullable(org.springframework.lang.Nullable)

Example 14 with PathPattern

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

the class PathPatternsRequestCondition method getDirectPaths.

/**
 * Return the mapping paths that are not patterns.
 */
public Set<String> getDirectPaths() {
    if (isEmptyPathMapping()) {
        return EMPTY_PATH;
    }
    Set<String> result = Collections.emptySet();
    for (PathPattern pattern : this.patterns) {
        if (!pattern.hasPatternSyntax()) {
            result = (result.isEmpty() ? new HashSet<>(1) : result);
            result.add(pattern.getPatternString());
        }
    }
    return result;
}
Also used : PathPattern(org.springframework.web.util.pattern.PathPattern)

Example 15 with PathPattern

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

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