workspace-explorer-app/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/server/WsUtil.java

90 lines
2.9 KiB
Java
Raw Normal View History

/**
*
*/
package org.gcube.portlets.user.workspaceexplorerapp.server;
import javax.servlet.http.HttpSession;
import org.gcube.application.framework.core.session.ASLSession;
import org.gcube.application.framework.core.session.SessionManager;
import org.gcube.common.homelibrary.home.HomeLibrary;
import org.gcube.common.homelibrary.home.exceptions.HomeNotFoundException;
import org.gcube.common.homelibrary.home.exceptions.InternalErrorException;
import org.gcube.common.homelibrary.home.workspace.Workspace;
import org.gcube.common.homelibrary.home.workspace.exceptions.WorkspaceFolderNotFoundException;
import org.gcube.common.scope.api.ScopeProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class WsUtil.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Mar 7, 2016
*/
public class WsUtil {
public static final String USERNAME_ATTRIBUTE = "username";
public static final String TEST_USER = "francesco.mangiacrapa";
public static final String TEST_SCOPE = "/gcube/devsec/devVRE";
public static final Logger logger = LoggerFactory.getLogger(WsUtil.class);
/**
* Gets the ASL session.
*
* @param httpSession the http session
* @return the ASL session
*/
public static ASLSession getASLSession(HttpSession httpSession) {
String sessionID = httpSession.getId();
String user = (String) httpSession.getAttribute(USERNAME_ATTRIBUTE);
//TODO we check for the older attribute name
if (user == null) {
user = (String) httpSession.getAttribute("user");
}
if (user == null) {
logger.error("WORKSPACE PORTLET STARTING IN TEST MODE - NO USER FOUND");
//for test only
// user = "test.user";
user = TEST_USER;
httpSession.setAttribute(USERNAME_ATTRIBUTE, user);
ASLSession session = SessionManager.getInstance().getASLSession(sessionID, user);
session.setScope(TEST_SCOPE);
return session;
}
else {
logger.trace("user found in session "+user);
}
return SessionManager.getInstance().getASLSession(sessionID, user);
}
/**
* Gets the workspace.
*
* @param httpSession the http session
* @return the workspace
* @throws InternalErrorException the internal error exception
* @throws HomeNotFoundException the home not found exception
* @throws WorkspaceFolderNotFoundException the workspace folder not found exception
*/
public static Workspace getWorkspace(HttpSession httpSession) throws InternalErrorException, HomeNotFoundException, WorkspaceFolderNotFoundException {
ASLSession session = getASLSession(httpSession);
logger.trace("Scope : "+session.getScope() + " username: "+session.getUsername());
String scope = session.getScope();
if(scope==null)
scope= WsUtil.TEST_SCOPE;
System.out.println("Scope : "+scope + " username: "+session.getUsername());
ScopeProvider.instance.set(scope);
Workspace workspace = HomeLibrary.getUserWorkspace(session.getUsername());
return workspace;
}
}