Flushes out RoutePredicates

This commit is contained in:
Spencer Gibb
2017-10-02 22:53:46 -04:00
parent 7cfd46d45b
commit 77d2de7d72

View File

@@ -17,11 +17,17 @@
package org.springframework.cloud.gateway.handler.predicate;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.springframework.web.server.ServerWebExchange;
import java.time.ZonedDateTime;
import java.util.function.Predicate;
import static org.springframework.cloud.gateway.handler.predicate.BetweenRoutePredicateFactory.DATETIME1_KEY;
import static org.springframework.cloud.gateway.handler.predicate.BetweenRoutePredicateFactory.DATETIME2_KEY;
import static org.springframework.cloud.gateway.handler.predicate.MethodRoutePredicateFactory.METHOD_KEY;
import static org.springframework.cloud.gateway.handler.predicate.RoutePredicateFactory.PATTERN_KEY;
import static org.springframework.tuple.TupleBuilder.tuple;
@@ -41,14 +47,18 @@ public class RoutePredicates {
public static Predicate<ServerWebExchange> between(ZonedDateTime datetime1, ZonedDateTime datetime2) {
return new BetweenRoutePredicateFactory().apply(tuple()
.of(BetweenRoutePredicateFactory.DATETIME1_KEY, datetime1, BetweenRoutePredicateFactory.DATETIME2_KEY, datetime2));
.of(DATETIME1_KEY, datetime1, DATETIME2_KEY, datetime2));
}
//TODO: add support for CookieRoutePredicateFactory
public static Predicate<ServerWebExchange> cookie(String name, String regex) {
return new CookieRoutePredicateFactory().apply(tuple()
.of(CookieRoutePredicateFactory.NAME_KEY, name, CookieRoutePredicateFactory.REGEXP_KEY, regex));
}
//TODO: add support for RoutePredicates
//TODO: add support for HeaderRoutePredicateFactory
public static Predicate<ServerWebExchange> header(String header, String regex) {
return new HeaderRoutePredicateFactory().apply(tuple()
.of(HeaderRoutePredicateFactory.HEADER_KEY, header, HeaderRoutePredicateFactory.REGEXP_KEY, regex));
}
public static Predicate<ServerWebExchange> host(String pattern) {
return new HostRoutePredicateFactory().apply(tuple().of(PATTERN_KEY, pattern));
@@ -62,11 +72,15 @@ public class RoutePredicates {
return new PathRoutePredicateFactory().apply(tuple().of(PATTERN_KEY, pattern));
}
//TODO: add support for PredicateDefinition
public static Predicate<ServerWebExchange> query(String param, String regex) {
return new QueryRoutePredicateFactory().apply(tuple().
of(QueryRoutePredicateFactory.PARAM_KEY, param, QueryRoutePredicateFactory.REGEXP_KEY, regex));
}
//TODO: add support for QueryRoutePredicateFactory
//TODO: add support for RemoteAddrRoutePredicateFactory
public static Predicate<ServerWebExchange> remoteAddr(String... addrs) {
List<String> names = IntStream.range(0, addrs.length).mapToObj(i -> "addr" + i).collect(Collectors.toList());
return new RemoteAddrRoutePredicateFactory().apply(tuple().ofNamesAndValues(names, Arrays.asList(addrs)));
}
public static Predicate<ServerWebExchange> alwaysTrue() {
return exchange -> true;