View Javadoc

1   /*
2    * Copyright (c) 2005-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
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   * Simple session manager implementation that uses cookies for storing
26   * session id on the client side. Sessions are not expired.
27   *
28   * @author Daniel Sendula
29   */
30  public class SimpleCookieSessionManager implements HTTPSessionManager {
31  
32      /** Default session cookie name */
33      public static final String SESSION_COOKIE_NAME = "JSESSIONID";
34  
35      /** Connection attribute name for session to be temporarely stored at */
36      public static final String SESSION_ATTRIBUTE = "org.abstracthorizon.danube.http.session.SimpleSessionManager";
37  
38      /** Default session timeout as 30 minutes */
39      public static final int DEFAULT_SESSION_TIMEOUT = 30 * 60 * 1000; // 30 minutes
40  
41      /** Map of sessions */
42      protected Map<Object, Session> sessions = new HashMap<Object, Session>();
43  
44      /** Name of session cookie. Defaulted to {@link #SESSION_COOKIE_NAME}. */
45      protected String sessionCookieName = SESSION_COOKIE_NAME;
46  
47      /** Random number for creating session ids */
48      protected static Random random = new Random();
49  
50      /**
51       * Finds a session for supplied connection
52       * @param connection connection
53       * @param create flag defining should session be created it doesn't exist already
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       * Searches for the session using givne session id
96       * @param sessionId session id object
97       * @param create flag defining should session be created it doesn't exist already
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      * Removes session from connection
113      * @param connection connection
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      * This object removes existing session
125      * @param session session to be removed
126      */
127     public void removeSession(Object session) {
128         synchronized (sessions) {
129             sessions.remove(session);
130         }
131     }
132 
133     /**
134      * Rewrites URL for given connection
135      * @param url URL to be rewritten
136      */
137     public String rewriteURL(HTTPConnection connection, String url) {
138         return url;
139     }
140 
141     /**
142      * Validates session id. This implementation does nothing
143      * @param sessionId session id
144      * @return session id
145      */
146     protected String validateSessionId(String sessionId) {
147         return sessionId;
148     }
149 
150     /**
151      * Creates session id
152      * @return new session id
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      * Creates new session object with given session id
165      * @param sessionId session id
166      * @return new session object
167      */
168     protected Session createSession(String sessionId) {
169         return new Session(sessionId);
170     }
171 
172     /**
173      * Returns session's cookie name
174      * @return session's cookie name
175      */
176     public String getSessionCookieName() {
177         return sessionCookieName;
178     }
179 
180     /**
181      * Sets session's cookie name
182      * @param sessionCookieName session's cookie name
183      */
184     public void setSessionCookieName(String sessionCookieName) {
185         this.sessionCookieName = sessionCookieName;
186     }
187 }