You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
187 lines
6.8 KiB
Java
187 lines
6.8 KiB
Java
/**
|
|
*
|
|
*/
|
|
package org.gcube.portlets.widgets.wsthreddssync.server;
|
|
|
|
import java.net.MalformedURLException;
|
|
import java.net.URL;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpSession;
|
|
|
|
//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.portal.PortalContext;
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
|
import org.gcube.common.storagehubwrapper.server.StorageHubWrapper;
|
|
import org.gcube.common.storagehubwrapper.server.tohl.Workspace;
|
|
import org.gcube.portlets.widgets.ckandatapublisherwidget.server.utils.the;
|
|
import org.gcube.portlets.widgets.wsthreddssync.shared.GatewayRolesThredds;
|
|
import org.gcube.vomanagement.usermanagement.GroupManager;
|
|
import org.gcube.vomanagement.usermanagement.RoleManager;
|
|
import org.gcube.vomanagement.usermanagement.exception.GroupRetrievalFault;
|
|
import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException;
|
|
import org.gcube.vomanagement.usermanagement.exception.UserRetrievalFault;
|
|
import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager;
|
|
import org.gcube.vomanagement.usermanagement.impl.LiferayRoleManager;
|
|
import org.gcube.vomanagement.usermanagement.model.GCubeGroup;
|
|
import org.gcube.vomanagement.usermanagement.model.GCubeRole;
|
|
//import org.gcube.portlets.user.workspace.server.util.WsUtil;
|
|
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import com.liferay.portal.service.UserLocalServiceUtil;
|
|
|
|
/**
|
|
* The Class WsUtil.
|
|
*
|
|
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
|
* Nov 25, 2016
|
|
*/
|
|
public class WsUtil {
|
|
|
|
|
|
|
|
/** The logger. */
|
|
private static Logger logger = LoggerFactory.getLogger(WsUtil.class);
|
|
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Checks if is session expired.
|
|
*
|
|
* @param httpServletRequest the http servlet request
|
|
* @return true, if is session expired
|
|
* @throws Exception the exception
|
|
*/
|
|
public static boolean isSessionExpired(HttpServletRequest httpServletRequest) throws Exception {
|
|
logger.trace("workspace session validating...");
|
|
return PortalContext.getConfiguration().getCurrentUser(httpServletRequest)==null;
|
|
}
|
|
|
|
/**
|
|
* Gets the workspace from storage hub.
|
|
*
|
|
* @param httpServletRequest the http servlet request
|
|
* @return the workspace from storage hub
|
|
* @throws Exception the exception
|
|
*/
|
|
public Workspace getWorkspaceFromStorageHub(HttpServletRequest httpServletRequest) throws Exception {
|
|
logger.trace("Get Workspace");
|
|
// String scope = PortalContext.getConfiguration().getCurrentScope(httpServletRequest);
|
|
|
|
GCubeUser user = null;
|
|
|
|
try {
|
|
String scope = PortalContext.getConfiguration().getCurrentScope(httpServletRequest);
|
|
user = PortalContext.getConfiguration().getCurrentUser(httpServletRequest);
|
|
if (user == null || user.getUsername().isEmpty())
|
|
throw new Exception("Session expired");
|
|
|
|
ScopeProvider.instance.set(scope);
|
|
logger.trace("Scope provider instancied at: "+scope);
|
|
|
|
logger.debug("Getting " + StorageHubWrapper.class.getSimpleName() + " for user: " + user.getUsername()
|
|
+ " by using the scope: " + scope);
|
|
|
|
String token = PortalContext.getConfiguration().getCurrentUserToken(scope, user.getUsername());
|
|
StorageHubWrapper shWrapper = new StorageHubWrapper(scope, token, false, false, true);
|
|
return shWrapper.getWorkspace();
|
|
} catch (Exception e) {
|
|
logger.error("Error on getting the Workspace via SHUB wrapper", e);
|
|
throw new Exception("Error on gettig the Workspace for userId: " + user);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Gets the list of Scopes (Root-VO, VOs and VREs) for user and the thredds roles the user has in them.
|
|
* retrieve the VREs to whom a given user
|
|
*
|
|
* @param user the user
|
|
* @return the list vre for user
|
|
*/
|
|
public static Map<String, List<GatewayRolesThredds>> getScopesThreddsRolesForUser(GCubeUser user){
|
|
logger.info("called getScopesThreddsRolesForUser user: "+user+", in all contexts");
|
|
|
|
GroupManager groupManager = new LiferayGroupManager();
|
|
Map<String, List<GatewayRolesThredds>> mapRoleByGroupSingleVre = new HashMap<String, List<GatewayRolesThredds>>();
|
|
//GCubeUser user;
|
|
try {
|
|
|
|
//Retrieving the list of VOs and VREs
|
|
List<GCubeGroup> listOfGroups = groupManager.listGroupsByUser(user.getUserId());
|
|
//adding also the ROOT-VO
|
|
listOfGroups.add(groupManager.getRootVO());
|
|
for (GCubeGroup gCubeGroup : listOfGroups) {
|
|
List<GatewayRolesThredds> threddsRoles = getThreddsRoleFor(user, gCubeGroup);
|
|
if(threddsRoles.size()>0) {
|
|
mapRoleByGroupSingleVre.put(gCubeGroup.getGroupName(), threddsRoles);
|
|
}
|
|
}
|
|
|
|
logger.info("For user: "+user+", returning Map (VRE, ThreddsRoles) " + mapRoleByGroupSingleVre);
|
|
return mapRoleByGroupSingleVre;
|
|
}catch (UserManagementSystemException | UserRetrievalFault | GroupRetrievalFault e) {
|
|
logger.error("An error occurred during geThreddsVreRolesForUser: "+user, e);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Gets the thredds role for the user in the vre
|
|
*
|
|
* @param user the user
|
|
* @param vre the vre
|
|
* @return the thredds role for
|
|
*/
|
|
public static List<GatewayRolesThredds> getThreddsRoleFor(GCubeUser user, GCubeGroup vre){
|
|
logger.info("called getThreddsRoleFor user: "+user+", in the scope: "+vre.getGroupName());
|
|
try {
|
|
RoleManager roleManager = new LiferayRoleManager();
|
|
List<GCubeRole> roles = roleManager.listRolesByUserAndGroup(user.getUserId(), vre.getGroupId());
|
|
List<GatewayRolesThredds> threddsRoles = new ArrayList<GatewayRolesThredds>();
|
|
for (GCubeRole gCubeRole : roles) {
|
|
if(gCubeRole.getRoleName().equalsIgnoreCase(GatewayRolesThredds.THREDDS_ADMIN.getRoleName())){
|
|
threddsRoles.add(GatewayRolesThredds.THREDDS_ADMIN);
|
|
}
|
|
if(gCubeRole.getRoleName().equalsIgnoreCase(GatewayRolesThredds.THREDDS_PUBLISHER.getRoleName())){
|
|
threddsRoles.add(GatewayRolesThredds.THREDDS_PUBLISHER);
|
|
}
|
|
}
|
|
|
|
logger.info("For user: "+user+" in the scope: "+vre.getGroupName()+" returning roles: " + threddsRoles);
|
|
return threddsRoles;
|
|
}catch (UserRetrievalFault | GroupRetrievalFault e) {
|
|
logger.error("An error occurred during getVreRoleForUser: "+user, e);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
}
|