Skip to content

Commit

Permalink
Polishing
Browse files Browse the repository at this point in the history
Reorder methods according to their call sequence.

See gh-586.
  • Loading branch information
mp911de committed Sep 24, 2020
1 parent e00a271 commit 7274675
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public VaultToken login() throws VaultException {
String.format("Cannot retrieve VaultToken from authentication chain. Got instead %s", state));
}

@SuppressWarnings({ "unchecked", "ConstantConditions" })
private Object evaluate(Iterable<Node<?>> steps) {

Object state = null;
Expand Down Expand Up @@ -144,28 +145,6 @@ private Object evaluate(Iterable<Node<?>> steps) {
return state;
}

private static Object doScalarValueStep(ScalarValueStep<Object> scalarValueStep) {
return scalarValueStep.get();
}

private static Object doSupplierStep(SupplierStep<Object> supplierStep) {
return supplierStep.get();
}

private static Object doMapStep(MapStep<Object, Object> o, Object state) {
return o.apply(state);
}

private Object doZipStep(ZipStep<Object, Object> o, Object state) {

Object result = evaluate(o.getRight());
return Pair.of(state, result);
}

private static Object doOnNext(OnNextStep<Object> o, Object state) {
return o.apply(state);
}

@SuppressWarnings("ConstantConditions")
@Nullable
private Object doHttpRequest(HttpRequestNode<Object> step, @Nullable Object state) {
Expand All @@ -187,7 +166,7 @@ private Object doHttpRequest(HttpRequestNode<Object> step, @Nullable Object stat

}

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);
Expand All @@ -200,4 +179,26 @@ private static HttpEntity<?> getEntity(HttpEntity<?> entity, @Nullable Object st
return entity;
}

private static Object doMapStep(MapStep<Object, Object> o, Object state) {
return o.apply(state);
}

private Object doZipStep(ZipStep<Object, Object> o, Object state) {

Object result = evaluate(o.getRight());
return Pair.of(state, result);
}

private static Object doOnNext(OnNextStep<Object> o, Object state) {
return o.apply(state);
}

private static Object doScalarValueStep(ScalarValueStep<Object> scalarValueStep) {
return scalarValueStep.get();
}

private static Object doSupplierStep(SupplierStep<Object> supplierStep) {
return supplierStep.get();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
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;
Expand Down Expand Up @@ -117,7 +118,7 @@ public Mono<VaultToken> getVaultToken() throws VaultException {
@SuppressWarnings("unchecked")
private Mono<Object> createMono(Iterable<Node<?>> steps) {

Mono<Object> state = Mono.just(Undefinded.INSTANCE);
Mono<Object> state = Mono.just(Undefinded.UNDEFINDED);

for (Node<?> o : steps) {

Expand Down Expand Up @@ -157,43 +158,6 @@ private Mono<Object> createMono(Iterable<Node<?>> steps) {
return state;
}

private static Object doScalarValueStep(ScalarValueStep<Object> scalarValueStep) {
return scalarValueStep.get();
}

private Mono<Object> doSupplierStepLater(SupplierStep<Object> supplierStep) {

Supplier<?> supplier = supplierStep.getSupplier();

if (!(supplier instanceof ResourceCredentialSupplier)) {
return Mono.fromSupplier(supplierStep.getSupplier()).subscribeOn(Schedulers.boundedElastic());
}

ResourceCredentialSupplier resourceSupplier = (ResourceCredentialSupplier) supplier;

return DataBufferUtils.join(DataBufferUtils.read(resourceSupplier.getResource(), this.factory, 4096))
.map(dataBuffer -> {
String result = dataBuffer.toString(ResourceCredentialSupplier.CHARSET);
DataBufferUtils.release(dataBuffer);
return (Object) result;
}).onErrorMap(IOException.class,
e -> new VaultException(
String.format("Credential retrieval from %s failed", resourceSupplier.getResource()),
e));
}

private static Object doMapStep(MapStep<Object, Object> o, Object state) {
return o.apply(state);
}

private Mono<Object> doZipStep(ZipStep<Object, Object> o) {
return createMono(o.getRight());
}

private static Object doOnNext(OnNextStep<Object> o, Object state) {
return o.apply(state);
}

private Mono<Object> doHttpRequest(HttpRequestNode<Object> step, Object state) {

HttpRequest<Object> definition = step.getDefinition();
Expand All @@ -213,14 +177,14 @@ private Mono<Object> doHttpRequest(HttpRequestNode<Object> step, Object state) {
spec = spec.header(header.getKey(), header.getValue().get(0));
}

if (entity.getBody() != null && !entity.getBody().equals(Undefinded.INSTANCE)) {
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(HttpEntity<?> entity, Object state) {
private static HttpEntity<?> getEntity(@Nullable HttpEntity<?> entity, @Nullable Object state) {

if (entity == null) {
return state == null ? HttpEntity.EMPTY : new HttpEntity<>(state);
Expand All @@ -233,13 +197,47 @@ private static HttpEntity<?> getEntity(HttpEntity<?> entity, Object state) {
return entity;
}

static class Undefinded {
private static Object doMapStep(MapStep<Object, Object> o, Object state) {
return o.apply(state);
}

static final Undefinded INSTANCE = new Undefinded();
private Mono<Object> doZipStep(ZipStep<Object, Object> o) {
return createMono(o.getRight());
}

private static void doOnNext(OnNextStep<Object> o, Object state) {
o.apply(state);
}

private static Object doScalarValueStep(ScalarValueStep<Object> scalarValueStep) {
return scalarValueStep.get();
}

private Mono<Object> doSupplierStepLater(SupplierStep<Object> supplierStep) {

Supplier<?> supplier = supplierStep.getSupplier();

private Undefinded() {
if (!(supplier instanceof ResourceCredentialSupplier)) {
return Mono.fromSupplier(supplierStep.getSupplier()).subscribeOn(Schedulers.boundedElastic());
}

ResourceCredentialSupplier resourceSupplier = (ResourceCredentialSupplier) supplier;

return DataBufferUtils.join(DataBufferUtils.read(resourceSupplier.getResource(), this.factory, 4096))
.map(dataBuffer -> {
String result = dataBuffer.toString(ResourceCredentialSupplier.CHARSET);
DataBufferUtils.release(dataBuffer);
return (Object) result;
}).onErrorMap(IOException.class,
e -> new VaultException(
String.format("Credential retrieval from %s failed", resourceSupplier.getResource()),
e));
}

enum Undefinded {

UNDEFINDED;

}

}

0 comments on commit 7274675

Please sign in to comment.