Consistently use doWithSession(…) from within VaultTemplate.

Closes gh-481.
This commit is contained in:
Mark Paluch
2019-09-30 13:42:03 +02:00
parent 9204ab9e47
commit 96e7c2a57d
2 changed files with 48 additions and 39 deletions

View File

@@ -189,10 +189,13 @@ public class ReactiveVaultTemplate implements ReactiveVaultOperations {
@Override
public <T> Mono<VaultResponseSupport<T>> read(String path, Class<T> responseType) {
ParameterizedTypeReference<VaultResponseSupport<T>> ref = VaultResponses
.getTypeReference(responseType);
return doWithSession(webClient -> {
return sessionClient.get().uri(path).exchange().flatMap(mapResponse(ref, path));
ParameterizedTypeReference<VaultResponseSupport<T>> ref = VaultResponses
.getTypeReference(responseType);
return webClient.get().uri(path).exchange().flatMap(mapResponse(ref, path));
});
}
@Override
@@ -208,7 +211,6 @@ public class ReactiveVaultTemplate implements ReactiveVaultOperations {
return read
.filter(response -> response.getData() != null
&& response.getData().containsKey("keys"))
//
.flatMapIterable(response -> (List<String>) response.getRequiredData()
.get("keys"));
}
@@ -218,15 +220,18 @@ public class ReactiveVaultTemplate implements ReactiveVaultOperations {
Assert.hasText(path, "Path must not be empty");
RequestBodySpec uri = sessionClient.post().uri(path);
Mono<ClientResponse> exchange;
if (body != null) {
exchange = uri.syncBody(body).exchange();
}
else {
exchange = uri.exchange();
}
return exchange.flatMap(mapResponse(VaultResponse.class, path));
return doWithSession(webClient -> {
RequestBodySpec uri = webClient.post().uri(path);
Mono<ClientResponse> exchange;
if (body != null) {
exchange = uri.syncBody(body).exchange();
}
else {
exchange = uri.exchange();
}
return exchange.flatMap(mapResponse(VaultResponse.class, path));
});
}
@Override
@@ -234,8 +239,8 @@ public class ReactiveVaultTemplate implements ReactiveVaultOperations {
Assert.hasText(path, "Path must not be empty");
return sessionClient.delete().uri(path).exchange()
.flatMap(mapResponse(String.class, path)).then();
return doWithSession(webClient -> webClient.delete().uri(path).exchange()
.flatMap(mapResponse(String.class, path)).then());
}
@Override

View File

@@ -297,20 +297,23 @@ public class VaultTemplate implements InitializingBean, VaultOperations, Disposa
ParameterizedTypeReference<VaultResponseSupport<T>> ref = VaultResponses
.getTypeReference(responseType);
try {
ResponseEntity<VaultResponseSupport<T>> exchange = sessionTemplate
.exchange(path, HttpMethod.GET, null, ref);
return doWithSession(restOperations -> {
return exchange.getBody();
}
catch (HttpStatusCodeException e) {
try {
ResponseEntity<VaultResponseSupport<T>> exchange = restOperations
.exchange(path, HttpMethod.GET, null, ref);
if (e.getStatusCode() == HttpStatus.NOT_FOUND) {
return null;
return exchange.getBody();
}
catch (HttpStatusCodeException e) {
throw VaultResponses.buildException(e, path);
}
if (e.getStatusCode() == HttpStatus.NOT_FOUND) {
return null;
}
throw VaultResponses.buildException(e, path);
}
});
}
@Override
@@ -336,12 +339,8 @@ public class VaultTemplate implements InitializingBean, VaultOperations, Disposa
Assert.hasText(path, "Path must not be empty");
try {
return sessionTemplate.postForObject(path, body, VaultResponse.class);
}
catch (HttpStatusCodeException e) {
throw VaultResponses.buildException(e, path);
}
return doWithSession(restOperations -> restOperations.postForObject(path, body,
VaultResponse.class));
}
@Override
@@ -349,17 +348,22 @@ public class VaultTemplate implements InitializingBean, VaultOperations, Disposa
Assert.hasText(path, "Path must not be empty");
try {
sessionTemplate.delete(path);
}
catch (HttpStatusCodeException e) {
doWithSession(restOperations -> {
if (e.getStatusCode() == HttpStatus.NOT_FOUND) {
return;
try {
restOperations.delete(path);
}
catch (HttpStatusCodeException e) {
if (e.getStatusCode() == HttpStatus.NOT_FOUND) {
return null;
}
throw VaultResponses.buildException(e, path);
}
throw VaultResponses.buildException(e, path);
}
return null;
});
}
@Override