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