public class DefaultWebSession extends java.lang.Object implements ConfigurableWebSession, java.io.Serializable
WebSession
.Modifier and Type | Class and Description |
---|---|
private static class |
DefaultWebSession.State |
Modifier and Type | Field and Description |
---|---|
private java.util.Map<java.lang.String,java.lang.Object> |
attributes |
private java.time.Clock |
clock |
private java.time.Instant |
creationTime |
private java.lang.String |
id |
private java.time.Instant |
lastAccessTime |
private java.time.Duration |
maxIdleTime |
private java.util.function.Supplier<<any>> |
saveOperation |
private static long |
serialVersionUID |
private java.util.concurrent.atomic.AtomicReference<DefaultWebSession.State> |
state |
Constructor and Description |
---|
DefaultWebSession(java.lang.String id,
java.time.Clock clock)
Constructor to create a new session.
|
DefaultWebSession(java.lang.String id,
java.util.Map<java.lang.String,java.lang.Object> attributes,
java.time.Clock clock,
java.time.Instant creationTime,
java.time.Instant lastAccessTime,
java.time.Duration maxIdleTime)
Constructor to load existing session.
|
Modifier and Type | Method and Description |
---|---|
<T> java.util.Optional<T> |
getAttribute(java.lang.String name)
Return the attribute value if present.
|
java.util.Map<java.lang.String,java.lang.Object> |
getAttributes()
Return a map that holds session attributes.
|
java.time.Instant |
getCreationTime()
Return the time when the session was created.
|
java.lang.String |
getId()
Return a unique session identifier.
|
java.time.Instant |
getLastAccessTime()
Return the last time of session access as a result of user activity such
as an HTTP request.
|
java.time.Duration |
getMaxIdleTime()
Return the maximum time after the
lastAccessTime before a session expires. |
protected java.util.function.Supplier<<any>> |
getSaveOperation() |
boolean |
isExpired()
Return
true if the session expired after maxIdleTime elapsed. |
boolean |
isStarted()
Whether a session with the client has been started explicitly via
WebSession.start() or implicitly by adding session attributes. |
<any> |
save()
Save the session persisting attributes (e.g.
|
void |
setLastAccessTime(java.time.Instant lastAccessTime)
Update the last access time for user-related session activity.
|
void |
setMaxIdleTime(java.time.Duration maxIdleTime)
By default this is set to 30 minutes.
|
void |
setSaveOperation(java.util.function.Supplier<<any>> saveOperation)
Set the operation to invoke when
WebSession.save() is invoked. |
void |
start()
Force the creation of a session causing the session id to be sent when
WebSession.save() is called. |
private static final long serialVersionUID
private final java.lang.String id
private final java.util.Map<java.lang.String,java.lang.Object> attributes
private final java.time.Clock clock
private final java.time.Instant creationTime
private volatile java.time.Instant lastAccessTime
private volatile java.time.Duration maxIdleTime
private java.util.concurrent.atomic.AtomicReference<DefaultWebSession.State> state
private transient volatile java.util.function.Supplier<<any>> saveOperation
public DefaultWebSession(java.lang.String id, java.time.Clock clock)
id
- the session idclock
- for access to current timepublic DefaultWebSession(java.lang.String id, java.util.Map<java.lang.String,java.lang.Object> attributes, java.time.Clock clock, java.time.Instant creationTime, java.time.Instant lastAccessTime, java.time.Duration maxIdleTime)
id
- the session idattributes
- the attributes of the sessionclock
- for access to current timecreationTime
- the creation timelastAccessTime
- the last access timemaxIdleTime
- the configured maximum session idle timepublic java.lang.String getId()
WebSession
getId
in interface WebSession
public java.util.Map<java.lang.String,java.lang.Object> getAttributes()
WebSession
getAttributes
in interface WebSession
public <T> java.util.Optional<T> getAttribute(java.lang.String name)
WebSession
getAttribute
in interface WebSession
T
- the attribute typename
- the attribute namepublic java.time.Instant getCreationTime()
WebSession
getCreationTime
in interface WebSession
public void setLastAccessTime(java.time.Instant lastAccessTime)
ConfigurableWebSession
setLastAccessTime
in interface ConfigurableWebSession
lastAccessTime
- the time of accesspublic java.time.Instant getLastAccessTime()
WebSession
maxIdleTimeInSeconds
this helps to determine when a session is
expired
.getLastAccessTime
in interface WebSession
public void setMaxIdleTime(java.time.Duration maxIdleTime)
By default this is set to 30 minutes.
setMaxIdleTime
in interface WebSession
maxIdleTime
- the max idle timepublic java.time.Duration getMaxIdleTime()
WebSession
lastAccessTime
before a session expires. A negative time indicates the
session doesn't expire.getMaxIdleTime
in interface WebSession
public void setSaveOperation(java.util.function.Supplier<<any>> saveOperation)
ConfigurableWebSession
WebSession.save()
is invoked.setSaveOperation
in interface ConfigurableWebSession
saveOperation
- the save operationprotected java.util.function.Supplier<<any>> getSaveOperation()
public void start()
WebSession
WebSession.save()
is called.start
in interface WebSession
public boolean isStarted()
WebSession
WebSession.start()
or implicitly by adding session attributes.
If "false" then the session id is not sent to the client and the
WebSession.save()
method is essentially a no-op.isStarted
in interface WebSession
public <any> save()
WebSession
Note that a session must be started explicitly via WebSession.start()
or
implicitly by adding attributes or otherwise this method has no effect.
save
in interface WebSession
Mono
to indicate completion with success or error
Typically this method should be automatically invoked just before the response is committed so applications don't have to by default.
public boolean isExpired()
WebSession
true
if the session expired after maxIdleTime
elapsed.
Typically expiration checks should be automatically made when a session
is accessed, a new WebSession
instance created if necessary, at
the start of request processing so that applications don't have to worry
about expired session by default.
isExpired
in interface WebSession