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      /** The session ID. */
33      private final String sessionId;
34  
35      /** Subject of this session. */
36      private Subject subject;
37  
38      /** Session inactivity timeout in milliseconds. */
39      private long inactivityTimeout;
40  
41      /** The last activity time of the user. */
42      private long lastActivity;
43  
44      /**
45       * Constructor.
46       * 
47       * @param id ID of the session
48       * @param timeout inactivity timeout for the session in milliseconds
49       */
50      public AbstractSession(String id, long timeout) {
51          sessionId = id;
52          subject = new Subject();
53          inactivityTimeout = timeout;
54          lastActivity = new DateTime().toDateTime(ISOChronology.getInstanceUTC()).getMillis();
55      }
56  
57      /** {@inheritDoc} */
58      public synchronized String getSessionID() {
59          return sessionId;
60      }
61  
62      /** {@inheritDoc} */
63      public synchronized Subject getSubject() {
64          return subject;
65      }
66  
67      /** {@inheritDoc} */
68      public synchronized void setSubject(Subject newSubject) {
69          subject = newSubject;
70      }
71  
72      /** {@inheritDoc} */
73      public synchronized String getPrincipalName() {
74          Set<Principal> principals = subject.getPrincipals();
75          if (principals != null && !principals.isEmpty()) {
76              return principals.iterator().next().getName();
77          } else {
78              return null;
79          }
80      }
81  
82      /** {@inheritDoc} */
83      public synchronized long getInactivityTimeout() {
84          return inactivityTimeout;
85      }
86  
87      /** {@inheritDoc} */
88      public synchronized DateTime getLastActivityInstant() {
89          return new DateTime(lastActivity, ISOChronology.getInstanceUTC());
90      }
91  
92      /** {@inheritDoc} */
93      public synchronized void setLastActivityInstant(DateTime activity) {
94          lastActivity = activity.toDateTime(ISOChronology.getInstanceUTC()).getMillis();
95      }
96  }