From 0c30f7ee8317e9381a9c503cfcd03e02c96c8139 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Fri, 17 Mar 2017 16:30:14 -0600 Subject: [PATCH] polish --- .../cloud/gateway/config/GatewayProperties.java | 2 ++ .../predicate/CookieRequestPredicateFactory.java | 8 ++++---- .../predicate/QueryRequestPredicateFactory.java | 13 +++++++------ .../RemoteAddrRequestPredicateFactory.java | 10 +++++----- .../handler/support/ExchangeServerRequest.java | 1 + 5 files changed, 19 insertions(+), 15 deletions(-) diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/config/GatewayProperties.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/config/GatewayProperties.java index 2acec5331..9b86f2314 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/config/GatewayProperties.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/config/GatewayProperties.java @@ -27,6 +27,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.cloud.gateway.model.FilterDefinition; import org.springframework.cloud.gateway.model.RouteDefinition; import org.springframework.cloud.gateway.filter.factory.RemoveNonProxyHeadersWebFilterFactory; +import org.springframework.validation.annotation.Validated; import static org.springframework.cloud.gateway.support.NameUtils.normalizeFilterName; @@ -34,6 +35,7 @@ import static org.springframework.cloud.gateway.support.NameUtils.normalizeFilte * @author Spencer Gibb */ @ConfigurationProperties("spring.cloud.gateway") +@Validated public class GatewayProperties { /** diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/CookieRequestPredicateFactory.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/CookieRequestPredicateFactory.java index bd6d3e874..fddb5b7fc 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/CookieRequestPredicateFactory.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/CookieRequestPredicateFactory.java @@ -19,11 +19,12 @@ package org.springframework.cloud.gateway.handler.predicate; import java.util.Arrays; import java.util.List; +import java.util.Optional; -import org.springframework.cloud.gateway.handler.support.ExchangeServerRequest; import org.springframework.http.HttpCookie; import org.springframework.tuple.Tuple; import org.springframework.web.reactive.function.server.RequestPredicate; +import org.springframework.web.server.ServerWebExchange; /** * @author Spencer Gibb @@ -44,9 +45,8 @@ public class CookieRequestPredicateFactory implements RequestPredicateFactory { String regexp = args.getString(REGEXP_KEY); return request -> { - //TODO: bad cast? - ExchangeServerRequest req = (ExchangeServerRequest) request; - List cookies = req.exchange().getRequest().getCookies().get(name); + Optional exchange = request.attribute("exchange"); + List cookies = exchange.get().getRequest().getCookies().get(name); for (HttpCookie cookie : cookies) { if (cookie.getValue().matches(regexp)) { return true; diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/QueryRequestPredicateFactory.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/QueryRequestPredicateFactory.java index 392733a38..d06487bb5 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/QueryRequestPredicateFactory.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/QueryRequestPredicateFactory.java @@ -17,13 +17,14 @@ package org.springframework.cloud.gateway.handler.predicate; -import org.springframework.cloud.gateway.handler.support.ExchangeServerRequest; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; + import org.springframework.tuple.Tuple; import org.springframework.web.reactive.function.server.RequestPredicate; import org.springframework.web.reactive.function.server.RequestPredicates; - -import java.util.Arrays; -import java.util.List; +import org.springframework.web.server.ServerWebExchange; /** * @author Spencer Gibb @@ -51,8 +52,8 @@ public class QueryRequestPredicateFactory implements RequestPredicateFactory { if (!args.hasFieldName(REGEXP_KEY)) { return req -> { //TODO: ServerRequest support for query params with no value - ExchangeServerRequest request = (ExchangeServerRequest) req; - return request.exchange().getRequest().getQueryParams().containsKey(param); + Optional exchange = req.attribute("exchange"); + return exchange.get().getRequest().getQueryParams().containsKey(param); }; } diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/RemoteAddrRequestPredicateFactory.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/RemoteAddrRequestPredicateFactory.java index c49f21f0b..03e2c3c37 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/RemoteAddrRequestPredicateFactory.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/RemoteAddrRequestPredicateFactory.java @@ -26,8 +26,8 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.cloud.gateway.support.SubnetUtils; import org.springframework.tuple.Tuple; -import org.springframework.cloud.gateway.handler.support.ExchangeServerRequest; import org.springframework.web.reactive.function.server.RequestPredicate; +import org.springframework.web.server.ServerWebExchange; /** * @author Spencer Gibb @@ -47,12 +47,12 @@ public class RemoteAddrRequestPredicateFactory implements RequestPredicateFactor } } - return req -> { - ExchangeServerRequest serverRequest = (ExchangeServerRequest) req; - Optional remoteAddress = serverRequest.exchange().getRequest().getRemoteAddress(); + return request -> { + Optional exchange = request.attribute("exchange"); + Optional remoteAddress = exchange.get().getRequest().getRemoteAddress(); if (remoteAddress.isPresent()) { String hostAddress = remoteAddress.get().getAddress().getHostAddress(); - String host = req.uri().getHost(); + String host = request.uri().getHost(); if (!hostAddress.equals(host)) { log.warn("Remote addresses didn't match " + hostAddress + " != " + host); diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/support/ExchangeServerRequest.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/support/ExchangeServerRequest.java index 5dae62351..98e671eab 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/support/ExchangeServerRequest.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/support/ExchangeServerRequest.java @@ -78,6 +78,7 @@ public class ExchangeServerRequest implements ServerRequest { this.exchange = exchange; this.strategies = strategies; this.headers = new ExchangeServerRequest.DefaultHeaders(); + this.exchange.getAttributes().put("exchange", this.exchange); }