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 void
executeWithAuthToken
(AuthToken authToken, Executable executable) Execute the givenExecutable
with a given token.static <T> T
executeWithAuthToken
(AuthToken authToken, Callable<T> callable) Execute the givenCallable
with a given token.static void
executeWithFallbackAuthToken
(Supplier<AuthToken> fallbackAuthToken, Executable executable) Execute the givenExecutable
, using the given token as fallback if there is no other token available.static <T> T
executeWithFallbackAuthToken
(Supplier<AuthToken> fallbackAuthToken, Callable<T> callable) Execute the givenCallable
, using the given token as fallback if there is no other token available.static AuthTokenFacade
TheAuthTokenFacade
instance.static AuthToken
Returns the currentAuthToken
.Global fallbackAuthToken
.static void
setAuthTokenFacade
(AuthTokenFacade requestFacade) Replaces the defaultAuthTokenFacade
instance.static void
setFallbackToken
(Supplier<AuthToken> fallbackToken) Global fallbackAuthToken
.static io.vavr.control.Try<AuthToken>
-
Method Details
-
setAuthTokenFacade
Replaces the defaultAuthTokenFacade
instance.- Parameters:
requestFacade
- An instance ofAuthTokenFacade
. Usenull
to 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, anAuthToken
is not available if no request is available or the request does not contain an "Authorization" header.
-
tryGetCurrentToken
Returns aTry
of the currentAuthToken
, or, if theTry
is a failure, the global fallback. AnAuthToken
is not available if no request is available or the request does not contain an "Authorization" header.- Returns:
- A
Try
of the currentAuthToken
-
executeWithAuthToken
@Nullable public static <T> T executeWithAuthToken(@Nonnull AuthToken authToken, @Nonnull Callable<T> callable) throws ThreadContextExecutionException Execute the givenCallable
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 givenExecutable
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 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
TheAuthTokenFacade
instance. -
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.
-