Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7c2a81e1072119c81e02752377910caad2b1ebe6 (plain) (blame)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//
//  ========================================================================
//  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.gcloud.session;

import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.SessionManager;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.session.AbstractSession;
import org.eclipse.jetty.server.session.AbstractSessionIdManager;
import org.eclipse.jetty.server.session.SessionHandler;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;

import com.google.gcloud.datastore.Datastore;
import com.google.gcloud.datastore.DatastoreFactory;
import com.google.gcloud.datastore.Entity;
import com.google.gcloud.datastore.Key;
import com.google.gcloud.datastore.KeyFactory;



/**
 * GCloudSessionIdManager
 *
 * 
 * 
 */
public class GCloudSessionIdManager extends AbstractSessionIdManager
{
    private  final static Logger LOG = Log.getLogger("org.eclipse.jetty.server.session");
    public static final int DEFAULT_IDLE_EXPIRY_MULTIPLE = 2;
    public static final String KIND = "GCloudSessionId";
    private Server _server;
    private Datastore _datastore;
    private KeyFactory _keyFactory;
    private GCloudConfiguration _config;
    
    
    
 
    /**
     * @param server
     */
    public GCloudSessionIdManager(Server server)
    {
        super();
        _server = server;
    }

    /**
     * @param server
     * @param random
     */
    public GCloudSessionIdManager(Server server, Random random)
    {
       super(random);
       _server = server;
    }



    /** 
     * Start the id manager.
     * @see org.eclipse.jetty.server.session.AbstractSessionIdManager#doStart()
     */
    @Override
    protected void doStart() throws Exception
    {
        if (_config == null)
            throw new IllegalStateException("No gcloud configuration specified");       
        

        _datastore = DatastoreFactory.instance().get(_config.getDatastoreOptions());
        _keyFactory = _datastore.newKeyFactory().kind(KIND);
  
        super.doStart();
    }


    
    /** 
     * Stop the id manager
     * @see org.eclipse.jetty.server.session.AbstractSessionIdManager#doStop()
     */
    @Override
    protected void doStop() throws Exception
    {
        super.doStop();
    }

    
   

    
    /** 
     * Check to see if the given session id is being
     * used by a session in any context.
     * 
     * This method will consult the cluster.
     * 
     * @see org.eclipse.jetty.server.SessionIdManager#idInUse(java.lang.String)
     */
    @Override
    public boolean idInUse(String id)
    {
        if (id == null)
            return false;
        
        String clusterId = getClusterId(id);
        
        //ask the cluster - this should also tickle the idle expiration timer on the sessionid entry
        //keeping it valid
        try
        {
            return exists(clusterId);
        }
        catch (Exception e)
        {
            LOG.warn("Problem checking inUse for id="+clusterId, e);
            return false;
        }
        
    }

    /** 
     * Remember a new in-use session id.
     * 
     * This will save the in-use session id to the cluster.
     * 
     * @see org.eclipse.jetty.server.SessionIdManager#addSession(javax.servlet.http.HttpSession)
     */
    @Override
    public void addSession(HttpSession session)
    {
        if (session == null)
            return;

        //insert into the store
        insert (((AbstractSession)session).getClusterId());
    }

  

    
    public GCloudConfiguration getConfig()
    {
        return _config;
    }

    public void setConfig(GCloudConfiguration config)
    {
        _config = config;
    }

    
    
    /** 
     * Remove a session id from the list of in-use ids.
     * 
     * This will remvove the corresponding session id from the cluster.
     * 
     * @see org.eclipse.jetty.server.SessionIdManager#removeSession(javax.servlet.http.HttpSession)
     */
    @Override
    public void removeSession(HttpSession session)
    {
        if (session == null)
            return;

        //delete from the cache
        delete (((AbstractSession)session).getClusterId());
    }

    /** 
     * Remove a session id. This compels all other contexts who have a session
     * with the same id to also remove it.
     * 
     * @see org.eclipse.jetty.server.SessionIdManager#invalidateAll(java.lang.String)
     */
    @Override
    public void invalidateAll(String id)
    {
        //delete the session id from list of in-use sessions
        delete (id);


        //tell all contexts that may have a session object with this id to
        //get rid of them
        Handler[] contexts = _server.getChildHandlersByClass(ContextHandler.class);
        for (int i=0; contexts!=null && i<contexts.length; i++)
        {
            SessionHandler sessionHandler = ((ContextHandler)contexts[i]).getChildHandlerByClass(SessionHandler.class);
            if (sessionHandler != null)
            {
                SessionManager manager = sessionHandler.getSessionManager();

                if (manager != null && manager instanceof GCloudSessionManager)
                {
                    ((GCloudSessionManager)manager).invalidateSession(id);
                }
            }
        }

    }

    /** 
     * Change a session id. 
     * 
     * Typically this occurs when a previously existing session has passed through authentication.
     * 
     * @see org.eclipse.jetty.server.session.AbstractSessionIdManager#renewSessionId(java.lang.String, java.lang.String, javax.servlet.http.HttpServletRequest)
     */
    @Override
    public void renewSessionId(String oldClusterId, String oldNodeId, HttpServletRequest request)
    {
        //generate a new id
        String newClusterId = newSessionId(request.hashCode());

        delete(oldClusterId);
        insert(newClusterId);


        //tell all contexts to update the id 
        Handler[] contexts = _server.getChildHandlersByClass(ContextHandler.class);
        for (int i=0; contexts!=null && i<contexts.length; i++)
        {
            SessionHandler sessionHandler = ((ContextHandler)contexts[i]).getChildHandlerByClass(SessionHandler.class);
            if (sessionHandler != null) 
            {
                SessionManager manager = sessionHandler.getSessionManager();

                if (manager != null && manager instanceof GCloudSessionManager)
                {
                    ((GCloudSessionManager)manager).renewSessionId(oldClusterId, oldNodeId, newClusterId, getNodeId(newClusterId, request));
                }
            }
        }

    }

    
    
    /**
     * Ask the datastore if a particular id exists.
     * 
     * @param id
     * @return
     */
    protected boolean exists (String id)
    {
        if (_datastore == null)
            throw new IllegalStateException ("No DataStore");
        Key key = _keyFactory.newKey(id);
        return _datastore.get(key) != null;
    }
    

    /**
     * Put a session id into the cluster.
     * 
     * @param id
     */
    protected void insert (String id)
    {        
        if (_datastore == null)
            throw new IllegalStateException ("No DataStore");

        Entity entity = Entity.builder(makeKey(id))
                        .set("id", id).build();
        _datastore.put(entity);
    }

   
   
    
    /**
     * Remove a session id from the cluster.
     * 
     * @param id
     */
    protected void delete (String id)
    {
        if (_datastore == null)
            throw new IllegalStateException ("No DataStore");
        
        _datastore.delete(makeKey(id));
    }
    
    

    /**
     * Generate a unique key from the session id.
     * 
     * @param id
     * @return
     */
    protected Key makeKey (String id)
    {
        return _keyFactory.newKey(id);
    }
}

Back to the top