127 lines
4.3 KiB
Java
127 lines
4.3 KiB
Java
package org.gcube.service.helloworld.utils;
|
|
|
|
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
|
|
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.stream.Stream;
|
|
|
|
import org.gcube.common.resources.gcore.ServiceEndpoint;
|
|
// import org.gcube.common.scope.api.ScopeProvider;
|
|
import org.gcube.common.security.AuthorizedTasks;
|
|
import org.gcube.common.security.secrets.Secret;
|
|
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
|
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
|
import org.gcube.smartgears.ContextProvider;
|
|
import org.gcube.smartgears.context.application.ApplicationContext;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
/**
|
|
* Utility class to query EndPoints and search for AccessPoints from IS
|
|
* @author Alfredo Oliviero (ISTI - CNR)
|
|
*/
|
|
|
|
public class InfrastrctureServiceClient {
|
|
private static final Logger logger = LoggerFactory.getLogger(InfrastrctureServiceClient.class);
|
|
|
|
/**
|
|
* Retrieve endpoints information from IS for DB
|
|
*
|
|
* @return list of endpoints for ckan database
|
|
* @throws Throwable
|
|
*/
|
|
public static List<ServiceEndpoint> getEndopintsFromIS(String resource_name, String category,
|
|
boolean root_service) {
|
|
SimpleQuery query = queryFor(ServiceEndpoint.class);
|
|
|
|
if (resource_name != null) {
|
|
query.addCondition("$resource/Profile/Name/text() eq '" + resource_name + "'");
|
|
}
|
|
if (category != null){
|
|
query.addCondition("$resource/Profile/Category/text() eq '" + category + "'");
|
|
}
|
|
DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
|
|
|
|
ApplicationContext ctx = ContextProvider.get();
|
|
|
|
String infra_context = "/" + ctx.container().configuration().infrastructure();
|
|
Secret secret = ctx.container().authorizationProvider().getSecretForContext(infra_context);
|
|
|
|
List<ServiceEndpoint> endpoints = null;
|
|
|
|
try {
|
|
if (root_service) {
|
|
|
|
endpoints = AuthorizedTasks.executeSafely(() -> {
|
|
// esegui la query
|
|
List<ServiceEndpoint> toReturn = client.submit(query);
|
|
return toReturn;
|
|
}, secret);
|
|
} else {
|
|
endpoints = client.submit(query);
|
|
}
|
|
} catch (Throwable e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
return endpoints;
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* @param resource_name
|
|
* @param category
|
|
* @param accessPointName
|
|
* @return
|
|
* @throws Exception
|
|
*/
|
|
public static List<ServiceEndpoint.AccessPoint> getAccessPointsFromIS(String resource_name, String category,
|
|
String accessPointName, boolean root_service) throws Exception {
|
|
|
|
List<ServiceEndpoint> resources = getEndopintsFromIS(resource_name, category, root_service);
|
|
|
|
if (resources.size() == 0) {
|
|
logger.error("There is no Runtime Resource having name " + resource_name + " and Category "
|
|
+ category + " in this scope.");
|
|
return null;
|
|
}
|
|
|
|
List<ServiceEndpoint.AccessPoint> response = new ArrayList<ServiceEndpoint.AccessPoint>();
|
|
resources.forEach(res -> {
|
|
Stream<ServiceEndpoint.AccessPoint> access_points_res = res.profile().accessPoints().stream();
|
|
|
|
if (accessPointName == null) {
|
|
access_points_res = access_points_res.filter(ap -> ap.name().equals(accessPointName));
|
|
}
|
|
|
|
access_points_res.forEach(a -> response.add(a));
|
|
});
|
|
return response;
|
|
}
|
|
|
|
/**
|
|
* @param resource_name
|
|
* @param category
|
|
* @param accessPointName
|
|
* @return
|
|
* @throws Exception
|
|
*/
|
|
public static ServiceEndpoint.AccessPoint getAccessPointFromIS(String resource_name, String category,
|
|
String accessPointName, boolean root_service) throws Exception {
|
|
|
|
List<ServiceEndpoint.AccessPoint> access_points = getAccessPointsFromIS(resource_name, category, accessPointName, root_service);
|
|
|
|
if (access_points.size() == 0) {
|
|
logger.error("Unable to retrieve service endpoint " + accessPointName);
|
|
return null;
|
|
}
|
|
|
|
return access_points.get(0);
|
|
|
|
}
|
|
|
|
}
|