206 lines
6.8 KiB
Java
206 lines
6.8 KiB
Java
/**
|
|
*
|
|
*/
|
|
package org.gcube.portlets.user.statisticalalgorithmsimporter.server;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import javax.servlet.ServletContext;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpSession;
|
|
|
|
import org.gcube.common.portal.PortalContext;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.server.file.CodeFileUploadSession;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.server.social.Recipient;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.server.util.ServiceCredentials;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.Constants;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.exception.StatAlgoImporterServiceException;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.exception.StatAlgoImporterSessionExpiredException;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.file.FileUploadMonitor;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.project.Project;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.session.SessionConstants;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
/**
|
|
*
|
|
* @author "Giancarlo Panichi" <a
|
|
* href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
|
|
*
|
|
*/
|
|
public class SessionUtil {
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(SessionUtil.class);
|
|
|
|
public static ServiceCredentials getServiceCredentials(
|
|
HttpServletRequest httpServletRequest)
|
|
throws StatAlgoImporterServiceException {
|
|
return getServiceCredentials(httpServletRequest, null);
|
|
}
|
|
|
|
public static ServiceCredentials getServiceCredentials(
|
|
HttpServletRequest httpServletRequest, String scopeGroupId)
|
|
throws StatAlgoImporterServiceException {
|
|
|
|
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);
|
|
}
|
|
|
|
if (scope == null || scope.isEmpty()) {
|
|
String error = "Error retrieving scope: " + scope;
|
|
logger.error(error);
|
|
throw new StatAlgoImporterServiceException(error);
|
|
}
|
|
|
|
userName = pContext.getCurrentUser(httpServletRequest)
|
|
.getUsername();
|
|
|
|
if (userName == null || userName.isEmpty()) {
|
|
String error = "Error retrieving username in scope " + scope
|
|
+ ": " + userName;
|
|
logger.error(error);
|
|
throw new StatAlgoImporterServiceException(error);
|
|
}
|
|
|
|
token = pContext.getCurrentUserToken(httpServletRequest);
|
|
|
|
if (token == null || token.isEmpty()) {
|
|
String error = "Error retrieving token for " + userName
|
|
+ " in " + scope + ": " + token;
|
|
logger.error(error);
|
|
throw new StatAlgoImporterServiceException(error);
|
|
}
|
|
|
|
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 ArrayList<Recipient> getRecipients(
|
|
ServletContext servletContest) {
|
|
@SuppressWarnings("unchecked")
|
|
ArrayList<Recipient> recipients = (ArrayList<Recipient>) servletContest
|
|
.getAttribute(Constants.RECIPIENTS);
|
|
return recipients;
|
|
|
|
}
|
|
|
|
public static ArrayList<Recipient> setRecipients(
|
|
ServletContext servletContest, ArrayList<Recipient> recipients) {
|
|
servletContest.setAttribute(Constants.RECIPIENTS, recipients);
|
|
return recipients;
|
|
|
|
}
|
|
|
|
//
|
|
public static FileUploadMonitor getFileUploadMonitor(HttpSession httpSession) {
|
|
FileUploadMonitor fileUploadMonitor = (FileUploadMonitor) httpSession
|
|
.getAttribute(SessionConstants.FILE_UPLOAD_MONITOR);
|
|
if (fileUploadMonitor != null) {
|
|
return fileUploadMonitor;
|
|
} else {
|
|
fileUploadMonitor = new FileUploadMonitor();
|
|
httpSession.setAttribute(SessionConstants.FILE_UPLOAD_MONITOR,
|
|
fileUploadMonitor);
|
|
return fileUploadMonitor;
|
|
}
|
|
}
|
|
|
|
public static void setFileUploadMonitor(HttpSession httpSession,
|
|
FileUploadMonitor fileUploadMonitor) {
|
|
FileUploadMonitor fum = (FileUploadMonitor) httpSession
|
|
.getAttribute(SessionConstants.FILE_UPLOAD_MONITOR);
|
|
if (fum != null) {
|
|
httpSession.removeAttribute(SessionConstants.FILE_UPLOAD_MONITOR);
|
|
}
|
|
httpSession.setAttribute(SessionConstants.FILE_UPLOAD_MONITOR,
|
|
fileUploadMonitor);
|
|
|
|
}
|
|
|
|
//
|
|
public static void setCodeFileUploadSession(HttpSession httpSession,
|
|
CodeFileUploadSession s) throws StatAlgoImporterServiceException {
|
|
|
|
CodeFileUploadSession session = (CodeFileUploadSession) httpSession
|
|
.getAttribute(SessionConstants.IMPORT_CODE_FILE_UPLOAD_SESSION);
|
|
if (session != null)
|
|
httpSession
|
|
.removeAttribute(SessionConstants.IMPORT_CODE_FILE_UPLOAD_SESSION);
|
|
httpSession.setAttribute(
|
|
SessionConstants.IMPORT_CODE_FILE_UPLOAD_SESSION, s);
|
|
|
|
}
|
|
|
|
public static CodeFileUploadSession getCodeFileUploadSession(
|
|
HttpSession httpSession) {
|
|
CodeFileUploadSession fileUploadSession = (CodeFileUploadSession) httpSession
|
|
.getAttribute(SessionConstants.IMPORT_CODE_FILE_UPLOAD_SESSION);
|
|
if (fileUploadSession == null) {
|
|
logger.error("CodeFileUploadSession was not acquired");
|
|
}
|
|
return fileUploadSession;
|
|
}
|
|
|
|
//
|
|
public static void setProjectSession(HttpSession httpSession,
|
|
Project project) throws StatAlgoImporterSessionExpiredException {
|
|
Project p = (Project) httpSession
|
|
.getAttribute(SessionConstants.PROJECT);
|
|
if (p != null)
|
|
httpSession.removeAttribute(SessionConstants.PROJECT);
|
|
httpSession.setAttribute(SessionConstants.PROJECT, project);
|
|
|
|
}
|
|
|
|
public static Project getProjectSession(HttpSession httpSession) {
|
|
Project project = (Project) httpSession
|
|
.getAttribute(SessionConstants.PROJECT);
|
|
if (project == null) {
|
|
logger.error("Project was not acquired");
|
|
}
|
|
return project;
|
|
}
|
|
|
|
}
|