Package ej.hoka.session
Class SessionHandler
- java.lang.Object
-
- ej.hoka.session.SessionHandler
-
public class SessionHandler extends java.lang.ObjectHandle sessions and stores active sessions in a database.
-
-
Constructor Summary
Constructors Constructor Description SessionHandler(java.util.Random random)Constructs aSessionHandlerwith 1-hour-long sessions.SessionHandler(java.util.Random random, long sessionLifetime)Constructs aSessionHandlerprovided sessionLifetime sessions and using an in-memory database.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected longgenerateExpiration()Generates the expiration date using the current real time.protected java.lang.StringgenerateSessionID()Generates a new session ID encoded in base64.SessiongetSession(java.lang.String sessionID)Gets the session associated to the given ID, if any.SessionnewSession()Creates a new session.voidrefresh(java.lang.String sessionID)Refreshes the expiration date of the session identified bysessionID.booleanremoveSession(java.lang.String sessionID)Removes the session identified bysessionIDfrom the active sessions.
-
-
-
Constructor Detail
-
SessionHandler
public SessionHandler(java.util.Random random)
Constructs aSessionHandlerwith 1-hour-long sessions. Use a secureRandomimplementation (see java.security.SecureRandom).- Parameters:
random- the random number generator used to create session IDs.
-
SessionHandler
public SessionHandler(java.util.Random random, long sessionLifetime)Constructs aSessionHandlerprovided sessionLifetime sessions and using an in-memory database. Use a secureRandomimplementation (see java.security.SecureRandom).- Parameters:
random- the random number generator used to create session IDs.sessionLifetime- the time before a session is considered invalid in seconds.
-
-
Method Detail
-
newSession
public Session newSession()
Creates a new session.- Returns:
- the generated session ID.
-
getSession
@Nullable public Session getSession(java.lang.String sessionID)
Gets the session associated to the given ID, if any.- Parameters:
sessionID- session id- Returns:
- session with the provided ID, or
nullif no session has been found for this ID or if the found session has expired.
-
refresh
public void refresh(java.lang.String sessionID)
Refreshes the expiration date of the session identified bysessionID.- Parameters:
sessionID- the identifier of the session.
-
removeSession
public boolean removeSession(java.lang.String sessionID)
Removes the session identified bysessionIDfrom the active sessions.- Parameters:
sessionID- the identifier of the session.- Returns:
falseif no sessions are referenced bysessionID,trueotherwise
-
generateSessionID
protected java.lang.String generateSessionID()
Generates a new session ID encoded in base64.- Returns:
- the generated session ID.
-
generateExpiration
protected long generateExpiration()
Generates the expiration date using the current real time.- Returns:
- the generated expiration date.
- See Also:
System.currentTimeMillis()
-
-