diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/AfterRoutePredicateFactory.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/AfterRoutePredicateFactory.java index 091eabee..fb40000d 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/AfterRoutePredicateFactory.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/AfterRoutePredicateFactory.java @@ -25,8 +25,6 @@ import java.util.function.Predicate; import org.springframework.tuple.Tuple; import org.springframework.web.server.ServerWebExchange; -import static org.springframework.cloud.gateway.handler.predicate.BetweenRoutePredicateFactory.parseZonedDateTime; - /** * @author Spencer Gibb */ @@ -41,11 +39,13 @@ public class AfterRoutePredicateFactory implements RoutePredicateFactory { @Override public Predicate apply(Tuple args) { - final ZonedDateTime dateTime = parseZonedDateTime(args.getString(DATETIME_KEY)); + Object value = args.getValue(DATETIME_KEY); + final ZonedDateTime dateTime = BetweenRoutePredicateFactory.getZonedDateTime(value); return exchange -> { final ZonedDateTime now = ZonedDateTime.now(); return now.isAfter(dateTime); }; } + } diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/BeforeRoutePredicateFactory.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/BeforeRoutePredicateFactory.java index a8345383..6895f864 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/BeforeRoutePredicateFactory.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/BeforeRoutePredicateFactory.java @@ -41,7 +41,8 @@ public class BeforeRoutePredicateFactory implements RoutePredicateFactory { @Override public Predicate apply(Tuple args) { - final ZonedDateTime dateTime = parseZonedDateTime(args.getString(DATETIME_KEY)); + Object value = args.getValue(DATETIME_KEY); + final ZonedDateTime dateTime = BetweenRoutePredicateFactory.getZonedDateTime(value); return exchange -> { final ZonedDateTime now = ZonedDateTime.now(); diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/BetweenRoutePredicateFactory.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/BetweenRoutePredicateFactory.java index 7aaa1b26..5ff4e79c 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/BetweenRoutePredicateFactory.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/BetweenRoutePredicateFactory.java @@ -37,10 +37,10 @@ public class BetweenRoutePredicateFactory implements RoutePredicateFactory { @Override public Predicate apply(Tuple args) { //TODO: is ZonedDateTime the right thing to use? - final ZonedDateTime dateTime1 = parseZonedDateTime(args.getString(DATETIME1_KEY)); - final ZonedDateTime dateTime2 = parseZonedDateTime(args.getString(DATETIME2_KEY)); - Assert.isTrue(dateTime1.isBefore(dateTime2), args.getString(DATETIME1_KEY) + - " must be before " + args.getString(DATETIME2_KEY)); + final ZonedDateTime dateTime1 = getZonedDateTime(args.getValue(DATETIME1_KEY)); + final ZonedDateTime dateTime2 = getZonedDateTime(args.getValue(DATETIME2_KEY)); + Assert.isTrue(dateTime1.isBefore(dateTime2), args.getValue(DATETIME1_KEY) + + " must be before " + args.getValue(DATETIME2_KEY)); return exchange -> { final ZonedDateTime now = ZonedDateTime.now(); @@ -48,6 +48,16 @@ public class BetweenRoutePredicateFactory implements RoutePredicateFactory { }; } + public static ZonedDateTime getZonedDateTime(Object value) { + ZonedDateTime dateTime; + if (value instanceof ZonedDateTime) { + dateTime = ZonedDateTime.class.cast(value); + } else { + dateTime = parseZonedDateTime(value.toString()); + } + return dateTime; + } + public static ZonedDateTime parseZonedDateTime(String dateString) { ZonedDateTime dateTime; try { diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/RoutePredicates.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/RoutePredicates.java index 0fad6194..306ee58a 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/RoutePredicates.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/RoutePredicates.java @@ -19,6 +19,7 @@ package org.springframework.cloud.gateway.handler.predicate; import org.springframework.web.server.ServerWebExchange; +import java.time.ZonedDateTime; import java.util.function.Predicate; import static org.springframework.cloud.gateway.handler.predicate.MethodRoutePredicateFactory.METHOD_KEY; @@ -30,11 +31,18 @@ import static org.springframework.tuple.TupleBuilder.tuple; */ public class RoutePredicates { - //TODO: add support for AfterRoutePredicateFactory + public static Predicate after(ZonedDateTime datetime) { + return new AfterRoutePredicateFactory().apply(tuple().of(AfterRoutePredicateFactory.DATETIME_KEY, datetime)); + } - //TODO: add support for BeforeRoutePredicateFactory + public static Predicate before(ZonedDateTime datetime) { + return new BeforeRoutePredicateFactory().apply(tuple().of(BeforeRoutePredicateFactory.DATETIME_KEY, datetime)); + } - //TODO: add support for BetweenRoutePredicateFactory + public static Predicate between(ZonedDateTime datetime1, ZonedDateTime datetime2) { + return new BetweenRoutePredicateFactory().apply(tuple() + .of(BetweenRoutePredicateFactory.DATETIME1_KEY, datetime1, BetweenRoutePredicateFactory.DATETIME2_KEY, datetime2)); + } //TODO: add support for CookieRoutePredicateFactory diff --git a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/handler/predicate/AfterRoutePredicateFactoryTests.java b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/handler/predicate/AfterRoutePredicateFactoryTests.java index f1c43129..c5efcb8a 100644 --- a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/handler/predicate/AfterRoutePredicateFactoryTests.java +++ b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/handler/predicate/AfterRoutePredicateFactoryTests.java @@ -19,6 +19,8 @@ package org.springframework.cloud.gateway.handler.predicate; import org.junit.Test; +import java.time.ZonedDateTime; + import static org.assertj.core.api.Assertions.assertThat; import static org.springframework.cloud.gateway.handler.predicate.AfterRoutePredicateFactory.DATETIME_KEY; import static org.springframework.cloud.gateway.handler.predicate.BetweenRoutePredicateFactoryTests.getExchange; @@ -69,6 +71,12 @@ public class AfterRoutePredicateFactoryTests { assertThat(result).isFalse(); } + @Test + public void testPredicates() { + boolean result = RoutePredicates.after(ZonedDateTime.now().minusHours(2)).test(getExchange()); + assertThat(result).isTrue(); + } + private boolean runPredicate(String dateString) { return new AfterRoutePredicateFactory().apply(tuple().of(DATETIME_KEY, dateString)).test(getExchange()); } diff --git a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/handler/predicate/BeforeRoutePredicateFactoryTests.java b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/handler/predicate/BeforeRoutePredicateFactoryTests.java index 1fe8612a..1b752412 100644 --- a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/handler/predicate/BeforeRoutePredicateFactoryTests.java +++ b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/handler/predicate/BeforeRoutePredicateFactoryTests.java @@ -19,6 +19,8 @@ package org.springframework.cloud.gateway.handler.predicate; import org.junit.Test; +import java.time.ZonedDateTime; + import static org.assertj.core.api.Assertions.assertThat; import static org.springframework.cloud.gateway.handler.predicate.BeforeRoutePredicateFactory.DATETIME_KEY; import static org.springframework.cloud.gateway.handler.predicate.BetweenRoutePredicateFactoryTests.getExchange; @@ -69,6 +71,12 @@ public class BeforeRoutePredicateFactoryTests { assertThat(result).isTrue(); } + @Test + public void testPredicates() { + boolean result = RoutePredicates.before(ZonedDateTime.now().minusHours(2)).test(getExchange()); + assertThat(result).isFalse(); + } + private boolean runPredicate(String dateString) { return new BeforeRoutePredicateFactory().apply(tuple().of(DATETIME_KEY, dateString)).test(getExchange()); } diff --git a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/handler/predicate/BetweenRoutePredicateFactoryTests.java b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/handler/predicate/BetweenRoutePredicateFactoryTests.java index add92802..b85a4126 100644 --- a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/handler/predicate/BetweenRoutePredicateFactoryTests.java +++ b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/handler/predicate/BetweenRoutePredicateFactoryTests.java @@ -95,6 +95,14 @@ public class BetweenRoutePredicateFactoryTests { assertThat(result).as("Now is not after %s", dateString1).isFalse(); } + @Test + public void testPredicates() { + boolean result = RoutePredicates + .between(ZonedDateTime.now().minusHours(2), ZonedDateTime.now().plusHours(1)) + .test(getExchange()); + assertThat(result).isTrue(); + } + boolean runPredicate(String dateString1, String dateString2) { return new BetweenRoutePredicateFactory().apply(tuple() .of(DATETIME1_KEY, dateString1, DATETIME2_KEY, dateString2)).test(getExchange());