Add utility method to remove routed attribute

This commit is contained in:
Ryan Baxter
2019-12-18 10:47:20 -05:00
parent 0732b51d10
commit bb1f4a2e9e
4 changed files with 10 additions and 6 deletions

View File

@@ -50,10 +50,10 @@ import org.springframework.web.util.UriComponentsBuilder;
import static java.util.Collections.singletonList;
import static java.util.Optional.ofNullable;
import static org.springframework.cloud.gateway.support.GatewayToStringStyler.filterToStringCreator;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ALREADY_ROUTED_ATTR;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.HYSTRIX_EXECUTION_EXCEPTION_ATTR;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.containsEncodedParts;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.removeAlreadyRouted;
/**
* Depends on `spring-cloud-starter-netflix-hystrix`,
@@ -276,7 +276,7 @@ public class HystrixGatewayFilterFactory
// Before we continue on remove the already routed attribute since the
// fallback may go back through the route handler if the fallback
// is to another route in the Gateway
mutated.getAttributes().remove(GATEWAY_ALREADY_ROUTED_ATTR);
removeAlreadyRouted(mutated);
return RxReactiveStreams.toObservable(getDispatcherHandler().handle(mutated));
}

View File

@@ -49,7 +49,7 @@ import org.springframework.web.server.ServerWebExchange;
import static org.springframework.cloud.gateway.support.GatewayToStringStyler.filterToStringCreator;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.CLIENT_RESPONSE_HEADER_NAMES;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ALREADY_ROUTED_ATTR;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.removeAlreadyRouted;
public class RetryGatewayFilterFactory
extends AbstractGatewayFilterFactory<RetryGatewayFilterFactory.RetryConfig> {
@@ -207,7 +207,7 @@ public class RetryGatewayFilterFactory
CLIENT_RESPONSE_HEADER_NAMES, Collections.emptySet());
addedHeaders
.forEach(header -> exchange.getResponse().getHeaders().remove(header));
exchange.getAttributes().remove(GATEWAY_ALREADY_ROUTED_ATTR);
removeAlreadyRouted(exchange);
}
@Deprecated

View File

@@ -37,9 +37,9 @@ import static java.util.Collections.singletonList;
import static java.util.Optional.ofNullable;
import static org.springframework.cloud.gateway.support.GatewayToStringStyler.filterToStringCreator;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.CIRCUITBREAKER_EXECUTION_EXCEPTION_ATTR;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ALREADY_ROUTED_ATTR;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.containsEncodedParts;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.removeAlreadyRouted;
/**
* @author Ryan Baxter
@@ -103,7 +103,7 @@ public abstract class SpringCloudCircuitBreakerFilterFactory extends
// Before we continue on remove the already routed attribute since the
// fallback may go back through the route handler if the fallback
// is to another route in the Gateway
exchange.getAttributes().remove(GATEWAY_ALREADY_ROUTED_ATTR);
removeAlreadyRouted(exchange);
ServerHttpRequest request = exchange.getRequest().mutate()
.uri(requestUrl).build();

View File

@@ -172,6 +172,10 @@ public final class ServerWebExchangeUtils {
exchange.getAttributes().put(GATEWAY_ALREADY_ROUTED_ATTR, true);
}
public static void removeAlreadyRouted(ServerWebExchange exchange) {
exchange.getAttributes().remove(GATEWAY_ALREADY_ROUTED_ATTR);
}
public static boolean isAlreadyRouted(ServerWebExchange exchange) {
return exchange.getAttributeOrDefault(GATEWAY_ALREADY_ROUTED_ATTR, false);
}