Updates to use new HttpStatusCode references/methods

This commit is contained in:
spencergibb
2022-05-04 14:17:41 -04:00
parent aaa52670ca
commit b7e8a8f0ef
10 changed files with 33 additions and 21 deletions

View File

@@ -44,6 +44,7 @@ import org.springframework.cloud.gateway.support.TimeoutException;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatus.Series;
import org.springframework.http.HttpStatusCode;
import org.springframework.util.Assert;
import org.springframework.web.server.ServerWebExchange;
@@ -84,7 +85,7 @@ public class RetryGatewayFilterFactory extends AbstractGatewayFilterFactory<Retr
return false;
}
HttpStatus statusCode = exchange.getResponse().getStatusCode();
HttpStatusCode statusCode = exchange.getResponse().getStatusCode();
boolean retryableStatusCode = retryConfig.getStatuses().contains(statusCode);
@@ -93,9 +94,12 @@ public class RetryGatewayFilterFactory extends AbstractGatewayFilterFactory<Retr
// try the series
retryableStatusCode = false;
for (int i = 0; i < retryConfig.getSeries().size(); i++) {
if (statusCode.series().equals(retryConfig.getSeries().get(i))) {
retryableStatusCode = true;
break;
if (statusCode instanceof HttpStatus) {
HttpStatus httpStatus = (HttpStatus) statusCode;
if (httpStatus.series().equals(retryConfig.getSeries().get(i))) {
retryableStatusCode = true;
break;
}
}
}
}

View File

@@ -25,7 +25,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.support.HttpStatusHolder;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.web.server.ServerWebExchange;
import static java.util.Collections.singletonList;
@@ -75,7 +75,7 @@ public class SetStatusGatewayFilterFactory extends AbstractGatewayFilterFactory<
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
// check not really needed, since it is guarded in setStatusCode,
// but it's a good example
HttpStatus statusCode = exchange.getResponse().getStatusCode();
HttpStatusCode statusCode = exchange.getResponse().getStatusCode();
boolean isStatusCodeUpdated = setResponseStatus(exchange, statusHolder);
if (isStatusCodeUpdated && originalStatusHeaderName != null) {
exchange.getResponse().getHeaders().set(originalStatusHeaderName,

View File

@@ -32,6 +32,7 @@ import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.support.HasRouteId;
import org.springframework.cloud.gateway.support.HttpStatusHolder;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.client.HttpStatusCodeException;
@@ -97,7 +98,7 @@ public abstract class SpringCloudCircuitBreakerFilterFactory
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
return cb.run(chain.filter(exchange).doOnSuccess(v -> {
if (statuses.contains(exchange.getResponse().getStatusCode())) {
HttpStatus status = exchange.getResponse().getStatusCode();
HttpStatusCode status = exchange.getResponse().getStatusCode();
throw new CircuitBreakerStatusCodeException(status);
}
}), t -> {
@@ -221,7 +222,7 @@ public abstract class SpringCloudCircuitBreakerFilterFactory
public class CircuitBreakerStatusCodeException extends HttpStatusCodeException {
public CircuitBreakerStatusCodeException(HttpStatus statusCode) {
public CircuitBreakerStatusCodeException(HttpStatusCode statusCode) {
super(statusCode);
}

View File

@@ -19,6 +19,7 @@ package org.springframework.cloud.gateway.support.tagsprovider;
import io.micrometer.core.instrument.Tags;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.server.reactive.AbstractServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
@@ -52,11 +53,14 @@ public class GatewayHttpTagsProvider implements GatewayTagsProvider {
}
}
else {
HttpStatus statusCode = exchange.getResponse().getStatusCode();
HttpStatusCode statusCode = exchange.getResponse().getStatusCode();
if (statusCode != null) {
httpStatusCodeStr = String.valueOf(statusCode.value());
outcome = statusCode.series().name();
status = statusCode.name();
if (statusCode instanceof HttpStatus) {
HttpStatus httpStatus = (HttpStatus) statusCode;
outcome = httpStatus.series().name();
status = httpStatus.name();
}
}
}

View File

@@ -46,6 +46,7 @@ import org.springframework.cloud.gateway.test.PermitAllSecurityConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
@@ -136,7 +137,7 @@ public class GatewayControllerEndpointTests {
testClient.delete().uri("http://localhost:" + port + "/actuator/gateway/routes/test-route-to-be-delete")
.exchange().expectStatus().isOk().expectBody(ResponseEntity.class).consumeWith(result -> {
HttpStatus httpStatus = result.getStatus();
HttpStatusCode httpStatus = result.getStatus();
Assert.assertEquals(HttpStatus.OK, httpStatus);
});
}

View File

@@ -31,6 +31,7 @@ import org.springframework.context.annotation.Import;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
@@ -62,8 +63,8 @@ public class SimpleUrlHandlerCorsTests extends BaseWebClientTests {
@Test
public void testCorsRequestNotHandledByGW() {
ResponseEntity<String> responseEntity = webClient.get().uri("/abc/123/function").header("Origin", "domain.com")
.header(HttpHeaders.HOST, "www.path.org").retrieve().onStatus(HttpStatus::isError, t -> Mono.empty())
.toEntity(String.class).block();
.header(HttpHeaders.HOST, "www.path.org").retrieve()
.onStatus(HttpStatusCode::isError, t -> Mono.empty()).toEntity(String.class).block();
HttpHeaders asHttpHeaders = responseEntity.getHeaders();
assertThat(responseEntity.getBody()).isNotNull();
assertThat(asHttpHeaders.getAccessControlAllowOrigin())

View File

@@ -274,7 +274,7 @@ class ReactiveLoadBalancerClientFilterTests {
filter.filter(exchange, chain).block();
}
catch (NotFoundException exception) {
assertThat(exception.getStatus()).isEqualTo(HttpStatus.NOT_FOUND);
assertThat(exception.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
}
}

View File

@@ -26,6 +26,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.web.server.ServerWebExchange;
@@ -54,7 +55,7 @@ public class HttpStatusInResponseHeadersFilterTests extends BaseWebClientTests {
return new HttpHeadersFilter() {
@Override
public HttpHeaders filter(HttpHeaders input, ServerWebExchange exchange) {
HttpStatus statusCode = exchange.getResponse().getStatusCode();
HttpStatusCode statusCode = exchange.getResponse().getStatusCode();
assertThat(statusCode).isEqualTo(HttpStatus.OK);
return input;
}

View File

@@ -18,7 +18,7 @@ package org.springframework.cloud.gateway.test;
import java.util.Map;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.web.reactive.function.client.ClientResponse;
import static org.assertj.core.api.Assertions.assertThat;
@@ -34,8 +34,8 @@ public class TestUtils {
return (Map<String, Object>) response.get(key);
}
public static void assertStatus(ClientResponse response, HttpStatus status) {
HttpStatus statusCode = response.statusCode();
public static void assertStatus(ClientResponse response, HttpStatusCode status) {
HttpStatusCode statusCode = response.statusCode();
assertThat(statusCode).isEqualTo(status);
}

View File

@@ -33,7 +33,7 @@ import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.RequestEntity;
import org.springframework.http.RequestEntity.BodyBuilder;
import org.springframework.http.ResponseEntity;
@@ -378,7 +378,7 @@ public class ProxyExchange<T> {
result = builder.headers(headers -> addHeaders(headers, exchange.getRequest().getHeaders())).retrieve();
}
}
return result.onStatus(HttpStatus::isError, t -> Mono.empty())
return result.onStatus(HttpStatusCode::isError, t -> Mono.empty())
.toEntity(ParameterizedTypeReference.forType(type));
}