From e5281d6658935b604b2fd095809a114e32f46ba9 Mon Sep 17 00:00:00 2001 From: sgibb Date: Mon, 4 Dec 2023 17:56:53 -0500 Subject: [PATCH] Adds GatewayRequestPredicates.host(...) and GatewayRequestPredicates.path(...) --- .../predicate/GatewayRequestPredicates.java | 30 +++++++++++++++++-- .../server/mvc/ServerMvcIntegrationTests.java | 5 ++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/predicate/GatewayRequestPredicates.java b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/predicate/GatewayRequestPredicates.java index 989a28f8..12006c24 100644 --- a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/predicate/GatewayRequestPredicates.java +++ b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/predicate/GatewayRequestPredicates.java @@ -126,12 +126,22 @@ public abstract class GatewayRequestPredicates { return RequestPredicates.methods(methods); } - @Shortcut public static RequestPredicate host(String pattern) { Assert.notNull(pattern, "'pattern' must not be null"); return hostPredicates(DEFAULT_HOST_INSTANCE).apply(pattern); } + @Shortcut + public static RequestPredicate host(String... patterns) { + Assert.notEmpty(patterns, "'patterns' must not be empty"); + RequestPredicate requestPredicate = hostPredicates(DEFAULT_HOST_INSTANCE).apply(patterns[0]); + // I'm sure there's a functional way to do this, I'm just tired... + for (int i = 1; i < patterns.length; i++) { + requestPredicate = requestPredicate.or(hostPredicates(DEFAULT_HOST_INSTANCE).apply(patterns[i])); + } + return requestPredicate; + } + /** * Return a function that creates new host-matching {@code RequestPredicates} from * pattern Strings using the given {@link PathPatternParser}. @@ -155,11 +165,27 @@ public abstract class GatewayRequestPredicates { * @return a predicate that tests against the given path pattern */ // TODO: find a different way to add shortcut to RequestPredicates.* - @Shortcut public static RequestPredicate path(String pattern) { return RequestPredicates.path(pattern); } + /** + * Return a {@code RequestPredicate} that tests the request path against the given + * path pattern. + * @param patterns the list of patterns to match + * @return a predicate that tests against the given path pattern + */ + @Shortcut + public static RequestPredicate path(String... patterns) { + Assert.notEmpty(patterns, "'patterns' must not be empty"); + RequestPredicate requestPredicate = RequestPredicates.path(patterns[0]); + // I'm sure there's a functional way to do this, I'm just tired... + for (int i = 1; i < patterns.length; i++) { + requestPredicate = requestPredicate.or(RequestPredicates.path(patterns[i])); + } + return requestPredicate; + } + public static RequestPredicate readBody(Class inClass, Predicate predicate) { return new ReadBodyPredicate<>(inClass, predicate); } diff --git a/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/ServerMvcIntegrationTests.java b/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/ServerMvcIntegrationTests.java index b8611b2c..21427778 100644 --- a/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/ServerMvcIntegrationTests.java +++ b/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/ServerMvcIntegrationTests.java @@ -45,6 +45,7 @@ import org.springframework.boot.test.web.server.LocalServerPort; import org.springframework.cloud.gateway.server.mvc.common.MvcUtils; import org.springframework.cloud.gateway.server.mvc.filter.ForwardedRequestHeadersFilter; import org.springframework.cloud.gateway.server.mvc.filter.XForwardedRequestHeadersFilter; +import org.springframework.cloud.gateway.server.mvc.predicate.GatewayRequestPredicates; import org.springframework.cloud.gateway.server.mvc.test.HttpbinTestcontainers; import org.springframework.cloud.gateway.server.mvc.test.HttpbinUriResolver; import org.springframework.cloud.gateway.server.mvc.test.LocalServerPortUriResolver; @@ -735,7 +736,7 @@ public class ServerMvcIntegrationTests { public RouterFunction gatewayRouterFunctionsHost() { // @formatter:off return route("testhostpredicate") - .route(host("{sub}.myjavadslhost.com").and(path("/anything/hostpredicate")), http()) + .route(host("{sub}.somehotherhost.com", "{sub}.myjavadslhost.com").and(path("/anything/hostpredicate")), http()) .before(new HttpbinUriResolver()) .before(preserveHostHeader()) .after(addResponseHeader("X-SubDomain", "{sub}")) @@ -818,7 +819,7 @@ public class ServerMvcIntegrationTests { @Bean public RouterFunction gatewayRouterFunctionsCookiePredicate() { // @formatter:off - return route(path("/cookieregex").and(cookie("mycookie", "fo.")), http()) + return route(GatewayRequestPredicates.path("/dummypath", "/cookieregex").and(cookie("mycookie", "fo.")), http()) .filter(new HttpbinUriResolver()) .filter(setPath("/headers")) .withAttribute(MvcUtils.GATEWAY_ROUTE_ID_ATTR, "testcookiepredicate");