package org.gcube.application.framework.core.session; import java.util.HashMap; import java.util.Set; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.gcube.application.framework.accesslogger.library.impl.AccessLogger; import org.gcube.application.framework.accesslogger.model.LoginToVreAccessLogEntry; import org.gcube.application.framework.core.security.PortalSecurityManager; import org.gcube.application.framework.core.util.ASLGroupModel; import org.gcube.application.framework.core.util.GenderType; import org.gcube.application.framework.core.util.SessionConstants; import org.gcube.application.framework.core.util.UserCredential; import org.gcube.common.core.scope.GCUBEScope; import org.gcube.common.scope.api.ScopeProvider; import org.gridforum.jgss.ExtendedGSSCredential; /** * @author Valia Tsagkalidou (NKUA) * */ public class ASLSession{ /** * */ private static final long serialVersionUID = 1L; private HashMap innerSession; private long lastUsedTime; private String externalSessionID; private String username; private ExtendedGSSCredential credential; private GCUBEScope scope; private HashMap notifiers; String scopeName; private ASLGroupModel groupModel; private boolean loggedIn = false; private String userEmailAddress; private String fullName; private String avatarId; private GenderType gender; /** The logger. */ private static final Logger logger = LoggerFactory.getLogger(ASLSession.class); // ACCESS LOGGER AccessLogger accessLogger = AccessLogger.getAccessLogger(); /** * A constructor based on the user and an external ID * @param externalSessionId the external id * @param user the username */ ASLSession(String externalSessionId, String user) { innerSession = new HashMap(); notifiers = new HashMap(); lastUsedTime = System.currentTimeMillis(); username = user; externalSessionID = externalSessionId; groupModel = new ASLGroupModel(); } private void initializeAttributes() { for (String key:innerSession.keySet()) { if (key.equals("collectionsPresentableFields") || key.equals(SessionConstants.collectionsHierarchy)) { innerSession.remove(key); break; } } } /** * @return whether the session is still valid or not */ public boolean isValid() { if((System.currentTimeMillis() - lastUsedTime) > 1800000) // 30 minutes return false; return true; } /** * @return whether the session is empty or not */ public boolean isEmpty() { lastUsedTime = System.currentTimeMillis(); return innerSession.isEmpty(); } /** * @param name the name of the attribute * @return whether the name attribute exists in the session */ public boolean hasAttribute(String name) { lastUsedTime = System.currentTimeMillis(); return innerSession.containsKey(name); } /** * @return a set of all the attributes in the session */ public Set getAttributeNames() { lastUsedTime = System.currentTimeMillis(); return innerSession.keySet(); } /** * @param name the name of the attribute * @return the value of the named attribute */ public Object getAttribute(String name) { lastUsedTime = System.currentTimeMillis(); return innerSession.get(name); } /** * @param name the name of the attribute * @param value the value of the attribute */ public void setAttribute(String name, Object value) { lastUsedTime = System.currentTimeMillis(); innerSession.put(name, value); } public String getOriginalScopeName() { return scopeName; } /** * Removes the named attribute from the session * @param name the name of the attribute * @return the removed object */ public Object removeAttribute(String name) { lastUsedTime = System.currentTimeMillis(); return innerSession.remove(name); } /** * Removes all the attributes from the session */ public void removeAll() { lastUsedTime = System.currentTimeMillis(); innerSession.clear(); } /** * invalidates the session */ public void invalidate() { lastUsedTime = System.currentTimeMillis() - 2000000; //more than 30 minutes } /** * @return the credential */ public ExtendedGSSCredential getCredential() { return credential; } /** * @return the external session id (passed to the constructor) */ public String getExternalSessionID() { return externalSessionID; } /** * @return the username */ public String getUsername() { return username; } /** * @return the scope */ public GCUBEScope getScope() { logger.debug("The scope is being returned - username: " + username + " scope: " + scope.getName()); ScopeProvider.instance.set(scope.toString()); return scope; } /** * @return the name of the scope (VRE) */ public String getScopeName() { logger.debug("The scope is name: " + scope); logger.debug("the username is: " + username); if (scope != null) return scope.toString(); else return null; } /** * @param scope the scope name (VRE) */ public void setScope(String scope) { logger.info("The scope about to set is: " + scope); lastUsedTime = System.currentTimeMillis(); String test = scope.trim(); if (test == null) logger.info("1"); test = scope.trim().substring(1); if (test == null) logger.info("2"); String[] split = scope.trim().substring(1).split("/",2); // Uncomment this and comment the line bellow for devsec // String vo = "/" + split[0].toLowerCase(); String vo = "/" + split[0]; if(split.length > 1) vo += "/" + split[1]; logger.info("*** VRE to be set:" + vo + " ***"); logger.info("GCube scope returns: " + GCUBEScope.getScope(vo)); this.scope = GCUBEScope.getScope(vo); String previousScopeName = scopeName; scopeName = vo; if(new PortalSecurityManager(this.scope).isSecurityEnabled()) this.credential = UserCredential.getCredential(username, scope); // get the attribute that indicates of log in has been done from the login portlet - or if the user logs in from a bookmark if (loggedIn == true) { // don't log initializeAttributes(); // clear the attribute loggedIn = false; logger.info("Passing the logging because the variable was set"); return; } if ((previousScopeName != null && !previousScopeName.equals(scopeName)) || previousScopeName == null) { logger.info("Logging the entrance"); innerSession.clear(); // ACCESS LOGGER LoginToVreAccessLogEntry loginEntry = new LoginToVreAccessLogEntry(); accessLogger.logEntry(username, scope, loginEntry); } else logger.info("Passing the logging because the scope was the same"); initializeAttributes(); } public void logUserLogin(String scope) { logger.info("LogUserLogin method called"); innerSession.clear(); loggedIn = true; // ACCESS LOGGER LoginToVreAccessLogEntry loginEntry = new LoginToVreAccessLogEntry(); accessLogger.logEntry(username, scope, loginEntry); } /** * @param notification the name of the notification to wait for * @throws InterruptedException when the thread is interrupted */ public void waitNotification(String notification) throws InterruptedException { Notifier notifier = notifiers.get(notification); if(notifier == null) { notifier = new Notifier(); notifiers.put(notification, notifier); } lastUsedTime = System.currentTimeMillis(); notifier.waitNotification(); } /** * @param notification the name of the notification to send notification * @throws InterruptedException when the thread is interrupted */ public void notifyAllWaiting(String notification) throws InterruptedException { Notifier notifier = notifiers.get(notification); if(notifier == null) { notifier = new Notifier(); notifiers.put(notification, notifier); } lastUsedTime = System.currentTimeMillis(); notifier.notifyAllWaiting(); } public void setGroupModelInfos(String groupName, long groupId) { groupModel.setGroupName(groupName); groupModel.setGroupId(groupId); } public long getGroupId() { return groupModel.getGroupId(); } public String getGroupName() { return groupModel.getGroupName(); } public void setUserEmailAddress(String email) { this.userEmailAddress = email; } public String getUserEmailAddress() { return this.userEmailAddress; } public void setUserFullName(String fullName) { this.fullName = fullName; } public String getUserFullName() { return this.fullName; } public void setUserAvatarId(String avatarId) { this.avatarId = avatarId; } public String getUserAvatarId() { return this.avatarId; } public void setUserGender(GenderType gender) { this.gender = gender; } public GenderType getUserGender() { return this.gender; } }