Class AuthTokenAccessor

java.lang.Object
com.sap.cloud.sdk.cloudplatform.security.AuthTokenAccessor

public final class AuthTokenAccessor extends Object
Accessor for retrieving the current AuthToken.

This accessor delegates the logic to implementations of the AuthTokenFacade interface, as determined by the ServiceLoader pattern.

If more than one implementation of the AuthTokenFacade could be found an Exception is thrown. If no implementation could be found a default implementation will be used, that throws an Exception on every invocation.

See Also:
  • Method Details

    • setAuthTokenFacade

      public static void setAuthTokenFacade(@Nullable AuthTokenFacade requestFacade)
      Replaces the default AuthTokenFacade instance.
      Parameters:
      requestFacade - An instance of AuthTokenFacade. Use null to reset the facade.
    • getCurrentToken

      @Nonnull public static AuthToken getCurrentToken() throws AuthTokenAccessException
      Returns the current AuthToken.
      Returns:
      The current AuthToken.
      Throws:
      AuthTokenAccessException - If there is an issue while trying to access the AuthToken. For instance, an AuthToken is not available if no request is available or the request does not contain an "Authorization" header.
    • tryGetCurrentToken

      @Nonnull public static io.vavr.control.Try<AuthToken> tryGetCurrentToken()
      Returns a Try of the current AuthToken, or, if the Try is a failure, the global fallback. An AuthToken is not available if no request is available or the request does not contain an "Authorization" header.
      Returns:
      A Try of the current AuthToken
    • executeWithAuthToken

      @Nullable public static <T> T executeWithAuthToken(@Nonnull AuthToken authToken, @Nonnull Callable<T> callable) throws ThreadContextExecutionException
      Execute the given Callable with a given token.
      Type Parameters:
      T - The type of the callable.
      Parameters:
      authToken - The token to execute with.
      callable - The callable to execute.
      Returns:
      The value computed by the callable.
      Throws:
      ThreadContextExecutionException - If there is an issue while running the code with the token.
    • executeWithAuthToken

      public static void executeWithAuthToken(@Nonnull AuthToken authToken, @Nonnull Executable executable) throws ThreadContextExecutionException
      Execute the given Executable with a given token.
      Parameters:
      authToken - The token to execute with.
      executable - The executable to execute.
      Throws:
      ThreadContextExecutionException - If there is an issue while running the code with the token.
    • executeWithFallbackAuthToken

      @Nullable public static <T> T executeWithFallbackAuthToken(@Nonnull Supplier<AuthToken> fallbackAuthToken, @Nonnull Callable<T> callable) throws ThreadContextExecutionException
      Execute the given Callable, using the given token as fallback if there is no other token available.
      Type Parameters:
      T - The type of the callable.
      Parameters:
      fallbackAuthToken - The token to fall back to.
      callable - The callable to execute.
      Returns:
      The value computed by the callable.
      Throws:
      ThreadContextExecutionException - If there is an issue while running the code with the token.
    • executeWithFallbackAuthToken

      public static void executeWithFallbackAuthToken(@Nonnull Supplier<AuthToken> fallbackAuthToken, @Nonnull Executable executable) throws ThreadContextExecutionException
      Execute the given Executable, using the given token as fallback if there is no other token available.
      Parameters:
      fallbackAuthToken - The token to fall back to.
      executable - The executable to execute.
      Throws:
      ThreadContextExecutionException - If there is an issue while running the code with the token.
    • getAuthTokenFacade

      @Nonnull public static AuthTokenFacade getAuthTokenFacade()
      The AuthTokenFacade instance.
    • getFallbackToken

      @Nullable public static Supplier<AuthToken> getFallbackToken()
      Global fallback AuthToken. By default, no fallback is used, i.e., the fallback is null. A global fallback can be useful to ensure a safe fallback or to ease testing with a mocked token.
    • setFallbackToken

      public static void setFallbackToken(@Nullable Supplier<AuthToken> fallbackToken)
      Global fallback AuthToken. By default, no fallback is used, i.e., the fallback is null. A global fallback can be useful to ensure a safe fallback or to ease testing with a mocked token.