From ec2131787820973726b047cec3219ce047bff049 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Thu, 1 Dec 2016 14:03:44 -0700 Subject: [PATCH] Creates Cookie Predicate --- .../config/GatewayAutoConfiguration.java | 22 +++++++---- .../predicate/CookiePredicateFactory.java | 37 +++++++++++++++++++ .../gateway/test/GatewayIntegrationTests.java | 1 + src/test/resources/application.yml | 1 + 4 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 src/main/java/org/springframework/cloud/gateway/handler/predicate/CookiePredicateFactory.java diff --git a/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java b/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java index 37e2bb9ce..58ec1ad4d 100644 --- a/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java +++ b/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java @@ -8,6 +8,7 @@ import org.springframework.cloud.gateway.actuate.GatewayEndpoint; import org.springframework.cloud.gateway.filter.GatewayFilter; import org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter; import org.springframework.cloud.gateway.handler.GatewayFilteringWebHandler; +import org.springframework.cloud.gateway.handler.predicate.CookiePredicateFactory; import org.springframework.cloud.gateway.handler.predicate.HeaderPredicateFactory; import org.springframework.cloud.gateway.handler.predicate.HostPredicateFactory; import org.springframework.cloud.gateway.handler.predicate.GatewayPredicateFactory; @@ -64,23 +65,23 @@ public class GatewayAutoConfiguration { } @Bean - public HostPredicateFactory hostPredicateFactory() { - return new HostPredicateFactory(); + public CookiePredicateFactory cookiePredicateFactory() { + return new CookiePredicateFactory(); } @Bean - public UrlPredicateFactory urlPredicateFactory() { - return new UrlPredicateFactory(); + public HeaderPredicateFactory headerPredicateFactory() { + return new HeaderPredicateFactory(); } @Bean - public MethodPredicateFactory methodPredicateFactory() { - return new MethodPredicateFactory(); + public HostPredicateFactory hostPredicateFactory() { + return new HostPredicateFactory(); } @Bean - public HeaderPredicateFactory headerPredicateFactory() { - return new HeaderPredicateFactory(); + public MethodPredicateFactory methodPredicateFactory() { + return new MethodPredicateFactory(); } @Bean @@ -88,6 +89,11 @@ public class GatewayAutoConfiguration { return new QueryPredicateFactory(); } + @Bean + public UrlPredicateFactory urlPredicateFactory() { + return new UrlPredicateFactory(); + } + @Configuration @ConditionalOnClass(Endpoint.class) protected static class GatewayActuatorConfiguration { diff --git a/src/main/java/org/springframework/cloud/gateway/handler/predicate/CookiePredicateFactory.java b/src/main/java/org/springframework/cloud/gateway/handler/predicate/CookiePredicateFactory.java new file mode 100644 index 000000000..689872623 --- /dev/null +++ b/src/main/java/org/springframework/cloud/gateway/handler/predicate/CookiePredicateFactory.java @@ -0,0 +1,37 @@ +package org.springframework.cloud.gateway.handler.predicate; + +import java.util.List; +import java.util.function.Predicate; + +import org.springframework.http.HttpCookie; +import org.springframework.util.Assert; +import org.springframework.web.server.ServerWebExchange; + +/** + * @author Spencer Gibb + */ +public class CookiePredicateFactory implements GatewayPredicateFactory { + + @Override + public String getName() { + return "Cookie"; + } + + @Override + public Predicate create(String name, String[] args) { + //TODO: caching can happen here + return exchange -> { + Assert.isTrue(args != null && args.length == 1, + "args must have one entry"); + + String regexp = args[0]; + List cookies = exchange.getRequest().getCookies().get(name); + for (HttpCookie cookie : cookies) { + if (cookie.getValue().matches(regexp)) { + return true; + } + } + return false; + }; + } +} diff --git a/src/test/java/org/springframework/cloud/gateway/test/GatewayIntegrationTests.java b/src/test/java/org/springframework/cloud/gateway/test/GatewayIntegrationTests.java index 47d9377e7..eb7c6c83e 100644 --- a/src/test/java/org/springframework/cloud/gateway/test/GatewayIntegrationTests.java +++ b/src/test/java/org/springframework/cloud/gateway/test/GatewayIntegrationTests.java @@ -89,6 +89,7 @@ public class GatewayIntegrationTests { GET("http://localhost:" + port + "/headers?foo=bar&baz") .header("Host", "www.foo.org") .header("X-Request-Id", "123") + .cookie("chocolate", "chip") .build() ); diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index 70dcd5959..c8f104ec7 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -19,6 +19,7 @@ spring: - Header=X-Request-Id, \d+ - Query=foo, ba. - Query=baz + - Cookie=chocolate, ch.p # ===================================== - id: default_path_to_httpbin