public interface SessionData
The session data can be obtained via the Session.getData()
method.
Every item is stored with a SessionKey
.
A key is comprised of a type (as a Class
) and an optional name.
The combination of type and name identifies the value.
Two keys with differing types but identical names are not considered to be equivalent.
When writing session data, the values are serialized immediately. That is, all objects are treated as immutable value objects from the perspective of this object. Any changes made to an object after it has been written to this object will NOT be persisted. If such changes are to be persisted, the object must be written again.
If a SessionSerializer
is not provided for a given get/set, the getDefaultSerializer()
will be used.
The SessionModule
provides a default implementation based on Java serialization.
An alternative implementation can be used by overriding the Guice binding for SessionSerializer
.
The session data is held in memory for a given request until the Session.save()
method is called on the corresponding session.
However, this object internally holds the data in serialized form.
Therefore, every read and write incurs the cost of serialization and deserialization.
Modifier and Type | Method and Description |
---|---|
void |
clear()
Remove all entries from the session data.
|
default <T> java.util.Optional<T> |
get(java.lang.Class<T> type)
Read the object with the given type, and no name, using the
default serializer . |
default <T> java.util.Optional<T> |
get(java.lang.Class<T> type,
SessionSerializer serializer)
Read the object with the given type, and no name.
|
default <T> java.util.Optional<T> |
get(SessionKey<T> key)
Fetch the object with the given key, using the
default serializer . |
<T> java.util.Optional<T> |
get(SessionKey<T> key,
SessionSerializer serializer)
Read the object with the given key.
|
default java.util.Optional<?> |
get(java.lang.String name)
Read the object with the given name, using the
default serializer . |
default java.util.Optional<?> |
get(java.lang.String name,
SessionSerializer serializer)
Read the object with the given name.
|
default SessionSerializer |
getDefaultSerializer()
|
default JavaSessionSerializer |
getJavaSerializer()
|
java.util.Set<SessionKey<?>> |
getKeys()
The keys of all objects currently in the session.
|
Session |
getSession()
The corresponding
Session object. |
default boolean |
isDirty()
See
Session.isDirty() . |
default void |
remove(java.lang.Class<?> type)
Removes the object with the given type and no name, if it exists.
|
void |
remove(SessionKey<?> key)
Removes the object with the given key, if it exists.
|
default void |
remove(java.lang.String name)
Removes the object with the name, if it exists.
|
default <T> T |
require(java.lang.Class<T> type)
Like
get(Class) , but throws NoSuchElementException on the absence of a value. |
default <T> T |
require(java.lang.Class<T> type,
SessionSerializer serializer)
Like
get(Class, SessionSerializer) , but throws NoSuchElementException on the absence of a value. |
default <T> T |
require(SessionKey<T> key)
Like
get(SessionKey) , but throws NoSuchElementException on the absence of a value. |
default <T> T |
require(SessionKey<T> key,
SessionSerializer serializer)
Like
get(SessionKey, SessionSerializer) , but throws NoSuchElementException on the absence of a value. |
default java.lang.Object |
require(java.lang.String name)
Like
get(String) , but throws NoSuchElementException on the absence of a value. |
default java.lang.Object |
require(java.lang.String name,
SessionSerializer serializer)
Like
get(String, SessionSerializer) , but throws NoSuchElementException on the absence of a value. |
default Operation |
save()
See
Session.save() . |
default <T> void |
set(java.lang.Class<T> type,
T value)
Sets the value for the given type, using the
default serializer . |
default <T> void |
set(java.lang.Class<T> type,
T value,
SessionSerializer serializer)
Sets the value for the given type.
|
default <T> void |
set(SessionKey<T> key,
T value)
Sets the value for the given key, using the
default serializer . |
<T> void |
set(SessionKey<T> key,
T value,
SessionSerializer serializer)
Sets the value for the given key.
|
default <T> void |
set(java.lang.String name,
T value)
Sets the value for the given name and type, using the runtime type of the value and the
default serializer . |
default <T> void |
set(java.lang.String name,
T value,
SessionSerializer serializer)
Sets the value for the given name and type, using the runtime type of the value.
|
default <T> void |
set(T value)
Sets the value for the type, using the runtime type of the value and the
default serializer . |
default <T> void |
set(T value,
SessionSerializer serializer)
Sets the value for the type, using the runtime type of the value.
|
default Operation |
terminate()
See
Session.terminate() . |
java.util.Set<SessionKey<?>> getKeys()
default <T> java.util.Optional<T> get(SessionKey<T> key) throws java.lang.Exception
default serializer
.T
- the type of objectkey
- the keyjava.lang.Exception
require(SessionKey)
<T> java.util.Optional<T> get(SessionKey<T> key, SessionSerializer serializer) throws java.lang.Exception
T
- the type of objectkey
- the keyserializer
- the serializerjava.lang.Exception
require(SessionKey, SessionSerializer)
default java.util.Optional<?> get(java.lang.String name) throws java.lang.Exception
default serializer
.
This method will throw an IllegalArgumentException
if there is more than one object who's key has the given name.
name
- the object namejava.lang.Exception
require(String)
default java.util.Optional<?> get(java.lang.String name, SessionSerializer serializer) throws java.lang.Exception
This method will throw an IllegalArgumentException
if there is more than one object who's key has the given name.
name
- the object nameserializer
- the serializerjava.lang.Exception
require(String, SessionSerializer)
default <T> java.util.Optional<T> get(java.lang.Class<T> type) throws java.lang.Exception
default serializer
.T
- the typetype
- the typejava.lang.Exception
require(Class)
default <T> java.util.Optional<T> get(java.lang.Class<T> type, SessionSerializer serializer) throws java.lang.Exception
T
- the typetype
- the typeserializer
- the serializerjava.lang.Exception
require(Class, SessionSerializer)
default <T> T require(SessionKey<T> key) throws java.lang.Exception
get(SessionKey)
, but throws NoSuchElementException
on the absence of a value.T
- the typekey
- the object keyjava.lang.Exception
default <T> T require(SessionKey<T> key, SessionSerializer serializer) throws java.lang.Exception
get(SessionKey, SessionSerializer)
, but throws NoSuchElementException
on the absence of a value.T
- the typekey
- the object keyserializer
- the serializerjava.lang.Exception
default <T> T require(java.lang.Class<T> type) throws java.lang.Exception
get(Class)
, but throws NoSuchElementException
on the absence of a value.T
- the typetype
- the typejava.lang.Exception
default <T> T require(java.lang.Class<T> type, SessionSerializer serializer) throws java.lang.Exception
get(Class, SessionSerializer)
, but throws NoSuchElementException
on the absence of a value.T
- the typetype
- the typeserializer
- the serializerjava.lang.Exception
default java.lang.Object require(java.lang.String name) throws java.lang.Exception
get(String)
, but throws NoSuchElementException
on the absence of a value.
This method will throw an IllegalArgumentException
if there is more than one object who's key has the given name.
name
- the object namejava.lang.Exception
default java.lang.Object require(java.lang.String name, SessionSerializer serializer) throws java.lang.Exception
get(String, SessionSerializer)
, but throws NoSuchElementException
on the absence of a value.
This method will throw an IllegalArgumentException
if there is more than one object who's key has the given name.
name
- the object nameserializer
- the serializerjava.lang.Exception
default <T> void set(SessionKey<T> key, T value) throws java.lang.Exception
default serializer
.T
- the typekey
- the keyvalue
- the valuejava.lang.Exception
<T> void set(SessionKey<T> key, T value, SessionSerializer serializer) throws java.lang.Exception
T
- the typekey
- the keyvalue
- the valueserializer
- the serializerjava.lang.Exception
default <T> void set(java.lang.Class<T> type, T value) throws java.lang.Exception
default serializer
.T
- the typetype
- the typevalue
- the valuejava.lang.Exception
default <T> void set(java.lang.Class<T> type, T value, SessionSerializer serializer) throws java.lang.Exception
T
- the typetype
- the typevalue
- the valueserializer
- the serializerjava.lang.Exception
default <T> void set(java.lang.String name, T value) throws java.lang.Exception
default serializer
.T
- the typename
- the namevalue
- the valuejava.lang.Exception
default <T> void set(java.lang.String name, T value, SessionSerializer serializer) throws java.lang.Exception
T
- the typename
- the namevalue
- the valueserializer
- the serializerjava.lang.Exception
default <T> void set(T value) throws java.lang.Exception
default serializer
.T
- the typevalue
- the valuejava.lang.Exception
default <T> void set(T value, SessionSerializer serializer) throws java.lang.Exception
T
- the typevalue
- the valueserializer
- the serializerjava.lang.Exception
void remove(SessionKey<?> key)
key
- the keydefault void remove(java.lang.Class<?> type)
type
- the typedefault void remove(java.lang.String name)
This method will throw a NoSuchElementException
if there are more than one entries with the given name.
name
- the namevoid clear()
default boolean isDirty()
Session.isDirty()
.default Operation save()
Session.save()
.default Operation terminate()
Session.terminate()
.default SessionSerializer getDefaultSerializer()
default JavaSessionSerializer getJavaSerializer()
Serializable
objects