Coverage Report - pt.digitalis.dif.controller.objects.DIFSession
 
Classes in this File Line Coverage Branch Coverage Complexity
DIFSession
0%
0/66
0%
0/16
1,192
 
 1  0
 /**
 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  0
     private Map<String, Object> attributes = new HashMap<String, Object>();
 35  
 
 36  
     /** Keeps record of session creation time. */
 37  0
     private long firstAccessTime = 0;
 38  
 
 39  
     /** The current session navigation history */
 40  0
     private INavigationHistory history = null;
 41  
 
 42  
     /** The current session user language */
 43  0
     private String language = null;
 44  
 
 45  
     /** Keeps record of last session access. */
 46  0
     private long lastAccessTime = 0;
 47  
 
 48  
     /** If the session been marked for removal */
 49  0
     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  0
     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  0
     public DIFSession(String sessionID)
 67  
     {
 68  0
         this.firstAccessTime = System.currentTimeMillis();
 69  0
         this.lastAccessTime = System.currentTimeMillis();
 70  0
         this.sessionID = sessionID;
 71  0
     }
 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  0
         this(sessionID);
 84  0
         this.user = user;
 85  0
     }
 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  0
         attributes.put(key, value);
 93  0
     }
 94  
 
 95  
     /**
 96  
      * @see pt.digitalis.dif.controller.interfaces.IDIFSession#containsAttribute(java.lang.String)
 97  
      */
 98  
     public boolean containsAttribute(String key)
 99  
     {
 100  0
         return attributes.containsKey(key);
 101  
     }
 102  
 
 103  
     /**
 104  
      * @see pt.digitalis.dif.controller.interfaces.IPrivateDIFSession#forceKeepAlive()
 105  
      */
 106  
     public void forceKeepAlive()
 107  
     {
 108  0
         this.setLastAccessTime(System.currentTimeMillis());
 109  
 
 110  0
     }
 111  
 
 112  
     /**
 113  
      * @see pt.digitalis.dif.controller.interfaces.IDIFSession#getAttribute(java.lang.String)
 114  
      */
 115  
     public Object getAttribute(String key)
 116  
     {
 117  0
         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  0
         return attributes;
 126  
     }
 127  
 
 128  
     /**
 129  
      * Inspector for the 'firstAccessTime' property.
 130  
      * 
 131  
      * @return the 'firstAccessTime' value
 132  
      */
 133  
     public long getFirstAccessTime()
 134  
     {
 135  0
         return this.firstAccessTime;
 136  
     }
 137  
 
 138  
     /**
 139  
      * @see pt.digitalis.dif.controller.interfaces.IDIFSession#getLanguage()
 140  
      */
 141  
     public String getLanguage()
 142  
     {
 143  0
         return language;
 144  
     }
 145  
 
 146  
     /**
 147  
      * Inspector for the 'lastAccessTime' property.
 148  
      * 
 149  
      * @return the 'lastAccessTime' value
 150  
      */
 151  
     public long getLastAccessTime()
 152  
     {
 153  0
         return this.lastAccessTime;
 154  
     }
 155  
 
 156  
     /**
 157  
      * @see pt.digitalis.dif.controller.interfaces.IDIFSession#getNavigationHistory()
 158  
      */
 159  
     public INavigationHistory getNavigationHistory()
 160  
     {
 161  0
         if (history == null)
 162  0
             history = DIFIoCRegistry.getRegistry().getImplementation(INavigationHistory.class);
 163  
 
 164  0
         return history;
 165  
     }
 166  
 
 167  
     /**
 168  
      * @see pt.digitalis.dif.controller.interfaces.IDIFSession#getSessionID()
 169  
      */
 170  
     public String getSessionID()
 171  
     {
 172  0
         return sessionID;
 173  
     }
 174  
 
 175  
     /**
 176  
      * @see pt.digitalis.dif.controller.interfaces.IDIFSession#getSessionTimeOut()
 177  
      */
 178  
     public long getSessionTimeOut()
 179  
     {
 180  0
         return this.sessionTimeOut;
 181  
     }
 182  
 
 183  
     /**
 184  
      * @see pt.digitalis.dif.controller.interfaces.IDIFSession#getUser()
 185  
      */
 186  
     public DIFUserInSession getUser()
 187  
     {
 188  0
         return user;
 189  
     }
 190  
 
 191  
     /**
 192  
      * @see pt.digitalis.dif.controller.interfaces.IPrivateDIFSession#hasExpiredAfterTimeOut()
 193  
      */
 194  
     public boolean hasExpiredAfterTimeOut()
 195  
     {
 196  0
         long currentTime = System.currentTimeMillis();
 197  
 
 198  0
         return (this.getLastAccessTime() + this.getSessionTimeOut()
 199  0
                 + 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  0
         long currentTime = System.currentTimeMillis();
 212  
 
 213  0
         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  0
         return (user != null);
 222  
     }
 223  
 
 224  
     /**
 225  
      * @see pt.digitalis.dif.controller.interfaces.IDIFSession#isMarkedForRemoval()
 226  
      */
 227  
     public boolean isMarkedForRemoval()
 228  
     {
 229  0
         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  0
         if (!isMarkedForRemoval() && !hasTimedOut())
 240  0
             forceKeepAlive();
 241  0
     }
 242  
 
 243  
     /**
 244  
      * @see pt.digitalis.dif.controller.interfaces.IDIFSession#setAttributes(Map)
 245  
      */
 246  
     public void setAttributes(Map<String, Object> attributes)
 247  
     {
 248  0
         this.attributes = attributes;
 249  0
     }
 250  
 
 251  
     /**
 252  
      * @see pt.digitalis.dif.controller.interfaces.IDIFSession#setLanguage(java.lang.String)
 253  
      */
 254  
     public void setLanguage(String language)
 255  
     {
 256  0
         this.language = language;
 257  
 
 258  0
     }
 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  0
         this.lastAccessTime = lastAccessTime;
 269  0
     }
 270  
 
 271  
     /**
 272  
      * @param markedForRemoval
 273  
      */
 274  
     public void setMarkedForRemoval(Boolean markedForRemoval)
 275  
     {
 276  0
         this.markedForRemoval = markedForRemoval;
 277  0
     }
 278  
 
 279  
     /**
 280  
      * @see pt.digitalis.dif.controller.interfaces.IDIFSession#setSessionTimeOut(long)
 281  
      */
 282  
     public void setSessionTimeOut(long sessionTimeOut)
 283  
     {
 284  0
         this.sessionTimeOut = sessionTimeOut;
 285  0
     }
 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  0
         this.user = user;
 293  0
     }
 294  
 
 295  
     /**
 296  
      * @see java.lang.Object#toString()
 297  
      */
 298  
     @Override
 299  
     public String toString()
 300  
     {
 301  0
         ObjectFormatter formatter = new ObjectFormatter();
 302  
 
 303  0
         formatter.addItem("Session ID", sessionID);
 304  0
         formatter.addItem("First Access Time", firstAccessTime);
 305  0
         formatter.addItem("Last Access Time", lastAccessTime);
 306  0
         formatter.addItem("language", language);
 307  0
         formatter.addItem("User in session", getUser());
 308  
 
 309  0
         Map<String, Object> parsedAttributes = new HashMap<String, Object>();
 310  
 
 311  0
         for (Entry<String, Object> entry: attributes.entrySet())
 312  0
             if (!entry.getKey().startsWith("JSONCache:"))
 313  0
                 parsedAttributes.put(entry.getKey(), entry.getValue());
 314  
 
 315  0
         formatter.addItemIfNotNull("Attributes", parsedAttributes);
 316  
 
 317  0
         return formatter.getFormatedObject();
 318  
     }
 319  
 }