data-miner-manager/src/main/java/org/gcube/portlets/user/dataminermanager/server/SessionUtil.java

149 lines
4.8 KiB
Java

/**
*
*/
package org.gcube.portlets.user.dataminermanager.server;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.gcube.common.portal.PortalContext;
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;
import org.gcube.portlets.user.dataminermanager.server.util.ServiceCredentials;
import org.gcube.portlets.user.dataminermanager.shared.Constants;
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);
public static ServiceCredentials getServiceCredentials(
HttpServletRequest httpServletRequest)
throws ServiceException {
return getServiceCredentials(httpServletRequest, null);
}
public static ServiceCredentials getServiceCredentials(
HttpServletRequest httpServletRequest, String scopeGroupId)
throws ServiceException {
ServiceCredentials sCredentials = null;
String userName = null;
String scope = null;
String token = null;
if (Constants.DEBUG_MODE) {
logger.info("No credential found in session, use test user!");
userName = Constants.DEFAULT_USER;
scope = Constants.DEFAULT_SCOPE;
token = Constants.DEFAULT_TOKEN;
sCredentials = new ServiceCredentials(userName, scope, token);
} else {
logger.info("Retrieving credential in session!");
PortalContext pContext = PortalContext.getConfiguration();
if(scopeGroupId!=null&&!scopeGroupId.isEmpty()){
scope = pContext.getCurrentScope(scopeGroupId);
} else {
scope = pContext.getCurrentScope(httpServletRequest);
}
userName = pContext.getCurrentUser(httpServletRequest)
.getUsername();
token = pContext.getCurrentUserToken(httpServletRequest);
String name = pContext.getCurrentUser(httpServletRequest)
.getFirstName();
String lastName = pContext.getCurrentUser(httpServletRequest)
.getLastName();
String fullName = pContext.getCurrentUser(httpServletRequest)
.getFullname();
String userAvatarURL = pContext.getCurrentUser(httpServletRequest)
.getUserAvatarURL();
String email = pContext.getCurrentUser(httpServletRequest)
.getEmail();
String groupId = String.valueOf(pContext
.getCurrentGroupId(httpServletRequest));
String groupName = pContext.getCurrentGroupName(httpServletRequest);
sCredentials = new ServiceCredentials(userName, fullName, name,
lastName, email, scope, groupId, groupName, userAvatarURL, token);
}
logger.info("ServiceCredentials: " + sCredentials);
return sCredentials;
}
public static SClient getSClient(ServiceCredentials serviceCredentials, HttpSession session)
throws Exception {
if (serviceCredentials == null) {
logger.error("ServiceCredentials is null!");
throw new ServiceException("Service Credentials is null!");
}
SClient sClient;
Object obj = session.getAttribute(Constants.SClientMap);
if (obj == null) {
logger.info("Create new SClientMap");
HashMap<String, SClient> sClientMap = new HashMap<>();
logger.info("Create new SClient");
SClientBuilder sBuilder = new SClient4WPSBuilder(serviceCredentials);
SClientDirector director = new SClientDirector();
director.setSClientBuilder(sBuilder);
director.constructSClient();
sClient = director.getSClient();
sClientMap.put(serviceCredentials.getScope(), sClient);
session.setAttribute(Constants.SClientMap, sClientMap);
} else {
if (obj instanceof HashMap<?, ?>) {
@SuppressWarnings("unchecked")
HashMap<String, SClient> sClientMap = (HashMap<String, SClient>) obj;
if (sClientMap.containsKey(serviceCredentials.getScope())) {
logger.info("Use SClient in session");
sClient = sClientMap.get(serviceCredentials.getScope());
} else {
logger.info("Create new SClient");
SClientBuilder sBuilder = new SClient4WPSBuilder(
serviceCredentials);
SClientDirector director = new SClientDirector();
director.setSClientBuilder(sBuilder);
director.constructSClient();
sClient = director.getSClient();
sClientMap.put(serviceCredentials.getScope(), sClient);
session.setAttribute(Constants.SClientMap, sClientMap);
}
} else {
logger.error("Attention no SClientMap in Session!");
throw new ServiceException(
"Sign Out, portlet is changed, a new session is required!");
}
}
return sClient;
}
}