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;
}
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");
}
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);
}
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;
}
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);
}
}
Aggregations