commit 219d07154fdeaf6abbe4211ad65697d0dbe26abb Author: fabio.simeoni Date: Tue Jan 8 08:15:10 2013 +0000 1.x branch (first created for gCube 2.12) git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/branches/information-system/ic-client/1.0@67187 82a268e6-3cf1-43bd-a215-b396298e98cf diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..534b5e5 --- /dev/null +++ b/.classpath @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..0f0211c --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + ic-client + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/distro/INSTALL b/distro/INSTALL new file mode 100644 index 0000000..8d1c8b6 --- /dev/null +++ b/distro/INSTALL @@ -0,0 +1 @@ + diff --git a/distro/LICENSE b/distro/LICENSE new file mode 100644 index 0000000..630ba97 --- /dev/null +++ b/distro/LICENSE @@ -0,0 +1,6 @@ +gCube System - License +------------------------------------------------------------ + +The gCube/gCore software is licensed as Free Open Source software conveying to the EUPL (http://ec.europa.eu/idabc/eupl). +The software and documentation is provided by its authors/distributors "as is" and no expressed or +implied warranty is given for its use, quality or fitness for a particular case. diff --git a/distro/MAINTAINERS b/distro/MAINTAINERS new file mode 100644 index 0000000..e58db00 --- /dev/null +++ b/distro/MAINTAINERS @@ -0,0 +1,2 @@ +* Fabio Simeoni (fabio.simeoni@fao.org), FAO of the UN, Italy +* Lucio Lelii (lucio.lelii@isti.cnr.it), CNR, Italy \ No newline at end of file diff --git a/distro/README b/distro/README new file mode 100644 index 0000000..e14fefb --- /dev/null +++ b/distro/README @@ -0,0 +1,39 @@ +The gCube System - ${name} +---------------------- + +This work has been partially supported by the following European projects: DILIGENT (FP6-2003-IST-2), D4Science (FP7-INFRA-2007-1.2.2), +D4Science-II (FP7-INFRA-2008-1.2.2), iMarine (FP7-INFRASTRUCTURES-2011-2), and EUBrazilOpenBio (FP7-ICT-2011-EU-Brazil). + +Authors +------- + +* Fabio Simeoni (fabio.simeoni@fao.org), FAO of the UN, Italy +* Lucio Lelii (lucio.lelii@isti.cnr.it), CNR, Italy + +Version and Release Date +------------------------ +${version} + +Description +----------- +${description} + +Download information +-------------------- + +Source code is available from SVN: +${scm.url} + +Binaries can be downloaded from: + + +Documentation +------------- +Documentation is available on-line from the Projects Documentation Wiki: +https://gcube.wiki.gcube-system.org/gcube/index.php + + +Licensing +--------- + +This software is licensed under the terms you may find in the file named "LICENSE" in this directory. diff --git a/distro/changelog.xml b/distro/changelog.xml new file mode 100644 index 0000000..07fb021 --- /dev/null +++ b/distro/changelog.xml @@ -0,0 +1,5 @@ + + + First Release + + \ No newline at end of file diff --git a/distro/descriptor.xml b/distro/descriptor.xml new file mode 100644 index 0000000..06c416f --- /dev/null +++ b/distro/descriptor.xml @@ -0,0 +1,38 @@ + + servicearchive + + tar.gz + + / + + + ${distroDirectory} + / + true + + README + LICENSE + INSTALL + MAINTAINERS + changelog.xml + profile.xml + + 755 + true + + + + + target/${build.finalName}.jar + /${artifactId} + + + ${distroDirectory}/svnpath.txt + /${artifactId} + true + + + \ No newline at end of file diff --git a/distro/profile.xml b/distro/profile.xml new file mode 100644 index 0000000..f6970f3 --- /dev/null +++ b/distro/profile.xml @@ -0,0 +1,26 @@ + + + + Service + + ${description} + InformationSystem + ${artifactId} + 1.0.0 + + + ${artifactId} + ${version} + + ${groupId} + ${artifactId} + ${version} + + + ${build.finalName}.jar + + + + + + diff --git a/distro/svnpath.txt b/distro/svnpath.txt new file mode 100644 index 0000000..f416f9d --- /dev/null +++ b/distro/svnpath.txt @@ -0,0 +1 @@ +${scm.url} diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..019320c --- /dev/null +++ b/pom.xml @@ -0,0 +1,127 @@ + + 4.0.0 + + org.gcube.tools + maven-parent + 1.0.0 + + + org.gcube.resources.discovery + ic-client + 1.0.0-SNAPSHOT + Information Collector Client + Client API for the Information Collector service + + + scm:svn:http://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/${project.artifactId} + scm:svn:https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/${project.artifactId} + http://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/${project.artifactId} + + + + distro + + + + + + + org.gcube.core + common-scope + [1.0.0-SNAPSHOT,2.0.0-SNAPSHOT) + + + + org.gcube.resources.discovery + discovery-client + [1.0.0-SNAPSHOT,2.0.0-SNAPSHOT) + + + + org.gcube.resources + common-gcore-resources + [1.0.0-SNAPSHOT,2.0.0-SNAPSHOT) + + + + org.gcube.core + common-gcore-stubs + [1.0.0-SNAPSHOT,2.0.0-SNAPSHOT) + + + + org.slf4j + slf4j-api + 1.6.4 + + + + org.slf4j + slf4j-simple + 1.6.4 + test + + + + junit + junit + 4.11 + test + + + + + + + + + org.apache.maven.plugins + maven-resources-plugin + 2.5 + + + copy-profile + install + + copy-resources + + + target + + + ${distroDirectory} + true + + profile.xml + + + + + + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + ${distroDirectory}/descriptor.xml + + + + + servicearchive + install + + single + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/org/gcube/resources/discovery/icclient/Helper.java b/src/main/java/org/gcube/resources/discovery/icclient/Helper.java new file mode 100644 index 0000000..ec353bf --- /dev/null +++ b/src/main/java/org/gcube/resources/discovery/icclient/Helper.java @@ -0,0 +1,206 @@ +package org.gcube.resources.discovery.icclient; + +import static org.gcube.common.scope.impl.ScopeBean.Type.*; + +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.common.scope.impl.ScopeBean; +import org.gcube.resources.discovery.icclient.stubs.MalformedQueryException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Helper { + + private static final Logger log = LoggerFactory.getLogger(Helper.class); + + public static String queryAddAuthenticationControl(String expression) throws MalformedQueryException + { + + String scope = ScopeProvider.instance.get(); + + if (scope==null || !new ScopeBean(scope).is(VRE)) + return expression; + + int wherePathIndex=0; + int returnPathIndex=0; + int collIndexEnd=0; + int collIndexStart=0; + boolean whereFinded=false; + String forInsertFinal=""; + String whereInsertFinal=""; + String temp=""; + + String forPropertiesString="\n *VAR* in *COLLECTION*/Data/child::*[local-name()='Scopes'] \n"; + String forString=" *VAR* in *COLLECTION*/Scopes \n"; + String authString=" (contains(*VAR*//Scope,'"+scope+"') or contains(*VAR*//Scope,'"+scope.substring(0,scope.lastIndexOf("/"))+"')) "; + String authStringNormal=" contains(*VAR*//Scope,'"+scope+"') "; + + String queryFiltered; + List collInsert= new ArrayList(); + Pattern wherePattern= Pattern.compile("where"); + //mathcing , $result in $outer/Data + Pattern inSubResult= Pattern.compile("(,\\s*([^\\s]*)\\s*in\\s*([^\\s]*)\\s*)(where|order\\sby|return)"); + Pattern returnPattern= Pattern.compile("order\\sby|return"); + + + Pattern collectionPattern= Pattern.compile("[^\\s]*\\s*in\\s*collection\\s*[^\\s,]*"); + Pattern varPattern= Pattern.compile("[^\\s]*"); + Pattern resourcePattern= Pattern.compile("\\scollection\\s*\\([^\\)]*.*/Resource", Pattern.DOTALL); + Pattern propertiesPattern=Pattern.compile("\\scollection\\s*\\([^\\)]*.*/Document", Pattern.DOTALL); + Matcher varMat; + Matcher resourceMat; + Matcher collMat=collectionPattern.matcher(expression); + collMat.reset(); + String forStringTemp=""; + while (collMat.find(collIndexEnd)) + { + + try{ + collIndexEnd=collMat.end(); + temp=collMat.group(); + collIndexStart=collMat.start(); + }catch(IllegalStateException e){ + log.warn("error parsing collection statement"); + } + varMat= varPattern.matcher(temp); + boolean propBool=false; + if (temp.contains("/Properties")) + { + resourceMat= propertiesPattern.matcher(temp); + propBool=true; + } + else resourceMat=resourcePattern.matcher(temp); + varMat.lookingAt(); + resourceMat.find(); + String tempPath=""; + try{ + tempPath= temp.substring(resourceMat.end()); + if (propBool) + { + String resourceMatString= resourceMat.group(); + forStringTemp=forPropertiesString.replace("*VAR*","$entry"+collInsert.size()+"ValueAuth" ).replace("*COLLECTION*", resourceMatString ); + Pair c= new Pair("let "+varMat.group()+" := "+" $entry"+collInsert.size()+"ValueAuth/.."+tempPath, true); + collInsert.add(c); + } + else + { + String resourceMatString= resourceMat.group(); + forStringTemp=forString.replace("*VAR*","$entry"+collInsert.size()+"ValueAuth" ).replace("*COLLECTION*",resourceMatString ); + Pair c= new Pair("let "+varMat.group()+" := "+" $entry"+collInsert.size()+"ValueAuth/.."+tempPath, (resourceMatString.contains("/Profiles/RunningInstance") || resourceMatString.contains("/Profiles/GHN") || resourceMatString.contains("/Profiles/Service"))); + collInsert.add(c); + } + }catch(IllegalStateException e){ log.debug("error parsing statement");} + expression=expression.substring(0, collIndexStart)+ forStringTemp +expression.substring(collIndexEnd); + collMat=collectionPattern.matcher(expression); + } + + + + if (collInsert.size()==0) return expression; + //concat the let statements + + + for (int i=0; i { + + private static final Logger log = LoggerFactory.getLogger(ICClient.class); + + //result split pattern + private static final Pattern pattern = Pattern.compile("(.*?)", Pattern.DOTALL); + + + public List submit(Query query) throws DiscoveryException { + + try { + CollectorStub stub = getStub(); + String results = callService(query, stub); + return splitIntoList(results); + } + catch(MalformedQueryException e) { + throw new DiscoveryException("malformed query "+query.expression(),e); + } + catch(SOAPFaultException e) { + throw new RuntimeException(JAXWSUtils.remoteCause(e)); + } + + } + + public Stream submitForStream(Query query) throws DiscoveryException, InvalidResultException { + // TODO: use real streaming if and when IC will support it + return convert(submit(query)); + } + + + + + //helper + private String callService(Query query, CollectorStub stub) { + + String expression = query.expression(); + + log.info("executing query {}",expression); + + long time = System.currentTimeMillis(); + + String submittedExpression = Helper.queryAddAuthenticationControl(expression); + + String response = stub.execute(submittedExpression); + + log.info("executed query {} in {} ms",expression,System.currentTimeMillis()-time); + + return response; + } + + //helper + private CollectorStub getStub() { + + //find endpoint address in service map currently in scope + String address = ServiceMap.instance.endpoint(localname); + + //obtain a JAXWS stub configured for gCube calls + return stubFor(collector).at(URI.create(address)); + } + + //helper + private List splitIntoList(String response) { + + List results = new ArrayList(); + + Matcher m = pattern.matcher(response); + + while (m.find()) + results.add(m.group(1).trim()); + + return results; + } +} diff --git a/src/main/java/org/gcube/resources/discovery/icclient/ICFactory.java b/src/main/java/org/gcube/resources/discovery/icclient/ICFactory.java new file mode 100644 index 0000000..81b86cf --- /dev/null +++ b/src/main/java/org/gcube/resources/discovery/icclient/ICFactory.java @@ -0,0 +1,131 @@ +package org.gcube.resources.discovery.icclient; + +import static java.lang.String.*; +import static org.gcube.resources.discovery.client.queries.impl.XQuery.*; + +import java.util.HashMap; +import java.util.Map; + +import org.gcube.common.resources.gcore.GCoreEndpoint; +import org.gcube.common.resources.gcore.GenericResource; +import org.gcube.common.resources.gcore.HostingNode; +import org.gcube.common.resources.gcore.ServiceEndpoint; +import org.gcube.common.resources.gcore.ServiceInstance; +import org.gcube.common.resources.gcore.Software; +import org.gcube.resources.discovery.client.api.DiscoveryClient; +import org.gcube.resources.discovery.client.api.ResultParser; +import org.gcube.resources.discovery.client.impl.DelegateClient; +import org.gcube.resources.discovery.client.impl.JAXBParser; +import org.gcube.resources.discovery.client.queries.impl.XQuery; + +/** + * Factory of {@link XQuery}s and {@link DiscoveryClient}s for the Information Collector service. + * + * @author Fabio Simeoni + * + */ +public class ICFactory { + + // known query parameters, indexed by resource type + private static Map, Map> registry = new HashMap, Map>(); + + public static final String nsDeclaration = "declare namespace ic = 'http://gcube-system.org/namespaces/informationsystem/registry';"; + + public static final String instancesNSDeclaration = nsDeclaration+ + "declare namespace gcube = 'http://gcube-system.org/namespaces/common/core/porttypes/GCUBEProvider';"; + + private final static String profile_range = "collection('/db/Profiles/%1$s')//Document/Data/ic:Profile/Resource"; + + // registers parameters for known queries + static { + + register(GenericResource.class, + params().add(ns, nsDeclaration).add(range, format(profile_range, "GenericResource")).build()); + register(ServiceEndpoint.class, + params().add(ns, nsDeclaration).add(range, format(profile_range, "RuntimeResource")).build()); + register(ServiceInstance.class, + params().add(ns, instancesNSDeclaration).add(range, "collection('/db/Properties')//Document").build()); + register(GCoreEndpoint.class, + params().add(ns, nsDeclaration).add(range, format(profile_range, "RunningInstance")).build()); + register(Software.class, + params().add(ns, nsDeclaration).add(range, format(profile_range, "Service")).build()); + register(HostingNode.class, + params().add(ns, nsDeclaration).add(range, format(profile_range, "GHN")).build()); + + } + + // helper + private static void register(Class type, Map parameters) throws IllegalStateException { + if (registry.containsKey(type)) + throw new IllegalStateException("a query is already registered for type" + type); + else + registry.put(type, parameters); + } + + /** + * Returns a {@link XQuery} for a given resource type. + * + * @param type the resource type + * @return the query + * @throws IllegalStateException if a query for the resource type has not been previously registered + */ + public static XQuery queryFor(Class type) throws IllegalStateException { + + if (registry.containsKey(type)) + return new XQuery(registry.get(type)); + else + throw new IllegalStateException("no query registered for " + type); + } + + /** + * Returns a {@link DiscoveryClient} that submits queries to the Information Collector service and parses query results with a + * given resource type. + * + * @param type the resource type + * @return the client + */ + public static DiscoveryClient clientFor(Class type) { + return new DelegateClient(new JAXBParser(type), new ICClient()); + } + + /** + * Returns a {@link DiscoveryClient} that submits queries to the Information Collector service without + * parsing the results. + * + * @return the client + */ + public static DiscoveryClient client() { + return new ICClient(); + } + + /** + * Returns a {@link DiscoveryClient} that submits queries to the Information Collector service and parses query results with a + * given {@link ResultParser}. + * + * @param parser the resource parser + * @return the client + */ + public static DiscoveryClient clientWith(ResultParser parser) { + return new DelegateClient(parser, new ICClient()); + } + + // utils + + public static ParameterBuilder params() { + return new ParameterBuilder(); + } + + public static class ParameterBuilder { + + private Map params = new HashMap(); + + public ParameterBuilder add(String name, String value) { + params.put(name, value); + return this; + } + + public Map build() { + return params; + } + } +} diff --git a/src/main/java/org/gcube/resources/discovery/icclient/stubs/CollectorConstants.java b/src/main/java/org/gcube/resources/discovery/icclient/stubs/CollectorConstants.java new file mode 100644 index 0000000..ac0097c --- /dev/null +++ b/src/main/java/org/gcube/resources/discovery/icclient/stubs/CollectorConstants.java @@ -0,0 +1,33 @@ +package org.gcube.resources.discovery.icclient.stubs; + +import static org.gcube.common.clients.stubs.jaxws.GCoreServiceBuilder.*; + +import javax.xml.namespace.QName; + +import org.gcube.common.clients.stubs.jaxws.GCoreService; + +/** + * Stub-related constants. + * + * @author Fabio Simeoni + * + */ +public class CollectorConstants { + + //public constants + public static final String namespace = "http://gcube-system.org/namespaces/informationsystem/collector/XQueryAccess/service"; + public static final String localname = "XQueryAccessService"; + public static final QName name = new QName(namespace,localname); + + public static String service_class="InformationSystem"; + public static String service_name="IS-Collector"; + + //package-private constants for JAXWS interface annotations + static final String target_namespace = "http://gcube-system.org/namespaces/informationsystem/collector/XQueryAccess"; + static final String portType = "XQueryAccessPortType"; + static final String port = "XQueryAccessPortTypePort"; + + public final static GCoreService collector = service().withName(name). + coordinates(service_class, service_name). + andInterface(CollectorStub.class); +} diff --git a/src/main/java/org/gcube/resources/discovery/icclient/stubs/CollectorStub.java b/src/main/java/org/gcube/resources/discovery/icclient/stubs/CollectorStub.java new file mode 100644 index 0000000..2046adf --- /dev/null +++ b/src/main/java/org/gcube/resources/discovery/icclient/stubs/CollectorStub.java @@ -0,0 +1,29 @@ +package org.gcube.resources.discovery.icclient.stubs; + +import static org.gcube.resources.discovery.icclient.stubs.CollectorConstants.*; + +import javax.jws.WebMethod; +import javax.jws.WebParam; +import javax.jws.WebResult; +import javax.jws.WebService; +import javax.xml.ws.soap.SOAPFaultException; + +/** + * A local interface to the resource discovery service. + * + * + */ +@WebService(name=portType,targetNamespace=target_namespace) +public interface CollectorStub { + + /** + * Executes a {@link QueryStub}. + * @param query the query + * @return the query results + * @throws MalformedQueryException if the query is malformed + * @throws SOAPFaultException if the query cannot be executed + */ + @WebMethod(operationName="XQueryExecute") + @WebResult(name="Dataset") + String execute(@WebParam(name="XQueryExpression") String query) throws MalformedQueryException; +} diff --git a/src/main/java/org/gcube/resources/discovery/icclient/stubs/MalformedQueryException.java b/src/main/java/org/gcube/resources/discovery/icclient/stubs/MalformedQueryException.java new file mode 100644 index 0000000..3b3bf64 --- /dev/null +++ b/src/main/java/org/gcube/resources/discovery/icclient/stubs/MalformedQueryException.java @@ -0,0 +1,25 @@ +package org.gcube.resources.discovery.icclient.stubs; + +import javax.xml.ws.WebFault; + +/** + * Thrown by {@link CollectorStub#execute(String)} when the query cannot be submitted to the Information Collector + * service. + */ +@WebFault(name = "XQueryFaultType") +public class MalformedQueryException extends RuntimeException { + + /** + * + */ + private static final long serialVersionUID = 1L; + + /** + * Creates an instance with a given message and an {@link AxisFaultInfo} payload + * @param message the message + * @param info the payload + */ + public MalformedQueryException(String message) { + super(message); + } +} diff --git a/src/test/java/org/acme/APIClient.java b/src/test/java/org/acme/APIClient.java new file mode 100644 index 0000000..7aca257 --- /dev/null +++ b/src/test/java/org/acme/APIClient.java @@ -0,0 +1,220 @@ +package org.acme; + +import static java.lang.String.*; +import static org.gcube.resources.discovery.icclient.ICFactory.*; + +import java.net.URI; +import java.util.List; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementRef; +import javax.xml.bind.annotation.XmlRootElement; + +import org.gcube.common.resources.gcore.GCoreEndpoint; +import org.gcube.common.resources.gcore.ServiceEndpoint; +import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; +import org.gcube.common.resources.gcore.ServiceInstance; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.resources.discovery.client.api.DiscoveryClient; +import org.gcube.resources.discovery.client.queries.api.Query; +import org.gcube.resources.discovery.client.queries.impl.XQuery; +import org.junit.BeforeClass; +import org.junit.Test; + +public class APIClient { + + @BeforeClass + public static void setup() { + + ScopeProvider.instance.set("/gcube/devsec"); + } + + + @Test + public void allServiceEndpoints() { + + + //pick a predefined query + Query query = queryFor(ServiceEndpoint.class); + + //pick a client that knows how to parse those resources + DiscoveryClient client = clientFor(ServiceEndpoint.class); + + //execute query in current scope + List resources = client.submit(query); + + System.out.println(resources); + + } + + @Test + public void someServiceEndpoints() { + + + XQuery query = queryFor(ServiceEndpoint.class); + query.addCondition("$resource/Profile/Category/text() eq 'Database'"); + query.setResult("$resource/Profile/AccessPoint/Interface/Endpoint/text()"); + + DiscoveryClient client = client(); + + List resources = client.submit(query); + + System.out.println(resources); + + } + + @Test + public void someServiceEndpointAddresses() { + + + XQuery query = queryFor(ServiceEndpoint.class); + query.addCondition("$resource/Profile/Category/text() eq 'Database'"); + query.setResult("$resource/Profile/AccessPoint/Interface/Endpoint/text()"); + + DiscoveryClient client = client(); + + List addresses = client.submit(query); + + System.out.println(addresses); + + } + + @Test + public void someGCoreServiceEndpointAddresses() { + + + XQuery query = queryFor(GCoreEndpoint.class); + query.addCondition("$resource/Profile/ServiceClass/text() eq 'DataAccess'"); + query.addCondition("$resource/Profile/ServiceName/text() eq 'tree-manager-service'"); + query.addVariable("$entry","$resource/Profile/AccessPoint/RunningInstanceInterfaces/Endpoint"); + query.addCondition("$entry/@EntryName/string() eq 'gcube/data/tm/binder'"); + query.setResult("$entry/text()"); + + DiscoveryClient client = client(); + + List addresses = client.submit(query); + + System.out.println(addresses); + + } + + @Test + public void someCustomServiceEndpointResults() { + + + XQuery query = queryFor(ServiceEndpoint.class); + query.addCondition("$resource/Profile/Category/text() eq 'Database'"); + query.setResult("" + + "{$resource/ID/text()}" + + "{$resource/Profile/AccessPoint}" + + ""); + + DiscoveryClient client = clientFor(PerfectResult.class); + + List results = client.submit(query); + + for (PerfectResult result : results) { + System.out.println(result.id+":"+result.ap); + } + + } + + @XmlRootElement(name="perfect") + private static class PerfectResult { + + @XmlElement(name="id") + String id; + + @XmlElementRef + AccessPoint ap; + } + + + @Test + public void someServiceEndpointsAccessData() { + + + XQuery query = queryFor(ServiceEndpoint.class); + query.addCondition("$resource/Profile/Category/text() eq 'Database'") + .setResult("$resource/Profile/AccessPoint"); + + DiscoveryClient client = clientFor(AccessPoint.class); + + List accesspoints = client.submit(query); + + for (AccessPoint point : accesspoints) { + System.out.println(point.name()+":"+point.address()); + } + + } + + @Test + public void someServiceInstances() { + + + XQuery query = queryFor(ServiceInstance.class); + query.addCondition("$resource/Data/gcube:ServiceClass/text() eq 'DataAccess'"). + addCondition("$resource/Data/gcube:ServiceName/text() eq 'tree-manager-service'"); + + DiscoveryClient client = clientFor(ServiceInstance.class); + + List props = client.submit(query); + + + + System.out.println(props); + + } + + @Test + public void someServiceInstanceReferences() { + + + XQuery query = queryFor(ServiceInstance.class); + query.addCondition("$resource/Data/gcube:ServiceClass/text() eq 'DataAccess'"). + addCondition("$resource/Data/gcube:ServiceName/text() eq 'tree-manager-service'") + .addCondition(format("$resource/Source/text()[ends-with(.,'%1s')]","gcube/data/tm/binder")); + + DiscoveryClient client = clientFor(ServiceInstance.class); + + List refs = client.submit(query); + + System.out.println(refs); + + } + + @Test + public void someServiceInstanceProperties() { + + + XQuery query = queryFor(ServiceInstance.class); + query.addNamespace("tm",URI.create("http://gcube-system.org/namespaces/data/tm")). + addCondition("$resource/Data/tm:Plugin/name/text() eq 'species-tree-plugin'"); + + DiscoveryClient client = clientFor(ServiceInstance.class); + + List props = client.submit(query); + + System.out.println(props); + + } + + @Test + public void someServiceEndpointsProperties() { + + + XQuery query = queryFor(ServiceEndpoint.class); + query.addVariable("$prop", "$resource/Profile/AccessPoint/Properties/Property"). + addCondition("$prop/Name/text() eq 'dbname'"). + addCondition("$prop/Value/text() eq 'timeseries'"). + setResult("$resource/Profile/AccessPoint/Interface/Endpoint/text()"); + + DiscoveryClient client = client(); + + List props = client.submit(query); + + System.out.println(props); + + } + +} diff --git a/src/test/java/org/acme/QueryClient.java b/src/test/java/org/acme/QueryClient.java new file mode 100644 index 0000000..9fa2b43 --- /dev/null +++ b/src/test/java/org/acme/QueryClient.java @@ -0,0 +1,48 @@ +package org.acme; + +import static org.gcube.resources.discovery.icclient.ICFactory.*; + +import java.util.Map; + +import org.gcube.common.resources.gcore.GenericResource; +import org.gcube.common.resources.gcore.ServiceInstance; +import org.gcube.resources.discovery.client.queries.api.Query; +import org.gcube.resources.discovery.client.queries.impl.QueryBox; +import org.gcube.resources.discovery.client.queries.impl.XQuery; +import org.gcube.resources.discovery.client.queries.impl.QueryTemplate; + +public class QueryClient { + + public static void main(String[] args) { + + //a custom query: client entirely exposed to query language + Query q1 = new QueryBox("my query"); + System.out.println(q1); + + //a custom template query: client provides template and parameters previously gathered + Map params = params().add("param","query").build(); + Query q2 = new QueryTemplate("my ",params); + System.out.println(q2); + + //another custom template query: client provides template and parameters in a phased manner + QueryTemplate q2bis = new QueryTemplate("my ",params); + q2bis.addParameter("param","newquery"); + System.out.println(q2bis); + + //a predefined simple query: client adds conditions + XQuery q3 = queryFor(GenericResource.class); + System.out.println(q3); + q3.addCondition("$resource/a/simple/condition/string() eq value"); + System.out.println(q3); + + //a predefined simple query: clients customises results + XQuery q4 = queryFor(GenericResource.class); + q4.setResult("$resource/ID"); + q4.setResult("$resource/ID"); + System.out.println(q4); + + XQuery q5 = queryFor(ServiceInstance.class); + q5.addVariable("$other", "$resource/some/prop"); + System.out.println(q5); + } +} diff --git a/src/test/java/org/acme/StubClient.java b/src/test/java/org/acme/StubClient.java new file mode 100644 index 0000000..1ddc07b --- /dev/null +++ b/src/test/java/org/acme/StubClient.java @@ -0,0 +1,45 @@ +package org.acme; + +import static org.gcube.common.clients.stubs.jaxws.StubFactory.*; +import static org.gcube.resources.discovery.icclient.ICFactory.*; +import static org.gcube.resources.discovery.icclient.stubs.CollectorConstants.*; + +import java.net.URI; + +import javax.xml.ws.soap.SOAPFaultException; + +import org.gcube.common.clients.stubs.jaxws.JAXWSUtils; +import org.gcube.common.resources.gcore.ServiceEndpoint; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.common.scope.api.ServiceMap; +import org.gcube.resources.discovery.client.queries.api.SimpleQuery; +import org.gcube.resources.discovery.icclient.stubs.CollectorStub; + +public class StubClient { + + + public static void main(String[] args) throws Exception { + + //when needed, setup tcpmon and decomment to see messages on the wire for debugging purposes + //StubFactory.setProxy("localhost", 8081); + + ScopeProvider.instance.set("/gcube/devsec"); + + String address = ServiceMap.instance.endpoint(localname); + + CollectorStub proxy = stubFor(collector).at(URI.create(address)); + + SimpleQuery query = queryFor(ServiceEndpoint.class); + + try { + + System.out.println(query.expression()); + String response = proxy.execute(query.expression()); + System.err.println(response); + } + catch(SOAPFaultException e) { + throw new RuntimeException(JAXWSUtils.remoteCause(e)); + } + } + +}