View Javadoc

1   // ========================================================================
2   // Copyright (c) 2008-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.security;
15  
16  import java.security.SecureRandom;
17  import java.util.HashMap;
18  import java.util.Map;
19  import java.util.Random;
20  
21  import javax.servlet.http.Cookie;
22  import javax.servlet.http.HttpServletRequest;
23  import javax.servlet.http.HttpServletResponse;
24  
25  /**
26   * @version $Rev: 4660 $ $Date: 2009-02-25 17:29:53 +0100 (Wed, 25 Feb 2009) $
27   */
28  public class HashCrossContextPsuedoSession<T> implements CrossContextPsuedoSession<T>
29  {
30      private final String _cookieName;
31  
32      private final String _cookiePath;
33  
34      private final Random _random = new SecureRandom();
35  
36      private final Map<String, T> _data = new HashMap<String, T>();
37  
38      public HashCrossContextPsuedoSession(String cookieName, String cookiePath)
39      {
40          this._cookieName = cookieName;
41          this._cookiePath = cookiePath == null ? "/" : cookiePath;
42      }
43  
44      public T fetch(HttpServletRequest request)
45      {
46          for (Cookie cookie : request.getCookies())
47          {
48              if (_cookieName.equals(cookie.getName()))
49              {
50                  String key = cookie.getValue();
51                  return _data.get(key);
52              }
53          }
54          return null;
55      }
56  
57      public void store(T datum, HttpServletResponse response)
58      {
59          String key;
60  
61          synchronized (_data)
62          {
63              // Create new ID
64              while (true)
65              {
66                  key = Long.toString(Math.abs(_random.nextLong()), 30 + (int) (System.currentTimeMillis() % 7));
67                  if (!_data.containsKey(key)) break;
68              }
69  
70              _data.put(key, datum);
71          }
72  
73          Cookie cookie = new Cookie(_cookieName, key);
74          cookie.setPath(_cookiePath);
75          response.addCookie(cookie);
76      }
77  
78      public void clear(HttpServletRequest request)
79      {
80          for (Cookie cookie : request.getCookies())
81          {
82              if (_cookieName.equals(cookie.getName()))
83              {
84                  String key = cookie.getValue();
85                  _data.remove(key);
86                  break;
87              }
88          }
89      }
90  }