Interface Session


  • public interface Session
    A mechanism for associating semi persistent data with an individual user/client.

    This object can be accessed via the context registry. Access to the actual session data is provided by getData() method this object.

    The persistence mechanism used is determined by the implementation of SessionStore available.

    See Also:
    SessionModule
    • Method Detail

      • getId

        java.lang.String getId()
        The unique ID for this session.

        Call this method will provision a new ID if necessary. Provisioning and tracking of the ID is provided by the bound SessionId implementation.

        Returns:
        the ID for this session
      • getData

        ratpack.exec.Promise<SessionData> getData()
        The session data.

        The data is available via a promise to support backing SessionStore implementations that load the data asynchronously.

        Returns:
        the session data
      • getKeys

        default ratpack.exec.Promise<java.util.Set<SessionKey<?>>> getKeys()
        A convenience shorthand for SessionData.getKeys().
        Returns:
        the keys of all objects currently in the session
      • get

        default <T> ratpack.exec.Promise<java.util.Optional<T>> get​(SessionKey<T> key)
        A convenience shorthand for SessionData.get(SessionKey).
        Type Parameters:
        T - the type of object
        Parameters:
        key - the key
        Returns:
        the value for the given key
        See Also:
        require(SessionKey)
      • get

        default ratpack.exec.Promise<java.util.Optional<?>> get​(java.lang.String name)
        A convenience shorthand for SessionData.get(String).
        Parameters:
        name - the object name
        Returns:
        the value for the given key
        See Also:
        require(String)
      • get

        default <T> ratpack.exec.Promise<java.util.Optional<T>> get​(java.lang.Class<T> type)
        A convenience shorthand for SessionData.get(Class).
        Type Parameters:
        T - the type
        Parameters:
        type - the type
        Returns:
        the value for the given key
        See Also:
        require(Class)
      • require

        default <T> ratpack.exec.Promise<T> require​(SessionKey<T> key)
        A convenience shorthand for SessionData.require(SessionKey).
        Type Parameters:
        T - the type
        Parameters:
        key - the object key
        Returns:
        the value for the given key
      • require

        default <T> ratpack.exec.Promise<T> require​(java.lang.Class<T> type)
        A convenience shorthand for SessionData.require(Class).
        Type Parameters:
        T - the type
        Parameters:
        type - the type
        Returns:
        the value for the given key
      • require

        default <T> ratpack.exec.Promise<T> require​(java.lang.Class<T> type,
                                                    SessionSerializer serializer)
        Type Parameters:
        T - the type
        Parameters:
        type - the type
        serializer - the serializer
        Returns:
        the value for the given key
      • require

        default ratpack.exec.Promise<?> require​(java.lang.String name)
        A convenience shorthand for SessionData.require(String).
        Parameters:
        name - the object name
        Returns:
        the value for the given key
      • set

        default <T> ratpack.exec.Operation set​(SessionKey<T> key,
                                               T value)
        A convenience shorthand for SessionData.set(SessionKey, Object).
        Type Parameters:
        T - the type
        Parameters:
        key - the key
        value - the value
        Returns:
        the operation for setting the value
      • set

        default <T> ratpack.exec.Operation set​(java.lang.Class<T> type,
                                               T value)
        A convenience shorthand for SessionData.set(Class, Object).
        Type Parameters:
        T - the type
        Parameters:
        type - the type
        value - the value
        Returns:
        the operation for setting the value
      • set

        default <T> ratpack.exec.Operation set​(java.lang.Class<T> type,
                                               T value,
                                               SessionSerializer serializer)
        Type Parameters:
        T - the type
        Parameters:
        type - the type
        value - the value
        serializer - the serializer
        Returns:
        the operation for setting the value
      • set

        default <T> ratpack.exec.Operation set​(java.lang.String name,
                                               T value)
        A convenience shorthand for SessionData.set(String, Object).
        Type Parameters:
        T - the type
        Parameters:
        name - the name
        value - the value
        Returns:
        the operation for setting the value
      • set

        default <T> ratpack.exec.Operation set​(java.lang.String name,
                                               T value,
                                               SessionSerializer serializer)
        Type Parameters:
        T - the type
        Parameters:
        name - the name
        value - the value
        serializer - the serializer
        Returns:
        the operation for setting the value
      • set

        default <T> ratpack.exec.Operation set​(T value)
        A convenience shorthand for SessionData.set(Object).
        Type Parameters:
        T - the type
        Parameters:
        value - the value
        Returns:
        the operation for setting the value
      • set

        default <T> ratpack.exec.Operation set​(T value,
                                               SessionSerializer serializer)
        A convenience shorthand for SessionData.set(Object, SessionSerializer).
        Type Parameters:
        T - the type
        Parameters:
        value - the value
        serializer - the serializer
        Returns:
        the operation for setting the value
      • remove

        default ratpack.exec.Operation remove​(SessionKey<?> key)
        A convenience shorthand for SessionData.remove(SessionKey).
        Parameters:
        key - the key
        Returns:
        the operation for removing the value
      • remove

        default ratpack.exec.Operation remove​(java.lang.Class<?> type)
        A convenience shorthand for SessionData.remove(Class).
        Parameters:
        type - the type
        Returns:
        the operation for removing the value
      • remove

        default ratpack.exec.Operation remove​(java.lang.String name)
        A convenience shorthand for SessionData.remove(String).
        Parameters:
        name - the name
        Returns:
        the operation for removing the value
      • clear

        default ratpack.exec.Operation clear()
        A convenience shorthand for SessionData.clear().
        Returns:
        the operation for clearing the session
      • isDirty

        boolean isDirty()
        Whether or not any changes have been made to the session data since it was accessed.
        Returns:
        whether or not any changes have been made to the session data since it was accessed
      • save

        ratpack.exec.Operation save()
        Persists the session data.

        It is generally not necessary to call this method explicitly. The SessionModule installs a response finalizer that will call this method if the session is dirty.

        This method is effectively a noop if the session data has not yet been accessed.

        If the session data has been accessed, calling this method will initiate a store of the data regardless of whether the data is dirty or not.

        The isDirty() will always return true after calling this method, until changes are made to the session data.

        Returns:
        the save operation
      • terminate

        ratpack.exec.Operation terminate()
        Terminates the session and session id.

        Calling this method will immediately reset the state of the session data, and initiate a call to SessionStore.remove(AsciiString) on the underlying store. This effectively resets the state of this object.

        This method also invokes the SessionId.terminate() method, which prevents the same ID from being used subsequently.

        Returns:
        the terminate operation
      • getJavaSerializer

        JavaSessionSerializer getJavaSerializer()
        The value serializer that is guaranteed to be able to serialize/deserialize any Java object that implements Serializable.

        Ratpack extensions, libraries etc. should explicitly use this serializer (e.g. with SessionData.set(SessionKey, Object, SessionSerializer) when reading and writing to the session as it is not guaranteed that the default serializer relies on Java serialization.

        Returns:
        a serializer for Serializable objects
      • getDefaultSerializer

        SessionSerializer getDefaultSerializer()
        The serializer that is used when a serializer is not explicitly given.

        The default configuration of SessionModule configures this serializer to be the same as the Java serializer. However, if you'd prefer to use a different serialization strategy by default in your application you can override this binding.

        Returns:
        the serializer to use by default