View Javadoc

1   /*
2    * Copyright 2006 University Corporation for Advanced Internet Development, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package edu.internet2.middleware.shibboleth.common.session.impl;
18  
19  import java.security.Principal;
20  import java.util.Set;
21  
22  import javax.security.auth.Subject;
23  
24  import org.joda.time.DateTime;
25  import org.joda.time.chrono.ISOChronology;
26  
27  import edu.internet2.middleware.shibboleth.common.session.Session;
28  
29  /** Base class for Shibboleth sessions. */
30  public abstract class AbstractSession implements Session {
31  
32      /** Serial version UID. */
33      private static final long serialVersionUID = 4726780089406295821L;
34  
35      /** The session ID. */
36      private final String sessionId;
37  
38      /** Subject of this session. */
39      private Subject subject;
40  
41      /** Session inactivity timeout in milliseconds. */
42      private long inactivityTimeout;
43  
44      /** The last activity time of the user. */
45      private long lastActivity;
46  
47      /**
48       * Constructor.
49       * 
50       * @param id ID of the session
51       * @param timeout inactivity timeout for the session in milliseconds
52       */
53      public AbstractSession(String id, long timeout) {
54          sessionId = id;
55          subject = new Subject();
56          inactivityTimeout = timeout;
57          lastActivity = new DateTime().toDateTime(ISOChronology.getInstanceUTC()).getMillis();
58      }
59  
60      /** {@inheritDoc} */
61      public synchronized String getSessionID() {
62          return sessionId;
63      }
64  
65      /** {@inheritDoc} */
66      public synchronized Subject getSubject() {
67          return subject;
68      }
69  
70      /** {@inheritDoc} */
71      public synchronized void setSubject(Subject newSubject) {
72          subject = newSubject;
73      }
74  
75      /** {@inheritDoc} */
76      public synchronized String getPrincipalName() {
77          Set<Principal> principals = subject.getPrincipals();
78          if (principals != null && !principals.isEmpty()) {
79              return principals.iterator().next().getName();
80          } else {
81              return null;
82          }
83      }
84  
85      /** {@inheritDoc} */
86      public synchronized long getInactivityTimeout() {
87          return inactivityTimeout;
88      }
89  
90      /** {@inheritDoc} */
91      public synchronized DateTime getLastActivityInstant() {
92          return new DateTime(lastActivity, ISOChronology.getInstanceUTC());
93      }
94  
95      /** {@inheritDoc} */
96      public synchronized void setLastActivityInstant(DateTime activity) {
97          lastActivity = activity.toDateTime(ISOChronology.getInstanceUTC()).getMillis();
98      }
99  }