summaryrefslogtreecommitdiff
path: root/src/com/p4square/session/Sessions.java
blob: 9f9dda089d2c7ad9fddf9efbce530a8e4695d377 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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);
    }

}