1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.danube.http.session;
14
15 import java.util.Arrays;
16 import java.util.HashMap;
17 import java.util.Map;
18 import java.util.Random;
19
20 import org.abstracthorizon.danube.http.HTTPConnection;
21 import org.abstracthorizon.danube.http.cookie.Cookie;
22 import org.abstracthorizon.danube.http.cookie.CookieUtilities;
23
24
25
26
27
28
29
30 public class SimpleCookieSessionManager implements HTTPSessionManager {
31
32
33 public static final String SESSION_COOKIE_NAME = "JSESSIONID";
34
35
36 public static final String SESSION_ATTRIBUTE = "org.abstracthorizon.danube.http.session.SimpleSessionManager";
37
38
39 public static final int DEFAULT_SESSION_TIMEOUT = 30 * 60 * 1000;
40
41
42 protected Map<Object, Session> sessions = new HashMap<Object, Session>();
43
44
45 protected String sessionCookieName = SESSION_COOKIE_NAME;
46
47
48 protected static Random random = new Random();
49
50
51
52
53
54
55 public Object findSession(HTTPConnection connection, boolean create) {
56 Session session = (Session)connection.getAttributes().get(SESSION_ATTRIBUTE);
57 if (session != null) {
58 return session;
59 }
60
61 Map<String, Cookie> cookies = CookieUtilities.getRequestCookies(connection);
62 Cookie cookie = cookies.get(sessionCookieName);
63 if (cookie == null) {
64 Map<String, Cookie> map = CookieUtilities.getResponseCookies(connection);
65 if (map != null) {
66 cookie = map.get(sessionCookieName);
67 }
68 }
69
70 String sessionId = null;
71 if (cookie != null) {
72 sessionId = cookie.getValue();
73 }
74 if (sessionId != null) {
75 sessionId = validateSessionId(sessionId);
76 }
77 if (create && (sessionId == null)) {
78 sessionId = createSessionId();
79 Cookie newSessionCookie = new Cookie();
80 newSessionCookie.setName(sessionCookieName);
81 newSessionCookie.setValue(sessionId);
82 newSessionCookie.setPath(connection.getContextPath());
83 CookieUtilities.addResponseCookies(connection, Arrays.asList(newSessionCookie));
84 }
85 if (sessionId != null) {
86 session = (Session)findSession(sessionId, create);
87 connection.getAttributes().put(SESSION_ATTRIBUTE, session);
88 return session;
89 } else {
90 return null;
91 }
92 }
93
94
95
96
97
98
99 public Object findSession(Object sessionId, boolean create) {
100 synchronized (sessions) {
101 Session session = sessions.get(sessionId);
102 if ((session == null) && create) {
103 session = createSession(sessionId.toString());
104 sessions.put(sessionId, session);
105 }
106 return session;
107 }
108 }
109
110
111
112
113
114
115 public void removeSession(HTTPConnection connection) {
116 Session session = (Session)findSession(connection, false);
117 if (session != null) {
118 removeSession(session);
119 connection.getAttributes().remove(SESSION_ATTRIBUTE);
120 }
121 }
122
123
124
125
126
127 public void removeSession(Object session) {
128 synchronized (sessions) {
129 sessions.remove(session);
130 }
131 }
132
133
134
135
136
137 public String rewriteURL(HTTPConnection connection, String url) {
138 return url;
139 }
140
141
142
143
144
145
146 protected String validateSessionId(String sessionId) {
147 return sessionId;
148 }
149
150
151
152
153
154 protected String createSessionId() {
155 long l = random.nextLong();
156 if (l < 0) {
157 l = -l;
158 }
159 String sessionId = Long.toString(l);
160 return sessionId;
161 }
162
163
164
165
166
167
168 protected Session createSession(String sessionId) {
169 return new Session(sessionId);
170 }
171
172
173
174
175
176 public String getSessionCookieName() {
177 return sessionCookieName;
178 }
179
180
181
182
183
184 public void setSessionCookieName(String sessionCookieName) {
185 this.sessionCookieName = sessionCookieName;
186 }
187 }