From d6ff780ccf8eba176ae181a315bde8ce166dad5b Mon Sep 17 00:00:00 2001 From: Lucio Lelii Date: Wed, 1 Feb 2017 17:25:57 +0000 Subject: [PATCH] release 4.3 git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/branches/common/common-jaxrs-client/1.0@142025 82a268e6-3cf1-43bd-a215-b396298e98cf --- .classpath | 36 +++++++++++ .project | 23 +++++++ .settings/org.eclipse.core.resources.prefs | 6 ++ .settings/org.eclipse.jdt.core.prefs | 5 ++ .settings/org.eclipse.m2e.core.prefs | 4 ++ distro/LICENSE | 1 + distro/README | 61 +++++++++++++++++++ distro/changelog.xml | 6 ++ distro/descriptor.xml | 31 ++++++++++ distro/profile.xml | 28 +++++++++ pom.xml | 40 ++++++++++++ .../common/calls/jaxrs/GcubeService.java | 38 ++++++++++++ .../calls/jaxrs/GcubeServiceBuilder.java | 38 ++++++++++++ .../calls/jaxrs/GcubeServiceBuilderDSL.java | 48 +++++++++++++++ .../calls/jaxrs/JaxRSEndpointReference.java | 60 ++++++++++++++++++ .../calls/jaxrs/JaxRSRequestFilter.java | 35 +++++++++++ .../org/gcube/common/calls/jaxrs/Utils.java | 9 +++ 17 files changed, 469 insertions(+) create mode 100644 .classpath create mode 100644 .project create mode 100644 .settings/org.eclipse.core.resources.prefs create mode 100644 .settings/org.eclipse.jdt.core.prefs create mode 100644 .settings/org.eclipse.m2e.core.prefs create mode 100644 distro/LICENSE create mode 100644 distro/README create mode 100644 distro/changelog.xml create mode 100644 distro/descriptor.xml create mode 100644 distro/profile.xml create mode 100644 pom.xml create mode 100644 src/main/java/org/gcube/common/calls/jaxrs/GcubeService.java create mode 100644 src/main/java/org/gcube/common/calls/jaxrs/GcubeServiceBuilder.java create mode 100644 src/main/java/org/gcube/common/calls/jaxrs/GcubeServiceBuilderDSL.java create mode 100644 src/main/java/org/gcube/common/calls/jaxrs/JaxRSEndpointReference.java create mode 100644 src/main/java/org/gcube/common/calls/jaxrs/JaxRSRequestFilter.java create mode 100644 src/main/java/org/gcube/common/calls/jaxrs/Utils.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..953de0b --- /dev/null +++ b/.classpath @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..3be7845 --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + common-jaxrs-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..3695e26 --- /dev/null +++ b/distro/LICENSE @@ -0,0 +1 @@ +${gcube.license} diff --git a/distro/README b/distro/README new file mode 100644 index 0000000..edf22f4 --- /dev/null +++ b/distro/README @@ -0,0 +1,61 @@ +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-AT-isti.cnr.it), CNR, Italy + +MAINTAINERS +-------------------------------------------------- + +* Lucio Lelii (lucio.lelii-AT-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} + +Documentation +-------------------------------------------------- + +Documentation is available on-line in the gCube Wiki: + ${gcube.wikiRoot} +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. diff --git a/distro/changelog.xml b/distro/changelog.xml new file mode 100644 index 0000000..6747294 --- /dev/null +++ b/distro/changelog.xml @@ -0,0 +1,6 @@ + + + first release + + + \ No newline at end of file diff --git a/distro/descriptor.xml b/distro/descriptor.xml new file mode 100644 index 0000000..67b0c2c --- /dev/null +++ b/distro/descriptor.xml @@ -0,0 +1,31 @@ + + servicearchive + + tar.gz + + / + + + ${distroDirectory} + / + true + + README + LICENSE + profile.xml + changelog.xml + + 755 + true + + + + + target/${build.finalName}.jar + /${artifactId} + + + \ No newline at end of file diff --git a/distro/profile.xml b/distro/profile.xml new file mode 100644 index 0000000..19b52ef --- /dev/null +++ b/distro/profile.xml @@ -0,0 +1,28 @@ + + + + Service + + ${description} + Common + ${artifactId} + 1.0.0 + + + ${description} + ${artifactId} + ${version} + + ${groupId} + ${artifactId} + ${version} + + library + + ${build.finalName}.jar + + + + + + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..07d2e99 --- /dev/null +++ b/pom.xml @@ -0,0 +1,40 @@ + + 4.0.0 + org.gcube.common + common-jaxrs-client + 1.0.0-SNAPSHOT + gcube-jaxrs-client + + + org.gcube.tools + maven-parent + 1.0.0 + + + + + + org.gcube.core + common-scope + [1.1.0-SNAPSHOT,2.0.0-SNAPSHOT) + + + org.gcube.core + common-gcube-calls + [1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT) + + + org.glassfish.jersey.core + jersey-client + 2.24.1 + + + javax.ws.rs + javax.ws.rs-api + 2.0 + + + + + \ No newline at end of file diff --git a/src/main/java/org/gcube/common/calls/jaxrs/GcubeService.java b/src/main/java/org/gcube/common/calls/jaxrs/GcubeService.java new file mode 100644 index 0000000..4445238 --- /dev/null +++ b/src/main/java/org/gcube/common/calls/jaxrs/GcubeService.java @@ -0,0 +1,38 @@ +package org.gcube.common.calls.jaxrs; + +import javax.xml.namespace.QName; + +import org.gcube.common.calls.Call; +import org.gcube.common.calls.jaxrs.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/common/calls/jaxrs/GcubeServiceBuilder.java b/src/main/java/org/gcube/common/calls/jaxrs/GcubeServiceBuilder.java new file mode 100644 index 0000000..7ac2d48 --- /dev/null +++ b/src/main/java/org/gcube/common/calls/jaxrs/GcubeServiceBuilder.java @@ -0,0 +1,38 @@ +package org.gcube.common.calls.jaxrs; + +import static org.gcube.common.calls.jaxrs.Utils.notNull; + +import javax.xml.namespace.QName; + +import org.gcube.common.calls.jaxrs.GcubeServiceBuilderDSL.NameClause; +import org.gcube.common.calls.jaxrs.GcubeServiceBuilderDSL.StubClause; + + +/** + * Builds {@link GCoreService} instances. + * + * @author Fabio Simeoni + * + */ +public class GcubeServiceBuilder implements NameClause, StubClause { + + private QName name; + + public StubClause withName(QName name) { + + notNull("service name", name); + + this.name=name; + + return this; + } + + public GcubeService andPath(String path) { + return new GcubeService(name, path); + } + + public GcubeService useRootPath() { + return new GcubeService(name, "/"); + } + +} diff --git a/src/main/java/org/gcube/common/calls/jaxrs/GcubeServiceBuilderDSL.java b/src/main/java/org/gcube/common/calls/jaxrs/GcubeServiceBuilderDSL.java new file mode 100644 index 0000000..eb2be80 --- /dev/null +++ b/src/main/java/org/gcube/common/calls/jaxrs/GcubeServiceBuilderDSL.java @@ -0,0 +1,48 @@ +package org.gcube.common.calls.jaxrs; + +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/common/calls/jaxrs/JaxRSEndpointReference.java b/src/main/java/org/gcube/common/calls/jaxrs/JaxRSEndpointReference.java new file mode 100644 index 0000000..95864ea --- /dev/null +++ b/src/main/java/org/gcube/common/calls/jaxrs/JaxRSEndpointReference.java @@ -0,0 +1,60 @@ +package org.gcube.common.calls.jaxrs; + +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/common/calls/jaxrs/JaxRSRequestFilter.java b/src/main/java/org/gcube/common/calls/jaxrs/JaxRSRequestFilter.java new file mode 100644 index 0000000..4637615 --- /dev/null +++ b/src/main/java/org/gcube/common/calls/jaxrs/JaxRSRequestFilter.java @@ -0,0 +1,35 @@ +package org.gcube.common.calls.jaxrs; + +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/common/calls/jaxrs/Utils.java b/src/main/java/org/gcube/common/calls/jaxrs/Utils.java new file mode 100644 index 0000000..9cc3551 --- /dev/null +++ b/src/main/java/org/gcube/common/calls/jaxrs/Utils.java @@ -0,0 +1,9 @@ +package org.gcube.common.calls.jaxrs; + +public class Utils { + + static void notNull(String message,Object o) { + if (o==null) + throw new IllegalArgumentException(o+" cannot be null"); + } +}