Skip to content

Commit

Permalink
Migrate off deprecated WebClient.exchange(…)
Browse files Browse the repository at this point in the history
We now use exchangeToMono(…) for a safe release of the body.

Closes gh-612
  • Loading branch information
mp911de committed Jan 15, 2021
1 parent 873e337 commit 1e453eb
Showing 1 changed file with 13 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public <T> Mono<VaultResponseSupport<T>> read(String path, Class<T> responseType

ParameterizedTypeReference<VaultResponseSupport<T>> ref = VaultResponses.getTypeReference(responseType);

return webClient.get().uri(path).exchange().flatMap(mapResponse(ref, path, HttpMethod.GET));
return webClient.get().uri(path).exchangeToMono(mapResponse(ref, path, HttpMethod.GET));
});
}

Expand All @@ -265,14 +265,12 @@ public Mono<VaultResponse> write(String path, @Nullable Object body) {
return doWithSession(webClient -> {

RequestBodySpec uri = webClient.post().uri(path);
Mono<ClientResponse> exchange;

if (body != null) {
exchange = uri.bodyValue(body).exchange();
}
else {
exchange = uri.exchange();
return uri.bodyValue(body).exchangeToMono(mapResponse(VaultResponse.class, path, HttpMethod.POST));
}
return exchange.flatMap(mapResponse(VaultResponse.class, path, HttpMethod.POST));

return uri.exchangeToMono(mapResponse(VaultResponse.class, path, HttpMethod.POST));
});
}

Expand All @@ -281,8 +279,8 @@ public Mono<Void> delete(String path) {

Assert.hasText(path, "Path must not be empty");

return doWithSession(webClient -> webClient.delete().uri(path).exchange()
.flatMap(mapResponse(String.class, path, HttpMethod.DELETE)).then());
return doWithSession(webClient -> webClient.delete().uri(path)
.exchangeToMono(mapResponse(String.class, path, HttpMethod.DELETE))).then();
}

@Override
Expand Down Expand Up @@ -316,16 +314,16 @@ public <V, T extends Publisher<V>> T doWithSession(Function<WebClient, ? extends
private <T> Mono<T> doRead(String path, Class<T> responseType) {

return doWithSession(client -> client.get() //
.uri(path).exchange().flatMap(mapResponse(responseType, path, HttpMethod.GET)));
.uri(path).exchangeToMono(mapResponse(responseType, path, HttpMethod.GET)));
}

private static <T> Function<ClientResponse, Mono<? extends T>> mapResponse(Class<T> bodyType, String path,
private static <T> Function<ClientResponse, Mono<T>> mapResponse(Class<T> bodyType, String path,
HttpMethod method) {
return response -> isSuccess(response) ? response.bodyToMono(bodyType) : mapOtherwise(response, path, method);
}

private static <T> Function<ClientResponse, Mono<? extends T>> mapResponse(
ParameterizedTypeReference<T> typeReference, String path, HttpMethod method) {
private static <T> Function<ClientResponse, Mono<T>> mapResponse(ParameterizedTypeReference<T> typeReference,
String path, HttpMethod method) {

return response -> isSuccess(response) ? response.body(BodyExtractors.toMono(typeReference))
: mapOtherwise(response, path, method);
Expand All @@ -335,10 +333,10 @@ private static boolean isSuccess(ClientResponse response) {
return response.statusCode().is2xxSuccessful();
}

private static <T> Mono<? extends T> mapOtherwise(ClientResponse response, String path, HttpMethod method) {
private static <T> Mono<T> mapOtherwise(ClientResponse response, String path, HttpMethod method) {

if (response.statusCode() == HttpStatus.NOT_FOUND && method == HttpMethod.GET) {
return Mono.empty();
return response.releaseBody().then(Mono.empty());
}

return response.bodyToMono(String.class).flatMap(body -> {
Expand Down

0 comments on commit 1e453eb

Please sign in to comment.