Package ej.hoka.session
Class SessionHandler
- java.lang.Object
-
- ej.hoka.session.SessionHandler
-
public class SessionHandler extends Object
Handle sessions and stores active sessions in a database.
-
-
Constructor Summary
Constructors Constructor Description SessionHandler(Random random)Constructs aSessionHandlerwith 1-hour-long sessions.SessionHandler(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 StringgenerateSessionID()Generates a new session ID encoded in base64.SessiongetSession(String sessionID)Gets the session associated to the given ID, if any.SessionnewSession()Creates a new session.voidrefresh(String sessionID)Refreshes the expiration date of the session identified bysessionID.booleanremoveSession(String sessionID)Removes the session identified bysessionIDfrom the active sessions.
-
-
-
Constructor Detail
-
SessionHandler
public SessionHandler(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(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(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(String sessionID)
Refreshes the expiration date of the session identified bysessionID.- Parameters:
sessionID- the identifier of the session.
-
removeSession
public boolean removeSession(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 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()
-
-