Skip to content

Commit e5281d6

Browse files
committed
Adds GatewayRequestPredicates.host(...) and GatewayRequestPredicates.path(...)
1 parent ff98d62 commit e5281d6

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/predicate/GatewayRequestPredicates.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,22 @@ public static RequestPredicate method(HttpMethod... methods) {
126126
return RequestPredicates.methods(methods);
127127
}
128128

129-
@Shortcut
130129
public static RequestPredicate host(String pattern) {
131130
Assert.notNull(pattern, "'pattern' must not be null");
132131
return hostPredicates(DEFAULT_HOST_INSTANCE).apply(pattern);
133132
}
134133

134+
@Shortcut
135+
public static RequestPredicate host(String... patterns) {
136+
Assert.notEmpty(patterns, "'patterns' must not be empty");
137+
RequestPredicate requestPredicate = hostPredicates(DEFAULT_HOST_INSTANCE).apply(patterns[0]);
138+
// I'm sure there's a functional way to do this, I'm just tired...
139+
for (int i = 1; i < patterns.length; i++) {
140+
requestPredicate = requestPredicate.or(hostPredicates(DEFAULT_HOST_INSTANCE).apply(patterns[i]));
141+
}
142+
return requestPredicate;
143+
}
144+
135145
/**
136146
* Return a function that creates new host-matching {@code RequestPredicates} from
137147
* pattern Strings using the given {@link PathPatternParser}.
@@ -155,11 +165,27 @@ public static Function<String, RequestPredicate> hostPredicates(PathPatternParse
155165
* @return a predicate that tests against the given path pattern
156166
*/
157167
// TODO: find a different way to add shortcut to RequestPredicates.*
158-
@Shortcut
159168
public static RequestPredicate path(String pattern) {
160169
return RequestPredicates.path(pattern);
161170
}
162171

172+
/**
173+
* Return a {@code RequestPredicate} that tests the request path against the given
174+
* path pattern.
175+
* @param patterns the list of patterns to match
176+
* @return a predicate that tests against the given path pattern
177+
*/
178+
@Shortcut
179+
public static RequestPredicate path(String... patterns) {
180+
Assert.notEmpty(patterns, "'patterns' must not be empty");
181+
RequestPredicate requestPredicate = RequestPredicates.path(patterns[0]);
182+
// I'm sure there's a functional way to do this, I'm just tired...
183+
for (int i = 1; i < patterns.length; i++) {
184+
requestPredicate = requestPredicate.or(RequestPredicates.path(patterns[i]));
185+
}
186+
return requestPredicate;
187+
}
188+
163189
public static <T> RequestPredicate readBody(Class<T> inClass, Predicate<T> predicate) {
164190
return new ReadBodyPredicate<>(inClass, predicate);
165191
}

spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/ServerMvcIntegrationTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.springframework.cloud.gateway.server.mvc.common.MvcUtils;
4646
import org.springframework.cloud.gateway.server.mvc.filter.ForwardedRequestHeadersFilter;
4747
import org.springframework.cloud.gateway.server.mvc.filter.XForwardedRequestHeadersFilter;
48+
import org.springframework.cloud.gateway.server.mvc.predicate.GatewayRequestPredicates;
4849
import org.springframework.cloud.gateway.server.mvc.test.HttpbinTestcontainers;
4950
import org.springframework.cloud.gateway.server.mvc.test.HttpbinUriResolver;
5051
import org.springframework.cloud.gateway.server.mvc.test.LocalServerPortUriResolver;
@@ -735,7 +736,7 @@ public RouterFunction<ServerResponse> gatewayRouterFunctionsLoadBalancer() {
735736
public RouterFunction<ServerResponse> gatewayRouterFunctionsHost() {
736737
// @formatter:off
737738
return route("testhostpredicate")
738-
.route(host("{sub}.myjavadslhost.com").and(path("/anything/hostpredicate")), http())
739+
.route(host("{sub}.somehotherhost.com", "{sub}.myjavadslhost.com").and(path("/anything/hostpredicate")), http())
739740
.before(new HttpbinUriResolver())
740741
.before(preserveHostHeader())
741742
.after(addResponseHeader("X-SubDomain", "{sub}"))
@@ -818,7 +819,7 @@ public RouterFunction<ServerResponse> gatewayRouterFunctionsHeaderPredicate() {
818819
@Bean
819820
public RouterFunction<ServerResponse> gatewayRouterFunctionsCookiePredicate() {
820821
// @formatter:off
821-
return route(path("/cookieregex").and(cookie("mycookie", "fo.")), http())
822+
return route(GatewayRequestPredicates.path("/dummypath", "/cookieregex").and(cookie("mycookie", "fo.")), http())
822823
.filter(new HttpbinUriResolver())
823824
.filter(setPath("/headers"))
824825
.withAttribute(MvcUtils.GATEWAY_ROUTE_ID_ATTR, "testcookiepredicate");

0 commit comments

Comments
 (0)