2016-04-06 18:28:32 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2016-05-12 19:20:39 +02:00
|
|
|
package org.gcube.portlets.user.dataminermanager.server;
|
2016-04-06 18:28:32 +02:00
|
|
|
|
|
|
|
import static org.gcube.common.authorization.client.Constants.authorizationService;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2016-06-24 14:24:29 +02:00
|
|
|
import java.util.HashMap;
|
2016-04-06 18:28:32 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpSession;
|
|
|
|
|
|
|
|
import org.apache.log4j.Logger;
|
|
|
|
import org.gcube.application.framework.core.session.ASLSession;
|
|
|
|
import org.gcube.application.framework.core.session.SessionManager;
|
2016-09-12 11:50:44 +02:00
|
|
|
import org.gcube.common.authorization.library.provider.UserInfo;
|
2016-04-06 18:28:32 +02:00
|
|
|
import org.gcube.portal.custom.scopemanager.scopehelper.ScopeHelper;
|
2016-09-14 14:27:54 +02:00
|
|
|
import org.gcube.portlets.user.dataminermanager.server.dmservice.SClient;
|
|
|
|
import org.gcube.portlets.user.dataminermanager.server.dmservice.SClient4WPSBuilder;
|
|
|
|
import org.gcube.portlets.user.dataminermanager.server.dmservice.SClientBuilder;
|
|
|
|
import org.gcube.portlets.user.dataminermanager.server.dmservice.SClientDirector;
|
2016-05-12 19:20:39 +02:00
|
|
|
import org.gcube.portlets.user.dataminermanager.server.util.ServiceCredential;
|
2016-04-06 18:28:32 +02:00
|
|
|
import org.gcube.portlets.user.dataminermanager.shared.Constants;
|
2016-06-21 18:41:03 +02:00
|
|
|
import org.gcube.portlets.user.dataminermanager.shared.exception.SessionExpiredServiceException;
|
2016-04-06 18:28:32 +02:00
|
|
|
import org.gcube.portlets.user.dataminermanager.shared.exception.ServiceException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @author Giancarlo Panichi email: <a
|
|
|
|
* href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class SessionUtil {
|
|
|
|
|
|
|
|
private static final Logger logger = Logger.getLogger(SessionUtil.class);
|
2016-09-12 11:50:44 +02:00
|
|
|
|
2016-04-06 18:28:32 +02:00
|
|
|
public static ASLSession getASLSession(HttpSession httpSession)
|
|
|
|
throws ServiceException {
|
|
|
|
String username = (String) httpSession
|
|
|
|
.getAttribute(ScopeHelper.USERNAME_ATTRIBUTE);
|
|
|
|
ASLSession aslSession;
|
|
|
|
if (username == null) {
|
|
|
|
if (Constants.DEBUG_MODE) {
|
|
|
|
logger.info("no user found in session, use test user");
|
|
|
|
username = Constants.DEFAULT_USER;
|
|
|
|
String scope = Constants.DEFAULT_SCOPE;
|
|
|
|
|
|
|
|
httpSession.setAttribute(ScopeHelper.USERNAME_ATTRIBUTE,
|
|
|
|
username);
|
|
|
|
aslSession = SessionManager.getInstance().getASLSession(
|
|
|
|
httpSession.getId(), username);
|
|
|
|
aslSession.setScope(scope);
|
|
|
|
} else {
|
2016-05-12 19:20:39 +02:00
|
|
|
logger.info("no user found in session!");
|
2016-06-21 18:41:03 +02:00
|
|
|
throw new SessionExpiredServiceException("Session Expired!");
|
2016-04-06 18:28:32 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
aslSession = SessionManager.getInstance().getASLSession(
|
|
|
|
httpSession.getId(), username);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.info("SessionUtil: aslSession " + aslSession.getUsername() + " "
|
|
|
|
+ aslSession.getScope());
|
|
|
|
|
|
|
|
return aslSession;
|
|
|
|
}
|
|
|
|
|
2016-09-19 10:27:40 +02:00
|
|
|
public static String getToken(ASLSession aslSession)
|
|
|
|
throws Exception {
|
2016-09-12 11:50:44 +02:00
|
|
|
String token = null;
|
2016-04-06 18:28:32 +02:00
|
|
|
if (Constants.DEBUG_MODE) {
|
2016-09-19 10:27:40 +02:00
|
|
|
token = Constants.DEFAULT_TOKEN;
|
2016-04-06 18:28:32 +02:00
|
|
|
} else {
|
|
|
|
token = aslSession.getSecurityToken();
|
|
|
|
}
|
|
|
|
logger.info("received token: " + token);
|
|
|
|
return token;
|
2016-09-12 11:50:44 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-09-19 10:27:40 +02:00
|
|
|
|
2016-06-24 14:24:29 +02:00
|
|
|
public static SClient getSClient(ASLSession aslSession, HttpSession session)
|
2016-04-06 18:28:32 +02:00
|
|
|
throws Exception {
|
|
|
|
|
2016-09-12 11:50:44 +02:00
|
|
|
if (aslSession == null) {
|
2016-05-20 17:57:35 +02:00
|
|
|
logger.error("ASLSession is null!");
|
2016-06-21 18:41:03 +02:00
|
|
|
throw new SessionExpiredServiceException("Session Expired!");
|
2016-09-12 11:50:44 +02:00
|
|
|
}
|
|
|
|
SClient sClient;
|
|
|
|
|
|
|
|
Object obj = session.getAttribute(Constants.SClientMap);
|
|
|
|
if (obj == null) {
|
2016-06-24 14:24:29 +02:00
|
|
|
logger.info("Create new SClientMap");
|
2016-09-12 11:50:44 +02:00
|
|
|
HashMap<String, SClient> sClientMap = new HashMap<>();
|
2016-06-24 14:24:29 +02:00
|
|
|
logger.info("Create new SClient");
|
2016-09-12 11:50:44 +02:00
|
|
|
ServiceCredential serviceCredential = new ServiceCredential(
|
|
|
|
aslSession.getUsername(), aslSession.getScope(),
|
2016-04-06 18:28:32 +02:00
|
|
|
SessionUtil.getToken(aslSession));
|
|
|
|
SClientBuilder sBuilder = new SClient4WPSBuilder(serviceCredential);
|
|
|
|
SClientDirector director = new SClientDirector();
|
|
|
|
director.setSClientBuilder(sBuilder);
|
|
|
|
director.constructSClient();
|
|
|
|
sClient = director.getSClient();
|
2016-09-12 11:50:44 +02:00
|
|
|
|
2016-06-24 14:24:29 +02:00
|
|
|
sClientMap.put(aslSession.getScope(), sClient);
|
|
|
|
session.setAttribute(Constants.SClientMap, sClientMap);
|
2016-05-19 19:35:48 +02:00
|
|
|
} else {
|
2016-06-24 14:24:29 +02:00
|
|
|
if (obj instanceof HashMap<?, ?>) {
|
|
|
|
@SuppressWarnings("unchecked")
|
2016-09-12 11:50:44 +02:00
|
|
|
HashMap<String, SClient> sClientMap = (HashMap<String, SClient>) obj;
|
|
|
|
if (sClientMap.containsKey(aslSession.getScope())) {
|
2016-06-24 14:24:29 +02:00
|
|
|
logger.info("Use SClient in session");
|
2016-09-12 11:50:44 +02:00
|
|
|
sClient = sClientMap.get(aslSession.getScope());
|
2016-06-24 14:24:29 +02:00
|
|
|
} else {
|
|
|
|
logger.info("Create new SClient");
|
2016-09-12 11:50:44 +02:00
|
|
|
ServiceCredential serviceCredential = new ServiceCredential(
|
|
|
|
aslSession.getUsername(), aslSession.getScope(),
|
2016-06-24 14:24:29 +02:00
|
|
|
SessionUtil.getToken(aslSession));
|
2016-09-12 11:50:44 +02:00
|
|
|
SClientBuilder sBuilder = new SClient4WPSBuilder(
|
|
|
|
serviceCredential);
|
2016-06-24 14:24:29 +02:00
|
|
|
|
|
|
|
SClientDirector director = new SClientDirector();
|
|
|
|
director.setSClientBuilder(sBuilder);
|
|
|
|
director.constructSClient();
|
|
|
|
sClient = director.getSClient();
|
|
|
|
sClientMap.put(aslSession.getScope(), sClient);
|
|
|
|
session.setAttribute(Constants.SClientMap, sClientMap);
|
|
|
|
}
|
2016-09-12 11:50:44 +02:00
|
|
|
|
2016-05-23 17:25:00 +02:00
|
|
|
} else {
|
2016-06-24 14:24:29 +02:00
|
|
|
logger.error("Attention no SClientMap in Session!");
|
2016-09-12 11:50:44 +02:00
|
|
|
throw new ServiceException(
|
|
|
|
"Sign Out, portlet is changed, a new session is required!");
|
2016-05-23 17:25:00 +02:00
|
|
|
}
|
2016-04-06 18:28:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return sClient;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|