Class Sessions

java.lang.Object
org.attribyte.snook.session.Sessions
All Implemented Interfaces:
com.codahale.metrics.Metric, com.codahale.metrics.MetricSet
Direct Known Subclasses:
InMemorySessions

public abstract class Sessions extends Object implements com.codahale.metrics.MetricSet
Manages sessions.
  • Field Details

    • DEFAULT_SAME_SITE_OPTION

      public static final Cookies.SameSiteOption DEFAULT_SAME_SITE_OPTION
      The default same-site option for the session cookie ().
    • cookieKey

      public final Cookies.CookieKey cookieKey
      The session cookie key.
    • cookieOptions

      public final com.google.common.collect.ImmutableSet<Cookies.Option> cookieOptions
      The cookie options.
    • sameSiteOption

      public final Cookies.SameSiteOption sameSiteOption
      The same-site cookie option.
  • Constructor Details

    • Sessions

      protected Sessions(Cookies.CookieKey cookieKey, EnumSet<Cookies.Option> cookieOptions, Cookies.SameSiteOption sameSiteOption, int maxAgeSeconds, int cleanIntervalSeconds)
      Creates a sessions store.
      Parameters:
      cookieKey - The key for the session cookie.
      cookieOptions - The cookie options when setting the session cookie.
      sameSiteOption - The same-site option for the session cookie.
      maxAgeSeconds - The maximum cookie age in seconds.
      cleanIntervalSeconds - The cookie clean interval in seconds. If < 1, no cleaning is scheduled.
  • Method Details

    • session

      public Optional<Session> session(jakarta.servlet.http.HttpServletRequest req)
      Gets the session for a request.
      Parameters:
      req - The request.
      Returns:
      The session or empty if none.
    • sessionOrNew

      public Session sessionOrNew(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse resp)
      Gets the session for a request. If none, creates a new (empty) session and sets a response cookie for the session.
      Parameters:
      req - The request.
      resp - The response.
      Returns:
      The existing or new session.
    • sessionOrNew

      public Session sessionOrNew(jakarta.servlet.http.HttpServletRequest req, Map<String,Object> data, jakarta.servlet.http.HttpServletResponse resp)
      Gets the session for a request. If none, creates a new session with data and sets a response cookie for the session.
      Parameters:
      req - The request.
      data - Session data.
      resp - The response.
      Returns:
      The existing or new session.
    • shutdown

      public void shutdown()
      Shutdown sessions.
    • get

      protected abstract Optional<Session> get(com.google.common.hash.HashCode token)
      Gets a session for a token.
      Parameters:
      token - The token.
      Returns:
      The session or empty if none.
    • save

      protected abstract boolean save(Session session)
      Saves a session.
      Parameters:
      session - The session.
      Returns:
      Was the session saved?
    • clearExpired

      protected abstract int clearExpired(int maxAgeSeconds)
      Clear expired tokens.
      Parameters:
      maxAgeSeconds - The maximum session age in seconds.
      Returns:
      The number of expired tokens.
    • getMetrics

      public final Map<String,com.codahale.metrics.Metric> getMetrics()
      Specified by:
      getMetrics in interface com.codahale.metrics.MetricSet