diff --git a/.classpath b/.classpath index 0f53f3e..534b5e5 100644 --- a/.classpath +++ b/.classpath @@ -1,10 +1,36 @@ - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/distro/MAINTAINERS b/distro/MAINTAINERS index 3d20d89..5e8aa8f 100644 --- a/distro/MAINTAINERS +++ b/distro/MAINTAINERS @@ -1,2 +1 @@ -* Fabio Simeoni (fabio.simeoni@fao.org), FAO of the UN, Italy -* Rena Tsantouli (e.tsantoylh@di.uoa.gr), University of Athens, Greece \ No newline at end of file +* Fabio Simeoni (fabio.simeoni@fao.org), FAO of the UN, Italy \ No newline at end of file diff --git a/distro/changelog.xml b/distro/changelog.xml index 5e477d8..2d8259f 100644 --- a/distro/changelog.xml +++ b/distro/changelog.xml @@ -5,7 +5,10 @@ Rewritten as a framework for the implementation of client libraries that comply with the CL Design Model - + Improved error handling in Plugin#convert() + + AddressUtils preserve protocol from URIs/URLs + \ No newline at end of file diff --git a/pom.xml b/pom.xml index a68f6c9..44712e8 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ org.gcube.core common-clients - 2.0.1-SNAPSHOT + 2.0.2-SNAPSHOT Common Clients A framework for client APIs diff --git a/src/main/java/org/gcube/common/clients/builders/AddressingUtils.java b/src/main/java/org/gcube/common/clients/builders/AddressingUtils.java index c0421ff..d76cc04 100644 --- a/src/main/java/org/gcube/common/clients/builders/AddressingUtils.java +++ b/src/main/java/org/gcube/common/clients/builders/AddressingUtils.java @@ -22,7 +22,7 @@ public class AddressingUtils { private static final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - private static final String scheme_prefix = "http://"; + private static final String scheme_prefix = "http"; private static final String keyElementPrefix = "key"; static { @@ -30,7 +30,7 @@ public class AddressingUtils { } /** - * Return the address of a service endpoint. + * Return the HTTP address of a service endpoint. * @param contextPath the context path of the service * @param service the name of the service * @param host the host of the endpoint @@ -40,8 +40,23 @@ public class AddressingUtils { */ public static W3CEndpointReference address(String contextPath,String service,String host, int port) throws IllegalArgumentException { + return address(scheme_prefix,contextPath,service,host,port); + } + + /** + * Return the address of a service endpoint. + * @param protocol the protocol required to contact endpoint (e.g. HTTPS) + * @param contextPath the context path of the service + * @param service the name of the service + * @param host the host of the endpoint + * @param port the port of the endpoint + * @return the address + * @throws IllegalArgumentException if an address cannot be derived from the inputs + */ + public static W3CEndpointReference address(String protocol,String contextPath,String service,String host, int port) throws IllegalArgumentException { + W3CEndpointReferenceBuilder builder = new W3CEndpointReferenceBuilder(); - builder.address(join(contextPath,service,host,port)); + builder.address(join(protocol,contextPath,service,host,port)); return builder.build(); } @@ -54,7 +69,19 @@ public class AddressingUtils { * @throws IllegalArgumentException if an address cannot be derived from the inputs */ public static W3CEndpointReference address(String contextPath,String service,URL address) throws IllegalArgumentException { - return address(contextPath,service, address.getHost(),portFrom(address)); + return address(address.getProtocol(),contextPath,service, address.getHost(),portFrom(address)); + } + + /** + * Return the address of a service endpoint. + * @param contextPath the context path of the service + * @param service the name of the service + * @param address the address of the endpoint as a {@link URL} + * @return the address + * @throws IllegalArgumentException if an address cannot be derived from the inputs + */ + public static W3CEndpointReference address(String protocol,String contextPath,String service,URL address) throws IllegalArgumentException { + return address(address.getProtocol(),contextPath,service, address.getHost(),portFrom(address)); } /** @@ -66,7 +93,7 @@ public class AddressingUtils { * @throws IllegalArgumentException if an address cannot be derived from the inputs */ public static W3CEndpointReference address(String contextPath,String service,URI address) throws IllegalArgumentException { - return address(contextPath,service, address.getHost(),portFrom(address)); + return address(address.getScheme(),contextPath,service, address.getHost(),portFrom(address)); } /** @@ -82,8 +109,26 @@ public class AddressingUtils { */ public static W3CEndpointReference address(String contextPath,String service, String namespace, String key, String host, int port) throws IllegalArgumentException { + return address(scheme_prefix,contextPath,service,namespace,key, host,port); + + } + + /** + * Returns the address of a service instance. + * @param protocol the protocol required to contact endpoint (e.g. HTTPS) + * @param contextPath the context path of the service + * @param service the name of the service + * @param namespace the namespace of the service + * @param key the key of the instance + * @param host the host of the instance + * @param port the port of the instance + * @return the address + * @throws IllegalArgumentException if an address cannot be derived from the inputs + */ + public static W3CEndpointReference address(String protocol,String contextPath,String service, String namespace, String key, String host, int port) throws IllegalArgumentException { + W3CEndpointReferenceBuilder builder = new W3CEndpointReferenceBuilder(); - builder.address(join(contextPath,service,host,port)); + builder.address(join(protocol,contextPath,service,host,port)); builder.referenceParameter(key(namespace,key)); return builder.build(); @@ -119,11 +164,11 @@ public class AddressingUtils { } //helper - private static String join(String path,String service,String host, int port) { + private static String join(String protocol,String path,String service,String host, int port) { //some tolerance - if (host.startsWith(scheme_prefix)) - host = host.substring(scheme_prefix.length(),host.length()); - String address = scheme_prefix + host + ":" + port + path + service; + if (host.startsWith(protocol)) + host = host.substring(protocol.length(),host.length()); + String address = protocol + "://"+ host + ":" + port + path + service; return address; } diff --git a/src/test/java/org/gcube/common/clients/AddressingUtilsTest.java b/src/test/java/org/gcube/common/clients/AddressingUtilsTest.java new file mode 100644 index 0000000..06dfdcd --- /dev/null +++ b/src/test/java/org/gcube/common/clients/AddressingUtilsTest.java @@ -0,0 +1,66 @@ +package org.gcube.common.clients; + +import static org.junit.Assert.*; + +import java.io.StringReader; +import java.io.StringWriter; +import java.net.URI; + +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.stream.StreamResult; +import javax.xml.ws.wsaddressing.W3CEndpointReference; + +import org.gcube.common.clients.builders.AddressingUtils; +import org.junit.BeforeClass; +import org.junit.Test; +import org.w3c.dom.Document; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; + +public class AddressingUtilsTest { + + private static final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + private static final String addressLocalName = "Address"; + + @BeforeClass + public static void setup() { + factory.setNamespaceAware(true); + } + + @Test + public void addressPreservesProtocol() { + + URI uri = URI.create("https://acme.org"); + + W3CEndpointReference reference = AddressingUtils.address("/some/context", "/someservice",uri); + + URI parsed = addressFromReference(reference); + + assertEquals(uri.getScheme(),parsed.getScheme()); + } + + //helper + private URI addressFromReference(W3CEndpointReference ref) { + + try { + + StringWriter w = new StringWriter(); + + ref.writeTo(new StreamResult(w)); + + Document document = factory.newDocumentBuilder().parse(new InputSource(new StringReader(w.toString()))); + + NodeList addresses = document.getElementsByTagNameNS("*", addressLocalName); + + if (addresses.getLength() == 0) + throw new RuntimeException("reference does not contain an address"); + + String address = addresses.item(0).getTextContent(); + + return URI.create(address); + } + catch(Exception e) { + throw new RuntimeException(e); + } + } +}