2013-10-07 14:40:14 +02:00
|
|
|
/**
|
2017-01-13 11:00:53 +01:00
|
|
|
*
|
2013-10-07 14:40:14 +02:00
|
|
|
*/
|
|
|
|
package org.gcube.portlets.user.speciesdiscovery.server.util;
|
|
|
|
|
2021-10-27 18:10:52 +02:00
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
2013-10-07 14:40:14 +02:00
|
|
|
import org.apache.log4j.Logger;
|
|
|
|
import org.gcube.application.framework.core.session.ASLSession;
|
2021-10-27 18:10:52 +02:00
|
|
|
import org.gcube.common.portal.PortalContext;
|
|
|
|
import org.gcube.common.storagehubwrapper.server.StorageHubWrapper;
|
|
|
|
import org.gcube.common.storagehubwrapper.server.tohl.Workspace;
|
|
|
|
|
|
|
|
import com.liferay.portal.service.UserLocalServiceUtil;
|
2013-10-07 14:40:14 +02:00
|
|
|
|
|
|
|
/**
|
2021-10-27 18:10:52 +02:00
|
|
|
* The Class GetWorkspaceUtil.
|
2013-10-07 14:40:14 +02:00
|
|
|
*
|
2021-10-27 18:10:52 +02:00
|
|
|
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
|
|
|
|
*
|
|
|
|
* Oct 27, 2021
|
2013-10-07 14:40:14 +02:00
|
|
|
*/
|
|
|
|
public class GetWorkspaceUtil {
|
|
|
|
|
|
|
|
protected static Logger logger = Logger.getLogger(GetWorkspaceUtil.class);
|
2017-01-13 11:00:53 +01:00
|
|
|
|
2021-10-27 18:10:52 +02:00
|
|
|
/**
|
|
|
|
* Gets the workspace.
|
|
|
|
*
|
|
|
|
* @param request the request
|
|
|
|
* @param session the session
|
|
|
|
* @return the workspace
|
|
|
|
* @throws Exception the exception
|
|
|
|
*/
|
|
|
|
public static Workspace getWorkspace(final HttpServletRequest request, ASLSession session) throws Exception {
|
2017-01-13 11:00:53 +01:00
|
|
|
|
2021-10-27 18:10:52 +02:00
|
|
|
if (session == null)
|
2013-10-07 14:40:14 +02:00
|
|
|
throw new Exception("ASL session is null");
|
2017-01-13 11:00:53 +01:00
|
|
|
|
2021-10-27 18:10:52 +02:00
|
|
|
if (session.getScope() == null)
|
2013-10-07 14:40:14 +02:00
|
|
|
throw new Exception("Scope into ASL session is null");
|
2017-01-13 11:00:53 +01:00
|
|
|
|
2013-10-07 14:40:14 +02:00
|
|
|
String scope = session.getScope().toString();
|
2021-10-27 18:10:52 +02:00
|
|
|
// logger.trace("Get workspace for scope "+scope);
|
|
|
|
// ScopeProvider.instance.set(scope);
|
|
|
|
// logger.trace("ScopeProvider instancied for scope "+scope);
|
|
|
|
logger.trace("retuning workspace for username " + session.getUsername());
|
|
|
|
return getStorageHubWrapper(null, null, session.getUsername()).getWorkspace();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if is within portal.
|
|
|
|
*
|
|
|
|
* @return true if you're running into the portal, false if in development
|
|
|
|
*/
|
|
|
|
public static boolean isWithinPortal() {
|
|
|
|
try {
|
|
|
|
UserLocalServiceUtil.getService();
|
|
|
|
return true;
|
|
|
|
} catch (Exception ex) {
|
|
|
|
logger.trace("Development Mode ON");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the storage hub wrapper.
|
|
|
|
*
|
|
|
|
* @param request the request
|
|
|
|
* @param scopeGroupId the scope group id. If scopeGroupId is null the scope is
|
|
|
|
* read by using the request else by using the scopeGroupId
|
|
|
|
* @param username the username
|
|
|
|
* @return the storage hub wrapper
|
|
|
|
* @throws Exception the exception
|
|
|
|
*/
|
|
|
|
public static StorageHubWrapper getStorageHubWrapper(final HttpServletRequest request, String scopeGroupId,
|
|
|
|
String username) throws Exception {
|
|
|
|
|
|
|
|
if (username == null || username.isEmpty())
|
|
|
|
throw new Exception("Session expired");
|
|
|
|
|
|
|
|
try {
|
|
|
|
String scope;
|
|
|
|
PortalContext pContext = PortalContext.getConfiguration();
|
|
|
|
if (isWithinPortal() && scopeGroupId != null) {
|
|
|
|
scope = pContext.getCurrentScope(scopeGroupId);
|
|
|
|
logger.debug(scope + " has retrieved by using the scopeGroupId=" + scopeGroupId);
|
|
|
|
} else
|
|
|
|
scope = pContext.getCurrentScope(request);
|
|
|
|
|
|
|
|
logger.debug("Getting " + StorageHubWrapper.class.getSimpleName() + " for user: " + username
|
|
|
|
+ " by using the scope: " + scope);
|
|
|
|
String token = pContext.getCurrentUserToken(scope, username);
|
|
|
|
return new StorageHubWrapper(scope, token, false, false, true);
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error("Error during getting storageHub wrapper", e);
|
|
|
|
throw new Exception("Error on gettig the StorageHub wrapper for userId: " + username);
|
|
|
|
}
|
2013-10-07 14:40:14 +02:00
|
|
|
}
|
2021-10-27 18:10:52 +02:00
|
|
|
|
2013-10-07 14:40:14 +02:00
|
|
|
}
|