From dd2300d1a9d9ae119fd230376072024d918f0f1f Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 17 Jan 2019 10:18:02 +0000 Subject: [PATCH] Refs #11455: Integrate GX REST in resource-registry client libraries Task-Url: https://support.d4science.org/issues/11455 git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry-client@176626 82a268e6-3cf1-43bd-a215-b396298e98cf --- pom.xml | 4 + .../client/ResourceRegistryClient.java | 4 +- .../client/ResourceRegistryClientImpl.java | 194 ++++++++---------- .../client/ResourceRegistryClientTest.java | 2 +- 4 files changed, 92 insertions(+), 112 deletions(-) diff --git a/pom.xml b/pom.xml index 1c873eb..b461c72 100644 --- a/pom.xml +++ b/pom.xml @@ -53,6 +53,10 @@ org.gcube.resources.discovery ic-client + + org.gcube.common + gxHTTP + org.gcube.information-system resource-registry-api diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClient.java b/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClient.java index a1fe9a8..4f8db24 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClient.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClient.java @@ -56,10 +56,10 @@ public interface ResourceRegistryClient { public , F extends Facet> List getFilteredResources( Class resourceClass, Class consistsOfClass, Class facetClass, boolean polymorphic, - Map map) throws ResourceRegistryException; + Map map) throws ResourceRegistryException; public String getFilteredResources(String resourceType, String consistsOfType, String facetType, - boolean polymorphic, Map map) throws ResourceRegistryException; + boolean polymorphic, Map map) throws ResourceRegistryException; public , RR extends Resource> List getRelatedResourcesFromReferenceResource( diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientImpl.java b/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientImpl.java index 343f34d..75abf90 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientImpl.java @@ -1,12 +1,12 @@ package org.gcube.informationsystem.resourceregistry.client; -import java.io.StringWriter; -import java.net.MalformedURLException; +import java.net.HttpURLConnection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; +import org.gcube.common.gxhttp.request.GXHTTPStringRequest; import org.gcube.informationsystem.model.impl.utils.ISMapper; import org.gcube.informationsystem.model.reference.ER; import org.gcube.informationsystem.model.reference.ISManageable; @@ -24,8 +24,7 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.context.Conte import org.gcube.informationsystem.resourceregistry.api.exceptions.query.InvalidQueryException; 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.resourceregistry.api.rest.httputils.HTTPUtility; import org.gcube.informationsystem.resourceregistry.api.utils.Utility; import org.gcube.informationsystem.types.TypeBinder; import org.gcube.informationsystem.types.TypeBinder.TypeDefinition; @@ -39,37 +38,24 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { private static final Logger logger = LoggerFactory.getLogger(ResourceRegistryClientImpl.class); - public static final String PATH_SEPARATOR = "/"; - protected final String address; - protected HTTPCall httpCall; public ResourceRegistryClientImpl(String address) { this.address = address; - - } - - private HTTPCall getHTTPCall() throws MalformedURLException { - if(httpCall == null) { - httpCall = new HTTPCall(address, ResourceRegistryClient.class.getSimpleName()); - } - return httpCall; } @Override public Context getCurrentContext() throws ContextNotFoundException, ResourceRegistryException { try { logger.info("Going to get current {} ", Context.NAME); - StringWriter stringWriter = new StringWriter(); - stringWriter.append(PATH_SEPARATOR); - stringWriter.append(AccessPath.ACCESS_PATH_PART); - stringWriter.append(PATH_SEPARATOR); - stringWriter.append(AccessPath.CONTEXTS_PATH_PART); - stringWriter.append(PATH_SEPARATOR); - stringWriter.append(AccessPath.CURRENT_CONTEXT); + GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); + gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName()); + gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART); + gxHTTPStringRequest.path(AccessPath.CONTEXTS_PATH_PART); + gxHTTPStringRequest.path(AccessPath.CURRENT_CONTEXT); - HTTPCall httpCall = getHTTPCall(); - Context context = httpCall.call(Context.class, stringWriter.toString(), HTTPMETHOD.GET); + HttpURLConnection httpURLConnection = gxHTTPStringRequest.get(); + Context context = HTTPUtility.getResponse(Context.class, httpURLConnection); logger.debug("Got Context is {}", ISMapper.marshal(context)); return context; @@ -91,16 +77,15 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { public Context getContext(UUID uuid) throws ContextNotFoundException, ResourceRegistryException { try { logger.info("Going to get {} with UUID {}", Context.NAME, uuid.toString()); - StringWriter stringWriter = new StringWriter(); - stringWriter.append(PATH_SEPARATOR); - stringWriter.append(AccessPath.ACCESS_PATH_PART); - stringWriter.append(PATH_SEPARATOR); - stringWriter.append(AccessPath.CONTEXTS_PATH_PART); - stringWriter.append(PATH_SEPARATOR); - stringWriter.append(uuid.toString()); - HTTPCall httpCall = getHTTPCall(); - Context context = httpCall.call(Context.class, stringWriter.toString(), HTTPMETHOD.GET); + GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); + gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName()); + gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART); + gxHTTPStringRequest.path(AccessPath.CONTEXTS_PATH_PART); + gxHTTPStringRequest.path(uuid.toString()); + + HttpURLConnection httpURLConnection = gxHTTPStringRequest.get(); + Context context = HTTPUtility.getResponse(Context.class, httpURLConnection); logger.debug("Got Context is {}", ISMapper.marshal(context)); return context; @@ -121,14 +106,13 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { public List getAllContext() throws ContextNotFoundException, ResourceRegistryException { try { logger.info("Going to read all {}s", Context.NAME); - StringWriter stringWriter = new StringWriter(); - stringWriter.append(PATH_SEPARATOR); - stringWriter.append(AccessPath.ACCESS_PATH_PART); - stringWriter.append(PATH_SEPARATOR); - stringWriter.append(AccessPath.CONTEXTS_PATH_PART); + GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); + gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName()); + gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART); + gxHTTPStringRequest.path(AccessPath.CONTEXTS_PATH_PART); - HTTPCall httpCall = getHTTPCall(); - String ret = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.GET); + HttpURLConnection httpURLConnection = gxHTTPStringRequest.get(); + String ret = HTTPUtility.getResponse(String.class, httpURLConnection); logger.debug("Got Contexts are {}", ret); return ISMapper.unmarshalList(Context.class, ret); @@ -151,20 +135,19 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { String type = Utility.getType(clazz); try { logger.info("Going to get {} schema", type); - StringWriter stringWriter = new StringWriter(); - stringWriter.append(PATH_SEPARATOR); - stringWriter.append(AccessPath.ACCESS_PATH_PART); - stringWriter.append(PATH_SEPARATOR); - stringWriter.append(AccessPath.TYPES_PATH_PART); - stringWriter.append(PATH_SEPARATOR); - stringWriter.append(type); + GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); + gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName()); + gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART); + gxHTTPStringRequest.path(AccessPath.TYPES_PATH_PART); + gxHTTPStringRequest.path(type); Map parameters = new HashMap<>(); parameters.put(AccessPath.POLYMORPHIC_PARAM, polymorphic.toString()); + gxHTTPStringRequest.queryParams(parameters); - HTTPCall httpCall = getHTTPCall(); - String json = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.GET, parameters); - + HttpURLConnection httpURLConnection = gxHTTPStringRequest.get(); + String json = HTTPUtility.getResponse(String.class, httpURLConnection); + logger.debug("Got schema for {} is {}", type, json); return TypeBinder.deserializeTypeDefinitions(json); } catch(ResourceRegistryException e) { @@ -191,18 +174,15 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { try { logger.info("Going to check if {} with UUID {} exists", type, uuid); - StringWriter stringWriter = new StringWriter(); - stringWriter.append(PATH_SEPARATOR); - stringWriter.append(AccessPath.ACCESS_PATH_PART); - stringWriter.append(PATH_SEPARATOR); - stringWriter.append(AccessPath.INSTANCES_PATH_PART); - stringWriter.append(PATH_SEPARATOR); - stringWriter.append(type); - stringWriter.append(PATH_SEPARATOR); - stringWriter.append(uuid.toString()); + GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); + gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName()); + gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART); + gxHTTPStringRequest.path(AccessPath.INSTANCES_PATH_PART); + gxHTTPStringRequest.path(type); + gxHTTPStringRequest.path(uuid.toString()); - HTTPCall httpCall = getHTTPCall(); - httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.HEAD); + HttpURLConnection httpURLConnection = gxHTTPStringRequest.head(); + HTTPUtility.getResponse(String.class, httpURLConnection); logger.debug("{} with UUID {} exists", type, uuid); return true; @@ -233,18 +213,15 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { try { logger.info("Going to get {} with UUID {}", type, uuid); - StringWriter stringWriter = new StringWriter(); - stringWriter.append(PATH_SEPARATOR); - stringWriter.append(AccessPath.ACCESS_PATH_PART); - stringWriter.append(PATH_SEPARATOR); - stringWriter.append(AccessPath.INSTANCES_PATH_PART); - stringWriter.append(PATH_SEPARATOR); - stringWriter.append(type); - stringWriter.append(PATH_SEPARATOR); - stringWriter.append(uuid.toString()); + GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); + gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName()); + gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART); + gxHTTPStringRequest.path(AccessPath.INSTANCES_PATH_PART); + gxHTTPStringRequest.path(type); + gxHTTPStringRequest.path(uuid.toString()); - HTTPCall httpCall = getHTTPCall(); - String ret = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.GET); + HttpURLConnection httpURLConnection = gxHTTPStringRequest.get(); + String ret = HTTPUtility.getResponse(String.class, httpURLConnection); logger.debug("Got {} with UUID {} is {}", type, uuid, ret); return ret; @@ -274,19 +251,19 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { public String getInstances(String type, Boolean polymorphic) throws ResourceRegistryException { try { logger.info("Going to get all instances of {} ", type); - StringWriter stringWriter = new StringWriter(); - stringWriter.append(PATH_SEPARATOR); - stringWriter.append(AccessPath.ACCESS_PATH_PART); - stringWriter.append(PATH_SEPARATOR); - stringWriter.append(AccessPath.INSTANCES_PATH_PART); - stringWriter.append(PATH_SEPARATOR); - stringWriter.append(type); + GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); + gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName()); + gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART); + gxHTTPStringRequest.path(AccessPath.INSTANCES_PATH_PART); + gxHTTPStringRequest.path(type); Map parameters = new HashMap<>(); parameters.put(AccessPath.POLYMORPHIC_PARAM, polymorphic.toString()); + gxHTTPStringRequest.queryParams(parameters); - HTTPCall httpCall = getHTTPCall(); - String ret = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.GET, parameters); + HttpURLConnection httpURLConnection = gxHTTPStringRequest.get(); + + String ret = HTTPUtility.getResponse(String.class, httpURLConnection); logger.debug("Got instances of {} are {}", type, ret); return ret; @@ -305,11 +282,10 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { try { logger.info("Going to query. {}", query); - StringWriter stringWriter = new StringWriter(); - stringWriter.append(PATH_SEPARATOR); - stringWriter.append(AccessPath.ACCESS_PATH_PART); - stringWriter.append(PATH_SEPARATOR); - stringWriter.append(AccessPath.QUERY_PATH_PART); + GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); + gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName()); + gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART); + gxHTTPStringRequest.path(AccessPath.QUERY_PATH_PART); Map parameters = new HashMap<>(); parameters.put(AccessPath.QUERY_PARAM, query); @@ -322,8 +298,10 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { parameters.put(AccessPath.FETCH_PLAN_PARAM, fetchPlan); } - HTTPCall httpCall = getHTTPCall(); - String ret = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.GET, parameters); + gxHTTPStringRequest.queryParams(parameters); + HttpURLConnection httpURLConnection = gxHTTPStringRequest.get(); + + String ret = HTTPUtility.getResponse(String.class, httpURLConnection); logger.debug("Query result is {}", ret); return ret; @@ -337,25 +315,21 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { } protected String getRelated(String entityType, String relationType, String referenceEntityType, - UUID referenceEntity, Direction direction, boolean polymorphic, Map map) + UUID referenceEntity, Direction direction, Boolean polymorphic, Map map) throws ResourceRegistryException { try { - StringWriter stringWriter = new StringWriter(); - stringWriter.append(PATH_SEPARATOR); - stringWriter.append(AccessPath.ACCESS_PATH_PART); - stringWriter.append(PATH_SEPARATOR); - stringWriter.append(AccessPath.QUERY_PATH_PART); - stringWriter.append(PATH_SEPARATOR); - stringWriter.append(entityType); - stringWriter.append(PATH_SEPARATOR); - stringWriter.append(relationType); - stringWriter.append(PATH_SEPARATOR); - stringWriter.append(referenceEntityType); + GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); + gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName()); + gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART); + gxHTTPStringRequest.path(AccessPath.QUERY_PATH_PART); + gxHTTPStringRequest.path(entityType); + gxHTTPStringRequest.path(relationType); + gxHTTPStringRequest.path(referenceEntityType); - Map parameters = new HashMap<>(); - parameters.put(AccessPath.DIRECTION_PARAM, direction); - parameters.put(AccessPath.POLYMORPHIC_PARAM, polymorphic); + Map parameters = new HashMap<>(); + parameters.put(AccessPath.DIRECTION_PARAM, direction.name()); + parameters.put(AccessPath.POLYMORPHIC_PARAM, polymorphic.toString()); if(referenceEntity == null) { if(map != null && map.size() > 0) { @@ -372,8 +346,10 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { parameters.put(AccessPath.REFERENCE_PARAM, referenceEntity.toString()); } - HTTPCall httpCall = getHTTPCall(); - String json = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.GET, parameters); + gxHTTPStringRequest.queryParams(parameters); + HttpURLConnection httpURLConnection = gxHTTPStringRequest.get(); + + String json = HTTPUtility.getResponse(String.class, httpURLConnection); if(referenceEntity == null) { logger.info("{} linked by {} to/from {} having {} are {}", entityType, relationType, @@ -428,7 +404,7 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { @Override public , F extends Facet> List getFilteredResources( Class resourceClass, Class consistsOfClass, Class facetClass, boolean polymorphic, - Map map) throws ResourceRegistryException { + Map map) throws ResourceRegistryException { String resourceType = Utility.getType(resourceClass); String consistsOfType = Utility.getType(consistsOfClass); String facetType = Utility.getType(facetClass); @@ -442,7 +418,7 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { @Override public String getFilteredResources(String resourceType, String consistsOfType, String facetType, - boolean polymorphic, Map map) throws ResourceRegistryException { + boolean polymorphic, Map map) throws ResourceRegistryException { return getRelated(resourceType, consistsOfType, facetType, Direction.out, polymorphic, map); } @@ -506,7 +482,7 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { // @Override protected , RE extends Entity> List getRelated(Class entityClass, Class relationClass, Class referenceEntityClass, Direction direction, boolean polymorphic, - Map map) throws ResourceRegistryException { + Map map) throws ResourceRegistryException { String entityType = Utility.getType(entityClass); String relationType = Utility.getType(relationClass); String referenceEntityType = Utility.getType(referenceEntityClass); @@ -520,7 +496,7 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { // @Override protected String getRelated(String entityType, String relationType, String referenceEntityType, Direction direction, - boolean polymorphic, Map map) throws ResourceRegistryException { + boolean polymorphic, Map map) throws ResourceRegistryException { return getRelated(entityType, relationType, referenceEntityType, null, direction, polymorphic, map); } diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientTest.java index 703f8cd..6583a1d 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientTest.java @@ -132,7 +132,7 @@ public class ResourceRegistryClientTest extends ScopedTest { // @Test public void testGetFilteredResourcesByClasses() throws ResourceRegistryException, JsonProcessingException { - Map map = new HashMap<>(); + Map map = new HashMap<>(); map.put("group", "VREManagement"); map.put("name", "SmartExecutor"); List eServices = resourceRegistryClient.getFilteredResources(EService.class, IsIdentifiedBy.class,