Move values to constants
This commit is contained in:
@@ -31,6 +31,9 @@ import reactor.core.publisher.Mono;
|
||||
*/
|
||||
public interface GatewayFilter {
|
||||
|
||||
String GATEWAY_ROUTE_ATTR = "gatewayRoute";
|
||||
String GATEWAY_REQUEST_URL_ATTR = "requestUrl";
|
||||
|
||||
default boolean shouldFilter(ServerWebExchange exchange) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ import org.springframework.web.server.WebHandler;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import static org.springframework.cloud.gateway.GatewayFilter.GATEWAY_ROUTE_ATTR;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@@ -123,7 +125,7 @@ public class GatewayHostHandlerMapping extends AbstractHandlerMapping {
|
||||
|
||||
if (handler instanceof RouteHolder) {
|
||||
RouteHolder holder = (RouteHolder) handler;
|
||||
exchange.getAttributes().put("gatewayRoute", holder.route);
|
||||
exchange.getAttributes().put(GATEWAY_ROUTE_ATTR, holder.route);
|
||||
return holder.webHandler;
|
||||
}
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ public class GatewayUrlHandlerMapping extends AbstractUrlHandlerMapping {
|
||||
return super.getHandler(exchange).map(o -> {
|
||||
if (o instanceof RouteHolder) {
|
||||
RouteHolder holder = (RouteHolder) o;
|
||||
exchange.getAttributes().put("gatewayRoute", holder.route);
|
||||
exchange.getAttributes().put(GatewayFilter.GATEWAY_ROUTE_ATTR, holder.route);
|
||||
return holder.webHandler;
|
||||
}
|
||||
return o;
|
||||
|
||||
@@ -13,6 +13,8 @@ import reactor.core.publisher.Mono;
|
||||
import java.net.URI;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.springframework.cloud.gateway.GatewayFilter.GATEWAY_REQUEST_URL_ATTR;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@@ -28,8 +30,7 @@ public class GatewayWebHandler implements WebHandler {
|
||||
|
||||
@Override
|
||||
public Mono<Void> handle(ServerWebExchange exchange) {
|
||||
//TODO: move to constant
|
||||
Optional<URI> requestUrl = exchange.getAttribute("requestUrl");
|
||||
Optional<URI> requestUrl = exchange.getAttribute(GATEWAY_REQUEST_URL_ATTR);
|
||||
ServerHttpRequest request = exchange.getRequest();
|
||||
ClientRequest<Void> clientRequest = ClientRequest
|
||||
.method(request.getMethod(), requestUrl.get())
|
||||
|
||||
@@ -20,6 +20,7 @@ import reactor.core.publisher.Mono;
|
||||
public class RouteToUrlFilter implements GatewayFilter, Ordered {
|
||||
|
||||
private static final Log log = LogFactory.getLog(RouteToUrlFilter.class);
|
||||
public static final int ROUTE_TO_URL_FILTER_ORDER = 500;
|
||||
|
||||
private final GatewayProperties properties;
|
||||
|
||||
@@ -29,23 +30,21 @@ public class RouteToUrlFilter implements GatewayFilter, Ordered {
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
// TODO: move to constant
|
||||
return 500;
|
||||
return ROUTE_TO_URL_FILTER_ORDER;
|
||||
}
|
||||
|
||||
// TODO: do we really need shouldFilter or just move the if into filter?
|
||||
@Override
|
||||
public boolean shouldFilter(ServerWebExchange exchange) {
|
||||
//TODO: move to constant
|
||||
return exchange.getAttributes().containsKey("gatewayRoute");
|
||||
return exchange.getAttributes().containsKey(GATEWAY_ROUTE_ATTR);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
|
||||
log.info("RouteToUrlFilter start");
|
||||
Object gatewayRoute = exchange.getAttributes().get("gatewayRoute");
|
||||
Object gatewayRoute = exchange.getAttributes().get(GATEWAY_ROUTE_ATTR);
|
||||
if (!(gatewayRoute instanceof Route)) {
|
||||
return Mono.error(new IllegalStateException("gatewayRoute" +
|
||||
return Mono.error(new IllegalStateException(GATEWAY_ROUTE_ATTR +
|
||||
" not an instance of " + Route.class.getSimpleName() +
|
||||
", is " + gatewayRoute.getClass()));
|
||||
}
|
||||
@@ -54,8 +53,7 @@ public class RouteToUrlFilter implements GatewayFilter, Ordered {
|
||||
.uri(route.getDownstreamUrl())
|
||||
.build(true)
|
||||
.toUri();
|
||||
//TODO: move to constant
|
||||
exchange.getAttributes().put("requestUrl", requestUrl);
|
||||
exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, requestUrl);
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user