From 7f30efb5b396f57fda420e10d5c356bd58a48b7a Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Wed, 14 Sep 2016 16:09:03 +0000 Subject: [PATCH] Implementing Client git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry-client@131369 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../client/plugin/AbstractPlugin.java | 3 +- .../plugin/ResourceRegistryClientPlugin.java | 16 ++--- .../client/proxy/JaxRSEndpointReference.java | 63 +++++++++++++++++++ .../client/proxy/ResourceRegistryClient.java | 39 +++++++----- .../proxy/ResourceRegistryClientTest.java | 36 +++++++++++ 5 files changed, 130 insertions(+), 27 deletions(-) create mode 100644 src/main/java/org/gcube/informationsystem/resourceregistry/client/proxy/JaxRSEndpointReference.java create mode 100644 src/test/java/org/gcube/informationsystem/resourceregistry/client/proxy/ResourceRegistryClientTest.java diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/client/plugin/AbstractPlugin.java b/src/main/java/org/gcube/informationsystem/resourceregistry/client/plugin/AbstractPlugin.java index 314ee42..7dcb8f7 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/client/plugin/AbstractPlugin.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/client/plugin/AbstractPlugin.java @@ -1,6 +1,7 @@ package org.gcube.informationsystem.resourceregistry.client.plugin; -import org.gcube.common.clients.Plugin; + +import org.gcube.common.clients.fw.plugin.Plugin; import org.gcube.informationsystem.resourceregistry.Constants; /** diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/client/plugin/ResourceRegistryClientPlugin.java b/src/main/java/org/gcube/informationsystem/resourceregistry/client/plugin/ResourceRegistryClientPlugin.java index c9af24d..4de0703 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/client/plugin/ResourceRegistryClientPlugin.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/client/plugin/ResourceRegistryClientPlugin.java @@ -7,6 +7,7 @@ import javax.xml.ws.EndpointReference; import org.gcube.common.clients.config.ProxyConfig; import org.gcube.common.clients.delegates.ProxyDelegate; +import org.gcube.informationsystem.resourceregistry.Constants; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.client.proxy.ResourceRegistryClient; @@ -17,12 +18,11 @@ import org.gcube.informationsystem.resourceregistry.client.proxy.ResourceRegistr public class ResourceRegistryClientPlugin extends AbstractPlugin{ public ResourceRegistryClientPlugin(){ - super(ResourceRegistryClientPlugin.class.getSimpleName()); + super(Constants.SERVICE_ENTRY_NAME); } @Override public String namespace() { - // TODO Auto-generated method stub return null; } @@ -39,24 +39,16 @@ public class ResourceRegistryClientPlugin extends AbstractPlugin config) throws Exception { - // TODO Auto-generated method stub - return null; + return address; } - /* (non-Javadoc) - * @see org.gcube.common.clients.delegates.ProxyPlugin#newProxy(org.gcube.common.clients.delegates.ProxyDelegate) - */ @Override public ResourceRegistryClient newProxy( ProxyDelegate delegate) { - // TODO Auto-generated method stub - return null; + return new ResourceRegistryClient(delegate); } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/client/proxy/JaxRSEndpointReference.java b/src/main/java/org/gcube/informationsystem/resourceregistry/client/proxy/JaxRSEndpointReference.java new file mode 100644 index 0000000..0266f08 --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/client/proxy/JaxRSEndpointReference.java @@ -0,0 +1,63 @@ +/** + * + */ +package org.gcube.informationsystem.resourceregistry.client.proxy; + +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; + +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); + } + + public JaxRSEndpointReference(EndpointReference reference) { + this(serialise(reference)); + } + + public 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/informationsystem/resourceregistry/client/proxy/ResourceRegistryClient.java b/src/main/java/org/gcube/informationsystem/resourceregistry/client/proxy/ResourceRegistryClient.java index 1f34ab4..3829177 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/client/proxy/ResourceRegistryClient.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/client/proxy/ResourceRegistryClient.java @@ -4,6 +4,7 @@ package org.gcube.informationsystem.resourceregistry.client.proxy; import java.io.BufferedReader; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; @@ -19,6 +20,7 @@ import org.gcube.common.clients.Call; import org.gcube.common.clients.delegates.AsyncProxyDelegate; import org.gcube.common.clients.delegates.ProxyDelegate; import org.gcube.common.clients.exceptions.ServiceException; +import org.gcube.common.scope.api.ScopeProvider; import org.gcube.informationsystem.resourceregistry.api.exceptions.InvalidQueryException; import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath; import org.slf4j.Logger; @@ -38,20 +40,26 @@ public class ResourceRegistryClient { this.delegate = new AsyncProxyDelegate(config); } - protected HttpURLConnection makeRequest(URL url, String method, - boolean includeTokenInHeader) throws Exception { + protected HttpURLConnection makeRequest(URL url, String method) throws Exception { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - if (includeTokenInHeader) { - connection.setRequestProperty(Constants.TOKEN_HEADER_ENTRY, - SecurityTokenProvider.instance.get()); + if (SecurityTokenProvider.instance.get()==null) { + if(ScopeProvider.instance.get()==null){ + throw new RuntimeException("Null Token and Scope. Please set your token first."); + } + connection.setRequestProperty("gcube-scope", ScopeProvider.instance.get()); + }else{ + connection.setRequestProperty(Constants.TOKEN_HEADER_ENTRY, SecurityTokenProvider.instance.get()); } + connection.setDoOutput(true); + connection.setDoInput(true); + connection.setRequestProperty("Content-type", "text/plain"); connection.setRequestMethod(method); return connection; } protected void appendQueryParameter(StringBuilder builder, String name, String value) throws UnsupportedEncodingException { - builder.append(name).append("="); + builder.append("?").append(name).append("="); String encodedValue = URLEncoder.encode(value, "UTF-8"); builder.append(encodedValue).append("&"); } @@ -61,12 +69,18 @@ public class ResourceRegistryClient { Call call = new Call() { + private String getURLStringFromEndpointReference(EndpointReference endpoint) throws IOException { + JaxRSEndpointReference jaxRSEndpointReference = new JaxRSEndpointReference(endpoint); + return jaxRSEndpointReference.toString(); + } + public String call(EndpointReference endpoint) throws Exception { - StringBuilder callUrl = new StringBuilder(endpoint.toString()); + String urlFromEndpointReference = getURLStringFromEndpointReference(endpoint); + + StringBuilder callUrl = new StringBuilder(urlFromEndpointReference); callUrl.append("/").append(AccessPath.ACCESS_PATH_PART) .append("/"); - appendQueryParameter(callUrl, AccessPath.QUERY_PARAM, query); if (fetchPlan != null) { @@ -75,11 +89,8 @@ public class ResourceRegistryClient { } URL url = new URL(callUrl.toString()); - HttpURLConnection connection = makeRequest(url, "GET", false); - connection.setDoOutput(true); - connection.setDoInput(false); - connection.setRequestProperty("Content-type", "text/plain"); - + HttpURLConnection connection = makeRequest(url, "GET"); + logger.debug("Response code for {} is {} : {}", callUrl.toString(), connection.getResponseCode(), connection.getResponseMessage()); @@ -100,7 +111,7 @@ public class ResourceRegistryClient { } return result.toString(); - }; + } }; diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/client/proxy/ResourceRegistryClientTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/client/proxy/ResourceRegistryClientTest.java new file mode 100644 index 0000000..e94fa96 --- /dev/null +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/client/proxy/ResourceRegistryClientTest.java @@ -0,0 +1,36 @@ +/** + * + */ +package org.gcube.informationsystem.resourceregistry.client.proxy; + +import javax.xml.ws.EndpointReference; + +import org.gcube.common.clients.fw.builders.StatelessBuilderImpl; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.informationsystem.resourceregistry.api.exceptions.InvalidQueryException; +import org.gcube.informationsystem.resourceregistry.client.plugin.ResourceRegistryClientPlugin; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ + * + */ +public class ResourceRegistryClientTest { + + private static Logger logger = LoggerFactory + .getLogger(ResourceRegistryClientTest.class); + + @Test + public void testQuery() throws InvalidQueryException{ + + ScopeProvider.instance.set("/gcube/devNext"); + ResourceRegistryClientPlugin plugin = new ResourceRegistryClientPlugin(); + + ResourceRegistryClient rrc = new StatelessBuilderImpl(plugin).build(); + + String res = rrc.query("SELECT FROM V", null); + logger.debug(res); + } +}