Class AbstractStateManager

All Implemented Interfaces:
Component, DestructableComponent, IdentifiableComponent, IdentifiedComponent, InitializableComponent, StateManager
Direct Known Subclasses:
CookieStateManager, PassthroughStateManager, StorageServiceStateManager

public abstract class AbstractStateManager extends AbstractIdentifiableInitializableComponent implements StateManager
Base class for StateManager implementations.
  • Field Details

    • log

      @Nonnull private final org.slf4j.Logger log
      Class logger.
    • httpRequestSupplier

      Supplier for the servlet request to read from.
    • identifierStrategy

      @NonnullAfterInit private IdentifierGenerationStrategy identifierStrategy
      Identifier generation.
    • prefix

      @Nonnull private String prefix
      Optional prefix to attach to token.
    • objectMapper

      @NonnullAfterInit private com.fasterxml.jackson.databind.ObjectMapper objectMapper
      JSON object mapper.
    • dataSealer

      @Nullable private DataSealer dataSealer
      Optional component to protect the data from tampering/visibility.
    • replayCache

      @Nullable private ReplayCache replayCache
      Optional component to prevent replay of state.
    • expiration

      @Nonnull private Duration expiration
      Expiration for state tokens.
  • Constructor Details

    • AbstractStateManager

      public AbstractStateManager()
      Constructor.
  • Method Details

    • setHttpServletRequestSupplier

      public void setHttpServletRequestSupplier(@Nonnull NonnullSupplier<HttpServletRequest> requestSupplier)
      Set the Supplier for the servlet request to read from.
      Parameters:
      requestSupplier - servlet request supplier
    • getIdentifierGenerationStrategy

      @NonnullAfterInit public IdentifierGenerationStrategy getIdentifierGenerationStrategy()
      Returns:
      identifier generator strategy
    • setIdentifierGenerationStrategy

      public void setIdentifierGenerationStrategy(@Nonnull IdentifierGenerationStrategy strategy)
      Set IdentifierGenerationStrategy to use.

      Defaults to a secure random source that produces 16 byte values.

      Parameters:
      strategy - identifier generator strategy
    • setPrefix

      public void setPrefix(@Nullable String s)
      Sets optional prefix to apply to the token.
      Parameters:
      s - prefix string
    • setObjectMapper

      public void setObjectMapper(@Nonnull com.fasterxml.jackson.databind.ObjectMapper mapper)
      Set the JSON ObjectMapper to use for serialization.
      Parameters:
      mapper - object mapper
    • getDataSealer

      @Nullable public DataSealer getDataSealer()
      Gets the DataSealer to use to protect data from tampering/visbility.
      Returns:
      data sealer or null
    • setDataSealer

      public void setDataSealer(@Nullable DataSealer sealer)
      Sets the DataSealer to use to protect data from tampering/visbility.
      Parameters:
      sealer - data sealer
    • getReplayCache

      @Nullable public ReplayCache getReplayCache()
      Gets the ReplayCache to use to prevent replay of state.
      Returns:
      replay cache or null
    • setReplayCache

      public void setReplayCache(@Nullable ReplayCache cache)
      Sets the ReplayCache to use to prevent replay of state.

      This is an additional layer of protection over and above the clearing of state that takes place routinely.

      Parameters:
      cache - replay cache
    • getExpiration

      @Nonnull public Duration getExpiration()
      Get the expiration limit for state tokens.
      Returns:
      expiration limit
    • setExpiration

      public void setExpiration(@Nonnull Duration exp)
      Set the expiration limit for state tokens.

      Defaults to PT30M.

      Parameters:
      exp - expiration limit
    • doInitialize

      protected void doInitialize() throws ComponentInitializationException
      Overrides:
      doInitialize in class AbstractIdentifiedInitializableComponent
      Throws:
      ComponentInitializationException
    • preserveToStateToken

      @Nonnull public String preserveToStateToken(@Nonnull Agent agent, @Nonnull Application application, @Nonnull StateData data) throws IOException
      Preserves data while transforming it into a state token.
      Specified by:
      preserveToStateToken in interface StateManager
      Parameters:
      agent - agent owning the state
      application - application owning the state
      data - data to preserve
      Returns:
      state token representing value
      Throws:
      IOException - if creation of token fails
    • recoverFromStateToken

      @Nullable public <T extends StateData> T recoverFromStateToken(@Nonnull Agent agent, @Nonnull Application application, @Nonnull @NotEmpty String token, @Nonnull Class<T> type) throws IOException
      Recovers stored data from a state token.

      In most implementations, the state token mapping should be cleared on successful use of this method.

      Specified by:
      recoverFromStateToken in interface StateManager
      Type Parameters:
      T - type of state
      Parameters:
      agent - agent owning the state
      application - application owning the state
      token - state token
      type - specific subtype of StateData to recover
      Returns:
      the recovered data, or null if unable to recover without underlying cause
      Throws:
      IOException - if recovery from token fails
    • doPreserve

      @Nonnull protected abstract String doPreserve(@Nonnull Agent agent, @Nonnull Application application, @Nonnull String data, boolean sealed) throws IOException
      Subclasses implement this method to preserve the transformed data in whatever way is necessary and return a token.
      Parameters:
      agent - agent owning the state
      application - application owning the state
      data - data to preserve
      sealed - whether the data was sealed (and thus base64-encoded)
      Returns:
      the state token
      Throws:
      IOException - if an error occurs
    • doRecover

      @Nullable protected abstract String doRecover(@Nonnull Agent agent, @Nonnull Application application, @Nonnull @NotEmpty String stateToken, boolean sealed) throws IOException
      Subclasses implement this method to recover the stored data in whatever way is necessary and return the supplied data string as a successful result.

      The implementation should ensure when possible that this method works only once for a given state token.

      Subclasses may assume that the state token inputs they receive will have been returned by them via the doPreserve(Agent, Application, String, boolean) method.

      Parameters:
      agent - agent owning the state
      application - application owning the state
      stateToken - the state token to map from/clear
      sealed - whether the data was sealed (and thus base64-encoded)
      Returns:
      the recovered data, or null if unable to recover without underlying cause
      Throws:
      IOException - if an error occurs
    • generateToken

      @Nonnull protected String generateToken()
      Generate a state token.
      Returns:
      a new state token
    • getClientAddress

      @Nullable protected String getClientAddress()
      Get the current client address if available.
      Returns:
      client address or null