From 889b5e06abef51f31b37e080477e2d20f78a04d2 Mon Sep 17 00:00:00 2001 From: "fabio.simeoni" Date: Thu, 24 Oct 2013 11:37:11 +0000 Subject: [PATCH] branched for 1.x releases git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/branches/common/common-jaxws-calls/1.0@83991 82a268e6-3cf1-43bd-a215-b396298e98cf --- .classpath | 36 +++++++ .project | 23 +++++ .settings/org.eclipse.core.resources.prefs | 7 ++ .settings/org.eclipse.jdt.core.prefs | 6 ++ .settings/org.eclipse.m2e.core.prefs | 5 + distro/INSTALL | 1 + distro/LICENSE | 6 ++ distro/MAINTAINERS | 2 + distro/README | 39 ++++++++ distro/changelog.xml | 5 + distro/descriptor.xml | 42 ++++++++ distro/profile.xml | 26 +++++ distro/svnpath.txt | 1 + pom.xml | 98 +++++++++++++++++++ .../gcube/common/calls/jaxws/Constants.java | 9 ++ .../common/calls/jaxws/GcubeService.java | 38 +++++++ .../calls/jaxws/GcubeServiceBuilder.java | 39 ++++++++ .../calls/jaxws/GcubeServiceBuilderDSL.java | 46 +++++++++ .../gcube/common/calls/jaxws/JAXWSUtils.java | 23 +++++ .../calls/jaxws/JaxWSEndpointReference.java | 68 +++++++++++++ .../gcube/common/calls/jaxws/StubFactory.java | 81 +++++++++++++++ .../common/calls/jaxws/StubFactoryDSL.java | 30 ++++++ .../calls/jaxws/handlers/JaxWSHandler.java | 70 +++++++++++++ 23 files changed, 701 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/INSTALL create mode 100644 distro/LICENSE create mode 100644 distro/MAINTAINERS 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 distro/svnpath.txt create mode 100644 pom.xml create mode 100644 src/main/java/org/gcube/common/calls/jaxws/Constants.java create mode 100644 src/main/java/org/gcube/common/calls/jaxws/GcubeService.java create mode 100644 src/main/java/org/gcube/common/calls/jaxws/GcubeServiceBuilder.java create mode 100644 src/main/java/org/gcube/common/calls/jaxws/GcubeServiceBuilderDSL.java create mode 100644 src/main/java/org/gcube/common/calls/jaxws/JAXWSUtils.java create mode 100644 src/main/java/org/gcube/common/calls/jaxws/JaxWSEndpointReference.java create mode 100644 src/main/java/org/gcube/common/calls/jaxws/StubFactory.java create mode 100644 src/main/java/org/gcube/common/calls/jaxws/StubFactoryDSL.java create mode 100644 src/main/java/org/gcube/common/calls/jaxws/handlers/JaxWSHandler.java 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..4f9b2fd --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + common-jaxws-calls + + + + + + 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..51884a7 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,7 @@ +#Wed Oct 09 11:35:27 CEST 2013 +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..daa8f83 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,6 @@ +#Tue Sep 17 11:10:21 CEST 2013 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..b0ae275 --- /dev/null +++ b/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,5 @@ +#Mon Sep 16 15:19:47 CEST 2013 +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 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..02d640e --- /dev/null +++ b/distro/MAINTAINERS @@ -0,0 +1,2 @@ +* Lucio Lelii (lucio.lelii@isti.cnr.it), CNR, Italy +* Fabio Simeoni (fabio.simeoni@fao.org), FAO of the UN, Italy \ No newline at end of file diff --git a/distro/README b/distro/README new file mode 100644 index 0000000..8ba3d75 --- /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 +------- + +* Lucio Lelii (lucio.lelii@isti.cnr.it), CNR, Italy +* Fabio Simeoni (fabio.simeoni@fao.org), FAO of the UN, 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..a4d71f9 --- /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..52cb8a5 --- /dev/null +++ b/distro/descriptor.xml @@ -0,0 +1,42 @@ + + servicearchive + + tar.gz + + / + + + ${distroDirectory} + / + true + + README + LICENSE + INSTALL + MAINTAINERS + changelog.xml + + 755 + true + + + + + ${distroDirectory}/profile.xml + /etc + 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..91c49e4 --- /dev/null +++ b/distro/profile.xml @@ -0,0 +1,26 @@ + + + + Service + + ${description} + Common + ${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..51b35be --- /dev/null +++ b/pom.xml @@ -0,0 +1,98 @@ + + 4.0.0 + + + org.gcube.tools + maven-parent + 1.0.0 + + + + org.gcube.core + common-jaxws-calls + 1.0.0-SNAPSHOT + + + distro + + + + scm:svn:http://svn.d4science.research-infrastructures.eu/gcube/trunk/Common/${project.artifactId} + scm:svn:https://svn.d4science.research-infrastructures.eu/gcube/trunk/Common/${project.artifactId} + http://svn.d4science.research-infrastructures.eu/gcube/trunk/Common/${project.artifactId} + + + + + + org.gcube.core + common-scope + [1.1.0-SNAPSHOT,2.0.0-SNAPSHOT) + + + org.gcube.core + common-gcube-calls + 1.0.0-SNAPSHOT + + + org.slf4j + slf4j-api + 1.7.5 + + + + + + + + + + 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/common/calls/jaxws/Constants.java b/src/main/java/org/gcube/common/calls/jaxws/Constants.java new file mode 100644 index 0000000..6d44729 --- /dev/null +++ b/src/main/java/org/gcube/common/calls/jaxws/Constants.java @@ -0,0 +1,9 @@ +package org.gcube.common.calls.jaxws; + +public class Constants { + + public static final String QNAME_KEY="qname"; + + public static final String TYPE_KEY="type"; + +} diff --git a/src/main/java/org/gcube/common/calls/jaxws/GcubeService.java b/src/main/java/org/gcube/common/calls/jaxws/GcubeService.java new file mode 100644 index 0000000..b14331d --- /dev/null +++ b/src/main/java/org/gcube/common/calls/jaxws/GcubeService.java @@ -0,0 +1,38 @@ +package org.gcube.common.calls.jaxws; + +import javax.xml.namespace.QName; + +import org.gcube.common.calls.Call; +import org.gcube.common.calls.jaxws.GcubeServiceBuilderDSL.NameClause; + +public class GcubeService { + + private final QName name; + private final Class type; + 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, Class type) { + this.name=name; + this.type=type; + } + + public QName name() { + return name; + } + + public Class type() { + return type; + } + + public Call call() { + return call; + } +} diff --git a/src/main/java/org/gcube/common/calls/jaxws/GcubeServiceBuilder.java b/src/main/java/org/gcube/common/calls/jaxws/GcubeServiceBuilder.java new file mode 100644 index 0000000..5ad7c0e --- /dev/null +++ b/src/main/java/org/gcube/common/calls/jaxws/GcubeServiceBuilder.java @@ -0,0 +1,39 @@ +package org.gcube.common.calls.jaxws; + +import static org.gcube.common.calls.jaxws.JAXWSUtils.*; + +import javax.xml.namespace.QName; + +import org.gcube.common.calls.jaxws.GcubeServiceBuilderDSL.NameClause; +import org.gcube.common.calls.jaxws.GcubeServiceBuilderDSL.StubClause; + +/** + * 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 andInterface(Class type) { + + notNull("service interface", type); + + return new GcubeService(name, type); + } + +} diff --git a/src/main/java/org/gcube/common/calls/jaxws/GcubeServiceBuilderDSL.java b/src/main/java/org/gcube/common/calls/jaxws/GcubeServiceBuilderDSL.java new file mode 100644 index 0000000..0b3935b --- /dev/null +++ b/src/main/java/org/gcube/common/calls/jaxws/GcubeServiceBuilderDSL.java @@ -0,0 +1,46 @@ +package org.gcube.common.calls.jaxws; + +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 andInterface(Class type); + } +} diff --git a/src/main/java/org/gcube/common/calls/jaxws/JAXWSUtils.java b/src/main/java/org/gcube/common/calls/jaxws/JAXWSUtils.java new file mode 100644 index 0000000..7b760ed --- /dev/null +++ b/src/main/java/org/gcube/common/calls/jaxws/JAXWSUtils.java @@ -0,0 +1,23 @@ +package org.gcube.common.calls.jaxws; + +import javax.xml.bind.annotation.XmlRootElement; + +/** + * Library-wide utilities. + * + * @author Fabio Simeoni + * + */ +public class JAXWSUtils { + + @XmlRootElement + public static class Empty{} + + public static final Empty empty = new Empty(); + + 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/common/calls/jaxws/JaxWSEndpointReference.java b/src/main/java/org/gcube/common/calls/jaxws/JaxWSEndpointReference.java new file mode 100644 index 0000000..a7f36ff --- /dev/null +++ b/src/main/java/org/gcube/common/calls/jaxws/JaxWSEndpointReference.java @@ -0,0 +1,68 @@ +package org.gcube.common.calls.jaxws; + +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 JaxWSEndpointReference { + + 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); + } + + JaxWSEndpointReference(EndpointReference reference) { + this(serialise(reference)); + } + + JaxWSEndpointReference(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(); + + /*NodeList keys = document.getElementsByTagNameNS("*", keyLocalName); + + if (keys.getLength() >1) + throw new RuntimeException("reference contains " + keys.getLength() + " resource key(s)"); + + if (keys.getLength()==1) + key = (Element) keys.item(0); + */ + } 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/jaxws/StubFactory.java b/src/main/java/org/gcube/common/calls/jaxws/StubFactory.java new file mode 100644 index 0000000..ec986ae --- /dev/null +++ b/src/main/java/org/gcube/common/calls/jaxws/StubFactory.java @@ -0,0 +1,81 @@ +package org.gcube.common.calls.jaxws; + +import java.net.URL; +import java.util.List; +import javax.xml.ws.Binding; +import javax.xml.ws.BindingProvider; +import javax.xml.ws.EndpointReference; +import javax.xml.ws.Service; +import javax.xml.ws.handler.Handler; + +import org.gcube.common.calls.jaxws.handlers.JaxWSHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class StubFactory implements StubFactoryDSL.AtClause{ + + private static Logger log = LoggerFactory.getLogger(StubFactory.class); + + private GcubeService target; + + public static StubFactory stubFor(GcubeService target){ + return new StubFactory(target); + } + + + private StubFactory(GcubeService target) { + this.target = target; + } + + + public T at(String address) { + + try{ + + String endpointAddress = address+"?wsdl"; + + log.debug("contcting endpoint "+endpointAddress); + + // get JAXWS service from endpoint address + Service service = Service.create(new URL(endpointAddress), target.name()); + + // get JAXWS stub + T stub = service.getPort(target.type()); + + BindingProvider provider = (BindingProvider) stub; + + // configure stub for gCube calls + registerHandler(provider, target); + + return stub; + + }catch (Exception e) { + log.error("error building service",e); + throw new RuntimeException("error building service",e); + } + + } + + + public T at(EndpointReference endpoint){ + return at(new JaxWSEndpointReference(endpoint).address); + } + + + // helper + private void registerHandler(BindingProvider provider, GcubeService context) { + + Binding binding = provider.getBinding(); + + @SuppressWarnings("rawtypes") + List currentChain = binding.getHandlerChain(); + + JaxWSHandler handler = new JaxWSHandler(context); + + currentChain.add(handler); + + binding.setHandlerChain(currentChain); + + } + +} diff --git a/src/main/java/org/gcube/common/calls/jaxws/StubFactoryDSL.java b/src/main/java/org/gcube/common/calls/jaxws/StubFactoryDSL.java new file mode 100644 index 0000000..980e757 --- /dev/null +++ b/src/main/java/org/gcube/common/calls/jaxws/StubFactoryDSL.java @@ -0,0 +1,30 @@ +package org.gcube.common.calls.jaxws; + + +/** + * Simple DSL for the {@link StubFactory} + * + * @author Fabio Simeoni + * + */ +public interface StubFactoryDSL { + + /** + * 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 + */ + T at(String address); + + + } +} diff --git a/src/main/java/org/gcube/common/calls/jaxws/handlers/JaxWSHandler.java b/src/main/java/org/gcube/common/calls/jaxws/handlers/JaxWSHandler.java new file mode 100644 index 0000000..0bc1d50 --- /dev/null +++ b/src/main/java/org/gcube/common/calls/jaxws/handlers/JaxWSHandler.java @@ -0,0 +1,70 @@ +package org.gcube.common.calls.jaxws.handlers; + +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import javax.xml.namespace.QName; +import javax.xml.ws.handler.MessageContext; +import javax.xml.ws.handler.soap.SOAPHandler; +import javax.xml.ws.handler.soap.SOAPMessageContext; + +import org.gcube.common.calls.Interceptors; +import org.gcube.common.calls.Request; +import org.gcube.common.calls.jaxws.GcubeService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class JaxWSHandler implements SOAPHandler{ + + private Logger logger = LoggerFactory.getLogger(JaxWSHandler.class); + + protected GcubeService context; + + public JaxWSHandler(GcubeService context) { + super(); + this.context = context; + } + + @Override + public void close(MessageContext arg0) {} + + @Override + public boolean handleFault(SOAPMessageContext arg0) { + return true; + } + + @Override + public boolean handleMessage(SOAPMessageContext messageContext) { + Boolean outbound = (Boolean) messageContext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); + + logger.trace("handling message"); + + if (outbound){ + Request requestContext = Interceptors.executeRequestChain(context.call()); + + @SuppressWarnings("unchecked") + Map> headers = (Map>) messageContext.get(MessageContext.HTTP_REQUEST_HEADERS); + + if (headers==null) + headers = new HashMap>(); + + for (Entry entry: requestContext.getHeaders()) + headers.put(entry.getKey(), Collections.singletonList(entry.getValue())); + + messageContext.put(MessageContext.HTTP_REQUEST_HEADERS, headers); + } + /*else{ + Response context = Interceptors.executeResponseChain(); + }*/ + return true; + }; + + @Override + public Set getHeaders() { + return null; + } + +}