Propagate not found exception for write and delete of absent paths

if 404, return Mono.empty -> read/list and Mono.error -> write/delete

Closes gh-503.
This commit is contained in:
Raoof Mohammed
2019-11-26 12:09:01 -05:00
committed by Mark Paluch
parent 75d887af06
commit a73a00f23e

View File

@@ -24,6 +24,7 @@ import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import org.springframework.core.ParameterizedTypeReference; import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.client.reactive.ClientHttpConnector; import org.springframework.http.client.reactive.ClientHttpConnector;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
@@ -55,6 +56,7 @@ import static org.springframework.web.reactive.function.client.ExchangeFilterFun
* into Vault on initialization and use the token throughout the whole lifetime. * into Vault on initialization and use the token throughout the whole lifetime.
* *
* @author Mark Paluch * @author Mark Paluch
* @author Raoof Mohammed
* @see SessionManager * @see SessionManager
* @since 2.0 * @since 2.0
*/ */
@@ -194,7 +196,8 @@ public class ReactiveVaultTemplate implements ReactiveVaultOperations {
ParameterizedTypeReference<VaultResponseSupport<T>> ref = VaultResponses ParameterizedTypeReference<VaultResponseSupport<T>> ref = VaultResponses
.getTypeReference(responseType); .getTypeReference(responseType);
return webClient.get().uri(path).exchange().flatMap(mapResponse(ref, path)); return webClient.get().uri(path).exchange()
.flatMap(mapResponse(ref, path, HttpMethod.GET));
}); });
} }
@@ -230,7 +233,8 @@ public class ReactiveVaultTemplate implements ReactiveVaultOperations {
else { else {
exchange = uri.exchange(); exchange = uri.exchange();
} }
return exchange.flatMap(mapResponse(VaultResponse.class, path)); return exchange
.flatMap(mapResponse(VaultResponse.class, path, HttpMethod.POST));
}); });
} }
@@ -240,7 +244,7 @@ public class ReactiveVaultTemplate implements ReactiveVaultOperations {
Assert.hasText(path, "Path must not be empty"); Assert.hasText(path, "Path must not be empty");
return doWithSession(webClient -> webClient.delete().uri(path).exchange() return doWithSession(webClient -> webClient.delete().uri(path).exchange()
.flatMap(mapResponse(String.class, path)).then()); .flatMap(mapResponse(String.class, path, HttpMethod.DELETE)).then());
} }
@Override @Override
@@ -276,21 +280,22 @@ public class ReactiveVaultTemplate implements ReactiveVaultOperations {
private <T> Mono<T> doRead(String path, Class<T> responseType) { private <T> Mono<T> doRead(String path, Class<T> responseType) {
return doWithSession(client -> client.get() // return doWithSession(client -> client.get() //
.uri(path).exchange().flatMap(mapResponse(responseType, path))); .uri(path).exchange()
.flatMap(mapResponse(responseType, path, HttpMethod.GET)));
} }
private static <T> Function<ClientResponse, Mono<? extends T>> mapResponse( private static <T> Function<ClientResponse, Mono<? extends T>> mapResponse(
Class<T> bodyType, String path) { Class<T> bodyType, String path, HttpMethod method) {
return response -> isSuccess(response) ? response.bodyToMono(bodyType) return response -> isSuccess(response) ? response.bodyToMono(bodyType)
: mapOtherwise(response, path); : mapOtherwise(response, path, method);
} }
private static <T> Function<ClientResponse, Mono<? extends T>> mapResponse( private static <T> Function<ClientResponse, Mono<? extends T>> mapResponse(
ParameterizedTypeReference<T> typeReference, String path) { ParameterizedTypeReference<T> typeReference, String path, HttpMethod method) {
return response -> isSuccess(response) return response -> isSuccess(response)
? response.body(BodyExtractors.toMono(typeReference)) ? response.body(BodyExtractors.toMono(typeReference))
: mapOtherwise(response, path); : mapOtherwise(response, path, method);
} }
private static boolean isSuccess(ClientResponse response) { private static boolean isSuccess(ClientResponse response) {
@@ -298,9 +303,9 @@ public class ReactiveVaultTemplate implements ReactiveVaultOperations {
} }
private static <T> Mono<? extends T> mapOtherwise(ClientResponse response, private static <T> Mono<? extends T> mapOtherwise(ClientResponse response,
String path) { String path, HttpMethod method) {
if (response.statusCode() == HttpStatus.NOT_FOUND) { if (response.statusCode() == HttpStatus.NOT_FOUND && method == HttpMethod.GET) {
return Mono.empty(); return Mono.empty();
} }