Adds support for after, before and between predicates.

This commit is contained in:
Spencer Gibb
2017-10-02 22:39:50 -04:00
parent bd5182ab33
commit 7cfd46d45b
7 changed files with 54 additions and 11 deletions

View File

@@ -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<ServerWebExchange> 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);
};
}
}

View File

@@ -41,7 +41,8 @@ public class BeforeRoutePredicateFactory implements RoutePredicateFactory {
@Override
public Predicate<ServerWebExchange> 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();

View File

@@ -37,10 +37,10 @@ public class BetweenRoutePredicateFactory implements RoutePredicateFactory {
@Override
public Predicate<ServerWebExchange> 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 {

View File

@@ -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<ServerWebExchange> after(ZonedDateTime datetime) {
return new AfterRoutePredicateFactory().apply(tuple().of(AfterRoutePredicateFactory.DATETIME_KEY, datetime));
}
//TODO: add support for BeforeRoutePredicateFactory
public static Predicate<ServerWebExchange> before(ZonedDateTime datetime) {
return new BeforeRoutePredicateFactory().apply(tuple().of(BeforeRoutePredicateFactory.DATETIME_KEY, datetime));
}
//TODO: add support for BetweenRoutePredicateFactory
public static Predicate<ServerWebExchange> between(ZonedDateTime datetime1, ZonedDateTime datetime2) {
return new BetweenRoutePredicateFactory().apply(tuple()
.of(BetweenRoutePredicateFactory.DATETIME1_KEY, datetime1, BetweenRoutePredicateFactory.DATETIME2_KEY, datetime2));
}
//TODO: add support for CookieRoutePredicateFactory

View File

@@ -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());
}

View File

@@ -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());
}

View File

@@ -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());