diff --git a/docs/src/main/asciidoc/spring-cloud-gateway.adoc b/docs/src/main/asciidoc/spring-cloud-gateway.adoc index 5f221f723..42a01db36 100644 --- a/docs/src/main/asciidoc/spring-cloud-gateway.adoc +++ b/docs/src/main/asciidoc/spring-cloud-gateway.adoc @@ -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 <> +A utility method is available to make access to these variables easier. + +[source,java] +---- +Map 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`. diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/SetPathGatewayFilterFactory.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/SetPathGatewayFilterFactory.java index 307faf0f9..d9ca270fd 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/SetPathGatewayFilterFactory.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/SetPathGatewayFilterFactory.java @@ -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 { - PathMatchInfo variables = exchange.getAttribute(URI_TEMPLATE_VARIABLES_ATTRIBUTE); ServerHttpRequest req = exchange.getRequest(); addOriginalRequestUrl(exchange, req.getURI()); - Map uriVariables; - if (variables != null) { - uriVariables = variables.getUriVariables(); - } else { - uriVariables = Collections.emptyMap(); - } + Map uriVariables = getPathPredicateVariables(exchange); URI uri = uriTemplate.expand(uriVariables); String newPath = uri.getRawPath(); diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/support/ServerWebExchangeUtils.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/support/ServerWebExchangeUtils.java index 2164b4ec5..5a1710996 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/support/ServerWebExchangeUtils.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/support/ServerWebExchangeUtils.java @@ -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 getPathPredicateVariables(ServerWebExchange exchange) { + return getPathMatchVariables(exchange, URI_TEMPLATE_VARIABLES_ATTRIBUTE); + } + + public static Map getPathMatchVariables(ServerWebExchange exchange, String attr) { + PathPattern.PathMatchInfo variables = exchange.getAttribute(URI_TEMPLATE_VARIABLES_ATTRIBUTE); + + if (variables != null) { + return variables.getUriVariables(); + } + return Collections.emptyMap(); + } }