|
|
|
|
@ -26,6 +26,7 @@ import java.util.List;
@@ -26,6 +26,7 @@ import java.util.List;
|
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.util.function.Consumer; |
|
|
|
|
import java.util.function.Function; |
|
|
|
|
import java.util.function.IntPredicate; |
|
|
|
|
import java.util.function.Predicate; |
|
|
|
|
import java.util.function.Supplier; |
|
|
|
|
|
|
|
|
|
@ -416,6 +417,8 @@ class DefaultWebClient implements WebClient {
@@ -416,6 +417,8 @@ class DefaultWebClient implements WebClient {
|
|
|
|
|
|
|
|
|
|
private static class DefaultResponseSpec implements ResponseSpec { |
|
|
|
|
|
|
|
|
|
private static final IntPredicate STATUS_CODE_ERROR = value -> value >= 400; |
|
|
|
|
|
|
|
|
|
private final Mono<ClientResponse> responseMono; |
|
|
|
|
|
|
|
|
|
private final Supplier<HttpRequest> requestSupplier; |
|
|
|
|
@ -425,17 +428,30 @@ class DefaultWebClient implements WebClient {
@@ -425,17 +428,30 @@ class DefaultWebClient implements WebClient {
|
|
|
|
|
DefaultResponseSpec(Mono<ClientResponse> responseMono, Supplier<HttpRequest> requestSupplier) { |
|
|
|
|
this.responseMono = responseMono; |
|
|
|
|
this.requestSupplier = requestSupplier; |
|
|
|
|
this.statusHandlers.add(new StatusHandler(HttpStatus::isError, ClientResponse::createException)); |
|
|
|
|
this.statusHandlers.add(new StatusHandler(STATUS_CODE_ERROR, ClientResponse::createException)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public ResponseSpec onStatus(Predicate<HttpStatus> statusPredicate, |
|
|
|
|
Function<ClientResponse, Mono<? extends Throwable>> exceptionFunction) { |
|
|
|
|
return onRawStatus(toIntPredicate(statusPredicate), exceptionFunction); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Assert.notNull(statusPredicate, "StatusPredicate must not be null"); |
|
|
|
|
private static IntPredicate toIntPredicate(Predicate<HttpStatus> predicate) { |
|
|
|
|
return value -> { |
|
|
|
|
HttpStatus status = HttpStatus.resolve(value); |
|
|
|
|
return (status != null) && predicate.test(status); |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public ResponseSpec onRawStatus(IntPredicate statusCodePredicate, |
|
|
|
|
Function<ClientResponse, Mono<? extends Throwable>> exceptionFunction) { |
|
|
|
|
|
|
|
|
|
Assert.notNull(statusCodePredicate, "StatusCodePredicate must not be null"); |
|
|
|
|
Assert.notNull(exceptionFunction, "Function must not be null"); |
|
|
|
|
|
|
|
|
|
this.statusHandlers.add(0, new StatusHandler(statusPredicate, exceptionFunction)); |
|
|
|
|
this.statusHandlers.add(0, new StatusHandler(statusCodePredicate, exceptionFunction)); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -452,17 +468,12 @@ class DefaultWebClient implements WebClient {
@@ -452,17 +468,12 @@ class DefaultWebClient implements WebClient {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private <T> Mono<T> handleBodyMono(ClientResponse response, Mono<T> bodyPublisher) { |
|
|
|
|
if (HttpStatus.resolve(response.rawStatusCode()) != null) { |
|
|
|
|
Mono<T> result = statusHandlers(response); |
|
|
|
|
if (result != null) { |
|
|
|
|
return result.switchIfEmpty(bodyPublisher); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
return bodyPublisher; |
|
|
|
|
} |
|
|
|
|
Mono<T> result = statusHandlers(response); |
|
|
|
|
if (result != null) { |
|
|
|
|
return result.switchIfEmpty(bodyPublisher); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
return response.createException().flatMap(Mono::error); |
|
|
|
|
return bodyPublisher; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -479,24 +490,20 @@ class DefaultWebClient implements WebClient {
@@ -479,24 +490,20 @@ class DefaultWebClient implements WebClient {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private <T> Publisher<T> handleBodyFlux(ClientResponse response, Flux<T> bodyPublisher) { |
|
|
|
|
if (HttpStatus.resolve(response.rawStatusCode()) != null) { |
|
|
|
|
Mono<T> result = statusHandlers(response); |
|
|
|
|
if (result != null) { |
|
|
|
|
return result.flux().switchIfEmpty(bodyPublisher); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
return bodyPublisher; |
|
|
|
|
} |
|
|
|
|
Mono<T> result = statusHandlers(response); |
|
|
|
|
if (result != null) { |
|
|
|
|
return result.flux().switchIfEmpty(bodyPublisher); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
return response.createException().flatMap(Mono::error); |
|
|
|
|
return bodyPublisher; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Nullable |
|
|
|
|
private <T> Mono<T> statusHandlers(ClientResponse response) { |
|
|
|
|
int statusCode = response.rawStatusCode(); |
|
|
|
|
for (StatusHandler handler : this.statusHandlers) { |
|
|
|
|
if (handler.test(response.statusCode())) { |
|
|
|
|
if (handler.test(statusCode)) { |
|
|
|
|
Mono<? extends Throwable> exMono; |
|
|
|
|
try { |
|
|
|
|
exMono = handler.apply(response); |
|
|
|
|
@ -508,7 +515,7 @@ class DefaultWebClient implements WebClient {
@@ -508,7 +515,7 @@ class DefaultWebClient implements WebClient {
|
|
|
|
|
} |
|
|
|
|
Mono<T> result = exMono.flatMap(Mono::error); |
|
|
|
|
HttpRequest request = this.requestSupplier.get(); |
|
|
|
|
return insertCheckpoint(result, response.statusCode(), request); |
|
|
|
|
return insertCheckpoint(result, statusCode, request); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return null; |
|
|
|
|
@ -522,10 +529,10 @@ class DefaultWebClient implements WebClient {
@@ -522,10 +529,10 @@ class DefaultWebClient implements WebClient {
|
|
|
|
|
.onErrorResume(ex2 -> Mono.empty()).thenReturn(ex); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private <T> Mono<T> insertCheckpoint(Mono<T> result, HttpStatus status, HttpRequest request) { |
|
|
|
|
private <T> Mono<T> insertCheckpoint(Mono<T> result, int statusCode, HttpRequest request) { |
|
|
|
|
String httpMethod = request.getMethodValue(); |
|
|
|
|
URI uri = request.getURI(); |
|
|
|
|
String description = status + " from " + httpMethod + " " + uri + " [DefaultWebClient]"; |
|
|
|
|
String description = statusCode + " from " + httpMethod + " " + uri + " [DefaultWebClient]"; |
|
|
|
|
return result.checkpoint(description); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -558,24 +565,25 @@ class DefaultWebClient implements WebClient {
@@ -558,24 +565,25 @@ class DefaultWebClient implements WebClient {
|
|
|
|
|
|
|
|
|
|
private static class StatusHandler { |
|
|
|
|
|
|
|
|
|
private final Predicate<HttpStatus> predicate; |
|
|
|
|
private final IntPredicate predicate; |
|
|
|
|
|
|
|
|
|
private final Function<ClientResponse, Mono<? extends Throwable>> exceptionFunction; |
|
|
|
|
|
|
|
|
|
public StatusHandler(Predicate<HttpStatus> predicate, |
|
|
|
|
public StatusHandler(IntPredicate predicate, |
|
|
|
|
Function<ClientResponse, Mono<? extends Throwable>> exceptionFunction) { |
|
|
|
|
|
|
|
|
|
this.predicate = predicate; |
|
|
|
|
this.exceptionFunction = exceptionFunction; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public boolean test(HttpStatus status) { |
|
|
|
|
public boolean test(int status) { |
|
|
|
|
return this.predicate.test(status); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public Mono<? extends Throwable> apply(ClientResponse response) { |
|
|
|
|
return this.exceptionFunction.apply(response); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|