diff --git a/pom.xml b/pom.xml index 006a474..d42236e 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ org.gcube.applicationsupportlayer aslcore - 4.6.3-SNAPSHOT + 5.0.0-SNAPSHOT Application Support Layer Core Library supporting dissemination of gCube content with OAI-ORE @@ -91,6 +91,14 @@ 2.3.0 + + javax.servlet + servlet-api + 2.5 + provided + + + diff --git a/src/main/java/org/gcube/application/framework/core/session/ASLSession.java b/src/main/java/org/gcube/application/framework/core/session/ASLSession.java index 8121049..665e91d 100644 --- a/src/main/java/org/gcube/application/framework/core/session/ASLSession.java +++ b/src/main/java/org/gcube/application/framework/core/session/ASLSession.java @@ -3,9 +3,12 @@ package org.gcube.application.framework.core.session; import java.io.File; import java.io.IOException; import java.net.URL; +import java.util.Enumeration; import java.util.HashMap; +import java.util.HashSet; import java.util.Set; +import javax.servlet.http.HttpSession; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; @@ -28,7 +31,6 @@ import org.w3c.dom.NodeList; //import org.gridforum.jgss.ExtendedGSSCredential; /** - * ASLSession is deprecated. Do all handling through HttpSession instead * * @author Valia Tsagkalidou (NKUA) */ @@ -37,22 +39,24 @@ public class ASLSession{ private static final long serialVersionUID = 1L; - private HashMap innerSession; - private long lastUsedTime; - private String externalSessionID; - private String username; - private String parentScope; - private String scope; - private String securityToken; - private HashMap notifiers; - String scopeName; - private ASLGroupModel groupModel; +// private HashMap innerSession; + private HttpSession session; + +// private long lastUsedTime; +// private String externalSessionID; +// private String username; +// private String parentScope; +// private String scope; +// private String securityToken; +// private HashMap notifiers; +// String scopeName; +// private ASLGroupModel groupModel; private boolean loggedIn = false; - private String userEmailAddress; - private String fullName; - private String avatarId; - private GenderType gender; +// private String userEmailAddress; +// private String fullName; +// private String avatarId; +// private GenderType gender; private long sessionTimeout = -1; //if < 0, not set @@ -67,23 +71,38 @@ public class ASLSession{ * @param externalSessionId the external id * @param user the username */ - ASLSession(String externalSessionId, String user) + ASLSession(HttpSession session, String user) { - innerSession = new HashMap(); - notifiers = new HashMap(); - lastUsedTime = System.currentTimeMillis(); - username = user; - externalSessionID = externalSessionId; - groupModel = new ASLGroupModel(); + this.session = session; + session.setAttribute("notifiers", new HashMap()); + session.setAttribute("lastUsedTime", System.currentTimeMillis()); + session.setAttribute("username", user); + session.setAttribute("groupModel", new ASLGroupModel()); + +// innerSession = new HashMap(); +// notifiers = new HashMap(); +// lastUsedTime = System.currentTimeMillis(); +// username = user; +// externalSessionID = session.getId(); +// groupModel = new ASLGroupModel(); } private void initializeAttributes() { - for (String key:innerSession.keySet()) { + Enumeration sessAttrNames = session.getAttributeNames(); + while(sessAttrNames.hasMoreElements()){ + String key = sessAttrNames.nextElement(); if (key.equals("collectionsPresentableFields") || key.equals(SessionConstants.collectionsHierarchy)) { - innerSession.remove(key); + session.removeAttribute(key); break; } } + //REPLACED BY ABOVE +// for (String key:innerSession.keySet()) { +// if (key.equals("collectionsPresentableFields") || key.equals(SessionConstants.collectionsHierarchy)) { +// innerSession.remove(key); +// break; +// } +// } } /** @@ -95,6 +114,10 @@ public class ASLSession{ * @throws ParserConfigurationException */ public long getSessionTimeoutMillis() throws IOException, ParserConfigurationException { + + return session.getMaxInactiveInterval() * 1000; + + /* if(sessionTimeout > 0) //means that is already set to a value ! return sessionTimeout; int timeoutMins; @@ -145,6 +168,7 @@ public class ASLSession{ sessionTimeout = timeoutMins * 60000; //in milliseconds logger.info("Session Timeout is: " + sessionTimeout); return sessionTimeout; + */ } /** @@ -154,10 +178,13 @@ public class ASLSession{ * @return the new timeout in milliseconds */ public long increaseSessionTimeout(long milliseconds, boolean resetCounting){ - if(resetCounting) - lastUsedTime = System.currentTimeMillis(); - sessionTimeout += milliseconds; - return sessionTimeout; + int secs = (int)milliseconds/1000; + session.setMaxInactiveInterval(session.getMaxInactiveInterval() + secs); + return session.getMaxInactiveInterval()*1000; +// if(resetCounting) +// lastUsedTime = System.currentTimeMillis(); +// sessionTimeout += milliseconds; +// return sessionTimeout; } @@ -166,6 +193,14 @@ public class ASLSession{ */ public boolean isValid() { + try { + session.getCreationTime(); + } catch (IllegalStateException ise) { + return false; + } + return true; + + /* long maxTime = -1; //it will never be -1 try { maxTime = getSessionTimeoutMillis(); @@ -174,15 +209,21 @@ public class ASLSession{ if((System.currentTimeMillis() - lastUsedTime) > maxTime) return false; return true; + */ } /** + * SHOULD NOT BE USED + * * @return whether the session is empty or not */ + @Deprecated public boolean isEmpty() { - lastUsedTime = System.currentTimeMillis(); - return innerSession.isEmpty(); +// lastUsedTime = System.currentTimeMillis(); + return getAttributeNames().isEmpty(); + //REPLACED BY ABOVE +// return innerSession.isEmpty(); } /** @@ -191,8 +232,10 @@ public class ASLSession{ */ public boolean hasAttribute(String name) { - lastUsedTime = System.currentTimeMillis(); - return innerSession.containsKey(name); +// lastUsedTime = System.currentTimeMillis(); + return (session.getAttribute(name)==null) ? false : true; + //REPLACED BY ABOVE +// return innerSession.containsKey(name); } /** @@ -200,8 +243,14 @@ public class ASLSession{ */ public Set getAttributeNames() { - lastUsedTime = System.currentTimeMillis(); - return innerSession.keySet(); +// lastUsedTime = System.currentTimeMillis(); + Enumeration names = session.getAttributeNames(); + Set output = new HashSet(); + while(names.hasMoreElements()) + output.add(names.nextElement()); + return output; + //REPLACED BY ABOVE +// return innerSession.keySet(); } /** @@ -213,12 +262,14 @@ public class ASLSession{ @Deprecated public Object getAttribute(String name) { - lastUsedTime = System.currentTimeMillis(); - return innerSession.get(name); +// lastUsedTime = System.currentTimeMillis(); + return session.getAttribute(name); + //REPLACED BY ABOVE +// return innerSession.get(name); } /** - * ASLSession is deprecated. Should not store attributes here. + * ASLSession is deprecated. * * @param name the name of the attribute * @param value the value of the attribute @@ -226,17 +277,20 @@ public class ASLSession{ @Deprecated public void setAttribute(String name, Object value) { - lastUsedTime = System.currentTimeMillis(); - innerSession.put(name, value); +// lastUsedTime = System.currentTimeMillis(); + session.setAttribute(name, value); + //REPLACED BY ABOVE +// innerSession.put(name, value); } public String getOriginalScopeName() { - return scopeName; + return (String) session.getAttribute("scopeName"); + //return scopeName; } /** * - * ASLSession is deprecated. Should not store attributes here. + * ASLSession is deprecated. * * * @param name the name of the attribute @@ -245,23 +299,32 @@ public class ASLSession{ @Deprecated public Object removeAttribute(String name) { - lastUsedTime = System.currentTimeMillis(); - return innerSession.remove(name); +// lastUsedTime = System.currentTimeMillis(); + Object attr = session.getAttribute(name); + session.removeAttribute(name); + return attr; + //REPLACED BY ABOVE +// return innerSession.remove(name); } /** + * DO NOT USE THIS + * * Removes all the attributes from the session */ + @Deprecated public void removeAll() { - lastUsedTime = System.currentTimeMillis(); - innerSession.clear(); +// lastUsedTime = System.currentTimeMillis(); + for(String name : getAttributeNames()) + session.removeAttribute(name); + //REPLACED BY ABOVE +// innerSession.clear(); } public String getParentScope(){ ScopeBean bean = new ScopeBean(getScope()); - parentScope = bean.enclosingScope().toString(); - return parentScope; + return bean.enclosingScope().toString(); } /** @@ -269,85 +332,65 @@ public class ASLSession{ */ public void invalidate() { - long maxTime = -1; //it will never be -1 - try { - maxTime = getSessionTimeoutMillis(); - } catch (Exception e) { e.printStackTrace();} + session.setMaxInactiveInterval(0); - lastUsedTime = System.currentTimeMillis() - maxTime - 120000; // 2 minutes excessive +// long maxTime = -1; //it will never be -1 +// try { +// maxTime = getSessionTimeoutMillis(); +// } catch (Exception e) { e.printStackTrace();} +// +// lastUsedTime = System.currentTimeMillis() - maxTime - 120000; // 2 minutes excessive } - /** - * @return the credential - */ -/* - DO NOT FORGET TO COMMENT OUT THIS WHEN THE NEW SECURITY MODEL IS AVAILABLE - - public ExtendedGSSCredential getCredential() { - return credential; - } - -*/ /** * @return the external session id (passed to the constructor) */ public String getExternalSessionID() { - return externalSessionID; + return session.getId(); +// return externalSessionID; } /** * @return the username */ public String getUsername() { - return username; + return (String)session.getAttribute("username"); +// return username; } /** * @return the scope */ public String getScope() { - if(scope==null) + String scp = (String)session.getAttribute("scope"); + if(scp==null) logger.debug("Scope is null, returning null"); - return scope; + return scp; } /** * @return the name of the scope (VRE) */ public String getScopeName(){ - if(scope==null) + String scp = (String)session.getAttribute("scope"); + if(scp==null) logger.debug("Scope is null, returning null"); - return scope; + return scp; } /** * @param scope the scope name (VRE) */ public void setScope(String scope) { + logger.info("The scope about to set is: " + scope); - lastUsedTime = System.currentTimeMillis(); - /* - 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]; - */ -// String currentScope = ScopeProvider.instance.get(); -// logger.info("GCube scope returns: " + currentScope); - String previousScopeName = this.scopeName; - this.scope = scope; - this.scopeName = scope; +// lastUsedTime = System.currentTimeMillis(); + String previousScopeName = (String)session.getAttribute("scope"); + session.setAttribute("scope", scope); + session.setAttribute("scopeName", scope); ScopeProvider.instance.set(scope); -/* DO NOT FORGET TO ADD THIS WHEN THE NEW SECURITY MODEL IS AVAILABLE ! - - 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 @@ -357,12 +400,13 @@ public class ASLSession{ logger.debug("Passing the logging because the variable was set"); return; } - if ((previousScopeName != null && !previousScopeName.equals(scopeName)) || previousScopeName == null) { + if ((previousScopeName != null && !previousScopeName.equals((String)session.getAttribute("scope"))) || previousScopeName == null) { logger.info("Logging the entrance"); - innerSession.clear(); + //TODO: Should do something with the below line +// innerSession.clear(); // ACCESS LOGGER LoginToVreAccessLogEntry loginEntry = new LoginToVreAccessLogEntry(); - accessLogger.logEntry(username, scope, loginEntry); + accessLogger.logEntry((String)session.getAttribute("username"), scope, loginEntry); } else logger.debug("Passing the logging because the scope was the same"); initializeAttributes(); @@ -370,18 +414,30 @@ public class ASLSession{ } + @SuppressWarnings("unchecked") + private void addNotifier(String key, Notifier value){ + ((HashMap)session.getAttribute("notifiers")).put(key, value); + } + + @SuppressWarnings("unchecked") + private Notifier getNotifier(String key){ + return ((HashMap)session.getAttribute("notifiers")).get(key); + } + public void setSecurityToken(String token){ SecurityTokenProvider.instance.set(token); - this.securityToken = token; + session.setAttribute("securityToken", token); + //REPLACED BY ABOVE +// this.securityToken = token; } public void logUserLogin(String scope) { - innerSession.clear(); +// innerSession.clear(); loggedIn = true; // ACCESS LOGGER LoginToVreAccessLogEntry loginEntry = new LoginToVreAccessLogEntry(); - accessLogger.logEntry(username, scope, loginEntry); + accessLogger.logEntry((String)session.getAttribute("username"), scope, loginEntry); } /** @@ -390,14 +446,14 @@ public class ASLSession{ */ public void waitNotification(String notification) throws InterruptedException { - Notifier notifier = notifiers.get(notification); + Notifier notifier = getNotifier(notification); if(notifier == null) { notifier = new Notifier(); - notifiers.put(notification, notifier); + addNotifier(notification, notifier); } - lastUsedTime = System.currentTimeMillis(); +// lastUsedTime = System.currentTimeMillis(); notifier.waitNotification(); } @@ -407,63 +463,89 @@ public class ASLSession{ */ public void notifyAllWaiting(String notification) throws InterruptedException { - Notifier notifier = notifiers.get(notification); + Notifier notifier = getNotifier(notification); if(notifier == null) { notifier = new Notifier(); - notifiers.put(notification, notifier); + addNotifier(notification, notifier); } - lastUsedTime = System.currentTimeMillis(); +// lastUsedTime = System.currentTimeMillis(); notifier.notifyAllWaiting(); } public void setGroupModelInfos(String groupName, long groupId) { - groupModel.setGroupName(groupName); - groupModel.setGroupId(groupId); + ASLGroupModel aslGM = ((ASLGroupModel)session.getAttribute("groupModel")); + aslGM.setGroupName(groupName); + aslGM.setGroupId(groupId); + session.setAttribute("groupModel", aslGM); + //REPLACED BY ABOVE +// groupModel.setGroupName(groupName); +// groupModel.setGroupId(groupId); } public long getGroupId() { - return groupModel.getGroupId(); + return ((ASLGroupModel)session.getAttribute("groupModel")).getGroupId(); + //REPLACED BY ABOVE +// return groupModel.getGroupId(); } public String getGroupName() { - return groupModel.getGroupName(); + return ((ASLGroupModel)session.getAttribute("groupModel")).getGroupName(); + //REPLACED BY ABOVE +// return groupModel.getGroupName(); } public void setUserEmailAddress(String email) { - this.userEmailAddress = email; + session.setAttribute("userEmailAddress", email); + //REPLACED BY ABOVE +// this.userEmailAddress = email; } public String getUserEmailAddress() { - return this.userEmailAddress; + return (String)session.getAttribute("userEmailAddress"); + //REPLACED BY ABOVE +// return this.userEmailAddress; } public void setUserFullName(String fullName) { - this.fullName = fullName; + session.setAttribute("fullName", fullName); + //REPLACED BY ABOVE +// this.fullName = fullName; } public String getUserFullName() { - return this.fullName; + return (String)session.getAttribute("fullName"); + //REPLACED BY ABOVE +// return this.fullName; } public void setUserAvatarId(String avatarId) { - this.avatarId = avatarId; + session.setAttribute("avatarId", avatarId); + //REPLACED BY ABOVE +// this.avatarId = avatarId; } public String getUserAvatarId() { - return this.avatarId; + return (String)session.getAttribute("avatarId"); + //REPLACED BY ABOVE +// return this.avatarId; } public void setUserGender(GenderType gender) { - this.gender = gender; + session.setAttribute("gender", gender); + //REPLACED BY ABOVE +// this.gender = gender; } public GenderType getUserGender() { - return this.gender; + return (GenderType)session.getAttribute("gender"); + //REPLACED BY ABOVE +// return this.gender; } public String getSecurityToken() { + String securityToken = (String)session.getAttribute("securityToken"); logger.debug("Getting security token: " + securityToken+" in thread "+Thread.currentThread().getId()); return securityToken; } diff --git a/src/main/java/org/gcube/application/framework/core/session/SessionManager.java b/src/main/java/org/gcube/application/framework/core/session/SessionManager.java index 9947178..466cc26 100644 --- a/src/main/java/org/gcube/application/framework/core/session/SessionManager.java +++ b/src/main/java/org/gcube/application/framework/core/session/SessionManager.java @@ -5,6 +5,8 @@ import java.util.HashMap; import java.util.Iterator; import java.util.Set; +import javax.servlet.http.HttpSession; + import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.scope.api.ScopeProvider; import org.slf4j.Logger; @@ -15,75 +17,81 @@ public class SessionManager { /** The logger. */ private static final Logger logger = LoggerFactory.getLogger(SessionManager.class); - protected static Thread thread = new CleanSessionThread(); +// protected static Thread thread = new CleanSessionThread(); protected static SessionManager sessionManager = new SessionManager(); protected HashMap sessions; protected SessionManager() { sessions = new HashMap(); - thread.setDaemon(true); - thread.start(); +// thread.setDaemon(true); +// thread.start(); } public static SessionManager getInstance() { return sessionManager; } + + public void setASLSession(HttpSession userSession, String username){ + ASLSession aslSession = new ASLSession(userSession, username); + sessions.put(userSession.getId() + "_" + username, aslSession); + } + public ASLSession getASLSession(String externalSessionID, String username) { ASLSession session = sessions.get(externalSessionID + "_" + username); - if(session == null || !session.isValid() || !session.getUsername().equals(username)) - { - session = new ASLSession(externalSessionID, username); - sessions.put(externalSessionID + "_" + username, session); - } - if (session.getScope()!=null) //covers first helper's invocation - ScopeProvider.instance.set(session.getScopeName()); - - if (session.getSecurityToken()!=null){ - logger.debug("Setting SecurityTokenProvider to: "+session.getSecurityToken()+" in thread "+Thread.currentThread().getId()); - SecurityTokenProvider.instance.set(session.getSecurityToken()); - } +// if(session == null || !session.isValid() || !session.getUsername().equals(username)) +// { +// session = new ASLSession(externalSessionID, username); +// sessions.put(externalSessionID + "_" + username, session); +// } +// if (session.getScope()!=null) //covers first helper's invocation +// ScopeProvider.instance.set(session.getScopeName()); +// +// if (session.getSecurityToken()!=null){ +// logger.debug("Setting SecurityTokenProvider to: "+session.getSecurityToken()+" in thread "+Thread.currentThread().getId()); +// SecurityTokenProvider.instance.set(session.getSecurityToken()); +// } return session; } - @Override - protected void finalize() throws Throwable { - thread.interrupt(); - logger.debug(new Date(System.currentTimeMillis()) + " clean thread was interrupted"); - thread.join(); - logger.debug(new Date(System.currentTimeMillis()) + " clean thread was joint"); - super.finalize(); - } +// @Override +// protected void finalize() throws Throwable { +// thread.interrupt(); +// logger.debug(new Date(System.currentTimeMillis()) + " clean thread was interrupted"); +// thread.join(); +// logger.debug(new Date(System.currentTimeMillis()) + " clean thread was joint"); +// super.finalize(); +// } - protected static class CleanSessionThread extends Thread - { - public void run() - { - while(true) - { - try { - Thread.sleep(300000); - } catch (InterruptedException e) { - logger.error("Exception:", e); - logger.debug(new Date(System.currentTimeMillis()) + " clean thread was interrupted (in clean thread)"); - break; - } - //TODO: cleanup invalid sessions: add locks... - Set keys = sessionManager.sessions.keySet(); - Iterator iter = keys.iterator(); - while(iter.hasNext()) - { - String extSessionID = iter.next(); - if(!sessionManager.sessions.get(extSessionID).isValid()) - { - sessionManager.sessions.remove(extSessionID); - } - } - } - logger.debug(new Date(System.currentTimeMillis()) + " clean thread was terminated"); - } - - } +// protected static class CleanSessionThread extends Thread +// { +// public void run() +// { +// while(true) +// { +// try { +// Thread.sleep(300000); +// } catch (InterruptedException e) { +// logger.error("Exception:", e); +// logger.debug(new Date(System.currentTimeMillis()) + " clean thread was interrupted (in clean thread)"); +// break; +// } +// //TODO: cleanup invalid sessions: add locks... +// Set keys = sessionManager.sessions.keySet(); +// Iterator iter = keys.iterator(); +// while(iter.hasNext()) +// { +// String extSessionID = iter.next(); +// if(!sessionManager.sessions.get(extSessionID).isValid()) +// { +// sessionManager.sessions.remove(extSessionID); +// } +// } +// } +// logger.debug(new Date(System.currentTimeMillis()) + " clean thread was terminated"); +// } +// +// } }