View Javadoc

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