commit 0192d2be464ca892549773276810ce89d1dbe447 Author: Lucio Lelii Date: Wed Mar 16 11:24:25 2016 +0000 branch for release 3.10.1 git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/branches/data-analysis/RConnectorClient/2.0@125433 82a268e6-3cf1-43bd-a215-b396298e98cf diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..e43402f --- /dev/null +++ b/.classpath @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..54d6e35 --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + r-connector-client + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..29abf99 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,6 @@ +eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 +encoding//src/main/resources=UTF-8 +encoding//src/test/java=UTF-8 +encoding//src/test/resources=UTF-8 +encoding/=UTF-8 diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..ec4300d --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,5 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/distro/LICENSE b/distro/LICENSE new file mode 100644 index 0000000..cdf50bd --- /dev/null +++ b/distro/LICENSE @@ -0,0 +1,4 @@ +gCube System - License +------------------------------------------------------------ + +${gcube.license} \ No newline at end of file diff --git a/distro/README b/distro/README new file mode 100644 index 0000000..5a6ef5c --- /dev/null +++ b/distro/README @@ -0,0 +1,66 @@ +The gCube System - ${name} +-------------------------------------------------- + +${description} + + +${gcube.description} + +${gcube.funding} + + +Version +-------------------------------------------------- + +${version} (${buildDate}) + +Please see the file named "changelog.xml" in this directory for the release notes. + + +Authors +-------------------------------------------------- + +* Lucio Lelii (lucio.lelii@isti.cnr.it), CNR, Italy + + +Maintainers +----------- + +* Lucio Lelii (lucio.lelii@isti.cnr.it), CNR, Italy + + +Download information +-------------------------------------------------- + +Source code is available from SVN: + ${scm.url} + +Binaries can be downloaded from the gCube website: + ${gcube.website} + + +Installation +-------------------------------------------------- + +Installation documentation is available on-line in the gCube Wiki: + ${gcube.wikiRoot}/RConnector + + +Documentation +-------------------------------------------------- + +Documentation is available on-line in the gCube Wiki: + ${gcube.wikiRoot}/RConnector + + +Support +-------------------------------------------------- + +Bugs and support requests can be reported in the gCube issue tracking tool: + ${gcube.issueTracking} + + +Licensing +-------------------------------------------------- + +This software is licensed under the terms you may find in the file named "LICENSE" in this directory. \ No newline at end of file diff --git a/distro/changelog.xml b/distro/changelog.xml new file mode 100644 index 0000000..010014f --- /dev/null +++ b/distro/changelog.xml @@ -0,0 +1,9 @@ + + + First Release + + + Added the method connect with empty arguments + Readme file changed + + \ No newline at end of file diff --git a/distro/descriptor.xml b/distro/descriptor.xml new file mode 100644 index 0000000..fe8e699 --- /dev/null +++ b/distro/descriptor.xml @@ -0,0 +1,32 @@ + + servicearchive + + tar.gz + + / + + + ${distroDirectory} + / + true + + README + LICENSE + changelog.xml + profile.xml + + 755 + true + + + + + target/${build.finalName}.${project.packaging} + /${artifactId} + + + + diff --git a/distro/profile.xml b/distro/profile.xml new file mode 100644 index 0000000..f387be9 --- /dev/null +++ b/distro/profile.xml @@ -0,0 +1,26 @@ + + + + Service + + ${description} + DataAnalysis + ${artifactId} + 1.0.0 + + + ${artifactId} + ${version} + + ${groupId} + ${artifactId} + ${version} + + + ${build.finalName}.jar + + + + + + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..519b8d7 --- /dev/null +++ b/pom.xml @@ -0,0 +1,114 @@ + + 4.0.0 + + + maven-parent + org.gcube.tools + 1.0.0 + + + org.gcube.data.analysis + r-connector-client + 2.0.0-SNAPSHOT + r-connector-client + r connector client + + + distro + + + + + org.gcube.core + common-generic-clients + [1.0.0-SNAPSHOT,2.0.0-SNAPSHOT) + + + org.gcube.core + common-gcube-calls + [1.0.0-SNAPSHOT,2.0.0-SNAPSHOT) + + + javax.ws.rs + javax.ws.rs-api + 2.0 + + + org.slf4j + slf4j-api + 1.7.5 + + + junit + junit + 4.11 + test + + + ch.qos.logback + logback-classic + 1.0.13 + test + + + org.glassfish.jersey.core + jersey-client + 2.13 + + + + + + + + + + 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 + 2.2 + + + ${distroDirectory}/descriptor.xml + + + + + servicearchive + install + + single + + + + + + + \ No newline at end of file diff --git a/src/main/java/org/gcube/data/analysis/rconnector/client/Constants.java b/src/main/java/org/gcube/data/analysis/rconnector/client/Constants.java new file mode 100644 index 0000000..6148d6f --- /dev/null +++ b/src/main/java/org/gcube/data/analysis/rconnector/client/Constants.java @@ -0,0 +1,33 @@ +package org.gcube.data.analysis.rconnector.client; + +import java.util.concurrent.TimeUnit; + +import javax.ws.rs.client.WebTarget; +import javax.xml.namespace.QName; + +import org.gcube.common.clients.ProxyBuilder; +import org.gcube.common.clients.ProxyBuilderImpl; +import org.gcube.data.analysis.rconnector.client.plugin.ConnectorPlugin; +import org.gcube.data.analysis.rconnector.client.proxy.ConnectorProxy; + +public class Constants { + + /** Service name. */ + public static final String SERVICE_NAME = "RConnector"; + + /** Service class. */ + public static final String SERVICE_CLASS = "DataAnalysis"; + + public static final String CONTEXT_SERVICE_NAME="r-connector"; + + public static final int DEFAULT_TIMEOUT= (int) TimeUnit.SECONDS.toMillis(10); + + private static final String TNS = "http://gcube-system.org/"; + + public static final QName CONNECTOR_QNAME = new QName(TNS, "connector"); + + public static ProxyBuilder rConnector() { + return new ProxyBuilderImpl(new ConnectorPlugin()); + } + +} diff --git a/src/main/java/org/gcube/data/analysis/rconnector/client/GcubeService.java b/src/main/java/org/gcube/data/analysis/rconnector/client/GcubeService.java new file mode 100644 index 0000000..894717a --- /dev/null +++ b/src/main/java/org/gcube/data/analysis/rconnector/client/GcubeService.java @@ -0,0 +1,38 @@ +package org.gcube.data.analysis.rconnector.client; + +import javax.xml.namespace.QName; + +import org.gcube.common.calls.Call; +import org.gcube.data.analysis.rconnector.client.GcubeServiceBuilderDSL.NameClause; + +public class GcubeService { + + private final QName name; + private String path; + private final Call call = new Call(); + + /** + * Starts the bulding process for a {@link GcubeService}. + * @return the service + */ + public static NameClause service() { + return new GcubeServiceBuilder(); + } + + public GcubeService(QName name, String path) { + this.name=name; + this.path = path; + } + + public String path() { + return path; + } + + public QName name() { + return name; + } + + public Call call() { + return call; + } +} diff --git a/src/main/java/org/gcube/data/analysis/rconnector/client/GcubeServiceBuilder.java b/src/main/java/org/gcube/data/analysis/rconnector/client/GcubeServiceBuilder.java new file mode 100644 index 0000000..90b7e85 --- /dev/null +++ b/src/main/java/org/gcube/data/analysis/rconnector/client/GcubeServiceBuilder.java @@ -0,0 +1,41 @@ +package org.gcube.data.analysis.rconnector.client; + +import javax.xml.namespace.QName; + +import org.gcube.data.analysis.rconnector.client.GcubeServiceBuilderDSL.NameClause; +import org.gcube.data.analysis.rconnector.client.GcubeServiceBuilderDSL.StubClause; + +import static org.gcube.data.analysis.rconnector.client.Utils.*; + + +/** + * Builds {@link GCoreService} instances. + * + * @author Fabio Simeoni + * + */ +public class GcubeServiceBuilder implements NameClause, StubClause { + + private QName name; + + @Override + public StubClause withName(QName name) { + + notNull("service name", name); + + this.name=name; + + return this; + } + + @Override + public GcubeService andPath(String path) { + return new GcubeService(name, path); + } + + @Override + public GcubeService useRootPath() { + return new GcubeService(name, "/"); + } + +} diff --git a/src/main/java/org/gcube/data/analysis/rconnector/client/GcubeServiceBuilderDSL.java b/src/main/java/org/gcube/data/analysis/rconnector/client/GcubeServiceBuilderDSL.java new file mode 100644 index 0000000..506c77c --- /dev/null +++ b/src/main/java/org/gcube/data/analysis/rconnector/client/GcubeServiceBuilderDSL.java @@ -0,0 +1,48 @@ +package org.gcube.data.analysis.rconnector.client; + +import javax.xml.namespace.QName; + +/** + * The clauses of a simple DSL to build {@link GCoreService}. + * + * @author Fabio Simeoni + * + */ +public interface GcubeServiceBuilderDSL { + + /** + * The clause that sets the name of the target service. + * + * @author Fabio Simeoni + * + */ + static interface NameClause { + + /** + * Sets the qualified name of the target service. + * + * @param name the qualified name of the target service + * @return the next clause + */ + StubClause withName(QName name); + + } + + /** + * The clause that sets the stub interface of the target service. + * + * @author Fabio Simeoni + * + */ + static interface StubClause { + + /** + * Sets the stub interface of the target service. + * @param type the interface + * @return the {@link GCoreService} that described the target service. + */ + GcubeService useRootPath(); + + GcubeService andPath(String path); + } +} diff --git a/src/main/java/org/gcube/data/analysis/rconnector/client/JaxRSEndpointReference.java b/src/main/java/org/gcube/data/analysis/rconnector/client/JaxRSEndpointReference.java new file mode 100644 index 0000000..1fd55c1 --- /dev/null +++ b/src/main/java/org/gcube/data/analysis/rconnector/client/JaxRSEndpointReference.java @@ -0,0 +1,60 @@ +package org.gcube.data.analysis.rconnector.client; + +import java.io.StringReader; +import java.io.StringWriter; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.stream.StreamResult; +import javax.xml.ws.EndpointReference; +import org.w3c.dom.Document; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; + +public class JaxRSEndpointReference { + + private static final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + + private static final String addressLocalName = "Address"; + //private static final String keyLocalName = "ResourceKey"; + + String address; + //Element key; + + static { + factory.setNamespaceAware(true); + } + + JaxRSEndpointReference(EndpointReference reference) { + this(serialise(reference)); + } + + JaxRSEndpointReference(String reference) { + + try { + + Document document = factory.newDocumentBuilder().parse(new InputSource(new StringReader(reference))); + + NodeList addresses = document.getElementsByTagNameNS("*", addressLocalName); + + if (addresses.getLength() == 0) + throw new RuntimeException("reference does not contain an address"); + + address = addresses.item(0).getTextContent(); + + } catch (Exception e) { + throw new IllegalArgumentException("reference is not a gCore reference", e); + } + + } + + @Override + public String toString() { + return address; + } + + // helper + private static String serialise(EndpointReference reference) { + StringWriter writer = new StringWriter(); + reference.writeTo(new StreamResult(writer)); + return writer.toString(); + } +} diff --git a/src/main/java/org/gcube/data/analysis/rconnector/client/JaxRSRequestFilter.java b/src/main/java/org/gcube/data/analysis/rconnector/client/JaxRSRequestFilter.java new file mode 100644 index 0000000..12db089 --- /dev/null +++ b/src/main/java/org/gcube/data/analysis/rconnector/client/JaxRSRequestFilter.java @@ -0,0 +1,35 @@ +package org.gcube.data.analysis.rconnector.client; + +import java.io.IOException; +import java.util.Collections; +import java.util.Map.Entry; + +import javax.ws.rs.client.ClientRequestContext; +import javax.ws.rs.client.ClientRequestFilter; + +import org.gcube.common.calls.Interceptors; +import org.gcube.common.calls.Request; +import org.gcube.common.scope.api.ScopeProvider; + +public class JaxRSRequestFilter implements ClientRequestFilter { + + private GcubeService service; + + public JaxRSRequestFilter(GcubeService service) { + super(); + this.service = service; + } + + @Override + public void filter(final ClientRequestContext rc) throws IOException { + System.out.println("request intercepted"); + if (ScopeProvider.instance.get()!=null){ + Request requestContext = Interceptors.executeRequestChain(service.call()); + + for (Entry entry: requestContext.getHeaders()){ + System.out.println("setting "+entry.getKey()+" "+entry.getValue()); + rc.getHeaders().put(entry.getKey(), Collections.singletonList((Object)entry.getValue())); + } + } + } +} diff --git a/src/main/java/org/gcube/data/analysis/rconnector/client/TargetFactory.java b/src/main/java/org/gcube/data/analysis/rconnector/client/TargetFactory.java new file mode 100644 index 0000000..0c60d3b --- /dev/null +++ b/src/main/java/org/gcube/data/analysis/rconnector/client/TargetFactory.java @@ -0,0 +1,62 @@ +package org.gcube.data.analysis.rconnector.client; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.WebTarget; +import javax.xml.ws.EndpointReference; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class TargetFactory implements TargetFactoryDSL.AtClause{ + + private static Logger log = LoggerFactory.getLogger(TargetFactory.class); + + private GcubeService target; + + public static TargetFactory stubFor(GcubeService target){ + return new TargetFactory(target); + } + + + private TargetFactory(GcubeService target) { + this.target = target; + } + + + public WebTarget at(String address) { + + try{ + + Client client = ClientBuilder.newClient(); + + String resourceAddress = address.substring(0, address.indexOf("/service")); + + WebTarget resourcetarget = client.target(resourceAddress).path("/resource/"); + + int status = resourcetarget.request().get().getStatus(); + + if (status!=200) + throw new Exception(); + + WebTarget webTarget = client.target(address); + + webTarget.path(target.path()); + + webTarget.register(new JaxRSRequestFilter(target)); + + return webTarget; + + }catch (Exception e) { + log.error("error building service",e); + throw new RuntimeException("error building service",e); + } + + } + + + public WebTarget at(EndpointReference endpoint){ + return at(new JaxRSEndpointReference(endpoint).address); + } + + +} diff --git a/src/main/java/org/gcube/data/analysis/rconnector/client/TargetFactoryDSL.java b/src/main/java/org/gcube/data/analysis/rconnector/client/TargetFactoryDSL.java new file mode 100644 index 0000000..31052cf --- /dev/null +++ b/src/main/java/org/gcube/data/analysis/rconnector/client/TargetFactoryDSL.java @@ -0,0 +1,34 @@ +package org.gcube.data.analysis.rconnector.client; + +import javax.ws.rs.client.WebTarget; + +import org.gcube.common.clients.stubs.jaxws.StubFactory; + + +/** + * Simple DSL for the {@link StubFactory} + * + * @author Fabio Simeoni + * + */ +public interface TargetFactoryDSL { + + /** + * Selects the address of the service endpoint or service instance. + * + * @author Fabio Simeoni + * + * @param + */ + interface AtClause { + + /** + * Returns a stub for a service endpoint at a given address. + * @param address the address + * @return the stub + */ + WebTarget at(String address); + + + } +} diff --git a/src/main/java/org/gcube/data/analysis/rconnector/client/Utils.java b/src/main/java/org/gcube/data/analysis/rconnector/client/Utils.java new file mode 100644 index 0000000..d105db2 --- /dev/null +++ b/src/main/java/org/gcube/data/analysis/rconnector/client/Utils.java @@ -0,0 +1,9 @@ +package org.gcube.data.analysis.rconnector.client; + +public class Utils { + + static void notNull(String message,Object o) { + if (o==null) + throw new IllegalArgumentException(o+" cannot be null"); + } +} diff --git a/src/main/java/org/gcube/data/analysis/rconnector/client/plugin/AbstractPlugin.java b/src/main/java/org/gcube/data/analysis/rconnector/client/plugin/AbstractPlugin.java new file mode 100644 index 0000000..668e4f9 --- /dev/null +++ b/src/main/java/org/gcube/data/analysis/rconnector/client/plugin/AbstractPlugin.java @@ -0,0 +1,37 @@ +package org.gcube.data.analysis.rconnector.client.plugin; + +import org.gcube.common.clients.Plugin; +import org.gcube.data.analysis.rconnector.client.Constants; + + + +public abstract class AbstractPlugin implements Plugin { + + + public final String name; + + public AbstractPlugin(String name) { + this.name=name; + } + + @Override + public String serviceClass() { + return Constants.SERVICE_CLASS; + } + + @Override + public String serviceName() { + return Constants.SERVICE_NAME; + } + + @Override + public String name() { + return name; + } + + @Override + public String namespace() { + return ""; + } + +} \ No newline at end of file diff --git a/src/main/java/org/gcube/data/analysis/rconnector/client/plugin/ConnectorPlugin.java b/src/main/java/org/gcube/data/analysis/rconnector/client/plugin/ConnectorPlugin.java new file mode 100644 index 0000000..b16e2b4 --- /dev/null +++ b/src/main/java/org/gcube/data/analysis/rconnector/client/plugin/ConnectorPlugin.java @@ -0,0 +1,40 @@ +package org.gcube.data.analysis.rconnector.client.plugin; + +import static org.gcube.data.analysis.rconnector.client.GcubeService.service; +import static org.gcube.data.analysis.rconnector.client.TargetFactory.stubFor; + +import javax.ws.rs.client.WebTarget; +import javax.xml.ws.EndpointReference; + +import org.gcube.common.clients.config.ProxyConfig; +import org.gcube.common.clients.delegates.ProxyDelegate; +import org.gcube.data.analysis.rconnector.client.Constants; +import org.gcube.data.analysis.rconnector.client.GcubeService; +import org.gcube.data.analysis.rconnector.client.proxy.ConnectorProxy; +import org.gcube.data.analysis.rconnector.client.proxy.DefaultConnectorProxy; + +public class ConnectorPlugin extends AbstractPlugin{ + + public ConnectorPlugin() { + super("r-connector/gcube/service"); + } + + @Override + public Exception convert(Exception fault, ProxyConfig config) { + return fault; + } + + @Override + public WebTarget resolve(EndpointReference address, ProxyConfig config) + throws Exception { + GcubeService service = service().withName(Constants.CONNECTOR_QNAME).useRootPath(); + return stubFor(service).at(address); + + } + + @Override + public ConnectorProxy newProxy(ProxyDelegate delegate) { + return new DefaultConnectorProxy(delegate); + } + +} diff --git a/src/main/java/org/gcube/data/analysis/rconnector/client/proxy/ConnectorProxy.java b/src/main/java/org/gcube/data/analysis/rconnector/client/proxy/ConnectorProxy.java new file mode 100644 index 0000000..98b8954 --- /dev/null +++ b/src/main/java/org/gcube/data/analysis/rconnector/client/proxy/ConnectorProxy.java @@ -0,0 +1,11 @@ +package org.gcube.data.analysis.rconnector.client.proxy; + +import java.net.URI; + +public interface ConnectorProxy { + + URI connect(Long tabularResourceId); + + URI connect(); + +} diff --git a/src/main/java/org/gcube/data/analysis/rconnector/client/proxy/DefaultConnectorProxy.java b/src/main/java/org/gcube/data/analysis/rconnector/client/proxy/DefaultConnectorProxy.java new file mode 100644 index 0000000..a4c876a --- /dev/null +++ b/src/main/java/org/gcube/data/analysis/rconnector/client/proxy/DefaultConnectorProxy.java @@ -0,0 +1,54 @@ +package org.gcube.data.analysis.rconnector.client.proxy; + +import static org.gcube.common.clients.exceptions.FaultDSL.again; + +import java.net.URI; + +import javax.ws.rs.client.WebTarget; + +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.common.clients.Call; +import org.gcube.common.clients.delegates.ProxyDelegate; + +public class DefaultConnectorProxy implements ConnectorProxy { + + private final ProxyDelegate delegate; + + public DefaultConnectorProxy(ProxyDelegate config){ + this.delegate = config; + } + + @Override + public URI connect(final Long trId) { + Call call = new Call() { + + @Override + public URI call(WebTarget endpoint) throws Exception { + return endpoint.path("connect/").path(trId.toString()).queryParam("gcube-token", SecurityTokenProvider.instance.get()) + .getUri(); + } + }; + try { + return delegate.make(call); + } catch (Exception e) { + throw again(e).asServiceException(); + } + } + + @Override + public URI connect() { + Call call = new Call() { + + @Override + public URI call(WebTarget endpoint) throws Exception { + return endpoint.path("connect").queryParam("gcube-token", SecurityTokenProvider.instance.get()).getUri(); + } + }; + try { + return delegate.make(call); + } catch (Exception e) { + throw again(e).asServiceException(); + } + } + +} diff --git a/src/test/java/org/gcube/data/analysis/tconnector/test/TESTConnection.java b/src/test/java/org/gcube/data/analysis/tconnector/test/TESTConnection.java new file mode 100644 index 0000000..5c56f63 --- /dev/null +++ b/src/test/java/org/gcube/data/analysis/tconnector/test/TESTConnection.java @@ -0,0 +1,33 @@ +package org.gcube.data.analysis.tconnector.test; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.common.scope.api.ScopeProvider; +import org.junit.Test; + +import static org.gcube.data.analysis.rconnector.client.Constants.*; +public class TESTConnection { + + @Test + public void connect(){ + ScopeProvider.instance.set("/gcube/devsec"); + SecurityTokenProvider.instance.set("df75336d-0944-4324-b444-c711d21f705b"); + System.out.println(rConnector().build().connect()); + } + + @Test + public void testte(){ + Pattern pattern = Pattern.compile("[^/]*//([^:]*)[^/]*/(.*)"); + + Matcher m = pattern.matcher("jdbc:postgresql://node7.d.d4science.research-infrastructures.eu:5432/tabulardata"); + + m.find(); + System.out.println(m.group(1)); + System.out.println(m.group(2)); + + + } + +}