From ab7399343e9971210d5307f7cdd30d1be48118a5 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Fri, 24 Feb 2017 10:00:12 +0000 Subject: [PATCH] Using HTTPCall utility present in api git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry-client@144235 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../client/proxy/ResourceRegistryCall.java | 37 +++ .../proxy/ResourceRegistryClientImpl.java | 290 ++---------------- .../proxy/ResourceRegistryClientTest.java | 7 +- .../client/proxy/ScopedTest.java | 4 +- 4 files changed, 64 insertions(+), 274 deletions(-) create mode 100644 src/main/java/org/gcube/informationsystem/resourceregistry/client/proxy/ResourceRegistryCall.java diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/client/proxy/ResourceRegistryCall.java b/src/main/java/org/gcube/informationsystem/resourceregistry/client/proxy/ResourceRegistryCall.java new file mode 100644 index 0000000..95ea9b2 --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/client/proxy/ResourceRegistryCall.java @@ -0,0 +1,37 @@ +package org.gcube.informationsystem.resourceregistry.client.proxy; + +import java.io.IOException; +import java.net.URL; + +import javax.xml.ws.EndpointReference; + +import org.gcube.common.clients.Call; +import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPCall; + +public class ResourceRegistryCall implements Call { + + protected final Class clazz; + protected final HTTPCall httpCall; + + public ResourceRegistryCall(Class clazz, HTTPCall httpCall) { + this.clazz = clazz; + this.httpCall = httpCall; + } + + protected String getURLStringFromEndpointReference( + EndpointReference endpoint) throws IOException { + JaxRSEndpointReference jaxRSEndpointReference = new JaxRSEndpointReference( + endpoint); + return jaxRSEndpointReference.toString(); + } + + @Override + public C call(EndpointReference endpoint) throws Exception { + String urlFromEndpointReference = getURLStringFromEndpointReference(endpoint); + StringBuilder callUrl = new StringBuilder(urlFromEndpointReference); + callUrl.append(httpCall.getPath()); + URL url = new URL(callUrl.toString()); + return httpCall.call(clazz, url, ResourceRegistryClient.class.getSimpleName()); + } + +} diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/client/proxy/ResourceRegistryClientImpl.java b/src/main/java/org/gcube/informationsystem/resourceregistry/client/proxy/ResourceRegistryClientImpl.java index 798a87c..8a9dca5 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/client/proxy/ResourceRegistryClientImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/client/proxy/ResourceRegistryClientImpl.java @@ -3,35 +3,21 @@ */ 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.StringWriter; -import java.io.UnsupportedEncodingException; -import java.net.HttpURLConnection; -import java.net.URL; -import java.net.URLEncoder; -import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Map.Entry; import java.util.UUID; import javax.xml.ws.EndpointReference; -import org.gcube.common.authorization.client.Constants; -import org.gcube.common.authorization.library.provider.SecurityTokenProvider; -import org.gcube.common.clients.Call; import org.gcube.common.clients.delegates.AsyncProxyDelegate; import org.gcube.common.clients.delegates.ProxyDelegate; -import org.gcube.common.scope.api.ScopeProvider; import org.gcube.informationsystem.impl.utils.ISMapper; import org.gcube.informationsystem.model.ER; import org.gcube.informationsystem.model.ISManageable; import org.gcube.informationsystem.model.entity.Entity; import org.gcube.informationsystem.model.entity.Resource; -import org.gcube.informationsystem.resourceregistry.api.exceptions.ExceptionMapper; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERException; import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException; @@ -39,6 +25,8 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.query.Invalid import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException; import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath; +import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPCall; +import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPCall.HTTPMETHOD; import org.gcube.informationsystem.types.TypeBinder; import org.gcube.informationsystem.types.TypeBinder.TypeDefinition; import org.slf4j.Logger; @@ -46,7 +34,6 @@ import org.slf4j.LoggerFactory; /** * @author Luca Frosini (ISTI - CNR) - * */ public class ResourceRegistryClientImpl implements ResourceRegistryClient { @@ -57,236 +44,10 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { public static final String PATH_SEPARATOR = "/"; - public final class RREntry implements Entry { - - private final K key; - private V value; - - public RREntry(K key, V value) { - this.key = key; - this.value = value; - } - - @Override - public K getKey() { - return key; - } - - @Override - public V getValue() { - return value; - } - - @Override - public V setValue(V value) { - V old = this.value; - this.value = value; - return old; - } - } - public ResourceRegistryClientImpl(ProxyDelegate config) { this.delegate = new AsyncProxyDelegate(config); } - protected enum HTTPMETHOD { - GET, POST, PUT, DELETE; - - @Override - public String toString() { - return this.name(); - } - } - - class HTTPInputs { - - public static final String PARAM_STARTER = "?"; - public static final String PARAM_EQUALS = "="; - public static final String PARAM_SEPARATOR = "&"; - public static final String UTF8 = "UTF-8"; - - protected final String path; - protected final HTTPMETHOD method; - protected final String urlParameters; - - protected String getParametersDataString( - List> parameters) - throws UnsupportedEncodingException { - if (parameters == null) { - return null; - } - - StringBuilder result = new StringBuilder(); - boolean first = true; - for (Entry entry : parameters) { - if (first) { - first = false; - } else { - result.append(PARAM_SEPARATOR); - } - - result.append(URLEncoder.encode(entry.getKey(), UTF8)); - result.append(PARAM_EQUALS); - result.append(URLEncoder.encode(entry.getValue(), UTF8)); - - } - - return result.toString(); - } - - /** - * @param path - * @param method - * @param requestProperties - * @throws UnsupportedEncodingException - */ - public HTTPInputs(String path, HTTPMETHOD method, - List> parameters) - throws UnsupportedEncodingException { - super(); - this.path = path; - this.method = method; - this.urlParameters = getParametersDataString(parameters); - } - - /** - * @return the path - */ - public String getPath() { - return path; - } - - /** - * @return the method - */ - public HTTPMETHOD getMethod() { - return method; - } - - /** - * @return the urlParameters - */ - public String getUrlParameters() { - return urlParameters; - } - - } - - class ResourceRegistryClientCall implements Call { - - protected final Class clazz; - protected final HTTPInputs httpInputs; - - public ResourceRegistryClientCall(Class clazz, HTTPInputs httpInputs) { - this.clazz = clazz; - this.httpInputs = httpInputs; - } - - protected String getURLStringFromEndpointReference( - EndpointReference endpoint) throws IOException { - JaxRSEndpointReference jaxRSEndpointReference = new JaxRSEndpointReference( - endpoint); - return jaxRSEndpointReference.toString(); - } - - protected HttpURLConnection getConnection(URL url, HTTPMETHOD method) - throws Exception { - /* - * if(method!=HTTPMETHOD.POST && - * httpInputs.getUrlParameters()!=null){ - */ - url = new URL(url + "?" + httpInputs.getUrlParameters()); - // } - - HttpURLConnection connection = (HttpURLConnection) url - .openConnection(); - 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.setRequestProperty("Content-type", "application/json"); - connection.setRequestProperty("User-Agent", - ResourceRegistryClient.class.getSimpleName()); - - connection.setRequestMethod(method.toString()); - - /* - * if(method==HTTPMETHOD.POST){ connection.setDoOutput(true); - * DataOutputStream wr = new - * DataOutputStream(connection.getOutputStream()); - * wr.writeBytes(httpInputs.getUrlParameters()); wr.flush(); - * wr.close(); } - */ - - return connection; - } - - @SuppressWarnings("unchecked") - @Override - public C call(EndpointReference endpoint) throws Exception { - String urlFromEndpointReference = getURLStringFromEndpointReference(endpoint); - StringBuilder callUrl = new StringBuilder(urlFromEndpointReference); - callUrl.append(httpInputs.getPath()); - - URL url = new URL(callUrl.toString()); - HttpURLConnection connection = getConnection(url, httpInputs.method); - - String responseMessage = connection.getResponseMessage(); - int responseCode = connection.getResponseCode(); - - if (responseCode != HttpURLConnection.HTTP_OK) { - - logger.error("Response code for {} is {} : {}", - connection.getURL(), responseCode, - responseMessage); - - ResourceRegistryException rre = null; - try { - rre = ExceptionMapper.unmarshal(ResourceRegistryException.class, responseMessage); - }catch (Exception e) { - rre = new ResourceRegistryException(responseMessage); - } - - throw rre; - - }else{ - logger.debug("Response code for {} is {} : {}", - connection.getURL(), responseCode, - responseMessage); - } - - StringBuilder result = new StringBuilder(); - try (BufferedReader reader = new BufferedReader( - new InputStreamReader((InputStream) connection.getContent()))) { - String line; - while ((line = reader.readLine()) != null) { - result.append(line); - } - } - - String res = result.toString(); - logger.trace("Server returned content : {}", res); - - if(Boolean.class.isAssignableFrom(clazz)){ - return (C) ((Boolean) Boolean.valueOf(res)) ; - }else if(ISManageable.class.isAssignableFrom(clazz)){ - return (C) ISMapper.unmarshal((Class) clazz, res); - } - - return (C) res; - } - - } - @Override public ERType getInstance(Class clazz, UUID uuid) throws ERNotFoundException, ResourceRegistryException { @@ -303,11 +64,11 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { stringWriter.append(PATH_SEPARATOR); stringWriter.append(uuid.toString()); - HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), + HTTPCall httpCall = new HTTPCall<>(stringWriter.toString(), HTTPMETHOD.GET, null); - ResourceRegistryClientCall call = new ResourceRegistryClientCall<>( - clazz, httpInputs); + ResourceRegistryCall call = new ResourceRegistryCall<>( + clazz, httpCall); ERType erType = delegate.make(call); logger.info("Got {} with UUID {} is {}", type, uuid, erType); @@ -333,15 +94,14 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { stringWriter.append(PATH_SEPARATOR); stringWriter.append(type); - List> parameters = new ArrayList<>(); - parameters.add(new RREntry( - AccessPath.POLYMORPHIC_PARAM, polymorphic.toString())); + Map parameters = new HashMap<>(); + parameters.put(AccessPath.POLYMORPHIC_PARAM, polymorphic.toString()); - HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), + HTTPCall httpCall = new HTTPCall<>(stringWriter.toString(), HTTPMETHOD.GET, parameters); - ResourceRegistryClientCall call = new ResourceRegistryClientCall<>( - String.class, httpInputs); + ResourceRegistryCall call = new ResourceRegistryCall<>( + String.class, httpCall); String ret = delegate.make(call); logger.info("Got instances of {} are {}", type, ret); @@ -370,19 +130,16 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { stringWriter.append(PATH_SEPARATOR); stringWriter.append(relationType); - List> parameters = new ArrayList<>(); - parameters.add(new RREntry( - AccessPath.POLYMORPHIC_PARAM, polymorphic.toString())); - parameters.add(new RREntry(AccessPath.REFERENCE, - reference.toString())); - parameters.add(new RREntry(AccessPath.DIRECTION, - direction.toString())); + Map parameters = new HashMap<>(); + parameters.put(AccessPath.POLYMORPHIC_PARAM, polymorphic.toString()); + parameters.put(AccessPath.REFERENCE, reference.toString()); + parameters.put(AccessPath.DIRECTION, direction.toString()); - HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), + HTTPCall httpCall = new HTTPCall<>(stringWriter.toString(), HTTPMETHOD.GET, parameters); - ResourceRegistryClientCall call = new ResourceRegistryClientCall<>( - String.class, httpInputs); + ResourceRegistryCall call = new ResourceRegistryCall<>( + String.class, httpCall); String ret = delegate.make(call); logger.info("Got instances of {} from/to {} are {}", relationType, @@ -412,15 +169,14 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { stringWriter.append(PATH_SEPARATOR); stringWriter.append(type); - List> parameters = new ArrayList<>(); - parameters.add(new RREntry( - AccessPath.POLYMORPHIC_PARAM, polymorphic.toString())); + Map parameters = new HashMap<>(); + parameters.put(AccessPath.POLYMORPHIC_PARAM, polymorphic.toString()); - HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), + HTTPCall httpCall = new HTTPCall<>(stringWriter.toString(), HTTPMETHOD.GET, parameters); - ResourceRegistryClientCall call = new ResourceRegistryClientCall<>( - String.class, httpInputs); + ResourceRegistryCall call = new ResourceRegistryCall<>( + String.class, httpCall); String schema = delegate.make(call); logger.info("Got schema for {} is {}", type, schema); 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 index 2bcc70b..634b3c3 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/client/proxy/ResourceRegistryClientTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/client/proxy/ResourceRegistryClientTest.java @@ -17,7 +17,6 @@ import org.slf4j.LoggerFactory; /** * @author Luca Frosini (ISTI - CNR) - * */ public class ResourceRegistryClientTest extends ScopedTest { @@ -36,7 +35,6 @@ public class ResourceRegistryClientTest extends ScopedTest { logger.trace(res); } - @Test public void testGetFacetSchema() throws SchemaNotFoundException { List typeDefinitions = resourceRegistryClient.getSchema(ContactFacet.class, true); @@ -54,10 +52,9 @@ public class ResourceRegistryClientTest extends ScopedTest { } - @Test + @Test(expected=SchemaNotFoundException.class) public void testException() throws SchemaNotFoundException { - List typeDefinitions = resourceRegistryClient.getSchema(Aux.class, true); - logger.trace("{}", typeDefinitions); + resourceRegistryClient.getSchema(Aux.class, true); } diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/client/proxy/ScopedTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/client/proxy/ScopedTest.java index 3e7be29..26e1e0c 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/client/proxy/ScopedTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/client/proxy/ScopedTest.java @@ -59,8 +59,8 @@ public class ScopedTest { GCUBE_DEVSEC = properties.getProperty(GCUBE_DEVSEC_VARNAME); GCUBE_DEVSEC_DEVVRE = properties.getProperty(GCUBE_DEVSEC_DEVVRE_VARNAME); - DEFAULT_TEST_SCOPE = GCUBE_DEVNEXT; - ALTERNATIVE_TEST_SCOPE = GCUBE_DEVNEXT_NEXTNEXT; + DEFAULT_TEST_SCOPE = GCUBE_DEVSEC; + ALTERNATIVE_TEST_SCOPE = GCUBE_DEVSEC_DEVVRE; } public static String getCurrentScope(String token) throws ObjectNotFound, Exception{