Class AuthTokenAccessor
java.lang.Object
com.sap.cloud.sdk.cloudplatform.security.AuthTokenAccessor
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 Summary
Modifier and TypeMethodDescriptionstatic voidexecuteWithAuthToken(AuthToken authToken, Executable executable) Execute the givenExecutablewith a given token.static <T> TexecuteWithAuthToken(AuthToken authToken, Callable<T> callable) Execute the givenCallablewith a given token.static voidexecuteWithFallbackAuthToken(Supplier<AuthToken> fallbackAuthToken, Executable executable) Execute the givenExecutable, using the given token as fallback if there is no other token available.static <T> TexecuteWithFallbackAuthToken(Supplier<AuthToken> fallbackAuthToken, Callable<T> callable) Execute the givenCallable, using the given token as fallback if there is no other token available.static AuthTokenFacadeTheAuthTokenFacadeinstance.static AuthTokenReturns the currentAuthToken.Global fallbackAuthToken.static voidsetAuthTokenFacade(AuthTokenFacade requestFacade) Replaces the defaultAuthTokenFacadeinstance.static voidsetFallbackToken(Supplier<AuthToken> fallbackToken) Global fallbackAuthToken.static io.vavr.control.Try<AuthToken>
-
Method Details
-
setAuthTokenFacade
Replaces the defaultAuthTokenFacadeinstance.- Parameters:
requestFacade- An instance ofAuthTokenFacade. Usenullto reset the facade.
-
getCurrentToken
Returns the currentAuthToken.- Returns:
- The current
AuthToken. - Throws:
AuthTokenAccessException- If there is an issue while trying to access theAuthToken. For instance, anAuthTokenis not available if no request is available or the request does not contain an "Authorization" header.
-
tryGetCurrentToken
Returns aTryof the currentAuthToken, or, if theTryis a failure, the global fallback. AnAuthTokenis not available if no request is available or the request does not contain an "Authorization" header.- Returns:
- A
Tryof the currentAuthToken
-
executeWithAuthToken
@Nullable public static <T> T executeWithAuthToken(@Nonnull AuthToken authToken, @Nonnull Callable<T> callable) throws ThreadContextExecutionException Execute the givenCallablewith 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 givenExecutablewith 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 givenCallable, 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 givenExecutable, 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
TheAuthTokenFacadeinstance. -
getFallbackToken
Global fallbackAuthToken. By default, no fallback is used, i.e., the fallback isnull. A global fallback can be useful to ensure a safe fallback or to ease testing with a mocked token. -
setFallbackToken
Global fallbackAuthToken. By default, no fallback is used, i.e., the fallback isnull. A global fallback can be useful to ensure a safe fallback or to ease testing with a mocked token.
-