From 21f165c313e4d755dd9c86828e25647449c5a4f9 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Mon, 10 Dec 2018 21:26:29 -0500 Subject: [PATCH] Adds convenience methods for working with Path Predicate variables. fixes gh-705 --- docs/src/main/asciidoc/spring-cloud-gateway.adoc | 9 +++++++++ .../factory/SetPathGatewayFilterFactory.java | 12 ++---------- .../gateway/support/ServerWebExchangeUtils.java | 16 ++++++++++++++++ 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/docs/src/main/asciidoc/spring-cloud-gateway.adoc b/docs/src/main/asciidoc/spring-cloud-gateway.adoc index 4f9431a8..11a73649 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 307faf0f..d9ca270f 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 b79a3628..e5eac5a2 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 @@ -123,4 +126,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(); + } }