2016-04-11 17:11:58 +02:00
|
|
|
package org.gcube.portlets.user.rstudio_wrapper_portlet.server;
|
|
|
|
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
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;
|
2016-09-15 17:36:41 +02:00
|
|
|
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;
|
2016-04-11 17:11:58 +02:00
|
|
|
import org.gcube.portal.custom.scopemanager.scopehelper.ScopeHelper;
|
|
|
|
import org.gcube.portlets.user.rstudio_wrapper_portlet.client.RStudioService;
|
2016-09-15 17:36:41 +02:00
|
|
|
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
|
|
|
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
2016-04-11 17:11:58 +02:00
|
|
|
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;
|
2016-09-15 17:36:41 +02:00
|
|
|
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
|
|
|
|
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
|
2016-04-11 17:11:58 +02:00
|
|
|
|
|
|
|
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The server side implementation of the RPC service.
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("serial")
|
|
|
|
public class RStudioServiceImpl extends RemoteServiceServlet implements RStudioService {
|
|
|
|
|
|
|
|
private static final Logger _log = LoggerFactory.getLogger(RStudioServiceImpl.class);
|
2016-09-15 17:36:41 +02:00
|
|
|
private static final String SERVICE_EP_NAME = "RConnector";
|
|
|
|
private static final String CATEGORY = "DataAnalysis";
|
2016-04-11 17:11:58 +02:00
|
|
|
/**
|
|
|
|
* the current ASLSession
|
|
|
|
* @return the session
|
|
|
|
*/
|
|
|
|
private ASLSession getASLSession() {
|
|
|
|
String sessionID = this.getThreadLocalRequest().getSession().getId();
|
|
|
|
String user = (String) this.getThreadLocalRequest().getSession().getAttribute(ScopeHelper.USERNAME_ATTRIBUTE);
|
|
|
|
if (user == null) {
|
|
|
|
_log.warn("USER IS NULL setting testing user and Running OUTSIDE PORTAL");
|
|
|
|
user = getDevelopmentUser();
|
|
|
|
}
|
|
|
|
return SessionManager.getInstance().getASLSession(sessionID, user);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* when packaging test will fail if the user is not set to test.user
|
|
|
|
* @return .
|
|
|
|
*/
|
|
|
|
public String getDevelopmentUser() {
|
|
|
|
String user = "test.user";
|
2016-04-26 10:18:38 +02:00
|
|
|
// user = "costantino.perciante";
|
2016-04-11 17:11:58 +02:00
|
|
|
return user;
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public String retrieveRStudioSecureURL() throws IllegalArgumentException {
|
2016-04-26 10:18:38 +02:00
|
|
|
String toReturn = "";
|
|
|
|
String token = getASLSession().getSecurityToken();
|
2016-04-11 17:11:58 +02:00
|
|
|
try {
|
2016-09-15 17:36:41 +02:00
|
|
|
String scope = getASLSession().getScope();
|
|
|
|
_log.debug("calling rConnector with scope = " + scope + " and token = "+token );
|
|
|
|
List<ServiceEndpoint> 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) {
|
2016-04-11 17:11:58 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
_log.debug("returning URL from rConnector = "+toReturn);
|
|
|
|
return toReturn;
|
|
|
|
}
|
2016-09-15 17:36:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return the
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
private List<ServiceEndpoint> 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<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
|
|
|
|
List<ServiceEndpoint> toReturn = client.submit(query);
|
|
|
|
ScopeProvider.instance.set(currScope);
|
|
|
|
return toReturn;
|
|
|
|
}
|
2016-04-11 17:11:58 +02:00
|
|
|
|
|
|
|
}
|