Merge branch '2.0.x'
This commit is contained in:
@@ -190,6 +190,15 @@ This route would match if the request path was, for example: `/foo/1` or `/foo/b
|
||||
|
||||
This predicate extracts the URI template variables (like `segment` defined in the example above) as a map of names and values and places it in the `ServerWebExchange.getAttributes()` with a key defined in `PathRoutePredicate.URL_PREDICATE_VARS_ATTR`. Those values are then available for use by <<gateway-route-filters,GatewayFilter Factories>>
|
||||
|
||||
A utility method is available to make access to these variables easier.
|
||||
|
||||
[source,java]
|
||||
----
|
||||
Map<String, String> uriVariables = ServerWebExchangeUtils.getPathPredicateVariables(exchange);
|
||||
|
||||
String segment = uriVariables.get("segment");
|
||||
----
|
||||
|
||||
=== Query Route Predicate Factory
|
||||
The Query Route Predicate Factory takes two parameters: a required `param` and an optional `regexp`.
|
||||
|
||||
|
||||
@@ -19,18 +19,16 @@ package org.springframework.cloud.gateway.filter.factory;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.web.util.UriTemplate;
|
||||
import org.springframework.web.util.pattern.PathPattern.PathMatchInfo;
|
||||
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.URI_TEMPLATE_VARIABLES_ATTRIBUTE;
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.addOriginalRequestUrl;
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.getPathPredicateVariables;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
@@ -53,16 +51,10 @@ public class SetPathGatewayFilterFactory extends AbstractGatewayFilterFactory<Se
|
||||
UriTemplate uriTemplate = new UriTemplate(config.template);
|
||||
|
||||
return (exchange, chain) -> {
|
||||
PathMatchInfo variables = exchange.getAttribute(URI_TEMPLATE_VARIABLES_ATTRIBUTE);
|
||||
ServerHttpRequest req = exchange.getRequest();
|
||||
addOriginalRequestUrl(exchange, req.getURI());
|
||||
Map<String, String> uriVariables;
|
||||
|
||||
if (variables != null) {
|
||||
uriVariables = variables.getUriVariables();
|
||||
} else {
|
||||
uriVariables = Collections.emptyMap();
|
||||
}
|
||||
Map<String, String> uriVariables = getPathPredicateVariables(exchange);
|
||||
|
||||
URI uri = uriTemplate.expand(uriVariables);
|
||||
String newPath = uri.getRawPath();
|
||||
|
||||
@@ -18,7 +18,9 @@
|
||||
package org.springframework.cloud.gateway.support;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
@@ -30,6 +32,7 @@ import org.springframework.cloud.gateway.handler.AsyncPredicate;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.server.reactive.AbstractServerHttpResponse;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.util.pattern.PathPattern;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
@@ -125,4 +128,17 @@ public class ServerWebExchangeUtils {
|
||||
Objects.requireNonNull(predicate, "predicate must not be null");
|
||||
return t -> Mono.just(predicate.test(t));
|
||||
}
|
||||
|
||||
public static Map<String, String> getPathPredicateVariables(ServerWebExchange exchange) {
|
||||
return getPathMatchVariables(exchange, URI_TEMPLATE_VARIABLES_ATTRIBUTE);
|
||||
}
|
||||
|
||||
public static Map<String, String> getPathMatchVariables(ServerWebExchange exchange, String attr) {
|
||||
PathPattern.PathMatchInfo variables = exchange.getAttribute(URI_TEMPLATE_VARIABLES_ATTRIBUTE);
|
||||
|
||||
if (variables != null) {
|
||||
return variables.getUriVariables();
|
||||
}
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user