View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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.server.session;
20  
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.Enumeration;
24  import java.util.HashMap;
25  import java.util.HashSet;
26  import java.util.Iterator;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.Set;
30  
31  import javax.servlet.ServletContext;
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpSessionActivationListener;
34  import javax.servlet.http.HttpSessionBindingEvent;
35  import javax.servlet.http.HttpSessionBindingListener;
36  import javax.servlet.http.HttpSessionContext;
37  import javax.servlet.http.HttpSessionEvent;
38  
39  import org.eclipse.jetty.server.SessionManager;
40  import org.eclipse.jetty.util.log.Logger;
41  
42  /**
43   *
44   * <p>
45   * Implements {@link javax.servlet.http.HttpSession} from the <code>javax.servlet</code> package.
46   * </p>
47   *
48   */
49  @SuppressWarnings("deprecation")
50  public abstract class AbstractSession implements AbstractSessionManager.SessionIf
51  {
52      final static Logger LOG = SessionHandler.LOG;
53      public final static String SESSION_KNOWN_ONLY_TO_AUTHENTICATED="org.eclipse.jetty.security.sessionKnownOnlytoAuthenticated";
54      private  String _clusterId; // ID unique within cluster
55      private  String _nodeId;    // ID unique within node
56      private final AbstractSessionManager _manager;
57      private final Map<String,Object> _attributes=new HashMap<String, Object>();
58      private boolean _idChanged;
59      private final long _created;
60      private long _cookieSet;
61      private long _accessed;         // the time of the last access
62      private long _lastAccessed;     // the time of the last access excluding this one
63      private boolean _invalid;
64      private boolean _doInvalidate;
65      private long _maxIdleMs;
66      private boolean _newSession;
67      private int _requests;
68  
69  
70  
71      /* ------------------------------------------------------------- */
72      protected AbstractSession(AbstractSessionManager abstractSessionManager, HttpServletRequest request)
73      {
74          _manager = abstractSessionManager;
75  
76          _newSession=true;
77          _created=System.currentTimeMillis();
78          _clusterId=_manager._sessionIdManager.newSessionId(request,_created);
79          _nodeId=_manager._sessionIdManager.getNodeId(_clusterId,request);
80          _accessed=_created;
81          _lastAccessed=_created;
82          _requests=1;
83          _maxIdleMs=_manager._dftMaxIdleSecs>0?_manager._dftMaxIdleSecs*1000L:-1;
84          if (LOG.isDebugEnabled())
85              LOG.debug("new session & id "+_nodeId+" "+_clusterId);
86      }
87  
88      /* ------------------------------------------------------------- */
89      protected AbstractSession(AbstractSessionManager abstractSessionManager, long created, long accessed, String clusterId)
90      {
91          _manager = abstractSessionManager;
92          _created=created;
93          _clusterId=clusterId;
94          _nodeId=_manager._sessionIdManager.getNodeId(_clusterId,null);
95          _accessed=accessed;
96          _lastAccessed=accessed;
97          _requests=1;
98          _maxIdleMs=_manager._dftMaxIdleSecs>0?_manager._dftMaxIdleSecs*1000L:-1;
99          if (LOG.isDebugEnabled())
100             LOG.debug("new session "+_nodeId+" "+_clusterId);
101     }
102 
103     /* ------------------------------------------------------------- */
104     /**
105      * asserts that the session is valid
106      */
107     protected void checkValid() throws IllegalStateException
108     {
109         if (_invalid)
110             throw new IllegalStateException();
111     }
112 
113     /* ------------------------------------------------------------- */
114     @Override
115     public AbstractSession getSession()
116     {
117         return this;
118     }
119 
120     /* ------------------------------------------------------------- */
121     public long getAccessed()
122     {
123         synchronized (this)
124         {
125             return _accessed;
126         }
127     }
128 
129     /* ------------------------------------------------------------- */
130     public Map<String,Object> getAttributeMap()
131     {
132         return _attributes;
133     }
134 
135     /* ------------------------------------------------------------ */
136     @Override
137     public Object getAttribute(String name)
138     {
139         synchronized (this)
140         {
141             checkValid();
142             return _attributes.get(name);
143         }
144     }
145 
146     /* ------------------------------------------------------------ */
147     public int getAttributes()
148     {
149         synchronized (this)
150         {
151             checkValid();
152             return _attributes.size();
153         }
154     }
155 
156     /* ------------------------------------------------------------ */
157     @SuppressWarnings({ "unchecked" })
158     @Override
159     public Enumeration<String> getAttributeNames()
160     {
161         synchronized (this)
162         {
163             checkValid();
164             List<String> names=_attributes==null?Collections.EMPTY_LIST:new ArrayList<String>(_attributes.keySet());
165             return Collections.enumeration(names);
166         }
167     }
168 
169     /* ------------------------------------------------------------ */
170     public Set<String> getNames()
171     {
172         synchronized (this)
173         {
174             return new HashSet<String>(_attributes.keySet());
175         }
176     }
177 
178     /* ------------------------------------------------------------- */
179     public long getCookieSetTime()
180     {
181         return _cookieSet;
182     }
183 
184     /* ------------------------------------------------------------- */
185     @Override
186     public long getCreationTime() throws IllegalStateException
187     {
188         return _created;
189     }
190 
191     /* ------------------------------------------------------------ */
192     @Override
193     public String getId() throws IllegalStateException
194     {
195         return _manager._nodeIdInSessionId?_nodeId:_clusterId;
196     }
197 
198     /* ------------------------------------------------------------- */
199     public String getNodeId()
200     {
201         return _nodeId;
202     }
203 
204     /* ------------------------------------------------------------- */
205     public String getClusterId()
206     {
207         return _clusterId;
208     }
209 
210     /* ------------------------------------------------------------- */
211     @Override
212     public long getLastAccessedTime() throws IllegalStateException
213     {
214         checkValid();
215         return _lastAccessed;
216     }
217     
218     /* ------------------------------------------------------------- */
219     public void setLastAccessedTime(long time)
220     {
221         _lastAccessed = time;
222     }
223 
224     /* ------------------------------------------------------------- */
225     @Override
226     public int getMaxInactiveInterval()
227     {
228         checkValid();
229         return (int)(_maxIdleMs/1000);
230     }
231 
232     /* ------------------------------------------------------------ */
233     /*
234      * @see javax.servlet.http.HttpSession#getServletContext()
235      */
236     @Override
237     public ServletContext getServletContext()
238     {
239         return _manager._context;
240     }
241 
242     /* ------------------------------------------------------------- */
243     @Deprecated
244     @Override
245     public HttpSessionContext getSessionContext() throws IllegalStateException
246     {
247         checkValid();
248         return AbstractSessionManager.__nullSessionContext;
249     }
250 
251     /* ------------------------------------------------------------- */
252     /**
253      * @deprecated As of Version 2.2, this method is replaced by
254      *             {@link #getAttribute}
255      */
256     @Deprecated
257     @Override
258     public Object getValue(String name) throws IllegalStateException
259     {
260         return getAttribute(name);
261     }
262 
263     /* ------------------------------------------------------------- */
264     /**
265      * @deprecated As of Version 2.2, this method is replaced by
266      *             {@link #getAttributeNames}
267      */
268     @Deprecated
269     @Override
270     public String[] getValueNames() throws IllegalStateException
271     {
272         synchronized(this)
273         {
274             checkValid();
275             if (_attributes==null)
276                 return new String[0];
277             String[] a=new String[_attributes.size()];
278             return (String[])_attributes.keySet().toArray(a);
279         }
280     }
281 
282 
283     /* ------------------------------------------------------------ */
284     public void renewId(HttpServletRequest request)
285     {
286         _manager._sessionIdManager.renewSessionId(getClusterId(), getNodeId(), request); 
287         setIdChanged(true);
288     }
289        
290     /* ------------------------------------------------------------- */
291     public SessionManager getSessionManager()
292     {
293         return _manager;
294     }
295 
296     /* ------------------------------------------------------------ */
297     protected void setClusterId (String clusterId)
298     {
299         _clusterId = clusterId;
300     }
301     
302     /* ------------------------------------------------------------ */
303     protected void setNodeId (String nodeId)
304     {
305         _nodeId = nodeId;
306     }
307     
308 
309     /* ------------------------------------------------------------ */
310     protected boolean access(long time)
311     {
312         synchronized(this)
313         {
314             if (_invalid)
315                 return false;
316             _newSession=false;
317             _lastAccessed=_accessed;
318             _accessed=time;
319 
320             if (_maxIdleMs>0 && _lastAccessed>0 && _lastAccessed + _maxIdleMs < time)
321             {
322                 invalidate();
323                 return false;
324             }
325             _requests++;
326             return true;
327         }
328     }
329 
330     /* ------------------------------------------------------------ */
331     protected void complete()
332     {
333         synchronized(this)
334         {
335             _requests--;
336             if (_doInvalidate && _requests<=0  )
337                 doInvalidate();
338         }
339     }
340 
341 
342     /* ------------------------------------------------------------- */
343     protected void timeout() throws IllegalStateException
344     {
345         // remove session from context and invalidate other sessions with same ID.
346         _manager.removeSession(this,true);
347 
348         // Notify listeners and unbind values
349         boolean do_invalidate=false;
350         synchronized (this)
351         {
352             if (!_invalid)
353             {
354                 if (_requests<=0)
355                     do_invalidate=true;
356                 else
357                     _doInvalidate=true;
358             }
359         }
360         if (do_invalidate)
361             doInvalidate();
362     }
363 
364     /* ------------------------------------------------------------- */
365     @Override
366     public void invalidate() throws IllegalStateException
367     {
368         // remove session from context and invalidate other sessions with same ID.
369         _manager.removeSession(this,true);
370         doInvalidate();
371     }
372 
373     /* ------------------------------------------------------------- */
374     protected void doInvalidate() throws IllegalStateException
375     {
376         try
377         {
378             LOG.debug("invalidate {}",_clusterId);
379             if (isValid())
380                 clearAttributes();
381         }
382         finally
383         {
384             synchronized (this)
385             {
386                 // mark as invalid
387                 _invalid=true;
388             }
389         }
390     }
391 
392     /* ------------------------------------------------------------- */
393     public void clearAttributes()
394     {
395         while (_attributes!=null && _attributes.size()>0)
396         {
397             ArrayList<String> keys;
398             synchronized(this)
399             {
400                 keys=new ArrayList<String>(_attributes.keySet());
401             }
402 
403             Iterator<String> iter=keys.iterator();
404             while (iter.hasNext())
405             {
406                 String key=(String)iter.next();
407 
408                 Object value;
409                 synchronized(this)
410                 {
411                     value=doPutOrRemove(key,null);
412                 }
413                 unbindValue(key,value);
414 
415                 _manager.doSessionAttributeListeners(this,key,value,null);
416             }
417         }
418         if (_attributes!=null)
419             _attributes.clear();
420     }
421 
422     /* ------------------------------------------------------------- */
423     public boolean isIdChanged()
424     {
425         return _idChanged;
426     }
427 
428     /* ------------------------------------------------------------- */
429     @Override
430     public boolean isNew() throws IllegalStateException
431     {
432         checkValid();
433         return _newSession;
434     }
435 
436     /* ------------------------------------------------------------- */
437     /**
438      * @deprecated As of Version 2.2, this method is replaced by
439      *             {@link #setAttribute}
440      */
441     @Deprecated
442     @Override
443     public void putValue(java.lang.String name, java.lang.Object value) throws IllegalStateException
444     {
445         setAttribute(name,value);
446     }
447 
448     /* ------------------------------------------------------------ */
449     @Override
450     public void removeAttribute(String name)
451     {
452         setAttribute(name,null);
453     }
454 
455     /* ------------------------------------------------------------- */
456     /**
457      * @deprecated As of Version 2.2, this method is replaced by
458      *             {@link #removeAttribute}
459      */
460     @Deprecated
461     @Override
462     public void removeValue(java.lang.String name) throws IllegalStateException
463     {
464         removeAttribute(name);
465     }
466 
467     /* ------------------------------------------------------------ */
468     protected Object doPutOrRemove(String name, Object value)
469     {
470         return value==null?_attributes.remove(name):_attributes.put(name,value);
471     }
472 
473     /* ------------------------------------------------------------ */
474     protected Object doGet(String name)
475     {
476         return _attributes.get(name);
477     }
478 
479     /* ------------------------------------------------------------ */
480     @Override
481     public void setAttribute(String name, Object value)
482     {
483         updateAttribute(name,value);
484     }
485     
486     /* ------------------------------------------------------------ */
487     protected boolean updateAttribute (String name, Object value)
488     {
489         Object old=null;
490         synchronized (this)
491         {
492             checkValid();
493             old=doPutOrRemove(name,value);
494         }
495 
496         if (value==null || !value.equals(old))
497         {
498             if (old!=null)
499                 unbindValue(name,old);
500             if (value!=null)
501                 bindValue(name,value);
502 
503             _manager.doSessionAttributeListeners(this,name,old,value);
504             return true;
505         }
506         return false;
507     }
508 
509     /* ------------------------------------------------------------ */
510     protected void addAttributes(Map<String,Object> map)
511     {
512         _attributes.putAll(map);
513     }
514 
515     /* ------------------------------------------------------------- */
516     public void setIdChanged(boolean changed)
517     {
518         _idChanged=changed;
519     }
520 
521     /* ------------------------------------------------------------- */
522     @Override
523     public void setMaxInactiveInterval(int secs)
524     {
525         _maxIdleMs=(long)secs*1000L;
526     }
527 
528     /* ------------------------------------------------------------- */
529     @Override
530     public String toString()
531     {
532         return this.getClass().getName()+":"+getId()+"@"+hashCode();
533     }
534 
535     /* ------------------------------------------------------------- */
536     /** If value implements HttpSessionBindingListener, call valueBound() */
537     public void bindValue(java.lang.String name, Object value)
538     {
539         if (value!=null&&value instanceof HttpSessionBindingListener)
540             ((HttpSessionBindingListener)value).valueBound(new HttpSessionBindingEvent(this,name));
541     }
542 
543     /* ------------------------------------------------------------ */
544     public boolean isValid()
545     {
546         return !_invalid;
547     }
548 
549     /* ------------------------------------------------------------- */
550     protected void cookieSet()
551     {
552         synchronized (this)
553         {
554             _cookieSet=_accessed;
555         }
556     }
557 
558     /* ------------------------------------------------------------ */
559     public int getRequests()
560     {
561         synchronized (this)
562         {
563             return _requests;
564         }
565     }
566 
567     /* ------------------------------------------------------------ */
568     public void setRequests(int requests)
569     {
570         synchronized (this)
571         {
572             _requests=requests;
573         }
574     }
575 
576     /* ------------------------------------------------------------- */
577     /** If value implements HttpSessionBindingListener, call valueUnbound() */
578     public void unbindValue(java.lang.String name, Object value)
579     {
580         if (value!=null&&value instanceof HttpSessionBindingListener)
581             ((HttpSessionBindingListener)value).valueUnbound(new HttpSessionBindingEvent(this,name));
582     }
583 
584     /* ------------------------------------------------------------- */
585     public void willPassivate()
586     {
587         synchronized(this)
588         {
589             HttpSessionEvent event = new HttpSessionEvent(this);
590             for (Iterator<Object> iter = _attributes.values().iterator(); iter.hasNext();)
591             {
592                 Object value = iter.next();
593                 if (value instanceof HttpSessionActivationListener)
594                 {
595                     HttpSessionActivationListener listener = (HttpSessionActivationListener) value;
596                     listener.sessionWillPassivate(event);
597                 }
598             }
599         }
600     }
601 
602     /* ------------------------------------------------------------- */
603     public void didActivate()
604     {
605         synchronized(this)
606         {
607             HttpSessionEvent event = new HttpSessionEvent(this);
608             for (Iterator<Object> iter = _attributes.values().iterator(); iter.hasNext();)
609             {
610                 Object value = iter.next();
611                 if (value instanceof HttpSessionActivationListener)
612                 {
613                     HttpSessionActivationListener listener = (HttpSessionActivationListener) value;
614                     listener.sessionDidActivate(event);
615                 }
616             }
617         }
618     }
619 
620 
621 }