Class SessionHandler


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

      Constructors 
      Constructor Description
      SessionHandler​(java.util.Random random)
      Constructs a SessionHandler with 1-hour-long sessions.
      SessionHandler​(java.util.Random random, long sessionLifetime)
      Constructs a SessionHandler provided sessionLifetime sessions and using an in-memory database.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected long generateExpiration()
      Generates the expiration date using the current real time.
      protected java.lang.String generateSessionID()
      Generates a new session ID encoded in base64.
      Session getSession​(java.lang.String sessionID)
      Gets the session associated to the given ID, if any.
      Session newSession()
      Creates a new session.
      void refresh​(java.lang.String sessionID)
      Refreshes the expiration date of the session identified by sessionID.
      boolean removeSession​(java.lang.String sessionID)
      Removes the session identified by sessionID from the active sessions.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • SessionHandler

        public SessionHandler​(java.util.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​(java.util.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​(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 null if 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 by sessionID.
        Parameters:
        sessionID - the identifier of the session.
      • removeSession

        public boolean removeSession​(java.lang.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 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()