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> Optional<T> |
get(Class<T> type)
Read the object with the given type, and no name, using the
default serializer . |
default <T> Optional<T> |
get(Class<T> type,
SessionSerializer serializer)
Read the object with the given type, and no name.
|
default <T> Optional<T> |
get(SessionKey<T> key)
Fetch the object with the given key, using the
default serializer . |
<T> Optional<T> |
get(SessionKey<T> key,
SessionSerializer serializer)
Read the object with the given key.
|
default Optional<?> |
get(String name)
Read the object with the given name, using the
default serializer . |
default Optional<?> |
get(String name,
SessionSerializer serializer)
Read the object with the given name.
|
default SessionSerializer |
getDefaultSerializer()
|
default JavaSessionSerializer |
getJavaSerializer()
|
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(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(String name)
Removes the object with the name, if it exists.
|
default <T> T |
require(Class<T> type)
Like
get(Class) , but throws NoSuchElementException on the absence of a value. |
default <T> T |
require(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 Object |
require(String name)
Like
get(String) , but throws NoSuchElementException on the absence of a value. |
default Object |
require(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(Class<T> type,
T value)
Sets the value for the given type, using the
default serializer . |
default <T> void |
set(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(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(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() . |
Set<SessionKey<?>> getKeys()
default <T> Optional<T> get(SessionKey<T> key) throws Exception
default serializer
.T
- the type of objectkey
- the keyException
require(SessionKey)
<T> Optional<T> get(SessionKey<T> key, SessionSerializer serializer) throws Exception
T
- the type of objectkey
- the keyserializer
- the serializerException
require(SessionKey, SessionSerializer)
default Optional<?> get(String name) throws 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 nameException
require(String)
default Optional<?> get(String name, SessionSerializer serializer) throws 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 serializerException
require(String, SessionSerializer)
default <T> Optional<T> get(Class<T> type) throws Exception
default serializer
.T
- the typetype
- the typeException
require(Class)
default <T> Optional<T> get(Class<T> type, SessionSerializer serializer) throws Exception
T
- the typetype
- the typeserializer
- the serializerException
require(Class, SessionSerializer)
default <T> T require(SessionKey<T> key) throws Exception
get(SessionKey)
, but throws NoSuchElementException
on the absence of a value.T
- the typekey
- the object keyException
default <T> T require(SessionKey<T> key, SessionSerializer serializer) throws Exception
get(SessionKey, SessionSerializer)
, but throws NoSuchElementException
on the absence of a value.T
- the typekey
- the object keyserializer
- the serializerException
default <T> T require(Class<T> type) throws Exception
get(Class)
, but throws NoSuchElementException
on the absence of a value.T
- the typetype
- the typeException
default <T> T require(Class<T> type, SessionSerializer serializer) throws Exception
get(Class, SessionSerializer)
, but throws NoSuchElementException
on the absence of a value.T
- the typetype
- the typeserializer
- the serializerException
default Object require(String name) throws 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 nameException
default Object require(String name, SessionSerializer serializer) throws 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 serializerException
default <T> void set(SessionKey<T> key, T value) throws Exception
default serializer
.T
- the typekey
- the keyvalue
- the valueException
<T> void set(SessionKey<T> key, T value, SessionSerializer serializer) throws Exception
T
- the typekey
- the keyvalue
- the valueserializer
- the serializerException
default <T> void set(Class<T> type, T value) throws Exception
default serializer
.T
- the typetype
- the typevalue
- the valueException
default <T> void set(Class<T> type, T value, SessionSerializer serializer) throws Exception
T
- the typetype
- the typevalue
- the valueserializer
- the serializerException
default <T> void set(String name, T value) throws Exception
default serializer
.T
- the typename
- the namevalue
- the valueException
default <T> void set(String name, T value, SessionSerializer serializer) throws Exception
T
- the typename
- the namevalue
- the valueserializer
- the serializerException
default <T> void set(T value) throws Exception
default serializer
.T
- the typevalue
- the valueException
default <T> void set(T value, SessionSerializer serializer) throws Exception
T
- the typevalue
- the valueserializer
- the serializerException
void remove(SessionKey<?> key)
key
- the keydefault void remove(Class<?> type)
type
- the typedefault void remove(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