From e46051e2528b67d65bc0370c5f29a5b79cb64e83 Mon Sep 17 00:00:00 2001 From: Massimiliano Assante Date: Mon, 13 Feb 2017 14:34:32 +0000 Subject: [PATCH] Partially implemented, still nedd to differentiate users per VRE git-svn-id: https://svn.research-infrastructures.eu/d4science/gcube/trunk/portlets/user/rstudio-wrapper-portlet@142504 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../client/RStudioWrapper.java | 11 +- .../server/RStudioServiceImpl.java | 111 ++++++++++++++---- .../WEB-INF/liferay-plugin-package.properties | 2 +- 3 files changed, 96 insertions(+), 28 deletions(-) diff --git a/src/main/java/org/gcube/portlets/user/rstudio_wrapper_portlet/client/RStudioWrapper.java b/src/main/java/org/gcube/portlets/user/rstudio_wrapper_portlet/client/RStudioWrapper.java index 646b185..8ca72e5 100644 --- a/src/main/java/org/gcube/portlets/user/rstudio_wrapper_portlet/client/RStudioWrapper.java +++ b/src/main/java/org/gcube/portlets/user/rstudio_wrapper_portlet/client/RStudioWrapper.java @@ -34,8 +34,13 @@ public class RStudioWrapper implements EntryPoint { @Override public void onSuccess(String result) { - RootPanel.get(CONTAINER_DIV).add(new HTML("If no new window appears, please click here to open RStudio")); - setWindowTarget(window, result); + if (result == null || result.compareTo("") == 0) { + RootPanel.get(CONTAINER_DIV).add(new HTML("There were problems contacting the server, please report this issue. ")); + } + else { + RootPanel.get(CONTAINER_DIV).add(new HTML("If no new window appears, please click here to open RStudio")); + setWindowTarget(window, result); + } } }); @@ -50,5 +55,5 @@ public class RStudioWrapper implements EntryPoint { private static native void setWindowTarget(JavaScriptObject window, String target)/*-{ window.location = target; }-*/; - + } diff --git a/src/main/java/org/gcube/portlets/user/rstudio_wrapper_portlet/server/RStudioServiceImpl.java b/src/main/java/org/gcube/portlets/user/rstudio_wrapper_portlet/server/RStudioServiceImpl.java index 4d6f3a4..bc4bac9 100644 --- a/src/main/java/org/gcube/portlets/user/rstudio_wrapper_portlet/server/RStudioServiceImpl.java +++ b/src/main/java/org/gcube/portlets/user/rstudio_wrapper_portlet/server/RStudioServiceImpl.java @@ -9,13 +9,14 @@ import java.util.HashMap; import java.util.List; import org.gcube.common.portal.PortalContext; -import org.gcube.common.resources.gcore.GCoreEndpoint; -import org.gcube.common.resources.gcore.GCoreEndpoint.Profile.Endpoint; +import org.gcube.common.resources.gcore.ServiceEndpoint; import org.gcube.common.scope.api.ScopeProvider; import org.gcube.portlets.user.rstudio_wrapper_portlet.client.RStudioService; import org.gcube.resources.discovery.client.api.DiscoveryClient; import org.gcube.resources.discovery.client.queries.api.SimpleQuery; import org.gcube.vomanagement.usermanagement.UserManager; +import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException; +import org.gcube.vomanagement.usermanagement.exception.UserRetrievalFault; import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager; import org.gcube.vomanagement.usermanagement.model.GCubeUser; import org.slf4j.Logger; @@ -30,11 +31,14 @@ import com.google.gwt.user.server.rpc.RemoteServiceServlet; public class RStudioServiceImpl extends RemoteServiceServlet implements RStudioService { private static final Logger _log = LoggerFactory.getLogger(RStudioServiceImpl.class); - private static final String SERVICE_NAME = "RConnector"; - private static final String SERVICECLASS = "DataAnalysis"; + private static final String RSTUDIO_URL = "RStudio-URL"; private static final String RCONNECTOR_EntryName = "org.gcube.data.analysis.rconnector.RConnector"; private static final String PATH_TO_RCONNECTOR = "/r-connector/gcube/service/connect"; + + private static final String SERVICE_EP_NAME = "RConnector"; + private static final String CATEGORY = "DataAnalysis"; + public static final String USER_ID_ATTR_NAME = "gcube-userId"; /** * when packaging test will fail if the user is not set to test.user * @return . @@ -48,33 +52,49 @@ public class RStudioServiceImpl extends RemoteServiceServlet implements RStudioS public String retrieveRStudioSecureURL() throws IllegalArgumentException { String toReturn = ""; PortalContext pContext = PortalContext.getConfiguration(); - GCubeUser curUser = pContext.getCurrentUser(getThreadLocalRequest()); - long userId = curUser.getUserId(); + String userIdNo = getThreadLocalRequest().getHeader(USER_ID_ATTR_NAME); + long userId = -1; + if (userIdNo != null) { + try { + _log.debug("The userIdNo is " + userIdNo); + userId = Long.parseLong(userIdNo); + + } catch (NumberFormatException e) { + _log.error("The userId is not a number -> " + userId); + } catch (Exception e) { + _log.error("Could not read the current userid from header param, either session expired or user not logged in, exception: " + e.getMessage()); + } + } + GCubeUser curUser = null; + try { + curUser = new LiferayUserManager().getUserById(userId); + } catch (UserManagementSystemException | UserRetrievalFault e1) { + e1.printStackTrace(); + } String scope = pContext.getCurrentScope(getThreadLocalRequest()); String token = pContext.getCurrentUserToken(scope, userId); try { _log.debug("calling rConnector with scope = " + scope + " and token = "+token ); - List resources = getRStudioServiceEndpoints(scope); + List resources = getRStudioServiceEndpoints(scope); if (resources.size() == 0){ - _log.warn("There is no gCORE Endpoint having name " + SERVICE_NAME +" and CLASS " + SERVICECLASS + " in this scope. Returning default instance"); + _log.warn("There is no Servcie Endpoint having CATEGORY " + CATEGORY +" and NAME " + SERVICE_EP_NAME + " in this scope. Returning default instance"); toReturn = rConnector().build().connect().toURL().toExternalForm(); } else { UserManager um = new LiferayUserManager(); _log.debug("Checking if user " + curUser.getFullname() + " has already an RStudio Instance set ... "); - //chec RStudio + //check RStudio if (um.readCustomAttr(userId, RSTUDIO_URL) != null && um.readCustomAttr(userId, RSTUDIO_URL).toString().compareTo("") != 0) { String hostedOn = (String) um.readCustomAttr(userId, RSTUDIO_URL); - _log.debug("**** The instance set for user " + curUser.getFullname() + " is " + hostedOn); + _log.info("**** The instance set for user " + curUser.getFullname() + " is " + hostedOn); toReturn = getRConnectorURL(hostedOn, token); _log.debug("User " + curUser.getFullname() + " has RStudio Instance set, returning rConnector URL " + toReturn); } else {//need to find the RStudio - _log.debug("User " + curUser.getFullname() + "DOES NOT HAVE RStudio Instance set, calculating allocation ... "); + _log.info("User " + curUser.getFullname() + "DOES NOT HAVE RStudio Instance set, calculating allocation ... "); HashMap rStudioDistributionMap = new HashMap<>(); - for (GCoreEndpoint res : resources) { - Endpoint ep = res.profile().endpointMap().get(RCONNECTOR_EntryName); - String hostedOn = ep.uri().getHost()+":"+ep.uri().getPort(); + for (ServiceEndpoint res : resources) { + String hostedOn = res.profile().runtime().hostedOn(); rStudioDistributionMap.put(hostedOn, 0); } List vreUsers = um.listUsersByGroup(pContext.getCurrentGroupId(getThreadLocalRequest()), false); @@ -88,19 +108,19 @@ public class RStudioServiceImpl extends RemoteServiceServlet implements RStudioS } } } - _log.debug("VRE - RStudio allocaiton map as follows: "); + _log.info("VRE - RStudio allocaiton map as follows: "); int min = 0; int i = 0; String host2Select = ""; for (String host : rStudioDistributionMap.keySet()) { - _log.debug("Host " + host + " has # users=" + rStudioDistributionMap.get(host)); + _log.info("Host " + host + " has # users=" + rStudioDistributionMap.get(host)); if (i==0) { host2Select = host; min = rStudioDistributionMap.get(host); } else { int usersNo = rStudioDistributionMap.get(host); if (usersNo < min) { - _log.debug("Host " + host + " has LESS users than " + host2Select + " updating"); + _log.info("Host " + host + " has LESS users than " + host2Select + " updating"); host2Select = host; } } @@ -135,22 +155,65 @@ public class RStudioServiceImpl extends RemoteServiceServlet implements RStudioS return "http://"+hostedOn+PATH_TO_RCONNECTOR+"?gcube-token="+token; } } - + +// private String getRConnectorURL(String hostedOn, String token) { +// _log.info("getRConnectorURL hostedOn:" + hostedOn); +// String[] splits = hostedOn.split(":"); +// String host = splits[0]; +// int port = 80; +// try { +// port = Integer.parseInt(splits[1]); +// } catch (Exception e) { +// _log.warn("Could not find an integer after :, using default port 80"); +// } +// +// String toReturn = ""; +// try { +// toReturn = rConnector().at(host, port).build().connect().toURL().toExternalForm(); +// } catch (MalformedURLException | IllegalArgumentException e) { +// e.printStackTrace(); +// return null; +// } +// if ((port == 443 || port == 8443) && !toReturn.startsWith("https")) { +// toReturn = toReturn.replaceFirst("http", "https"); +// return toReturn; +// } +// else { +// return toReturn; +// } +// } /** * * @return the * @throws Exception */ - private List getRStudioServiceEndpoints(String scope) throws Exception { + private List getRStudioServiceEndpoints(String scope) throws Exception { _log.debug("getRStudioServiceEndpoints on scope="+scope ); String currScope = ScopeProvider.instance.get(); ScopeProvider.instance.set(scope); - SimpleQuery query = queryFor(GCoreEndpoint.class); - query.addCondition("$resource/Profile/ServiceName/text() eq '"+ SERVICE_NAME +"'"); - query.addCondition("$resource/Profile/ServiceClass/text() eq '"+ SERVICECLASS +"'"); - DiscoveryClient client = clientFor(GCoreEndpoint.class); - List toReturn = client.submit(query); + SimpleQuery query = queryFor(ServiceEndpoint.class); + query.addCondition("$resource/Profile/Name/text() eq '"+ SERVICE_EP_NAME +"'"); + query.addCondition("$resource/Profile/Category/text() eq '"+ CATEGORY +"'"); + DiscoveryClient client = clientFor(ServiceEndpoint.class); + List toReturn = client.submit(query); ScopeProvider.instance.set(currScope); return toReturn; } +// /** +// * +// * @return the +// * @throws Exception +// */ +// private List getRStudioServiceEndpoints(String scope) throws Exception { +// _log.debug("getRStudioServiceEndpoints on scope="+scope ); +// String currScope = ScopeProvider.instance.get(); +// ScopeProvider.instance.set(scope); +// SimpleQuery query = queryFor(GCoreEndpoint.class); +// query.addCondition("$resource/Profile/ServiceName/text() eq '"+ SERVICE_NAME +"'"); +// query.addCondition("$resource/Profile/ServiceClass/text() eq '"+ SERVICECLASS +"'"); +// DiscoveryClient client = clientFor(GCoreEndpoint.class); +// List toReturn = client.submit(query); +// ScopeProvider.instance.set(currScope); +// return toReturn; +// } } diff --git a/src/main/webapp/WEB-INF/liferay-plugin-package.properties b/src/main/webapp/WEB-INF/liferay-plugin-package.properties index 701d923..77afcd9 100644 --- a/src/main/webapp/WEB-INF/liferay-plugin-package.properties +++ b/src/main/webapp/WEB-INF/liferay-plugin-package.properties @@ -1,6 +1,6 @@ name=RStudioWrapper module-group-id=liferay -module-incremental-version=1 +module-incremental-version=3 tags= short-description= change-log=