127 lines
4.0 KiB
Java
127 lines
4.0 KiB
Java
/**
|
|
*
|
|
*/
|
|
package org.gcube.portlets.user.dataminermanager.server.util;
|
|
|
|
import static org.gcube.common.authorization.client.Constants.authorizationService;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import javax.activation.DataSource;
|
|
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;
|
|
import org.gcube.portal.custom.scopemanager.scopehelper.ScopeHelper;
|
|
import org.gcube.portlets.user.dataminermanager.server.smservice.SClient;
|
|
import org.gcube.portlets.user.dataminermanager.server.smservice.SClient4WPSBuilder;
|
|
import org.gcube.portlets.user.dataminermanager.server.smservice.SClientBuilder;
|
|
import org.gcube.portlets.user.dataminermanager.server.smservice.SClientDirector;
|
|
import org.gcube.portlets.user.dataminermanager.shared.Constants;
|
|
import org.gcube.portlets.user.dataminermanager.shared.exception.ExpiredSessionServiceException;
|
|
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);
|
|
// private final static String hostUrl =
|
|
// "http://pc-angela.isti.cnr.it:9090";
|
|
// private static URI host = URI.create(hostUrl);
|
|
// private final static String SCOPE = "/gcube";
|
|
|
|
public static final String DATASOURCE_ATTRIBUTE_NAME = "TDW.DATASOURCE";
|
|
|
|
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");
|
|
/*
|
|
* throw new ExpiredSessionServiceException("Session Expired!");
|
|
*/
|
|
|
|
// Remove comment for Test
|
|
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 {
|
|
logger.info("no user found in session, use test user");
|
|
throw new ExpiredSessionServiceException("Session Expired!");
|
|
|
|
}
|
|
} else {
|
|
aslSession = SessionManager.getInstance().getASLSession(
|
|
httpSession.getId(), username);
|
|
|
|
}
|
|
|
|
logger.info("SessionUtil: aslSession " + aslSession.getUsername() + " "
|
|
+ aslSession.getScope());
|
|
|
|
return aslSession;
|
|
}
|
|
|
|
public static String getToken(ASLSession aslSession) {
|
|
String token=null;
|
|
if (Constants.DEBUG_MODE) {
|
|
List<String> userRoles = new ArrayList<>();
|
|
userRoles.add(Constants.DEFAULT_ROLE);
|
|
/*if (aslSession.getUsername().compareTo("lucio.lelii") == 0)
|
|
userRoles.add("VRE-Manager");*/
|
|
token = authorizationService().build().generate(
|
|
aslSession.getUsername(), userRoles);
|
|
|
|
} else {
|
|
token = aslSession.getSecurityToken();
|
|
}
|
|
logger.info("received token: " + token);
|
|
return token;
|
|
|
|
|
|
}
|
|
|
|
public static DataSource getDataSource(HttpSession httpSession)
|
|
throws ServiceException {
|
|
ASLSession session = getASLSession(httpSession);
|
|
return (DataSource) session.getAttribute(DATASOURCE_ATTRIBUTE_NAME);
|
|
}
|
|
|
|
public static SClient getSClient(HttpSession session, ASLSession aslSession)
|
|
throws Exception {
|
|
|
|
SClient sClient = (SClient) session.getAttribute(Constants.SClient);
|
|
|
|
if (sClient == null) {
|
|
ServiceCredential serviceCredential=new ServiceCredential(aslSession.getUsername(), aslSession.getScope(),
|
|
SessionUtil.getToken(aslSession));
|
|
SClientBuilder sBuilder = new SClient4WPSBuilder(serviceCredential);
|
|
|
|
SClientDirector director = new SClientDirector();
|
|
director.setSClientBuilder(sBuilder);
|
|
director.constructSClient();
|
|
sClient = director.getSClient();
|
|
session.setAttribute(Constants.SClient, sClient);
|
|
}
|
|
|
|
return sClient;
|
|
|
|
}
|
|
|
|
}
|