From 2db3cfde05d03f96c3b3d2cdcee5c06136df8cb5 Mon Sep 17 00:00:00 2001 From: "nikolas.laskaris" Date: Mon, 19 Sep 2016 11:56:53 +0000 Subject: [PATCH] git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/application-support-layer/applicationSupportLayerCore@131484 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../framework/core/session/ASLSession.java | 429 ++++++++++++++---- 1 file changed, 331 insertions(+), 98 deletions(-) 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 665e91d..5584ad6 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 @@ -39,24 +39,24 @@ public class ASLSession{ private static final long serialVersionUID = 1L; -// private HashMap innerSession; + 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 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,24 +67,26 @@ public class ASLSession{ AccessLogger accessLogger = AccessLogger.getAccessLogger(); /** - * A constructor based on the user and an external ID - * @param externalSessionId the external id + * A constructor based on the user and a HttpSession + * @param session the HttpSession * @param user the username */ ASLSession(HttpSession session, String user) { + //NEW 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(); + //OLD, for backwards compatibility + innerSession = new HashMap(); + notifiers = new HashMap(); + lastUsedTime = System.currentTimeMillis(); + username = user; + externalSessionID = session.getId(); + groupModel = new ASLGroupModel(); } private void initializeAttributes() { @@ -96,13 +98,13 @@ public class ASLSession{ break; } } - //REPLACED BY ABOVE -// for (String key:innerSession.keySet()) { -// if (key.equals("collectionsPresentableFields") || key.equals(SessionConstants.collectionsHierarchy)) { -// innerSession.remove(key); -// break; -// } -// } + //for backwards compatibility + for (String key:innerSession.keySet()) { + if (key.equals("collectionsPresentableFields") || key.equals(SessionConstants.collectionsHierarchy)) { + innerSession.remove(key); + break; + } + } } /** @@ -115,8 +117,11 @@ public class ASLSession{ */ public long getSessionTimeoutMillis() throws IOException, ParserConfigurationException { + //NEW WAY, BASED ON HTTPSESSION return session.getMaxInactiveInterval() * 1000; + + //OLD WAY TO CHECK THE SESSION TIMEOUT (WILL BECOME DEPRECATED). /* if(sessionTimeout > 0) //means that is already set to a value ! return sessionTimeout; @@ -178,9 +183,12 @@ public class ASLSession{ * @return the new timeout in milliseconds */ public long increaseSessionTimeout(long milliseconds, boolean resetCounting){ + //NEW WAY int secs = (int)milliseconds/1000; session.setMaxInactiveInterval(session.getMaxInactiveInterval() + secs); return session.getMaxInactiveInterval()*1000; + + //OLD WAY, for backwards compatibility // if(resetCounting) // lastUsedTime = System.currentTimeMillis(); // sessionTimeout += milliseconds; @@ -193,6 +201,8 @@ public class ASLSession{ */ public boolean isValid() { + + //NEW WAY try { session.getCreationTime(); } catch (IllegalStateException ise) { @@ -200,6 +210,7 @@ public class ASLSession{ } return true; + //OLD WAY /* long maxTime = -1; //it will never be -1 try { @@ -220,9 +231,10 @@ public class ASLSession{ @Deprecated public boolean isEmpty() { -// lastUsedTime = System.currentTimeMillis(); + //NEW WAY return getAttributeNames().isEmpty(); - //REPLACED BY ABOVE + //OLD WAY, for backwards compatibility +// lastUsedTime = System.currentTimeMillis(); // return innerSession.isEmpty(); } @@ -232,9 +244,22 @@ public class ASLSession{ */ public boolean hasAttribute(String name) { + //HYBRID, TEMPORARY SOLUTION FOR FEATURE #5033 + lastUsedTime = System.currentTimeMillis(); + if(innerSession.containsKey(name)) + return true; + if(!innerSession.containsKey(name) && (session.getAttribute(name)==null)) + return false; + if(!innerSession.containsKey(name) && (session.getAttribute(name)!=null)){ + innerSession.put(name, session.getAttribute(name)); + return true; + } + + //NEW WAY +// return (session.getAttribute(name)==null) ? false : true; + + //OLD WAY, for backwards compatibility // lastUsedTime = System.currentTimeMillis(); - return (session.getAttribute(name)==null) ? false : true; - //REPLACED BY ABOVE // return innerSession.containsKey(name); } @@ -243,13 +268,24 @@ public class ASLSession{ */ public Set getAttributeNames() { -// lastUsedTime = System.currentTimeMillis(); + //HYBRID, TEMPORARY SOLUTION FOR FEATURE #5033 Enumeration names = session.getAttributeNames(); Set output = new HashSet(); while(names.hasMoreElements()) output.add(names.nextElement()); + output.addAll(innerSession.keySet()); return output; - //REPLACED BY ABOVE + + + //NEW WAY +// Enumeration names = session.getAttributeNames(); +// Set output = new HashSet(); +// while(names.hasMoreElements()) +// output.add(names.nextElement()); +// return output; + + //OLD WAY, for backwards compatibility +// lastUsedTime = System.currentTimeMillis(); // return innerSession.keySet(); } @@ -262,9 +298,23 @@ public class ASLSession{ @Deprecated public Object getAttribute(String name) { + //HYBRID, TEMPORARY SOLUTION FOR FEATURE #5033 + lastUsedTime = System.currentTimeMillis(); + Object innerObj = innerSession.get(name); + Object httpObj = session.getAttribute(name); + if(innerObj != null) + return innerObj; + else if((httpObj!=null)){ + innerSession.put(name, httpObj); + return httpObj; + } + return null; + + + //NEW WAY +// return session.getAttribute(name); + //OLD WAY, for backwards compatibility // lastUsedTime = System.currentTimeMillis(); - return session.getAttribute(name); - //REPLACED BY ABOVE // return innerSession.get(name); } @@ -277,15 +327,29 @@ public class ASLSession{ @Deprecated public void setAttribute(String name, Object value) { -// lastUsedTime = System.currentTimeMillis(); + //NEW WAY session.setAttribute(name, value); - //REPLACED BY ABOVE -// innerSession.put(name, value); + //OLD WAY, for backwards compatibility + lastUsedTime = System.currentTimeMillis(); + innerSession.put(name, value); } public String getOriginalScopeName() { - return (String) session.getAttribute("scopeName"); - //return scopeName; + + //HYBRID, TEMPORARY SOLUTION FOR FEATURE #5033 + String httpSessionScope = (String) session.getAttribute("scopeName"); + if(scopeName != null) + return scopeName; + else if(httpSessionScope != null){ + scopeName = httpSessionScope; + return httpSessionScope; + } + return null; + + //NEW WAY +// return (String) session.getAttribute("scopeName"); + //OLD WAY, for backwards compatibility +// return scopeName; } /** @@ -299,11 +363,19 @@ public class ASLSession{ @Deprecated public Object removeAttribute(String name) { -// lastUsedTime = System.currentTimeMillis(); - Object attr = session.getAttribute(name); + //HYBRID, TEMPORARY SOLUTION FOR FEATURE #5033 + lastUsedTime = System.currentTimeMillis(); + Object httpAttrib = session.getAttribute(name); session.removeAttribute(name); - return attr; - //REPLACED BY ABOVE + Object innerAttrib = innerSession.remove(name); + return (httpAttrib == null) ? innerAttrib : httpAttrib; + + //NEW WAY +// Object attr = session.getAttribute(name); +// session.removeAttribute(name); +// return attr; + //OLD WAY, for backwards compatibility +// lastUsedTime = System.currentTimeMillis(); // return innerSession.remove(name); } @@ -315,11 +387,12 @@ public class ASLSession{ @Deprecated public void removeAll() { -// lastUsedTime = System.currentTimeMillis(); + //NEW WAY for(String name : getAttributeNames()) session.removeAttribute(name); - //REPLACED BY ABOVE -// innerSession.clear(); + //OLD WAY, for backwards compatibility + lastUsedTime = System.currentTimeMillis(); + innerSession.clear(); } public String getParentScope(){ @@ -332,22 +405,25 @@ public class ASLSession{ */ public void invalidate() { + //NEW WAY session.setMaxInactiveInterval(0); + //OLD WAY, for backwards compatibility + long maxTime = -1; //it will never be -1 + try { + maxTime = getSessionTimeoutMillis(); + } catch (Exception e) { e.printStackTrace();} -// long maxTime = -1; //it will never be -1 -// try { -// maxTime = getSessionTimeoutMillis(); -// } catch (Exception e) { e.printStackTrace();} -// -// lastUsedTime = System.currentTimeMillis() - maxTime - 120000; // 2 minutes excessive + lastUsedTime = System.currentTimeMillis() - maxTime - 120000; // 2 minutes excessive } /** - * @return the external session id (passed to the constructor) + * @return the session id */ public String getExternalSessionID() { + //NEW WAY return session.getId(); + //OLD WAY // return externalSessionID; } @@ -355,7 +431,20 @@ public class ASLSession{ * @return the username */ public String getUsername() { - return (String)session.getAttribute("username"); + + //HYBRID, TEMPORARY SOLUTION FOR FEATURE #5033 + String httpUN = (String)session.getAttribute("username"); + if(username != null) + return username; + else if(httpUN != null){ + username = httpUN; + return httpUN; + } + return null; + + //NEW WAY +// return (String)session.getAttribute("username"); + //OLD WAY, for backwards compatibility // return username; } @@ -363,34 +452,78 @@ public class ASLSession{ * @return the scope */ public String getScope() { - String scp = (String)session.getAttribute("scope"); - if(scp==null) - logger.debug("Scope is null, returning null"); - return scp; + //HYBRID, TEMPORARY SOLUTION FOR FEATURE #5033 + String httpScope = (String)session.getAttribute("scope"); + if(scope!=null) + return scope; + else if(httpScope != null){ + scope = httpScope; + return httpScope; + } + return null; + + + //NEW WAY +// String scp = (String)session.getAttribute("scope"); +// if(scp==null) +// logger.debug("Scope is null, returning null"); +// return scp; + //OLD WAY, for backwards compatibility +// if(scope==null) +// logger.debug("Scope is null, returning null"); +// return scope; } /** * @return the name of the scope (VRE) */ public String getScopeName(){ - String scp = (String)session.getAttribute("scope"); - if(scp==null) - logger.debug("Scope is null, returning null"); - return scp; + + //HYBRID, TEMPORARY SOLUTION FOR FEATURE #5033 + String httpScope = (String)session.getAttribute("scope"); + if(scope!=null) + return scope; + else if(httpScope != null){ + scope = httpScope; + return httpScope; + } + return null; + + //NEW WAY +// String scp = (String)session.getAttribute("scope"); +// if(scp==null) +// logger.debug("Scope is null, returning null"); +// return scp; + //OLD WAY, for backwards compatibility +// if(scope==null) +// logger.debug("Scope is null, returning null"); +// return scope; } /** * @param scope the scope name (VRE) */ public void setScope(String scope) { + String previousScopeName = null; + //NEW WAY logger.info("The scope about to set is: " + scope); -// lastUsedTime = System.currentTimeMillis(); - String previousScopeName = (String)session.getAttribute("scope"); + previousScopeName = (String)session.getAttribute("scope"); session.setAttribute("scope", scope); session.setAttribute("scopeName", scope); ScopeProvider.instance.set(scope); + //OLD WAY, for backwards compatibility + logger.info("The scope about to set is: " + scope); + lastUsedTime = System.currentTimeMillis(); +// String currentScope = ScopeProvider.instance.get(); +// logger.info("GCube scope returns: " + currentScope); + previousScopeName = this.scopeName; + this.scope = scope; + this.scopeName = scope; + ScopeProvider.instance.set(scope); + + //THE BELOW PART REMAINS THE SAME FOR BOTH NEW AND OLD // 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 @@ -411,7 +544,7 @@ public class ASLSession{ logger.debug("Passing the logging because the scope was the same"); initializeAttributes(); - + } @SuppressWarnings("unchecked") @@ -426,10 +559,11 @@ public class ASLSession{ public void setSecurityToken(String token){ + //NEW WAY SecurityTokenProvider.instance.set(token); session.setAttribute("securityToken", token); - //REPLACED BY ABOVE -// this.securityToken = token; + //OLD WAY, for backwards compatibility + this.securityToken = token; } public void logUserLogin(String scope) { @@ -453,7 +587,7 @@ public class ASLSession{ addNotifier(notification, notifier); } -// lastUsedTime = System.currentTimeMillis(); + lastUsedTime = System.currentTimeMillis(); notifier.waitNotification(); } @@ -470,83 +604,182 @@ public class ASLSession{ addNotifier(notification, notifier); } -// lastUsedTime = System.currentTimeMillis(); + lastUsedTime = System.currentTimeMillis(); notifier.notifyAllWaiting(); } public void setGroupModelInfos(String groupName, long groupId) { + //NEW WAY 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); + //OLD WAY, for backwards compatibility + groupModel.setGroupName(groupName); + groupModel.setGroupId(groupId); } public long getGroupId() { - return ((ASLGroupModel)session.getAttribute("groupModel")).getGroupId(); - //REPLACED BY ABOVE + + //HYBRID, TEMPORARY SOLUTION FOR FEATURE #5033 + ASLGroupModel agmHttp = (ASLGroupModel) session.getAttribute("groupModel"); + if(groupModel != null) + return groupModel.getGroupId(); + else if(agmHttp != null){ + groupModel = agmHttp; + return agmHttp.getGroupId(); + } + return Long.MIN_VALUE; //should throw an exception instead... but long's primitive and we need not to change the api + + //NEW WAY +// return ((ASLGroupModel)session.getAttribute("groupModel")).getGroupId(); + //OLD WAY, for backwards compatibility // return groupModel.getGroupId(); } public String getGroupName() { - return ((ASLGroupModel)session.getAttribute("groupModel")).getGroupName(); - //REPLACED BY ABOVE + + //HYBRID, TEMPORARY SOLUTION FOR FEATURE #5033 + ASLGroupModel agmHttp = (ASLGroupModel) session.getAttribute("groupModel"); + if(groupModel != null) + return groupModel.getGroupName(); + else if(agmHttp != null){ + groupModel = agmHttp; + return agmHttp.getGroupName(); + } + return null; + + //NEW WAY +// return ((ASLGroupModel)session.getAttribute("groupModel")).getGroupName(); + //OLD WAY, for backwards compatibility // return groupModel.getGroupName(); } public void setUserEmailAddress(String email) { + //NEW WAY session.setAttribute("userEmailAddress", email); - //REPLACED BY ABOVE -// this.userEmailAddress = email; + //OLD WAY, for backwards compatibility + this.userEmailAddress = email; } public String getUserEmailAddress() { - return (String)session.getAttribute("userEmailAddress"); - //REPLACED BY ABOVE + + //HYBRID, TEMPORARY SOLUTION FOR FEATURE #5033 + String httpUEA = (String)session.getAttribute("userEmailAddress"); + if(userEmailAddress!=null) + return userEmailAddress; + else if(httpUEA != null){ + userEmailAddress = httpUEA; + return httpUEA; + } + return null; + + + //NEW WAY +// return (String)session.getAttribute("userEmailAddress"); + //OLD WAY, for backwards compatibility // return this.userEmailAddress; } public void setUserFullName(String fullName) { + //NEW WAY session.setAttribute("fullName", fullName); - //REPLACED BY ABOVE -// this.fullName = fullName; + //OLD WAY, for backwards compatibility + this.fullName = fullName; } public String getUserFullName() { - return (String)session.getAttribute("fullName"); - //REPLACED BY ABOVE + + //HYBRID, TEMPORARY SOLUTION FOR FEATURE #5033 + String httpFN = (String)session.getAttribute("fullName"); + if(fullName != null) + return this.fullName; + else if(httpFN != null){ + this.fullName = httpFN; + return httpFN; + } + return null; + + + //NEW WAY +// return (String)session.getAttribute("fullName"); + //OLD WAY, for backwards compatibility // return this.fullName; } public void setUserAvatarId(String avatarId) { + //NEW WAY session.setAttribute("avatarId", avatarId); - //REPLACED BY ABOVE -// this.avatarId = avatarId; + //OLD WAY, for backwards compatibility + this.avatarId = avatarId; } public String getUserAvatarId() { - return (String)session.getAttribute("avatarId"); - //REPLACED BY ABOVE + + //HYBRID, TEMPORARY SOLUTION FOR FEATURE #5033 + String httpUAid = (String)session.getAttribute("avatarId"); + if(this.avatarId != null) + return this.avatarId; + else if(httpUAid != null){ + this.avatarId = httpUAid; + return httpUAid; + } + return null; + + //NEW WAY +// return (String)session.getAttribute("avatarId"); + //OLD WAY, for backwards compatibility // return this.avatarId; } public void setUserGender(GenderType gender) { + //NEW WAY session.setAttribute("gender", gender); - //REPLACED BY ABOVE -// this.gender = gender; + //OLD WAY, for backwards compatibility + this.gender = gender; } + public GenderType getUserGender() { - return (GenderType)session.getAttribute("gender"); - //REPLACED BY ABOVE + + //HYBRID, TEMPORARY SOLUTION FOR FEATURE #5033 + Object httpGObj = session.getAttribute("gender"); + if(this.gender != null) + return this.gender; + else if(httpGObj != null){ + this.gender = (GenderType) httpGObj; + return (GenderType) httpGObj; + } + return null; + + //NEW WAY +// return (GenderType)session.getAttribute("gender"); + //OLD WAY, for backwards compatibility // 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; + + //HYBRID, TEMPORARY SOLUTION FOR FEATURE #5033 + String httpSecurityToken = (String)session.getAttribute("securityToken"); + if(this.securityToken != null){ + logger.debug("Getting security token: " + this.securityToken + " in thread "+Thread.currentThread().getId()); + return this.securityToken; + } + else if(httpSecurityToken != null){ + logger.debug("Getting security token: " + httpSecurityToken + " in thread "+Thread.currentThread().getId()); + this.securityToken = httpSecurityToken; + return httpSecurityToken; + } + return null; //if reached this point, means that all security tokens are null + + //NEW WAY +// String securityToken = (String)session.getAttribute("securityToken"); +// logger.debug("Getting security token: " + securityToken+" in thread "+Thread.currentThread().getId()); +// return securityToken; + //OLD WAY, for backwards compatibility +// logger.debug("Getting security token: " + securityToken+" in thread "+Thread.currentThread().getId()); +// return securityToken; + } }