Minor refactor
This commit is contained in:
@@ -6,7 +6,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.gateway.actuate.GatewayEndpoint;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
||||
import org.springframework.cloud.gateway.filter.RouteToUrlFilter;
|
||||
import org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter;
|
||||
import org.springframework.cloud.gateway.handler.GatewayFilteringWebHandler;
|
||||
import org.springframework.cloud.gateway.handler.GatewayHostHandlerMapping;
|
||||
import org.springframework.cloud.gateway.handler.GatewayUrlHandlerMapping;
|
||||
@@ -32,8 +32,8 @@ public class GatewayAutoConfiguration {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RouteToUrlFilter findRouteFilter(GatewayProperties properties) {
|
||||
return new RouteToUrlFilter(properties);
|
||||
public RouteToRequestUrlFilter findRouteFilter(GatewayProperties properties) {
|
||||
return new RouteToRequestUrlFilter(properties);
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -32,7 +32,7 @@ import reactor.core.publisher.Mono;
|
||||
public interface GatewayFilter {
|
||||
|
||||
String GATEWAY_ROUTE_ATTR = "gatewayRoute";
|
||||
String GATEWAY_REQUEST_URL_ATTR = "requestUrl";
|
||||
String GATEWAY_REQUEST_URL_ATTR = "gatewayRequestUrl";
|
||||
String GATEWAY_HANDLER_MAPPER_ATTR = "gatewayHandlerMapper";
|
||||
|
||||
/**
|
||||
@@ -44,4 +44,14 @@ public interface GatewayFilter {
|
||||
*/
|
||||
Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain);
|
||||
|
||||
default <T> T getAttribute(ServerWebExchange exchange, String attributeName, Class<T> type) {
|
||||
if (exchange.getAttributes().containsKey(attributeName)) {
|
||||
Object attr = exchange.getAttributes().get(attributeName);
|
||||
if (type.isAssignableFrom(attr.getClass())) {
|
||||
return type.cast(attr);
|
||||
}
|
||||
throw new ClassCastException(attributeName + " is not of type " + type);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,14 +16,14 @@ import reactor.core.publisher.Mono;
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
public class RouteToUrlFilter implements GatewayFilter, Ordered {
|
||||
public class RouteToRequestUrlFilter implements GatewayFilter, Ordered {
|
||||
|
||||
private static final Log log = LogFactory.getLog(RouteToUrlFilter.class);
|
||||
private static final Log log = LogFactory.getLog(RouteToRequestUrlFilter.class);
|
||||
public static final int ROUTE_TO_URL_FILTER_ORDER = 500;
|
||||
|
||||
private final GatewayProperties properties;
|
||||
|
||||
public RouteToUrlFilter(GatewayProperties properties) {
|
||||
public RouteToRequestUrlFilter(GatewayProperties properties) {
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
@@ -34,17 +34,11 @@ public class RouteToUrlFilter implements GatewayFilter, Ordered {
|
||||
|
||||
@Override
|
||||
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
|
||||
if (!exchange.getAttributes().containsKey(GATEWAY_ROUTE_ATTR)) {
|
||||
Route route = getAttribute(exchange, GATEWAY_ROUTE_ATTR, Route.class);
|
||||
if (route == null) {
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
log.info("RouteToUrlFilter start");
|
||||
Object gatewayRoute = exchange.getAttributes().get(GATEWAY_ROUTE_ATTR);
|
||||
if (!(gatewayRoute instanceof Route)) {
|
||||
return Mono.error(new IllegalStateException(GATEWAY_ROUTE_ATTR +
|
||||
" not an instance of " + Route.class.getSimpleName() +
|
||||
", is " + gatewayRoute.getClass()));
|
||||
}
|
||||
Route route = (Route) gatewayRoute;
|
||||
log.info("RouteToRequestUrlFilter start");
|
||||
URI requestUrl = UriComponentsBuilder.fromHttpRequest(exchange.getRequest())
|
||||
.uri(route.getDownstreamUrl())
|
||||
.build(true)
|
||||
@@ -1,10 +1,10 @@
|
||||
package org.springframework.cloud.gateway;
|
||||
package org.springframework.cloud.gateway.test;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.SpringBootConfiguration;
|
||||
import org.springframework.boot.context.embedded.LocalServerPort;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
||||
@@ -32,6 +32,7 @@ import reactor.test.StepVerifier;
|
||||
public class GatewayIntegrationTests {
|
||||
|
||||
public static final String HANDLER_MAPPER_HEADER = "X-Gateway-Handler-Mapper-Class";
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@@ -76,7 +77,7 @@ public class GatewayIntegrationTests {
|
||||
.verify();
|
||||
}
|
||||
|
||||
@SpringBootApplication
|
||||
@SpringBootConfiguration
|
||||
public static class TestConfig {
|
||||
|
||||
private static final Log log = LogFactory.getLog(TestConfig.class);
|
||||
@@ -1,16 +1,14 @@
|
||||
package org.springframework.cloud.gateway;
|
||||
package org.springframework.cloud.gateway.test;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.SpringBootConfiguration;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
|
||||
@SpringBootConfiguration
|
||||
@EnableAutoConfiguration
|
||||
public class GatewayTestApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
new SpringApplicationBuilder()
|
||||
.sources(GatewayTestApplication.class)
|
||||
.run(args);
|
||||
SpringApplication.run(GatewayTestApplication.class, args);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user