ws-task-executor-widget/src/main/java/org/gcube/portlets/widgets/wstaskexecutor/server/WsTaskExecutorWidgetService...

216 lines
7.1 KiB
Java

package org.gcube.portlets.widgets.wstaskexecutor.server;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.gcube.common.portal.PortalContext;
import org.gcube.common.workspacetaskexecutor.dataminer.WorkspaceDataMinerTaskExecutor;
import org.gcube.common.workspacetaskexecutor.shared.dataminer.TaskConfiguration;
import org.gcube.common.workspacetaskexecutor.shared.exception.ItemNotConfiguredException;
import org.gcube.common.workspacetaskexecutor.shared.exception.WorkspaceFolderLocked;
import org.gcube.common.workspacetaskexecutor.util.EncrypterUtil;
import org.gcube.portlets.widgets.wstaskexecutor.client.rpc.WsTaskExecutorWidgetService;
import org.gcube.portlets.widgets.wstaskexecutor.server.util.PortalContextUtil;
import org.gcube.portlets.widgets.wstaskexecutor.shared.GcubeScope;
import org.gcube.portlets.widgets.wstaskexecutor.shared.GcubeScopeType;
import org.gcube.vomanagement.usermanagement.GroupManager;
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.model.GCubeGroup;
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.liferay.portal.service.UserLocalServiceUtil;
/**
* The server side implementation of the RPC service.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* May 4, 2018
*/
@SuppressWarnings("serial")
public class WsTaskExecutorWidgetServiceImpl extends RemoteServiceServlet implements
WsTaskExecutorWidgetService {
private static Logger logger = LoggerFactory.getLogger(WsTaskExecutorWidgetServiceImpl.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.info("Development Mode ON");
return false;
}
}
/**
* Gets the task executor.
*
* @return the task executor
*/
private WorkspaceDataMinerTaskExecutor getTaskExecutor(){
GCubeUser user = PortalContextUtil.getUserLogged(this.getThreadLocalRequest());
WorkspaceDataMinerTaskExecutor exec = WorkspaceDataMinerTaskExecutor.getInstance();
exec.withOwner(user.getUsername());
return exec;
}
/* (non-Javadoc)
* @see org.gcube.portlets.widgets.wstaskexecutor.client.GreetingService#getListOfScopesForLoggedUser()
*/
@Override
public List<GcubeScope> getListOfScopesForLoggedUser()
throws Exception {
logger.debug("getListOfVREsForLoggedUser...: ");
GCubeUser user = PortalContext.getConfiguration().getCurrentUser(this.getThreadLocalRequest());
long userId = user.getUserId();
// Instanciate the manager
GroupManager groupManager = new LiferayGroupManager();
List<GcubeScope> listOfScopes = new ArrayList<GcubeScope>();
if (!isWithinPortal()){
listOfScopes.add(new GcubeScope("devVRE", "/gcube/devsec/devVRE", GcubeScopeType.VRE));
listOfScopes.add(new GcubeScope("NextNext", "/gcube/devNext/NextNext", GcubeScopeType.VRE));
listOfScopes.add(new GcubeScope("devNext", "/gcube/devNext", GcubeScopeType.VO));
listOfScopes.add(new GcubeScope("devsec", "/gcube/devsec", GcubeScopeType.VO));
listOfScopes.add(new GcubeScope("gcube", "/gcube", GcubeScopeType.ROOT));
Collections.sort(listOfScopes);
return listOfScopes;
}
try {
List<GCubeGroup> listOfGroups = groupManager.listGroupsByUser(userId);
for (GCubeGroup gCubeGroup : listOfGroups) {
GcubeScopeType scopeType=null;
if(groupManager.isVRE(gCubeGroup.getGroupId())){
scopeType = GcubeScopeType.VRE;
}
// else if(groupManager.isVO(gCubeGroup.getGroupId())){
// scopeType = GcubeScopeType.VO;
// }
// }else if(groupManager.isRootVO(gCubeGroup.getGroupId())){
// scopeType = GcubeScopeType.ROOT;
// }
if(scopeType!=null){
GcubeScope gcubeVRE = new GcubeScope(gCubeGroup.getGroupName(), groupManager.getInfrastructureScope(gCubeGroup.getGroupId()), scopeType);
listOfScopes.add(gcubeVRE);
}
}
//ADDING THE ROOT SCOPE
// String infraName = PortalContext.getConfiguration().getInfrastructureName();
// GcubeScope gcubeRoot = new GcubeScope(infraName, "/"+infraName, GcubeScopeType.ROOT);
// listOfScopes.add(gcubeRoot);
}
catch (UserRetrievalFault | UserManagementSystemException
| GroupRetrievalFault e) {
logger.error("Error occurred server-side getting VRE folders: ", e);
throw new Exception("Sorry, an error occurred server-side getting VRE folders, try again later");
}
Collections.sort(listOfScopes);
logger.info("Returning list of VREs: "+listOfScopes);
return listOfScopes;
}
/**
* Creates the task configuration.
*
* @param workspaceItemId the workspace item id
* @param taskConfiguration the task configuration
* @return the task configuration
* @throws Exception the exception
*/
public TaskConfiguration createTaskConfiguration(String workspaceItemId, TaskConfiguration taskConfiguration) throws Exception{
WorkspaceDataMinerTaskExecutor exec = getTaskExecutor();
taskConfiguration = setMaskedToken(taskConfiguration);
exec.setTaskConfiguration(taskConfiguration);
return taskConfiguration;
}
/**
* Check item task configurations.
*
* @param itemId the item id
* @return the list
* @throws Exception
*/
@Override
public List<TaskConfiguration> checkItemTaskConfigurations(String itemId) throws Exception {
WorkspaceDataMinerTaskExecutor exec = WorkspaceDataMinerTaskExecutor.getInstance();
List<TaskConfiguration> confs = null;
try {
confs = exec.checkItemConfigurations(itemId);
}
catch (ItemNotConfiguredException e){
String msg = "No TaskConfiguration found for itemId: "+itemId+", retuning null";
logger.info(msg);
throw e;
}catch (WorkspaceFolderLocked e) {
logger.info(e.getMessage());
throw e;
}catch (Exception e) {
logger.error("Error on checking TaskConfigurations for itemId: "+itemId,e);
throw new Exception("Error occurred during checking Task Configurations for id: "+itemId+ ". Refresh and try again later");
}
logger.debug("Returning configurations: "+confs);
return confs;
}
/**
* Sets the masked token.
*
* @param taskConfiguration the task configuration
* @return the task configuration
* @throws Exception the exception
*/
private TaskConfiguration setMaskedToken(TaskConfiguration taskConfiguration) throws Exception{
String scope = taskConfiguration.getScope();
if(scope==null)
throw new Exception("Missing scope in the input configuration. Set it and try again");
GCubeUser user = PortalContextUtil.getUserLogged(this.getThreadLocalRequest());
String token = PortalContextUtil.getTokenFor(scope, user.getUsername());
taskConfiguration.setMaskedToken(EncrypterUtil.encryptString(token));
return taskConfiguration;
}
}