Class SessionManager
- All Implemented Interfaces:
Iterable<ComponentType>
,Channel
,ComponentType
,Eligible
,Manager
- Direct Known Subclasses:
InMemorySessionManager
A session manager associates
Request
events with a
for a Supplier<Optional<Session>>
Session
using Session.class
as association identifier
(see Session.from(org.jgrapes.core.Associator)
). Note that the Optional
will never by
empty. The return type has been chosen to be in accordance with
Associator.associatedGet(Class)
.
The Request
handler has a default priority of 1000.
Managers track requests using a cookie with a given name and path. The
path is a prefix that has to be matched by the request, often “/”.
If no cookie with the given name (see idName()
) is found,
a new cookie with that name and the specified path is created.
The cookie’s value is the unique session id that is used to lookup
the session object.
Session managers provide additional support for web sockets. If a
web socket is accepted, the session associated with the request
is automatically made available to the IOSubchannel
that
is subsequently used for the web socket events. This allows
handlers for web socket messages to access the session like
Request
handlers (see onProtocolSwitchAccepted(org.jgrapes.http.events.ProtocolSwitchAccepted, org.jgrapes.io.IOSubchannel)
).
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
The session manager information.static interface
An MBean interface for getting information about the established sessions.static interface
An MBean interface for getting information about all session managers.Nested classes/interfaces inherited from interface org.jgrapes.core.Channel
Channel.Default
-
Field Summary
-
Constructor Summary
ConstructorDescriptionCreates a new session manager with its channel set to itself and the path set to “/”.SessionManager
(String path) Creates a new session manager with its channel set to itself and the path set to the given path.SessionManager
(Channel componentChannel) Creates a new session manager with its channel set to the given channel and the path to “/”.SessionManager
(Channel componentChannel, String path) Creates a new session manager with the given channel and path.SessionManager
(Channel componentChannel, String pattern, int priority, String path) Creates a new session manager using the given channel and path. -
Method Summary
Modifier and TypeMethodDescriptionprotected String
addSessionCookie
(HttpResponse response, String sessionId) Creates a session id and adds the corresponding cookie to the response.protected abstract Session
createSession
(String sessionId) Creates a new session with the given id.protected static String
derivePattern
(String path) Derives the resource pattern from the path.protected boolean
hasTimedOut
(Session session) Checks if the absolute or idle timeout has been reached.idName()
The name used for the session id cookie.lookupSession
(String sessionId) Lookup the session with the given id.int
void
onDiscard
(DiscardSession event) Discards the given session.void
onProtocolSwitchAccepted
(ProtocolSwitchAccepted event, IOSubchannel channel) Associates the channel with a
for the session.Supplier<Optional<Session>>
void
onRequest
(Request.In event) Associates the event with aSession
object usingSession.class
as association identifier.path()
Returns the path.protected abstract void
removeSession
(String sessionId) Removes the given session from the cache.protected abstract int
Return the number of established sessions.setAbsoluteTimeout
(Duration timeout) Sets the absolute timeout for a session.setIdleTimeout
(Duration timeout) Sets the idle timeout for a session.setMaxSessions
(int maxSessions) Set the maximum number of sessions.protected void
setSessionSupplier
(Associator holder, String sessionId) Associated the associator with a session supplier for the given session id and notethis
as session manager.startDiscarding
(long absoluteTimeout, long idleTimeout) Start discarding all sessions (generateDiscardSession
events) that have reached their absolute or idle timeout.Methods inherited from class org.jgrapes.core.Component
channel, component, defaultCriterion, isEligibleFor, setName
Methods inherited from class org.jgrapes.core.internal.ComponentVertex
activeEventPipeline, addHandler, attach, channelReplacements, children, componentPath, componentVertex, detach, fire, initComponentsHandlers, iterator, name, newEventPipeline, newEventPipeline, parent, registerAsGenerator, root, toString, unregisterAsGenerator
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
SessionManager
public SessionManager()Creates a new session manager with its channel set to itself and the path set to “/”.The manager handles all
Request
events. -
SessionManager
Creates a new session manager with its channel set to itself and the path set to the given path.The manager handles all requests that match the given path, using the same rules as browsers do for selecting the cookies that are to be sent.
- Parameters:
path
- the path
-
SessionManager
Creates a new session manager with its channel set to the given channel and the path to “/”.The manager handles all
Request
events.- Parameters:
componentChannel
- the component channel
-
SessionManager
Creates a new session manager with the given channel and path.The manager handles all requests that match the given path, using the same rules as browsers do for selecting the cookies that are to be sent.
- Parameters:
componentChannel
- the component channelpath
- the path
-
SessionManager
Creates a new session manager using the given channel and path.The manager handles only requests that match the given pattern. The handler is registered with the given priority.
This constructor can be used if special handling of top level requests is needed.
- Parameters:
componentChannel
- the component channelpattern
- the path part of aResourcePattern
priority
- the prioritypath
- the path
-
-
Method Details
-
path
Returns the path.- Returns:
- the string
-
derivePattern
Derives the resource pattern from the path.- Parameters:
path
- the path- Returns:
- the pattern
-
idName
The name used for the session id cookie.Defaults to “
id
”.- Returns:
- the id name
-
setIdName
- Parameters:
idName
- the id name to set- Returns:
- the session manager for easy chaining
-
setMaxSessions
Set the maximum number of sessions.If the value is zero or less, an unlimited number of sessions is supported. The default value is 1000.
If adding a new session would exceed the limit, first all sessions older than
absoluteTimeout()
are removed. If this doesn’t free a slot, the least recently used session is removed.- Parameters:
maxSessions
- the maxSessions to set- Returns:
- the session manager for easy chaining
-
maxSessions
- Returns:
- the maxSessions
-
setAbsoluteTimeout
Sets the absolute timeout for a session.The absolute timeout is the time after which a session is invalidated (relative to its creation time). Defaults to 9 hours. Zero or less disables the timeout.
- Parameters:
timeout
- the absolute timeout- Returns:
- the session manager for easy chaining
-
absoluteTimeout
- Returns:
- the absolute session timeout (in seconds)
-
setIdleTimeout
Sets the idle timeout for a session.Defaults to 30 minutes. Zero or less disables the timeout.
- Parameters:
timeout
- the absolute timeout- Returns:
- the session manager for easy chaining
-
idleTimeout
- Returns:
- the idle timeout
-
onRequest
Associates the event with aSession
object usingSession.class
as association identifier.Does nothing if a session is already associated or the request has already been fulfilled.
- Parameters:
event
- the event
-
setSessionSupplier
Associated the associator with a session supplier for the given session id and notethis
as session manager.- Parameters:
holder
- the channelsessionId
- the session id
-
addSessionCookie
Creates a session id and adds the corresponding cookie to the response.- Parameters:
response
- the response- Returns:
- the session id
-
hasTimedOut
Checks if the absolute or idle timeout has been reached.- Parameters:
session
- the session- Returns:
- true, if successful
-
startDiscarding
Start discarding all sessions (generateDiscardSession
events) that have reached their absolute or idle timeout.Do not make the sessions unavailable yet.
Returns the time when the next timeout occurs. This method is called only if at least one of the timeouts has been specified.
Implementations have to take care that sessions are only discarded once. As they must remain available while the
DiscardSession
event is handled this may require marking them as being discarded.- Parameters:
absoluteTimeout
- the absolute timeoutidleTimeout
- the idle timeout- Returns:
- the next timeout (empty if no sessions left)
-
createSession
Creates a new session with the given id.- Parameters:
sessionId
-- Returns:
- the session
-
lookupSession
Lookup the session with the given id.Lookup will fail if the session has timed out.
- Parameters:
sessionId
-- Returns:
- the session
-
removeSession
Removes the given session from the cache.- Parameters:
sessionId
- the session id
-
sessionCount
Return the number of established sessions.- Returns:
- the result
-
onDiscard
Discards the given session.The handler has a priority of -1000, thus allowing other handler to make use of the session (for a time) before it becomes unavailable.
- Parameters:
event
- the event
-
onProtocolSwitchAccepted
@Handler(priority=1000) public void onProtocolSwitchAccepted(ProtocolSwitchAccepted event, IOSubchannel channel) Associates the channel with a
for the session.Supplier<Optional<Session>>
Initially, the associated session is the session associated with the protocol switch event. If this session times out, a new session is returned as a fallback, thus making sure that the
Optional
is never empty. The new session is, however, created independently of any new session created byonRequest(org.jgrapes.http.events.Request.In)
.Applications should avoid any ambiguity by executing a proper cleanup of the web application in response to a
DiscardSession
event (including reestablishing the web socket connections from new requests).- Parameters:
event
- the eventchannel
- the channel
-