View Javadoc

1   /**
2    * - Digitalis Internal Framework v2.0 - (C) 2007, Digitalis Informatica. Distribuicao e Gestao de Informatica, Lda.
3    * Estrada de Paco de Arcos num.9 - Piso -1 2780-666 Paco de Arcos Telefone: (351) 21 4408990 Fax: (351) 21 4408999
4    * http://www.digitalis.pt
5    */
6   package pt.digitalis.dif.controller.objects;
7   
8   import java.util.HashMap;
9   import java.util.Map;
10  import java.util.Map.Entry;
11  
12  import pt.digitalis.dif.controller.interfaces.INavigationHistory;
13  import pt.digitalis.dif.controller.interfaces.IPrivateDIFSession;
14  import pt.digitalis.dif.ioc.DIFIoCRegistry;
15  import pt.digitalis.dif.startup.DIFGeneralConfigurationParameters;
16  import pt.digitalis.dif.utils.ObjectFormatter;
17  
18  /**
19   * Base implementation of a DIF Session. Should be extended by each specific channel session. (i.e. DIFSessionHTTPImpl)
20   * 
21   * @author Rodrigo Gonçalves <a href="mailto:rgoncalves@digitalis.pt">rgoncalves@digitalis.pt</a>
22   * @author Pedro Viegas <a href="mailto:pviegas@digitalis.pt">pviegas@digitalis.pt</a>
23   * @created Nov 29, 2007
24   */
25  public class DIFSession implements IPrivateDIFSession {
26  
27      /** The session key indicating that authentication was performed by a remote provider */
28      public static final String REMOTE_AUTHENTICATION_PROVIDER_LOGIN = "remote_authentication_provider_login";
29  
30      /** The session key indicating that authentication was performed by a remote provider */
31      public static final String REMOTE_AUTHENTICATION_PROVIDER_LOGOUT = "remote_authentication_provider_logout";
32  
33      /** Session attributes. */
34      private Map<String, Object> attributes = new HashMap<String, Object>();
35  
36      /** Keeps record of session creation time. */
37      private long firstAccessTime = 0;
38  
39      /** The current session navigation history */
40      private INavigationHistory history = null;
41  
42      /** The current session user language */
43      private String language = null;
44  
45      /** Keeps record of last session access. */
46      private long lastAccessTime = 0;
47  
48      /** If the session been marked for removal */
49      private Boolean markedForRemoval = false;
50  
51      /** The session unique ID */
52      private final String sessionID;
53  
54      /** The defined time out value for the session. */
55      private long sessionTimeOut = 0;
56  
57      /** The user associated to the session. */
58      private DIFUserInSession user;
59  
60      /**
61       * Default constructor. Sets the first access time.
62       * 
63       * @param sessionID
64       *            the session unique identifier
65       */
66      public DIFSession(String sessionID)
67      {
68          this.firstAccessTime = System.currentTimeMillis();
69          this.lastAccessTime = System.currentTimeMillis();
70          this.sessionID = sessionID;
71      }
72  
73      /**
74       * One-argument constructor that receives an user.
75       * 
76       * @param sessionID
77       *            the session unique identifier
78       * @param user
79       *            a DIF user
80       */
81      public DIFSession(String sessionID, DIFUserInSession user)
82      {
83          this(sessionID);
84          this.user = user;
85      }
86  
87      /**
88       * @see pt.digitalis.dif.controller.interfaces.IDIFSession#addAttribute(java.lang.String, java.lang.Object)
89       */
90      public void addAttribute(String key, Object value)
91      {
92          attributes.put(key, value);
93      }
94  
95      /**
96       * @see pt.digitalis.dif.controller.interfaces.IDIFSession#containsAttribute(java.lang.String)
97       */
98      public boolean containsAttribute(String key)
99      {
100         return attributes.containsKey(key);
101     }
102 
103     /**
104      * @see pt.digitalis.dif.controller.interfaces.IPrivateDIFSession#forceKeepAlive()
105      */
106     public void forceKeepAlive()
107     {
108         this.setLastAccessTime(System.currentTimeMillis());
109 
110     }
111 
112     /**
113      * @see pt.digitalis.dif.controller.interfaces.IDIFSession#getAttribute(java.lang.String)
114      */
115     public Object getAttribute(String key)
116     {
117         return attributes.get(key);
118     }
119 
120     /**
121      * @see pt.digitalis.dif.controller.interfaces.IDIFSession#getAttributes()
122      */
123     public Map<String, Object> getAttributes()
124     {
125         return attributes;
126     }
127 
128     /**
129      * Inspector for the 'firstAccessTime' property.
130      * 
131      * @return the 'firstAccessTime' value
132      */
133     public long getFirstAccessTime()
134     {
135         return this.firstAccessTime;
136     }
137 
138     /**
139      * @see pt.digitalis.dif.controller.interfaces.IDIFSession#getLanguage()
140      */
141     public String getLanguage()
142     {
143         return language;
144     }
145 
146     /**
147      * Inspector for the 'lastAccessTime' property.
148      * 
149      * @return the 'lastAccessTime' value
150      */
151     public long getLastAccessTime()
152     {
153         return this.lastAccessTime;
154     }
155 
156     /**
157      * @see pt.digitalis.dif.controller.interfaces.IDIFSession#getNavigationHistory()
158      */
159     public INavigationHistory getNavigationHistory()
160     {
161         if (history == null)
162             history = DIFIoCRegistry.getRegistry().getImplementation(INavigationHistory.class);
163 
164         return history;
165     }
166 
167     /**
168      * @see pt.digitalis.dif.controller.interfaces.IDIFSession#getSessionID()
169      */
170     public String getSessionID()
171     {
172         return sessionID;
173     }
174 
175     /**
176      * @see pt.digitalis.dif.controller.interfaces.IDIFSession#getSessionTimeOut()
177      */
178     public long getSessionTimeOut()
179     {
180         return this.sessionTimeOut;
181     }
182 
183     /**
184      * @see pt.digitalis.dif.controller.interfaces.IDIFSession#getUser()
185      */
186     public DIFUserInSession getUser()
187     {
188         return user;
189     }
190 
191     /**
192      * @see pt.digitalis.dif.controller.interfaces.IPrivateDIFSession#hasExpiredAfterTimeOut()
193      */
194     public boolean hasExpiredAfterTimeOut()
195     {
196         long currentTime = System.currentTimeMillis();
197 
198         return (this.getLastAccessTime() + this.getSessionTimeOut()
199                 + DIFGeneralConfigurationParameters.getInstance().getSessionExpirationTimeAfterTimeout() < currentTime);
200     }
201 
202     /**
203      * The criteria for a timed out session is that the last access time plus the session time out must be higher than
204      * the current time.
205      * 
206      * @see pt.digitalis.dif.controller.interfaces.IDIFSession#hasTimedOut()
207      */
208     public boolean hasTimedOut()
209     {
210 
211         long currentTime = System.currentTimeMillis();
212 
213         return (this.getLastAccessTime() + this.getSessionTimeOut() < currentTime);
214     }
215 
216     /**
217      * @see pt.digitalis.dif.controller.interfaces.IDIFSession#isLogged()
218      */
219     public boolean isLogged()
220     {
221         return (user != null);
222     }
223 
224     /**
225      * @see pt.digitalis.dif.controller.interfaces.IDIFSession#isMarkedForRemoval()
226      */
227     public boolean isMarkedForRemoval()
228     {
229         return markedForRemoval;
230     }
231 
232     /**
233      * Sets the 'lastAccesTime' property to "now".
234      * 
235      * @see pt.digitalis.dif.controller.interfaces.IPrivateDIFSession#keepAlive()
236      */
237     public void keepAlive()
238     {
239         if (!isMarkedForRemoval() && !hasTimedOut())
240             forceKeepAlive();
241     }
242 
243     /**
244      * @see pt.digitalis.dif.controller.interfaces.IDIFSession#setAttributes(Map)
245      */
246     public void setAttributes(Map<String, Object> attributes)
247     {
248         this.attributes = attributes;
249     }
250 
251     /**
252      * @see pt.digitalis.dif.controller.interfaces.IDIFSession#setLanguage(java.lang.String)
253      */
254     public void setLanguage(String language)
255     {
256         this.language = language;
257 
258     }
259 
260     /**
261      * Modifier for the 'lastAccessTime' property.
262      * 
263      * @param lastAccessTime
264      *            the 'lastAccessTime' new value to set
265      */
266     public void setLastAccessTime(long lastAccessTime)
267     {
268         this.lastAccessTime = lastAccessTime;
269     }
270 
271     /**
272      * @param markedForRemoval
273      */
274     public void setMarkedForRemoval(Boolean markedForRemoval)
275     {
276         this.markedForRemoval = markedForRemoval;
277     }
278 
279     /**
280      * @see pt.digitalis.dif.controller.interfaces.IDIFSession#setSessionTimeOut(long)
281      */
282     public void setSessionTimeOut(long sessionTimeOut)
283     {
284         this.sessionTimeOut = sessionTimeOut;
285     }
286 
287     /**
288      * @see pt.digitalis.dif.controller.interfaces.IDIFSession#setUser(pt.digitalis.dif.controller.objects.DIFUserInSession)
289      */
290     public void setUser(DIFUserInSession user)
291     {
292         this.user = user;
293     }
294 
295     /**
296      * @see java.lang.Object#toString()
297      */
298     @Override
299     public String toString()
300     {
301         ObjectFormatter formatter = new ObjectFormatter();
302 
303         formatter.addItem("Session ID", sessionID);
304         formatter.addItem("First Access Time", firstAccessTime);
305         formatter.addItem("Last Access Time", lastAccessTime);
306         formatter.addItem("language", language);
307         formatter.addItem("User in session", getUser());
308 
309         Map<String, Object> parsedAttributes = new HashMap<String, Object>();
310 
311         for (Entry<String, Object> entry: attributes.entrySet())
312             if (!entry.getKey().startsWith("JSONCache:"))
313                 parsedAttributes.put(entry.getKey(), entry.getValue());
314 
315         formatter.addItemIfNotNull("Attributes", parsedAttributes);
316 
317         return formatter.getFormatedObject();
318     }
319 }