Class PrincipalAccessor

java.lang.Object
com.sap.cloud.sdk.cloudplatform.security.principal.PrincipalAccessor

public final class PrincipalAccessor extends Object
Accessor for retrieving the current Principal.
  • Method Details

    • getPrincipalFacade

      @Nonnull public static PrincipalFacade getPrincipalFacade()
      Returns the PrincipalFacade instance.
      Returns:
      The PrincipalFacade instance.
    • tryGetPrincipalFacade

      @Nonnull public static io.vavr.control.Try<PrincipalFacade> tryGetPrincipalFacade()
      Returns a Try of the PrincipalFacade instance.
      Returns:
      A Try of the PrincipalFacade instance.
    • setPrincipalFacade

      public static void setPrincipalFacade(@Nullable PrincipalFacade principalFacade)
      Replaces the default PrincipalFacade instance.
      Parameters:
      principalFacade - An instance of PrincipalFacade. Use null to reset the facade to default.
    • getCurrentPrincipal

      @Nonnull public static Principal getCurrentPrincipal() throws PrincipalAccessException
      Returns the current Principal.
      Returns:
      The current Principal.
      Throws:
      PrincipalAccessException - If there is an issue while accessing the Principal.
    • tryGetCurrentPrincipal

      @Nonnull public static io.vavr.control.Try<Principal> tryGetCurrentPrincipal()
      Returns a Try of the current Principal, or, if the Try is a failure, the global fallback.
      Returns:
      A Try of the current Principal.
    • executeWithPrincipal

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

      public static void executeWithPrincipal(@Nonnull Principal principal, @Nonnull Executable executable) throws ThreadContextExecutionException
      Execute the given Executable on behalf of a given principal.
      Parameters:
      principal - The principal to execute on behalf of.
      executable - The executable to execute.
      Throws:
      ThreadContextExecutionException - If there is an issue while running the code on behalf of the principal.
    • executeWithFallbackPrincipal

      @Nullable public static <T> T executeWithFallbackPrincipal(@Nonnull Supplier<Principal> fallbackPrincipal, @Nonnull Callable<T> callable) throws ThreadContextExecutionException
      Execute the given Callable, using the given principal as fallback if there is no other principal available.
      Type Parameters:
      T - The type of the callable.
      Parameters:
      fallbackPrincipal - The principal 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 on behalf of the principal.
    • executeWithFallbackPrincipal

      public static void executeWithFallbackPrincipal(@Nonnull Supplier<Principal> fallbackPrincipal, @Nonnull Executable executable) throws ThreadContextExecutionException
      Execute the given Executable, using the given principal as fallback if there is no other principal available.
      Parameters:
      fallbackPrincipal - The principal to fall back to.
      executable - The executable to execute.
      Throws:
      ThreadContextExecutionException - If there is an issue while running the code on behalf of the principal.
    • getFallbackPrincipal

      @Nullable public static Supplier<Principal> getFallbackPrincipal()
      Global fallback Principal. 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 principal.
    • setFallbackPrincipal

      public static void setFallbackPrincipal(@Nullable Supplier<Principal> fallbackPrincipal)
      Global fallback Principal. 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 principal.