Interface SessionData


@Experimental @ThreadSafe @Provider public interface SessionData
A container for data that is specific to a session. All components may use this storage to associate arbitrary data with a session.

Unlike a cache, this session data is not subject to purging. For this same reason, session data should also not be abused as a cache (i.e. for storing values that can be re-calculated) to avoid memory exhaustion.

Note: Actual implementations must be thread-safe.

Since:
4.0.0
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static final class 
    Key used to query the session data
  • Method Summary

    Modifier and Type
    Method
    Description
    <T> T
    Retrieve of compute the data associated with the specified key.
    <T> T
    Gets the session data associated with the specified key.
    static <T> SessionData.Key<T>
    key(Class<T> clazz)
    Create a key using the given class as an identifier and as the type of the object.
    static <T> SessionData.Key<T>
    key(Class<T> clazz, Object id)
    Create a key using the given class and id.
    <T> boolean
    replace(SessionData.Key<T> key, T oldValue, T newValue)
    Associates the specified session data with the given key if the key is currently mapped to the given value.
    <T> void
    set(SessionData.Key<T> key, T value)
    Associates the specified session data with the given key.
  • Method Details

    • set

      <T> void set(@Nonnull SessionData.Key<T> key, @Nullable T value)
      Associates the specified session data with the given key.
      Parameters:
      key - the key under which to store the session data, must not be null
      value - the data to associate with the key, may be null to remove the mapping
    • replace

      <T> boolean replace(@Nonnull SessionData.Key<T> key, @Nullable T oldValue, @Nullable T newValue)
      Associates the specified session data with the given key if the key is currently mapped to the given value. This method provides an atomic compare-and-update of some key's value.
      Parameters:
      key - the key under which to store the session data, must not be null
      oldValue - the expected data currently associated with the key, may be null
      newValue - the data to associate with the key, may be null to remove the mapping
      Returns:
      true if the key mapping was successfully updated from the old value to the new value, false if the current key mapping didn't match the expected value and was not updated.
    • get

      @Nullable <T> T get(@Nonnull SessionData.Key<T> key)
      Gets the session data associated with the specified key.
      Parameters:
      key - the key for which to retrieve the session data, must not be null
      Returns:
      the session data associated with the key or null if none
    • computeIfAbsent

      @Nullable <T> T computeIfAbsent(@Nonnull SessionData.Key<T> key, @Nonnull Supplier<T> supplier)
      Retrieve of compute the data associated with the specified key.
      Parameters:
      key - the key for which to retrieve the session data, must not be null
      supplier - the supplier will compute the new value
      Returns:
      the session data associated with the key
    • key

      static <T> SessionData.Key<T> key(Class<T> clazz)
      Create a key using the given class as an identifier and as the type of the object.
    • key

      static <T> SessionData.Key<T> key(Class<T> clazz, Object id)
      Create a key using the given class and id.