@ -5,6 +5,7 @@ import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor ;
import java.util.List ;
import java.util.Objects ;
import javax.validation.constraints.NotNull ;
import javax.ws.rs.GET ;
@ -15,10 +16,11 @@ import javax.ws.rs.core.MediaType;
import lombok.extern.slf4j.Slf4j ;
import org.gcube.common.resources.gcore. ServiceEndpoint ;
import org.gcube.common.resources.gcore. * ;
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.gcube.resources.discovery.client.queries.impl.XQuery ;
@Slf4j
@Path ( "ServiceEndpoint" )
@ -51,6 +53,39 @@ public class ServiceEndpointResource {
return endpoints ;
}
@GET
@Path ( "/{category}/{name}/{ap}" )
@Produces ( MediaType . TEXT_XML )
public String retrieve ( @NotNull @PathParam ( "name" ) String resourceName ,
@NotNull @PathParam ( "category" ) String resourceCategory ,
@NotNull @PathParam ( "ap" ) String accessPoint ) {
log . info ( "ServiceEndpoint called with category {}, name {} and accessPoint {} in scope {}" , resourceCategory , resourceName , accessPoint , ScopeProvider . instance . get ( ) ) ;
// SimpleQuery query = getQuery(resourceName, resourceCategory);
// query.setResult("$resource/Profile/AccessPoint/Interface/Endpoint[@EntryName='"+accessPoint+"'");
//// DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
// DiscoveryClient<String> client = client();
//// List<ServiceEndpoint> endpoints = client.submit(query);
// log.debug("retrieved endpoint is "+endpoints);
// if (Objects.nonNull(endpoints))
// return endpoints.get(0).toString();
// else
// log.warn("endpoint not found with following coordinates: {} {} and accesspoint: {}", resourceCategory, resourceName,accessPoint);
// return null;
XQuery query = queryFor ( ServiceEndpoint . class ) ;
query . addCondition ( String . format ( "$resource/Profile/Name/text() eq '%s'" , resourceName ) ) ;
query . addCondition ( String . format ( "$resource/Profile/Category/text() eq '%s'" , resourceCategory ) ) ;
query . setResult ( "$resource/Profile/AccessPoint/Interface/Endpoint[@EntryName='" + accessPoint + "']/text()" ) ;
DiscoveryClient < String > client = client ( ) ;
List < String > accessList = client . submit ( query ) ;
if ( Objects . nonNull ( accessList ) )
return accessList . get ( 0 ) . toString ( ) ;
else
log . warn ( "endpoint not found with following coordinates: {} {} and accesspoint: {}" , resourceCategory , resourceName , accessPoint ) ;
return null ;
}
@GET
@Path ( "/{category}/{name}/Result/{result:([^$\\?]+)}" )
@Produces ( MediaType . TEXT_XML )
@ -90,5 +125,21 @@ public class ServiceEndpointResource {
query . addCondition ( String . format ( "$resource/Profile/Category/text() eq '%s'" , resourceCategory ) ) ;
return query ;
}
public static < T extends Resource > XQuery getSpecificXQuery ( T resource ) {
XQuery query = null ;
if ( resource . type ( ) . toString ( ) . equalsIgnoreCase ( "RuntimeResource" ) ) {
query = queryFor ( ServiceEndpoint . class ) ;
} else if ( resource . type ( ) . toString ( ) . equalsIgnoreCase ( "GenericResource" ) ) {
query = queryFor ( GenericResource . class ) ;
} else if ( resource . type ( ) . toString ( ) . equalsIgnoreCase ( "RunningInstance" ) ) {
query = queryFor ( GCoreEndpoint . class ) ;
} else if ( resource . type ( ) . toString ( ) . equalsIgnoreCase ( "GHN" ) ) {
query = queryFor ( HostingNode . class ) ;
} else {
throw new RuntimeException ( "The following resource type is not managed: " + resource ) ;
}
return query ;
}
}