merged from trunk
git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/branches/common/common-clients/2.0@73661 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
e3f4f60abe
commit
48e934b2c5
38
.classpath
38
.classpath
|
@ -1,10 +1,36 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<classpath>
|
<classpath>
|
||||||
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
|
<classpathentry kind="src" output="target/classes" path="src/main/java">
|
||||||
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
|
<attributes>
|
||||||
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
|
<attribute name="optional" value="true"/>
|
||||||
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/>
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
|
</attributes>
|
||||||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
|
</classpathentry>
|
||||||
|
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="optional" value="true"/>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
<classpathentry kind="output" path="target/classes"/>
|
<classpathentry kind="output" path="target/classes"/>
|
||||||
</classpath>
|
</classpath>
|
||||||
|
|
|
@ -1,2 +1 @@
|
||||||
* Fabio Simeoni (fabio.simeoni@fao.org), FAO of the UN, Italy
|
* Fabio Simeoni (fabio.simeoni@fao.org), FAO of the UN, Italy
|
||||||
* Rena Tsantouli (e.tsantoylh@di.uoa.gr), University of Athens, Greece
|
|
|
@ -5,7 +5,10 @@
|
||||||
<Changeset component="common-clients-2.0.0" date="2012-09-01">
|
<Changeset component="common-clients-2.0.0" date="2012-09-01">
|
||||||
<Change>Rewritten as a framework for the implementation of client libraries that comply with the CL Design Model</Change>
|
<Change>Rewritten as a framework for the implementation of client libraries that comply with the CL Design Model</Change>
|
||||||
</Changeset>
|
</Changeset>
|
||||||
<Changeset component="${build.finalName}" date="2013-01-11">
|
<Changeset component="common-clients-2.0.1" date="2013-01-11">
|
||||||
<Change>Improved error handling in Plugin#convert()</Change>
|
<Change>Improved error handling in Plugin#convert()</Change>
|
||||||
</Changeset>
|
</Changeset>
|
||||||
|
<Changeset component="${build.finalName}" date="2013-15-04">
|
||||||
|
<Change>AddressUtils preserve protocol from URIs/URLs</Change>
|
||||||
|
</Changeset>
|
||||||
</ReleaseNotes>
|
</ReleaseNotes>
|
2
pom.xml
2
pom.xml
|
@ -10,7 +10,7 @@
|
||||||
|
|
||||||
<groupId>org.gcube.core</groupId>
|
<groupId>org.gcube.core</groupId>
|
||||||
<artifactId>common-clients</artifactId>
|
<artifactId>common-clients</artifactId>
|
||||||
<version>2.0.1-SNAPSHOT</version>
|
<version>2.0.2-SNAPSHOT</version>
|
||||||
|
|
||||||
<name>Common Clients</name>
|
<name>Common Clients</name>
|
||||||
<description>A framework for client APIs</description>
|
<description>A framework for client APIs</description>
|
||||||
|
|
|
@ -22,7 +22,7 @@ public class AddressingUtils {
|
||||||
|
|
||||||
|
|
||||||
private static final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
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";
|
private static final String keyElementPrefix = "key";
|
||||||
|
|
||||||
static {
|
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 contextPath the context path of the service
|
||||||
* @param service the name of the service
|
* @param service the name of the service
|
||||||
* @param host the host of the endpoint
|
* @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 {
|
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();
|
W3CEndpointReferenceBuilder builder = new W3CEndpointReferenceBuilder();
|
||||||
builder.address(join(contextPath,service,host,port));
|
builder.address(join(protocol,contextPath,service,host,port));
|
||||||
return builder.build();
|
return builder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,7 +69,19 @@ public class AddressingUtils {
|
||||||
* @throws IllegalArgumentException if an address cannot be derived from the inputs
|
* @throws IllegalArgumentException if an address cannot be derived from the inputs
|
||||||
*/
|
*/
|
||||||
public static W3CEndpointReference address(String contextPath,String service,URL address) throws IllegalArgumentException {
|
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
|
* @throws IllegalArgumentException if an address cannot be derived from the inputs
|
||||||
*/
|
*/
|
||||||
public static W3CEndpointReference address(String contextPath,String service,URI address) throws IllegalArgumentException {
|
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 {
|
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();
|
W3CEndpointReferenceBuilder builder = new W3CEndpointReferenceBuilder();
|
||||||
builder.address(join(contextPath,service,host,port));
|
builder.address(join(protocol,contextPath,service,host,port));
|
||||||
builder.referenceParameter(key(namespace,key));
|
builder.referenceParameter(key(namespace,key));
|
||||||
return builder.build();
|
return builder.build();
|
||||||
|
|
||||||
|
@ -119,11 +164,11 @@ public class AddressingUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
//helper
|
//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
|
//some tolerance
|
||||||
if (host.startsWith(scheme_prefix))
|
if (host.startsWith(protocol))
|
||||||
host = host.substring(scheme_prefix.length(),host.length());
|
host = host.substring(protocol.length(),host.length());
|
||||||
String address = scheme_prefix + host + ":" + port + path + service;
|
String address = protocol + "://"+ host + ":" + port + path + service;
|
||||||
return address;
|
return address;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue