@Override public CompletableFuture<String> get() { return tokensSupplier.get().thenApply(Tokens::getAccessToken); }
@Override public CompletionStage<String> get() { return tokensSupplier.get().thenApply(Tokens::getAccessToken); }
@Override public CompletionStage<String> get() { rejectExcutionIfClosed("Token supplier is already closed."); return tokensSupplier.get().thenApply(Tokens::getAccessToken); }
@Override public CompletionStage<String> get() { rejectExcutionIfClosed("Token supplier is already closed."); return tokensSupplier.get().thenApply(Tokens::getAccessToken); }
/** * Fetches a new access token using the client credentials flow. * * {@include.example io.sphere.sdk.client.TokensFacadeIntegrationTest#fetchAccessToken()} * * @param authConfig the commercetools project which the token should belong to * @return token */ public static CompletionStage<Tokens> fetchTokens(final SphereAuthConfig authConfig) { return TokensSupplier.of(authConfig, SphereClientFactory.of().createHttpClient(), true).get(); }
/** * Fetches a new access token using the client credentials flow. * * {@include.example io.sphere.sdk.client.TokensFacadeIntegrationTest#fetchAccessToken()} * * @param authConfig the commercetools project which the token should belong to * @return token */ public static CompletionStage<Tokens> fetchTokens(final SphereAuthConfig authConfig) { return TokensSupplier.of(authConfig, SphereClientFactory.of().createHttpClient(), true).get(); }
/** * Fetches a new access token using the customer password flow. * * {@include.example io.sphere.sdk.client.TokensFacadeIntegrationTest#passwordFlowDemo()} * * @param authConfig authConfig * @param email email * @param password password * @return token */ public static CompletionStage<Tokens> fetchCustomerPasswordFlowTokens(final SphereAuthConfig authConfig, final String email, final String password) { return TokensSupplier.ofCustomerPasswordFlowTokens(authConfig, email, password, SphereClientFactory.of().createHttpClient(), true).get(); } }
/** * Fetches a new access token using the customer password flow. * * {@include.example io.sphere.sdk.client.TokensFacadeIntegrationTest#passwordFlowDemo()} * * @param authConfig authConfig * @param email email * @param password password * @return token */ public static CompletionStage<Tokens> fetchCustomerPasswordFlowTokens(final SphereAuthConfig authConfig, final String email, final String password) { return TokensSupplier.ofCustomerPasswordFlowTokens(authConfig, email, password, SphereClientFactory.of().createHttpClient(), true).get(); } }
private void process(final FetchTokenFromSphereMessage m) { if (!isWaitingForToken) { isWaitingForToken = true; //for users it is fail fast but in the background it will be attempted again final CompletionStage<Tokens> future = m.attempt > 0 ? supervisedTokenSupplier.apply(() -> internalTokensSupplier.get()) : internalTokensSupplier.get(); onSuccess(future, tokens -> tell(new SuccessfulTokenFetchMessage(tokens))); onFailure(future, e -> { requestUpdateFailedStatus.accept(e); tell(new FailedTokenFetchMessage(e)); tell(new FetchTokenFromSphereMessage(1)); }); } }
private void process(final FetchTokenFromSphereMessage m) { if (!isWaitingForToken) { isWaitingForToken = true; //for users it is fail fast but in the background it will be attempted again final CompletionStage<Tokens> future = m.attempt > 0 ? supervisedTokenSupplier.apply(() -> internalTokensSupplier.get()) : internalTokensSupplier.get(); onSuccess(future, tokens -> tell(new SuccessfulTokenFetchMessage(tokens))); onFailure(future, e -> { requestUpdateFailedStatus.accept(e); tell(new FailedTokenFetchMessage(e)); tell(new FetchTokenFromSphereMessage(1)); }); } }
@Override public CompletionStage<String> getNewToken() { authActor.tell(new AuthActorProtocol.FetchTokenFromSphereMessage()); /* Two times a token will be fetched, once to update the cached token and the other time to get a new token to the caller. This way it minimizes the possibility of race conditions but is less effective. But this should be okay since it should be a rare case that his happens. */ return tokensSupplier.get().thenApplyAsync(tokens -> tokens.getAccessToken()); }
@Override public CompletionStage<String> getNewToken() { authActor.tell(new AuthActorProtocol.FetchTokenFromSphereMessage()); /* Two times a token will be fetched, once to update the cached token and the other time to get a new token to the caller. This way it minimizes the possibility of race conditions but is less effective. But this should be okay since it should be a rare case that his happens. */ return tokensSupplier.get().thenApplyAsync(tokens -> tokens.getAccessToken()); }
@Test public void shouldSendCorrelationId() throws Exception { final CompletableFuture<HttpResponse> successful = CompletableFutureUtils .successful(HttpResponse.of(200, "{\"access_token\": \"access_token\"}")); when(httpClient.execute(requestCaptor.capture())).thenReturn(successful); tokensSupplier.get().toCompletableFuture().get(); final HttpRequest httpRequest = requestCaptor.getValue(); final Optional<String> correlationIdHeader = httpRequest.getHeaders().findFlatHeader(HttpHeaders.X_CORRELATION_ID); assertThat(correlationIdHeader).isPresent(); final String correlationId = correlationIdHeader.get(); final String[] correlationIdParts = correlationId.split("/"); assertThat(correlationIdParts).hasSize(2); assertThat(correlationIdParts[0]).isEqualTo(authConfig.getProjectKey()); } }
private void process(final FetchTokenFromSphereMessage m) { if (!isWaitingForToken) { isWaitingForToken = true; final CompletableFuture<Tokens> future = internalTokensSupplier.get(); onSuccess(future, tokens -> tell(new SuccessfulTokenFetchMessage(tokens))); onFailure(future, e -> tell(new FailedTokenFetchMessage(e, m.attempt + 1))); } }
private void process(final FetchTokenFromSphereMessage m) { if (!isWaitingForToken) { isWaitingForToken = true; final CompletionStage<Tokens> future = internalTokensSupplier.get(); onSuccess(future, tokens -> tell(new SuccessfulTokenFetchMessage(tokens))); onFailure(future, e -> tell(new FailedTokenFetchMessage(e, m.attempt + 1))); } }