Adds GatewayRequestPredicates.host(...) and GatewayRequestPredicates.path(...)
This commit is contained in:
@@ -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 <T> RequestPredicate readBody(Class<T> inClass, Predicate<T> predicate) {
|
||||
return new ReadBodyPredicate<>(inClass, predicate);
|
||||
}
|
||||
|
||||
@@ -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<ServerResponse> 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<ServerResponse> 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");
|
||||
|
||||
Reference in New Issue
Block a user