added support for service endpoint discovery

master
parent faaca57b91
commit cda0c029fe
  1. 87
      src/main/java/org/gcube/portal/ddas/BrokerServiceEndpoint.java
  2. 6
      src/main/java/org/gcube/portal/ddas/DdasVREService.java

@ -0,0 +1,87 @@
package org.gcube.portal.ddas;
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
import org.gcube.common.encryption.encrypter.StringEncrypter;
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.resources.gcore.utils.Group;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.slf4j.LoggerFactory;
import net.spy.memcached.KetamaConnectionFactory;
import net.spy.memcached.MemcachedClient;
/**
* @author Massimiliano Assante at ISTI-CNR
*/
public class BrokerServiceEndpoint {
// Logger
private static final org.slf4j.Logger _log = LoggerFactory.getLogger(BrokerServiceEndpoint.class);
private static final String BC_BROKER_RESOURCE_NAME = "Blue-Cloud-DataDownloadAndAccess";
private static final String CATEGORY_NAME = "Service";
private static final String CALLBACK_PROPERTY_NAME = "vre_callback_url";
/**
* Retrieve endpoint resoruce from IS
* @return List of InetSocketAddresses
* @throws Exception
*/
public static String getVRECallbackURLFromServiceEndpoint(){
String currentScope = ScopeProvider.instance.get();
String infrastructure = "/"+PortalContext.getConfiguration().getInfrastructureName();
ScopeProvider.instance.set(infrastructure);
try{
SimpleQuery query = queryFor(ServiceEndpoint.class);
query.addCondition("$resource/Profile/Name/text() eq '"+ BC_BROKER_RESOURCE_NAME +"'");
query.addCondition("$resource/Profile/Category/text() eq '"+ CATEGORY_NAME +"'");
DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
try {
List<ServiceEndpoint> list = client.submit(query);
if (list.size() > 1) {
System.out.println("Too many Service Endpoints having name " + BC_BROKER_RESOURCE_NAME +" in this scope having Category " + CATEGORY_NAME);
}
else if (list.size() == 0){
System.out.println("There is no Service Endpoint having name " + BC_BROKER_RESOURCE_NAME +" and Category " + CATEGORY_NAME + " in this context " + infrastructure);
}
else {
for (ServiceEndpoint res : list) {
String resourceName = res.profile().name();
AccessPoint[] accessPoints = (AccessPoint[]) res.profile().accessPoints().toArray(new AccessPoint[res.profile().accessPoints().size()]);
for (AccessPoint found : accessPoints) {
for (ServiceEndpoint.Property prop : found.properties()) {
System.out.println(prop.name());
if (prop.name().compareTo(CALLBACK_PROPERTY_NAME) == 0)
return prop.value();
}
}
_log.warn("There is no Service Endpoint having name: " + BC_BROKER_RESOURCE_NAME + " and Category " + CATEGORY_NAME + " on root context");
}
}
} catch (Exception e) {
e.printStackTrace();
}
} catch(Exception e){
_log.error("There is no Service Endpoint having name: " + BC_BROKER_RESOURCE_NAME + " and Category " + CATEGORY_NAME + " on root context");
}finally{
ScopeProvider.instance.set(currentScope);
}
ScopeProvider.instance.set(currentScope);
return null;
}
}

@ -28,7 +28,6 @@ public class DdasVREService extends HttpServlet {
private static final long serialVersionUID = 1L;
private static com.liferay.portal.kernel.log.Log _log = LogFactoryUtil.getLog(DdasVREService.class);
private static final int CACHE_SECONDS_EXPIRATION = 1800; //30 minutes
private static final String VRE_CONFIRM_URL = "https://blue-cloud.d4science.org/group/bluecloud-gateway/vre-data-pool";
/**
* @see HttpServlet#HttpServlet()
*/
@ -84,10 +83,11 @@ public class DdasVREService extends HttpServlet {
Base64.Encoder withoutPaddingEncoder = Base64.getEncoder().withoutPadding();
String requestIdParam = withoutPaddingEncoder.encodeToString("otp".getBytes());
String message = "Started downloading order files to Virtual Research Environment '"+requestId+"'";
String vreConfirmUrl = new StringBuilder(VRE_CONFIRM_URL).append("?").append(requestIdParam).append("=").append(otp).toString(); //redirect URL
String vreCallBackURL = BrokerServiceEndpoint.getVRECallbackURLFromServiceEndpoint();
String vreConfirmUrl = new StringBuilder(vreCallBackURL).append("?").append(requestIdParam).append("=").append(otp).toString(); //redirect URL
URL theURL = new URL(vreConfirmUrl);
theResponse = new BrokerResponse(status, message, theURL);
_log.debug("Response to DD&AS BROKER:\n" +theResponse.toString());
}
else {
theResponse = new BrokerResponse(500, "An error occurred in the VRE Service, downlaod not possible", null);

Loading…
Cancel
Save