summaryrefslogtreecommitdiff
path: root/src/main/java/com/p4square/session/Sessions.java
diff options
context:
space:
mode:
authorJesse Morgan <jesse@jesterpm.net>2016-04-09 15:53:24 -0700
committerJesse Morgan <jesse@jesterpm.net>2016-04-09 15:53:24 -0700
commit371ccae3d1f31ec38f4af77fb7fcd175d49b3cd5 (patch)
tree38c4f1e8828f9af9c4b77a173bee0d312b321698 /src/main/java/com/p4square/session/Sessions.java
parentbbf907e51dfcf157bdee24dead1d531122aa25db (diff)
parent3102d8bce3426d9cf41aeaf201c360d342677770 (diff)
Merge pull request #10 from PuyallupFoursquare/maven
Switching from Ivy+Ant to Maven.
Diffstat (limited to 'src/main/java/com/p4square/session/Sessions.java')
-rw-r--r--src/main/java/com/p4square/session/Sessions.java155
1 files changed, 155 insertions, 0 deletions
diff --git a/src/main/java/com/p4square/session/Sessions.java b/src/main/java/com/p4square/session/Sessions.java
new file mode 100644
index 0000000..9f9dda0
--- /dev/null
+++ b/src/main/java/com/p4square/session/Sessions.java
@@ -0,0 +1,155 @@
+/*
+ * Copyright 2013 Jesse Morgan
+ */
+
+package com.p4square.session;
+
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.Map;
+import java.util.Timer;
+import java.util.TimerTask;
+
+import org.restlet.Response;
+import org.restlet.Request;
+import org.restlet.data.CookieSetting;
+import org.restlet.security.User;
+
+/**
+ * Singleton Session Manager.
+ *
+ * @author Jesse Morgan <jesse@jesterpm.net>
+ */
+public class Sessions {
+ private static final String COOKIE_NAME = "S";
+ private static final int DELETE = 0;
+
+ private static final Sessions THE = new Sessions();
+ public static Sessions getInstance() {
+ return THE;
+ }
+
+ private final Map<String, Session> mSessions;
+ private final Timer mCleanupTimer;
+
+ private Sessions() {
+ mSessions = new ConcurrentHashMap<String, Session>();
+
+ mCleanupTimer = new Timer("sessionCleaner", true);
+ mCleanupTimer.scheduleAtFixedRate(new TimerTask() {
+ @Override
+ public void run() {
+ for (Session s : mSessions.values()) {
+ if (s.isExpired()) {
+ mSessions.remove(s.getId());
+ }
+ }
+ }
+ }, Session.LIFETIME, Session.LIFETIME);
+ }
+
+ /**
+ * Get a session by ID.
+ *
+ * @param sessionid
+ * The Session id
+ * @return The Session if found and not expired, null otherwise.
+ */
+ public Session get(String sessionid) {
+ Session s = mSessions.get(sessionid);
+
+ if (s != null && !s.isExpired()) {
+ s.touch();
+ return s;
+ }
+
+ return null;
+ }
+
+ /**
+ * Get the Session associated with the Request.
+ *
+ * @param request
+ * The request to fetch a session for.
+ * @return A session or null if no session is found.
+ */
+ public Session get(Request request) {
+ final String cookie = request.getCookies().getFirstValue(COOKIE_NAME);
+
+ if (cookie != null) {
+ return get(cookie);
+ }
+
+ return null;
+ }
+
+ /**
+ * Create a new Session for the given User object.
+ *
+ * @param user
+ * The User to associate with the Session.
+ * @return The new Session object.
+ */
+ public Session create(User user) {
+ if (user == null) {
+ throw new IllegalArgumentException("Can not create session for null user.");
+ }
+
+ Session s = new Session(user);
+ mSessions.put(s.getId(), s);
+
+ return s;
+ }
+
+ /**
+ * Delete a Session.
+ *
+ * @param sessionid
+ * The id of the Session to remove.
+ */
+ public void delete(String sessionid) {
+ mSessions.remove(sessionid);
+ }
+
+ /**
+ * Create a new Session and add the Session cookie to the response.
+ *
+ * @param request
+ * The request to create the Session for.
+ * @param response
+ * The response to add the session cookie to.
+ * @return The new Session.
+ */
+ public Session create(Request request, Response response) {
+ Session s = create(request.getClientInfo().getUser());
+
+ CookieSetting cookie = new CookieSetting(COOKIE_NAME, s.getId());
+ cookie.setPath("/");
+
+ request.getCookies().add(cookie);
+ response.getCookieSettings().add(cookie);
+
+ return s;
+ }
+
+ /**
+ * Remove a Session and delete the cookies.
+ *
+ * @param request
+ * The request with the session cookie to remove
+ * @param response
+ * The response to remove the session cookie from.
+ */
+ public void delete(Request request, Response response) {
+ final String sessionid = request.getCookies().getFirstValue(COOKIE_NAME);
+
+ delete(sessionid);
+
+ CookieSetting cookie = new CookieSetting(COOKIE_NAME, "");
+ cookie.setPath("/");
+ cookie.setMaxAge(DELETE);
+
+ request.getCookies().add(cookie);
+ response.getCookieSettings().add(cookie);
+ }
+
+}