Browse Source

Creates Cookie Predicate

pull/41/head
Spencer Gibb 8 years ago
parent
commit
ec21317878
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 22
      src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java
  2. 37
      src/main/java/org/springframework/cloud/gateway/handler/predicate/CookiePredicateFactory.java
  3. 1
      src/test/java/org/springframework/cloud/gateway/test/GatewayIntegrationTests.java
  4. 1
      src/test/resources/application.yml

22
src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java

@ -8,6 +8,7 @@ import org.springframework.cloud.gateway.actuate.GatewayEndpoint; @@ -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 { @@ -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 { @@ -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 {

37
src/main/java/org/springframework/cloud/gateway/handler/predicate/CookiePredicateFactory.java

@ -0,0 +1,37 @@ @@ -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<ServerWebExchange> 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<HttpCookie> cookies = exchange.getRequest().getCookies().get(name);
for (HttpCookie cookie : cookies) {
if (cookie.getValue().matches(regexp)) {
return true;
}
}
return false;
};
}
}

1
src/test/java/org/springframework/cloud/gateway/test/GatewayIntegrationTests.java

@ -89,6 +89,7 @@ public class GatewayIntegrationTests { @@ -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()
);

1
src/test/resources/application.yml

@ -19,6 +19,7 @@ spring: @@ -19,6 +19,7 @@ spring:
- Header=X-Request-Id, \d+
- Query=foo, ba.
- Query=baz
- Cookie=chocolate, ch.p
# =====================================
- id: default_path_to_httpbin

Loading…
Cancel
Save