private void process(final FailedTokenFetchMessage m) { isWaitingForToken = false; AUTH_LOGGER.error(() -> "Can't fetch tokens.", m.cause); requestUpdateFailedStatus.accept(m.cause); }
private void process(final FailedTokenFetchMessage m) { isWaitingForToken = false; AUTH_LOGGER.error(() -> "Can't fetch tokens.", m.cause); requestUpdateFailedStatus.accept(m.cause); }
static void closeQuietly(final AutoCloseable closeable) { try { if (closeable != null) { closeable.close(); } } catch (final Exception e) { SphereInternalLogger.getLogger("io").error(() -> "Error on closing resource.", e); } } }
static void closeQuietly(final AutoCloseable closeable) { try { if (closeable != null) { closeable.close(); } } catch (final Exception e) { SphereInternalLogger.getLogger(AutoCloseableService.class).error(() -> "Error on closing resource.", e); } } }
static void closeQuietly(final AutoCloseable closeable) { try { if (closeable != null) { closeable.close(); } } catch (final Exception e) { SphereInternalLogger.getLogger("io").error(() -> "Error on closing resource.", e); } } }
static void closeQuietly(final AutoCloseable closeable) { try { if (closeable != null) { closeable.close(); } } catch (final Exception e) { SphereInternalLogger.getLogger(AutoCloseableService.class).error(() -> "Error on closing resource.", e); } } }
private void logTokenResult(final Tokens nullableTokens, final Throwable nullableThrowable) { if (nullableTokens != null) { AUTH_LOGGER.debug(() -> "Successfully fetched token that expires in " + Optional.ofNullable(nullableTokens.getExpiresIn()).map(x -> x.toString()).orElse("an unknown time") + "."); } else { AUTH_LOGGER.error(() -> "Failed to fetch token.", nullableThrowable); } }
/** Pretty prints given JSON string, replacing passwords by {@code 'xxxxx'}. * @param json JSON code as String which should be formatted * @return <code>json</code> formatted */ public static String prettyPrintJsonStringSecure(String json) { try { ObjectMapper jsonParser = new ObjectMapper(); JsonNode jsonTree = jsonParser.readValue(json, JsonNode.class); secure(jsonTree); ObjectWriter writer = jsonParser.writerWithDefaultPrettyPrinter(); return writer.writeValueAsString(jsonTree); } catch (IOException e) { LOGGER.error(() -> "invalid JSON for pretty printing", e); return json; } }
private void logTokenResult(final Tokens nullableTokens, final Throwable nullableThrowable) { if (nullableTokens != null) { AUTH_LOGGER.debug(() -> "Successfully fetched token that expires in " + Optional.ofNullable(nullableTokens.getExpiresIn()).map(x -> x.toString()).orElse("an unknown time") + "."); } else { AUTH_LOGGER.error(() -> "Failed to fetch token.", nullableThrowable); } }
private void logTokenResult(final Tokens nullableTokens, final Throwable nullableThrowable) { if (nullableTokens != null) { AUTH_LOGGER.debug(() -> "Successfully fetched token that expires in " + Optional.ofNullable(nullableTokens.getExpiresIn()).map(x -> x.toString()).orElse("an unknown time") + "."); } else { AUTH_LOGGER.error(() -> "Failed to fetch token.", nullableThrowable); } }
private void logTokenResult(final CompletableFuture<Tokens> result) { result.whenComplete((tokens, e) -> { if (tokens != null) { AUTH_LOGGER.debug(() -> "Successfully fetched token that expires in " + tokens.getExpiresIn().map(x -> x.toString()).orElse("an unknown time") + "."); } else { AUTH_LOGGER.error(() -> "Failed to fetch token." + tokens.getExpiresIn(), e); } }); }
public final Optional<JsonNode> getJsonBody() { final Function<byte[], JsonNode> f = body -> JsonUtils.readTree(body); try { return httpResponse.flatMap(r -> r.getResponseBody().map(f)); } catch (final Exception e) { SphereInternalLogger.getLogger(SphereServiceException.class).error(() -> "Cannot provide JSON body.", e); return Optional.empty(); } } }
private void process(final FailedTokenFetchMessage m) { isWaitingForToken = false; final boolean failReasonIsInvalidCredentials = m.cause.getCause() != null && m.cause.getCause() instanceof InvalidClientCredentialsException; if (failReasonIsInvalidCredentials) { AUTH_LOGGER.error(() -> "Can't fetch tokens due to invalid credentials.", m.cause); subscribers.forEach(subscriber -> subscriber.tell(new TokenDeliveryFailedMessage(m.cause))); } else { AUTH_LOGGER.error(() -> "Can't fetch tokens.", m.cause); final long tryAgainIn = m.attempt * DEFAULT_WAIT_TIME_UNTIL_RETRY_MILLISECONDS; schedule(new FetchTokenFromSphereMessage(m.attempt), tryAgainIn, MILLISECONDS); if (m.attempt > 2) { subscribers.forEach(subscriber -> subscriber.tell(new TokenDeliveryFailedMessage(m.cause))); } } }
private void process(final FailedTokenFetchMessage m) { isWaitingForToken = false; AUTH_LOGGER.error(() -> "Can't fetch tokens.", m.cause); final long tryAgainIn = m.attempt * DEFAULT_WAIT_TIME_UNTIL_RETRY_MILLISECONDS; schedule(new FetchTokenFromSphereMessage(m.attempt), tryAgainIn, MILLISECONDS); if (m.attempt > 2) { subscribers.forEach(subscriber -> subscriber.tell(new TokenDeliveryFailedMessage(m.cause))); } }
@Override protected void receive(final Object message) { receiveBuilder(message) .when(TokenDeliveredMessage.class, m -> { if (!currentTokensOption.isPresent() || currentTokenIsOlder(m.tokens)) { updateToken(m.tokens); } }) .when(TokenDeliveryFailedMessage.class, m -> { final boolean hasInvalidCredentials = m.cause.getCause() != null && m.cause.getCause() instanceof InvalidClientCredentialsException; if (hasInvalidCredentials) { AUTH_LOGGER.error(() -> "Invalid client credentials shutting down.", m.cause); currentAccessTokenFuture.completeExceptionally(m.cause);//in case it is still empty currentAccessTokenFuture = CompletableFutureUtils.failed(m.cause);//in case it was not empty close(); } else if (!currentTokensOption.isPresent()) { currentAccessTokenFuture.completeExceptionally(m.cause); } else if (lastTokenIsStillValid()) { //keep the old token } else { currentTokensOption = Optional.empty(); currentAccessTokenFuture = CompletableFutureUtils.failed(m.cause); } }); } }
private void update(Tokens tokens, Exception e) { synchronized (accessTokenLock) { try { if (e == null) { AccessToken newToken = new AccessToken(tokens.getAccessToken(), tokens.getExpiresIn(), System.currentTimeMillis()); this.accessTokenResult = Optional.of(new ValidationE<>(newToken, null)); AUTH_LOGGER.debug(() -> "Refreshed access token."); scheduleNextRefresh(tokens); } else { this.accessTokenResult = Optional.of(ValidationE.<AccessToken>error(new SphereClientException(e))); AUTH_LOGGER.error(() -> "Failed to refresh access token.", e); } } finally { accessTokenLock.notifyAll(); } } }