From 72746754833d78eecc4b6c7f88dc2858537d6b3c Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Thu, 24 Sep 2020 10:50:24 +0200 Subject: [PATCH] Polishing Reorder methods according to their call sequence. See gh-586. --- .../AuthenticationStepsExecutor.java | 47 ++++---- .../AuthenticationStepsOperator.java | 112 +++++++++--------- 2 files changed, 79 insertions(+), 80 deletions(-) diff --git a/spring-vault-core/src/main/java/org/springframework/vault/authentication/AuthenticationStepsExecutor.java b/spring-vault-core/src/main/java/org/springframework/vault/authentication/AuthenticationStepsExecutor.java index 06ea54e6..3e6c0ca1 100644 --- a/spring-vault-core/src/main/java/org/springframework/vault/authentication/AuthenticationStepsExecutor.java +++ b/spring-vault-core/src/main/java/org/springframework/vault/authentication/AuthenticationStepsExecutor.java @@ -92,6 +92,7 @@ public class AuthenticationStepsExecutor implements ClientAuthentication { String.format("Cannot retrieve VaultToken from authentication chain. Got instead %s", state)); } + @SuppressWarnings({ "unchecked", "ConstantConditions" }) private Object evaluate(Iterable> steps) { Object state = null; @@ -144,28 +145,6 @@ public class AuthenticationStepsExecutor implements ClientAuthentication { return state; } - private static Object doScalarValueStep(ScalarValueStep scalarValueStep) { - return scalarValueStep.get(); - } - - private static Object doSupplierStep(SupplierStep supplierStep) { - return supplierStep.get(); - } - - private static Object doMapStep(MapStep o, Object state) { - return o.apply(state); - } - - private Object doZipStep(ZipStep o, Object state) { - - Object result = evaluate(o.getRight()); - return Pair.of(state, result); - } - - private static Object doOnNext(OnNextStep o, Object state) { - return o.apply(state); - } - @SuppressWarnings("ConstantConditions") @Nullable private Object doHttpRequest(HttpRequestNode step, @Nullable Object state) { @@ -187,7 +166,7 @@ public class AuthenticationStepsExecutor implements ClientAuthentication { } - private static HttpEntity getEntity(HttpEntity entity, @Nullable Object state) { + private static HttpEntity getEntity(@Nullable HttpEntity entity, @Nullable Object state) { if (entity == null) { return state == null ? HttpEntity.EMPTY : new HttpEntity<>(state); @@ -200,4 +179,26 @@ public class AuthenticationStepsExecutor implements ClientAuthentication { return entity; } + private static Object doMapStep(MapStep o, Object state) { + return o.apply(state); + } + + private Object doZipStep(ZipStep o, Object state) { + + Object result = evaluate(o.getRight()); + return Pair.of(state, result); + } + + private static Object doOnNext(OnNextStep o, Object state) { + return o.apply(state); + } + + private static Object doScalarValueStep(ScalarValueStep scalarValueStep) { + return scalarValueStep.get(); + } + + private static Object doSupplierStep(SupplierStep supplierStep) { + return supplierStep.get(); + } + } diff --git a/spring-vault-core/src/main/java/org/springframework/vault/authentication/AuthenticationStepsOperator.java b/spring-vault-core/src/main/java/org/springframework/vault/authentication/AuthenticationStepsOperator.java index 64f2737c..8416e326 100644 --- a/spring-vault-core/src/main/java/org/springframework/vault/authentication/AuthenticationStepsOperator.java +++ b/spring-vault-core/src/main/java/org/springframework/vault/authentication/AuthenticationStepsOperator.java @@ -29,6 +29,7 @@ import org.springframework.core.io.buffer.DataBufferFactory; import org.springframework.core.io.buffer.DataBufferUtils; import org.springframework.core.io.buffer.DefaultDataBufferFactory; import org.springframework.http.HttpEntity; +import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.vault.VaultException; import org.springframework.vault.authentication.AuthenticationSteps.HttpRequest; @@ -117,7 +118,7 @@ public class AuthenticationStepsOperator implements VaultTokenSupplier { @SuppressWarnings("unchecked") private Mono createMono(Iterable> steps) { - Mono state = Mono.just(Undefinded.INSTANCE); + Mono state = Mono.just(Undefinded.UNDEFINDED); for (Node o : steps) { @@ -157,6 +158,57 @@ public class AuthenticationStepsOperator implements VaultTokenSupplier { return state; } + private Mono doHttpRequest(HttpRequestNode step, Object state) { + + HttpRequest definition = step.getDefinition(); + HttpEntity entity = getEntity(definition.getEntity(), state); + + RequestBodySpec spec; + if (definition.getUri() == null) { + + spec = this.webClient.method(definition.getMethod()).uri(definition.getUriTemplate(), + definition.getUrlVariables()); + } + else { + spec = this.webClient.method(definition.getMethod()).uri(definition.getUri()); + } + + for (Entry> header : entity.getHeaders().entrySet()) { + spec = spec.header(header.getKey(), header.getValue().get(0)); + } + + if (entity.getBody() != null && !entity.getBody().equals(Undefinded.UNDEFINDED)) { + return spec.bodyValue(entity.getBody()).retrieve().bodyToMono(definition.getResponseType()); + } + + return spec.retrieve().bodyToMono(definition.getResponseType()); + } + + private static HttpEntity getEntity(@Nullable HttpEntity entity, @Nullable Object state) { + + if (entity == null) { + return state == null ? HttpEntity.EMPTY : new HttpEntity<>(state); + } + + if (entity.getBody() == null && state != null) { + return new HttpEntity<>(state, entity.getHeaders()); + } + + return entity; + } + + private static Object doMapStep(MapStep o, Object state) { + return o.apply(state); + } + + private Mono doZipStep(ZipStep o) { + return createMono(o.getRight()); + } + + private static void doOnNext(OnNextStep o, Object state) { + o.apply(state); + } + private static Object doScalarValueStep(ScalarValueStep scalarValueStep) { return scalarValueStep.get(); } @@ -182,63 +234,9 @@ public class AuthenticationStepsOperator implements VaultTokenSupplier { e)); } - private static Object doMapStep(MapStep o, Object state) { - return o.apply(state); - } + enum Undefinded { - private Mono doZipStep(ZipStep o) { - return createMono(o.getRight()); - } - - private static Object doOnNext(OnNextStep o, Object state) { - return o.apply(state); - } - - private Mono doHttpRequest(HttpRequestNode step, Object state) { - - HttpRequest definition = step.getDefinition(); - HttpEntity entity = getEntity(definition.getEntity(), state); - - RequestBodySpec spec; - if (definition.getUri() == null) { - - spec = this.webClient.method(definition.getMethod()).uri(definition.getUriTemplate(), - definition.getUrlVariables()); - } - else { - spec = this.webClient.method(definition.getMethod()).uri(definition.getUri()); - } - - for (Entry> header : entity.getHeaders().entrySet()) { - spec = spec.header(header.getKey(), header.getValue().get(0)); - } - - if (entity.getBody() != null && !entity.getBody().equals(Undefinded.INSTANCE)) { - return spec.bodyValue(entity.getBody()).retrieve().bodyToMono(definition.getResponseType()); - } - - return spec.retrieve().bodyToMono(definition.getResponseType()); - } - - private static HttpEntity getEntity(HttpEntity entity, Object state) { - - if (entity == null) { - return state == null ? HttpEntity.EMPTY : new HttpEntity<>(state); - } - - if (entity.getBody() == null && state != null) { - return new HttpEntity<>(state, entity.getHeaders()); - } - - return entity; - } - - static class Undefinded { - - static final Undefinded INSTANCE = new Undefinded(); - - private Undefinded() { - } + UNDEFINDED; }