nikolas.laskaris 2016-09-19 11:56:53 +00:00
parent bc6696d189
commit 2db3cfde05
1 changed files with 331 additions and 98 deletions

View File

@ -39,24 +39,24 @@ public class ASLSession{
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
// private HashMap<String, Object> innerSession; private HashMap<String, Object> innerSession;
private HttpSession session; private HttpSession session;
// private long lastUsedTime; private long lastUsedTime;
// private String externalSessionID; private String externalSessionID;
// private String username; private String username;
// private String parentScope; private String parentScope;
// private String scope; private String scope;
// private String securityToken; private String securityToken;
// private HashMap<String, Notifier> notifiers; private HashMap<String, Notifier> notifiers;
// String scopeName; String scopeName;
// private ASLGroupModel groupModel; private ASLGroupModel groupModel;
private boolean loggedIn = false; private boolean loggedIn = false;
// private String userEmailAddress; private String userEmailAddress;
// private String fullName; private String fullName;
// private String avatarId; private String avatarId;
// private GenderType gender; private GenderType gender;
private long sessionTimeout = -1; //if < 0, not set private long sessionTimeout = -1; //if < 0, not set
@ -67,24 +67,26 @@ public class ASLSession{
AccessLogger accessLogger = AccessLogger.getAccessLogger(); AccessLogger accessLogger = AccessLogger.getAccessLogger();
/** /**
* A constructor based on the user and an external ID * A constructor based on the user and a HttpSession
* @param externalSessionId the external id * @param session the HttpSession
* @param user the username * @param user the username
*/ */
ASLSession(HttpSession session, String user) ASLSession(HttpSession session, String user)
{ {
//NEW
this.session = session; this.session = session;
session.setAttribute("notifiers", new HashMap<String, Notifier>()); session.setAttribute("notifiers", new HashMap<String, Notifier>());
session.setAttribute("lastUsedTime", System.currentTimeMillis()); session.setAttribute("lastUsedTime", System.currentTimeMillis());
session.setAttribute("username", user); session.setAttribute("username", user);
session.setAttribute("groupModel", new ASLGroupModel()); session.setAttribute("groupModel", new ASLGroupModel());
// innerSession = new HashMap<String, Object>(); //OLD, for backwards compatibility
// notifiers = new HashMap<String, Notifier>(); innerSession = new HashMap<String, Object>();
// lastUsedTime = System.currentTimeMillis(); notifiers = new HashMap<String, Notifier>();
// username = user; lastUsedTime = System.currentTimeMillis();
// externalSessionID = session.getId(); username = user;
// groupModel = new ASLGroupModel(); externalSessionID = session.getId();
groupModel = new ASLGroupModel();
} }
private void initializeAttributes() { private void initializeAttributes() {
@ -96,13 +98,13 @@ public class ASLSession{
break; break;
} }
} }
//REPLACED BY ABOVE //for backwards compatibility
// for (String key:innerSession.keySet()) { for (String key:innerSession.keySet()) {
// if (key.equals("collectionsPresentableFields") || key.equals(SessionConstants.collectionsHierarchy)) { if (key.equals("collectionsPresentableFields") || key.equals(SessionConstants.collectionsHierarchy)) {
// innerSession.remove(key); innerSession.remove(key);
// break; break;
// } }
// } }
} }
/** /**
@ -115,8 +117,11 @@ public class ASLSession{
*/ */
public long getSessionTimeoutMillis() throws IOException, ParserConfigurationException { public long getSessionTimeoutMillis() throws IOException, ParserConfigurationException {
//NEW WAY, BASED ON HTTPSESSION
return session.getMaxInactiveInterval() * 1000; 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 ! if(sessionTimeout > 0) //means that is already set to a value !
return sessionTimeout; return sessionTimeout;
@ -178,9 +183,12 @@ public class ASLSession{
* @return the new timeout in milliseconds * @return the new timeout in milliseconds
*/ */
public long increaseSessionTimeout(long milliseconds, boolean resetCounting){ public long increaseSessionTimeout(long milliseconds, boolean resetCounting){
//NEW WAY
int secs = (int)milliseconds/1000; int secs = (int)milliseconds/1000;
session.setMaxInactiveInterval(session.getMaxInactiveInterval() + secs); session.setMaxInactiveInterval(session.getMaxInactiveInterval() + secs);
return session.getMaxInactiveInterval()*1000; return session.getMaxInactiveInterval()*1000;
//OLD WAY, for backwards compatibility
// if(resetCounting) // if(resetCounting)
// lastUsedTime = System.currentTimeMillis(); // lastUsedTime = System.currentTimeMillis();
// sessionTimeout += milliseconds; // sessionTimeout += milliseconds;
@ -193,6 +201,8 @@ public class ASLSession{
*/ */
public boolean isValid() public boolean isValid()
{ {
//NEW WAY
try { try {
session.getCreationTime(); session.getCreationTime();
} catch (IllegalStateException ise) { } catch (IllegalStateException ise) {
@ -200,6 +210,7 @@ public class ASLSession{
} }
return true; return true;
//OLD WAY
/* /*
long maxTime = -1; //it will never be -1 long maxTime = -1; //it will never be -1
try { try {
@ -220,9 +231,10 @@ public class ASLSession{
@Deprecated @Deprecated
public boolean isEmpty() public boolean isEmpty()
{ {
// lastUsedTime = System.currentTimeMillis(); //NEW WAY
return getAttributeNames().isEmpty(); return getAttributeNames().isEmpty();
//REPLACED BY ABOVE //OLD WAY, for backwards compatibility
// lastUsedTime = System.currentTimeMillis();
// return innerSession.isEmpty(); // return innerSession.isEmpty();
} }
@ -232,9 +244,22 @@ public class ASLSession{
*/ */
public boolean hasAttribute(String name) 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(); // lastUsedTime = System.currentTimeMillis();
return (session.getAttribute(name)==null) ? false : true;
//REPLACED BY ABOVE
// return innerSession.containsKey(name); // return innerSession.containsKey(name);
} }
@ -243,13 +268,24 @@ public class ASLSession{
*/ */
public Set<String> getAttributeNames() public Set<String> getAttributeNames()
{ {
// lastUsedTime = System.currentTimeMillis(); //HYBRID, TEMPORARY SOLUTION FOR FEATURE #5033
Enumeration<String> names = session.getAttributeNames(); Enumeration<String> names = session.getAttributeNames();
Set<String> output = new HashSet<String>(); Set<String> output = new HashSet<String>();
while(names.hasMoreElements()) while(names.hasMoreElements())
output.add(names.nextElement()); output.add(names.nextElement());
output.addAll(innerSession.keySet());
return output; return output;
//REPLACED BY ABOVE
//NEW WAY
// Enumeration<String> names = session.getAttributeNames();
// Set<String> output = new HashSet<String>();
// while(names.hasMoreElements())
// output.add(names.nextElement());
// return output;
//OLD WAY, for backwards compatibility
// lastUsedTime = System.currentTimeMillis();
// return innerSession.keySet(); // return innerSession.keySet();
} }
@ -262,9 +298,23 @@ public class ASLSession{
@Deprecated @Deprecated
public Object getAttribute(String name) 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(); // lastUsedTime = System.currentTimeMillis();
return session.getAttribute(name);
//REPLACED BY ABOVE
// return innerSession.get(name); // return innerSession.get(name);
} }
@ -277,15 +327,29 @@ public class ASLSession{
@Deprecated @Deprecated
public void setAttribute(String name, Object value) public void setAttribute(String name, Object value)
{ {
// lastUsedTime = System.currentTimeMillis(); //NEW WAY
session.setAttribute(name, value); session.setAttribute(name, value);
//REPLACED BY ABOVE //OLD WAY, for backwards compatibility
// innerSession.put(name, value); lastUsedTime = System.currentTimeMillis();
innerSession.put(name, value);
} }
public String getOriginalScopeName() { 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 @Deprecated
public Object removeAttribute(String name) public Object removeAttribute(String name)
{ {
// lastUsedTime = System.currentTimeMillis(); //HYBRID, TEMPORARY SOLUTION FOR FEATURE #5033
Object attr = session.getAttribute(name); lastUsedTime = System.currentTimeMillis();
Object httpAttrib = session.getAttribute(name);
session.removeAttribute(name); session.removeAttribute(name);
return attr; Object innerAttrib = innerSession.remove(name);
//REPLACED BY ABOVE 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); // return innerSession.remove(name);
} }
@ -315,11 +387,12 @@ public class ASLSession{
@Deprecated @Deprecated
public void removeAll() public void removeAll()
{ {
// lastUsedTime = System.currentTimeMillis(); //NEW WAY
for(String name : getAttributeNames()) for(String name : getAttributeNames())
session.removeAttribute(name); session.removeAttribute(name);
//REPLACED BY ABOVE //OLD WAY, for backwards compatibility
// innerSession.clear(); lastUsedTime = System.currentTimeMillis();
innerSession.clear();
} }
public String getParentScope(){ public String getParentScope(){
@ -332,22 +405,25 @@ public class ASLSession{
*/ */
public void invalidate() public void invalidate()
{ {
//NEW WAY
session.setMaxInactiveInterval(0); 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 lastUsedTime = System.currentTimeMillis() - maxTime - 120000; // 2 minutes excessive
// try {
// maxTime = getSessionTimeoutMillis();
// } catch (Exception e) { e.printStackTrace();}
//
// lastUsedTime = System.currentTimeMillis() - maxTime - 120000; // 2 minutes excessive
} }
/** /**
* @return the external session id (passed to the constructor) * @return the session id
*/ */
public String getExternalSessionID() { public String getExternalSessionID() {
//NEW WAY
return session.getId(); return session.getId();
//OLD WAY
// return externalSessionID; // return externalSessionID;
} }
@ -355,7 +431,20 @@ public class ASLSession{
* @return the username * @return the username
*/ */
public String getUsername() { 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; // return username;
} }
@ -363,34 +452,78 @@ public class ASLSession{
* @return the scope * @return the scope
*/ */
public String getScope() { public String getScope() {
String scp = (String)session.getAttribute("scope"); //HYBRID, TEMPORARY SOLUTION FOR FEATURE #5033
if(scp==null) String httpScope = (String)session.getAttribute("scope");
logger.debug("Scope is null, returning null"); if(scope!=null)
return scp; 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) * @return the name of the scope (VRE)
*/ */
public String getScopeName(){ public String getScopeName(){
String scp = (String)session.getAttribute("scope");
if(scp==null) //HYBRID, TEMPORARY SOLUTION FOR FEATURE #5033
logger.debug("Scope is null, returning null"); String httpScope = (String)session.getAttribute("scope");
return scp; 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) * @param scope the scope name (VRE)
*/ */
public void setScope(String scope) { public void setScope(String scope) {
String previousScopeName = null;
//NEW WAY
logger.info("The scope about to set is: " + scope); logger.info("The scope about to set is: " + scope);
// lastUsedTime = System.currentTimeMillis(); previousScopeName = (String)session.getAttribute("scope");
String previousScopeName = (String)session.getAttribute("scope");
session.setAttribute("scope", scope); session.setAttribute("scope", scope);
session.setAttribute("scopeName", scope); session.setAttribute("scopeName", scope);
ScopeProvider.instance.set(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 // 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) { if (loggedIn == true) {
// don't log // don't log
@ -411,7 +544,7 @@ public class ASLSession{
logger.debug("Passing the logging because the scope was the same"); logger.debug("Passing the logging because the scope was the same");
initializeAttributes(); initializeAttributes();
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -426,10 +559,11 @@ public class ASLSession{
public void setSecurityToken(String token){ public void setSecurityToken(String token){
//NEW WAY
SecurityTokenProvider.instance.set(token); SecurityTokenProvider.instance.set(token);
session.setAttribute("securityToken", token); session.setAttribute("securityToken", token);
//REPLACED BY ABOVE //OLD WAY, for backwards compatibility
// this.securityToken = token; this.securityToken = token;
} }
public void logUserLogin(String scope) { public void logUserLogin(String scope) {
@ -453,7 +587,7 @@ public class ASLSession{
addNotifier(notification, notifier); addNotifier(notification, notifier);
} }
// lastUsedTime = System.currentTimeMillis(); lastUsedTime = System.currentTimeMillis();
notifier.waitNotification(); notifier.waitNotification();
} }
@ -470,83 +604,182 @@ public class ASLSession{
addNotifier(notification, notifier); addNotifier(notification, notifier);
} }
// lastUsedTime = System.currentTimeMillis(); lastUsedTime = System.currentTimeMillis();
notifier.notifyAllWaiting(); notifier.notifyAllWaiting();
} }
public void setGroupModelInfos(String groupName, long groupId) { public void setGroupModelInfos(String groupName, long groupId) {
//NEW WAY
ASLGroupModel aslGM = ((ASLGroupModel)session.getAttribute("groupModel")); ASLGroupModel aslGM = ((ASLGroupModel)session.getAttribute("groupModel"));
aslGM.setGroupName(groupName); aslGM.setGroupName(groupName);
aslGM.setGroupId(groupId); aslGM.setGroupId(groupId);
session.setAttribute("groupModel", aslGM); session.setAttribute("groupModel", aslGM);
//REPLACED BY ABOVE //OLD WAY, for backwards compatibility
// groupModel.setGroupName(groupName); groupModel.setGroupName(groupName);
// groupModel.setGroupId(groupId); groupModel.setGroupId(groupId);
} }
public long getGroupId() { 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(); // return groupModel.getGroupId();
} }
public String getGroupName() { 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(); // return groupModel.getGroupName();
} }
public void setUserEmailAddress(String email) { public void setUserEmailAddress(String email) {
//NEW WAY
session.setAttribute("userEmailAddress", email); session.setAttribute("userEmailAddress", email);
//REPLACED BY ABOVE //OLD WAY, for backwards compatibility
// this.userEmailAddress = email; this.userEmailAddress = email;
} }
public String getUserEmailAddress() { 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; // return this.userEmailAddress;
} }
public void setUserFullName(String fullName) { public void setUserFullName(String fullName) {
//NEW WAY
session.setAttribute("fullName", fullName); session.setAttribute("fullName", fullName);
//REPLACED BY ABOVE //OLD WAY, for backwards compatibility
// this.fullName = fullName; this.fullName = fullName;
} }
public String getUserFullName() { 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; // return this.fullName;
} }
public void setUserAvatarId(String avatarId) { public void setUserAvatarId(String avatarId) {
//NEW WAY
session.setAttribute("avatarId", avatarId); session.setAttribute("avatarId", avatarId);
//REPLACED BY ABOVE //OLD WAY, for backwards compatibility
// this.avatarId = avatarId; this.avatarId = avatarId;
} }
public String getUserAvatarId() { 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; // return this.avatarId;
} }
public void setUserGender(GenderType gender) { public void setUserGender(GenderType gender) {
//NEW WAY
session.setAttribute("gender", gender); session.setAttribute("gender", gender);
//REPLACED BY ABOVE //OLD WAY, for backwards compatibility
// this.gender = gender; this.gender = gender;
} }
public GenderType getUserGender() { 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; // return this.gender;
} }
public String getSecurityToken() { public String getSecurityToken() {
String securityToken = (String)session.getAttribute("securityToken");
logger.debug("Getting security token: " + securityToken+" in thread "+Thread.currentThread().getId()); //HYBRID, TEMPORARY SOLUTION FOR FEATURE #5033
return securityToken; 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;
} }
} }