Class SessionHandler


  • public class SessionHandler
    extends Object
    Handle sessions and stores active sessions in a database.
    • Constructor Detail

      • SessionHandler

        public SessionHandler​(Random random)
        Constructs a SessionHandler with 1-hour-long sessions. Use a secure Random implementation (see java.security.SecureRandom).
        Parameters:
        random - the random number generator used to create session IDs.
      • SessionHandler

        public SessionHandler​(Random random,
                              long sessionLifetime)
        Constructs a SessionHandler provided sessionLifetime sessions and using an in-memory database. Use a secure Random implementation (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 null if 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 by sessionID.
        Parameters:
        sessionID - the identifier of the session.
      • removeSession

        public boolean removeSession​(String sessionID)
        Removes the session identified by sessionID from the active sessions.
        Parameters:
        sessionID - the identifier of the session.
        Returns:
        false if no sessions are referenced by sessionID, true otherwise
      • 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()