diff --git a/.classpath b/.classpath index 29e015f..09d95e2 100644 --- a/.classpath +++ b/.classpath @@ -1,18 +1,18 @@ - + - + - + @@ -56,5 +56,5 @@ - + diff --git a/.project b/.project index d8746bc..fcaa210 100644 --- a/.project +++ b/.project @@ -49,5 +49,6 @@ org.eclipse.wst.common.project.facet.core.nature org.eclipse.wst.jsdt.core.jsNature com.google.gwt.eclipse.core.gwtNature + com.liferay.ide.core.liferayNature diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component index 77237b0..18e58c6 100644 --- a/.settings/org.eclipse.wst.common.component +++ b/.settings/org.eclipse.wst.common.component @@ -1,5 +1,5 @@ - + diff --git a/.settings/org.eclipse.wst.common.project.facet.core.xml b/.settings/org.eclipse.wst.common.project.facet.core.xml index 0777079..3fb44fe 100644 --- a/.settings/org.eclipse.wst.common.project.facet.core.xml +++ b/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -2,8 +2,8 @@ - + - + diff --git a/pom.xml b/pom.xml index 7f406ff..6e53060 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ org.gcube.portlets.user rstudio-wrapper-portlet war - 1.1.0-SNAPSHOT + 1.2.0-SNAPSHOT RStudio Wrapper Portlet @@ -70,6 +70,11 @@ custom-portal-handler provided + + org.gcube.common.portal + portal-manager + provided + org.gcube.core common-scope-maps 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 c61cf98..f785144 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 @@ -7,13 +7,21 @@ import java.util.List; import org.gcube.application.framework.core.session.ASLSession; import org.gcube.application.framework.core.session.SessionManager; import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.common.portal.PortalContext; +import org.gcube.common.resources.gcore.ServiceEndpoint; +import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; +import org.gcube.common.scope.api.ScopeProvider; import org.gcube.portal.custom.scopemanager.scopehelper.ScopeHelper; 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.slf4j.Logger; import org.slf4j.LoggerFactory; import static org.gcube.common.authorization.client.Constants.authorizationService; import static org.gcube.data.analysis.rconnector.client.Constants.rConnector; +import static org.gcube.resources.discovery.icclient.ICFactory.clientFor; +import static org.gcube.resources.discovery.icclient.ICFactory.queryFor; import com.google.gwt.user.server.rpc.RemoteServiceServlet; @@ -24,6 +32,8 @@ 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_EP_NAME = "RConnector"; + private static final String CATEGORY = "DataAnalysis"; /** * the current ASLSession * @return the session @@ -51,13 +61,55 @@ public class RStudioServiceImpl extends RemoteServiceServlet implements RStudioS String toReturn = ""; String token = getASLSession().getSecurityToken(); try { - _log.debug("calling rConnector with token = "+token); - toReturn = rConnector().build().connect().toURL().toExternalForm(); - } catch (MalformedURLException e) { + String scope = getASLSession().getScope(); + _log.debug("calling rConnector with scope = " + scope + " and token = "+token ); + List resources = getRStudioServiceEndpoints(scope); + if (resources.size() > 1) { + _log.error("Too many Service Endpoints having name " + SERVICE_EP_NAME +" in this scope: " + scope); + throw new TooManyRStudioResourcesException("There exist more than 1 Runtime Resource in this scope having name " + + SERVICE_EP_NAME + " and Category " + CATEGORY + ". Only one allowed per scope."); + } + else if (resources.size() == 0){ + _log.warn("There is no Service Endpoint having name " + SERVICE_EP_NAME +" and Category " + CATEGORY + " in this scope. Returning default instance"); + toReturn = rConnector().build().connect().toURL().toExternalForm(); + } else { + ServiceEndpoint res = resources.get(0); + String hostedOn = res.profile().runtime().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"); + } + toReturn = rConnector().at(host, port).build().connect().toURL().toExternalForm(); + + } + + } catch (Exception e) { e.printStackTrace(); } _log.debug("returning URL from rConnector = "+toReturn); 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(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; + } } diff --git a/src/main/java/org/gcube/portlets/user/rstudio_wrapper_portlet/server/TooManyRStudioResourcesException.java b/src/main/java/org/gcube/portlets/user/rstudio_wrapper_portlet/server/TooManyRStudioResourcesException.java new file mode 100644 index 0000000..d6035cf --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/rstudio_wrapper_portlet/server/TooManyRStudioResourcesException.java @@ -0,0 +1,8 @@ +package org.gcube.portlets.user.rstudio_wrapper_portlet.server; + +@SuppressWarnings("serial") +public class TooManyRStudioResourcesException extends Exception { + public TooManyRStudioResourcesException(String message) { + super(message); + } +} \ No newline at end of file