From 4d2f87e83c8c064e50b71730f28a489913b870a5 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Wed, 9 Feb 2022 13:20:48 +0100 Subject: [PATCH] Added missing APIs and aligned API names --- .../client/ResourceRegistryClient.java | 177 ++++++--- .../client/ResourceRegistryClientImpl.java | 348 +++++++++++++++++- .../client/ResourceRegistryClientTest.java | 12 +- ...esourceRegistryClientTestWikiExamples.java | 4 +- 4 files changed, 460 insertions(+), 81 deletions(-) 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 2964b4b..2b541c5 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClient.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClient.java @@ -4,18 +4,22 @@ import java.util.List; import java.util.Map; import java.util.UUID; +import org.gcube.com.fasterxml.jackson.databind.JsonNode; import org.gcube.informationsystem.base.reference.Direction; import org.gcube.informationsystem.contexts.reference.entities.Context; import org.gcube.informationsystem.model.reference.ERElement; +import org.gcube.informationsystem.model.reference.entities.Entity; import org.gcube.informationsystem.model.reference.entities.Facet; import org.gcube.informationsystem.model.reference.entities.Resource; import org.gcube.informationsystem.model.reference.relations.ConsistsOf; import org.gcube.informationsystem.model.reference.relations.IsRelatedTo; +import org.gcube.informationsystem.queries.templates.reference.entities.QueryTemplate; import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.queries.InvalidQueryException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.queries.templates.QueryTemplateNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaNotFoundException; import org.gcube.informationsystem.types.reference.Type; @@ -23,83 +27,146 @@ import org.gcube.informationsystem.types.reference.Type; * @author Luca Frosini (ISTI - CNR) */ public interface ResourceRegistryClient { + + public List getAllContext() throws ResourceRegistryException; + public boolean existContext(String uuid) throws ContextNotFoundException, ResourceRegistryException; + + public boolean existContext(UUID uuid) throws ContextNotFoundException, ResourceRegistryException; + + public Context getContext(String uuid) throws ContextNotFoundException, ResourceRegistryException; + + public Context getContext(UUID uuid) throws ContextNotFoundException, ResourceRegistryException; + + public Context getCurrentContext() throws ContextNotFoundException, ResourceRegistryException; + + /* ---------------------------------------------------------------------- */ + + public boolean existType(String typeName) throws ResourceRegistryException; + + public boolean existType(Class clazz) throws ResourceRegistryException; + + public String getType(String typeName, Boolean polymorphic) + throws SchemaNotFoundException, ResourceRegistryException; + + public List getType(Class clazz, Boolean polymorphic) + throws SchemaNotFoundException, ResourceRegistryException; + + /* ---------------------------------------------------------------------- */ + public List getInstances(Class clazz, Boolean polymorphic) throws ResourceRegistryException; - + public String getInstances(String type, Boolean polymorphic) throws ResourceRegistryException; - - public boolean exists(Class clazz, UUID uuid) - throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException; - - public boolean exists(String type, UUID uuid) - throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException; - + + public boolean existInstance(Class clazz, UUID uuid) + throws AvailableInAnotherContextException, ResourceRegistryException; + + public boolean existInstance(String type, UUID uuid) + throws AvailableInAnotherContextException, ResourceRegistryException; + public ERElem getInstance(Class clazz, UUID uuid) throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException; - + public String getInstance(String type, UUID uuid) throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException; - - public , F extends Facet> List getResourcesFromReferenceFacet( - Class resourceClass, Class consistsOfClass, F referenceFacet, - boolean polymorphic) throws ResourceRegistryException; - - public , F extends Facet> List getResourcesFromReferenceFacet( + + /* ---------------------------------------------------------------------- */ + + public Map getInstanceContexts(Class clazz, UUID uuid) + throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException; + + public Map getInstanceContexts(String type, UUID uuid) + throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException; + + /* ---------------------------------------------------------------------- */ + + public List getAllQueryTemplates() throws ResourceRegistryException; + + public boolean existQueryTemplate(QueryTemplate queryTemplate) throws ResourceRegistryException; + + public boolean existQueryTemplate(String queryTemplateName) throws ResourceRegistryException; + + public QueryTemplate readQueryTemplate(QueryTemplate queryTemplate) + throws QueryTemplateNotFoundException, ResourceRegistryException; + + public QueryTemplate readQueryTemplate(String queryTemplateName) + throws QueryTemplateNotFoundException, ResourceRegistryException; + + public String readQueryTemplateAsString(String queryTemplateName) + throws QueryTemplateNotFoundException, ResourceRegistryException; + + public String runQueryTemplateGetString(String name) + throws QueryTemplateNotFoundException, ResourceRegistryException; + + public List runQueryTemplate(String name) + throws QueryTemplateNotFoundException, ResourceRegistryException; + + public List runQueryTemplate(QueryTemplate queryTemplate) + throws QueryTemplateNotFoundException, ResourceRegistryException; + + public String runQueryTemplate(String name, String params) + throws QueryTemplateNotFoundException, ResourceRegistryException; + + public List runQueryTemplate(String name, JsonNode jsonNode) + throws QueryTemplateNotFoundException, ResourceRegistryException; + + public List runQueryTemplate(QueryTemplate queryTemplate, JsonNode jsonNode) + throws QueryTemplateNotFoundException, ResourceRegistryException; + + /* ---------------------------------------------------------------------- */ + + public , F extends Facet> List getResourcesFromReferenceFacet( + Class resourceClass, Class consistsOfClass, F referenceFacet, boolean polymorphic) + throws ResourceRegistryException; + + public , F extends Facet> List getResourcesFromReferenceFacet( Class resourceClass, Class consistsOfClass, Class facetClass, UUID referenceFacetUUID, boolean polymorphic) throws ResourceRegistryException; - + public String getResourcesFromReferenceFacet(String resourceType, String consistsOfType, String facetType, UUID referenceFacetUUID, boolean polymorphic) throws ResourceRegistryException; - - - public , F extends Facet> List getFilteredResources( + + public , F extends Facet> List getFilteredResources( Class resourceClass, Class consistsOfClass, Class facetClass, boolean polymorphic, - Map facetConstraint) throws ResourceRegistryException; - + Map facetConstraint) throws ResourceRegistryException; + public String getFilteredResources(String resourceType, String consistsOfType, String facetType, - boolean polymorphic, Map facetConstraint) throws ResourceRegistryException; - - - public , RR extends Resource> List getRelatedResourcesFromReferenceResource( - Class resourceClass, Class isRelatedToClass, RR referenceResource, - Direction direction, boolean polymorphic) throws ResourceRegistryException; - - public , RR extends Resource> List getRelatedResourcesFromReferenceResource( - Class resourceClass, Class isRelatedToClass, Class referenceResourceClass, UUID referenceResourceUUID, - Direction direction, boolean polymorphic) throws ResourceRegistryException; - + boolean polymorphic, Map facetConstraint) throws ResourceRegistryException; + + public , RR extends Resource> List getRelatedResourcesFromReferenceResource( + Class resourceClass, Class isRelatedToClass, RR referenceResource, Direction direction, + boolean polymorphic) throws ResourceRegistryException; + + public , RR extends Resource> List getRelatedResourcesFromReferenceResource( + Class resourceClass, Class isRelatedToClass, Class referenceResourceClass, + UUID referenceResourceUUID, Direction direction, boolean polymorphic) throws ResourceRegistryException; + public String getRelatedResourcesFromReferenceResource(String resourceType, String isRelatedToType, String referenceResourceType, UUID referenceResourceUUID, Direction direction, boolean polymorphic) throws ResourceRegistryException; - - - public , RR extends Resource> List getRelatedResources( + + public , RR extends Resource> List getRelatedResources( Class resourceClass, Class isRelatedToClass, Class referenceResourceClass, Direction direction, boolean polymorphic) throws ResourceRegistryException; - + public String getRelatedResources(String resourceType, String isRelatedToType, String referenceResourceType, Direction direction, boolean polymorphic) throws ResourceRegistryException; - - public String query(final String query, final int limit, final String fetchPlan) + + /* ---------------------------------------------------------------------- */ + + public String jsonQuery(final String query) + throws InvalidQueryException, ResourceRegistryException; + + public List jsonQuery(final JsonNode jsonNode) throws InvalidQueryException, ResourceRegistryException; - public String query(final String query, final int limit, final String fetchPlan, boolean raw) + /* ---------------------------------------------------------------------- */ + + public String rawQuery(final String query, final int limit, final String fetchPlan) throws InvalidQueryException, ResourceRegistryException; - - public List getSchema(Class clazz, Boolean polymorphic) - throws SchemaNotFoundException, ResourceRegistryException; - - public Context getContext(UUID uuid) throws ContextNotFoundException, ResourceRegistryException; - - public Context getCurrentContext() throws ContextNotFoundException, ResourceRegistryException; - - public List getAllContext() throws ResourceRegistryException; - - public Map getInstanceContexts(Class clazz, UUID uuid) - throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException; - - public Map getInstanceContexts(String type, UUID uuid) - throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException; - + + public String rawQuery(final String query, final int limit, final String fetchPlan, boolean raw) + throws InvalidQueryException, ResourceRegistryException; + } 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 ca10a8c..fbd0c4b 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientImpl.java @@ -2,11 +2,15 @@ package org.gcube.informationsystem.resourceregistry.client; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; +import org.gcube.com.fasterxml.jackson.databind.JavaType; +import org.gcube.com.fasterxml.jackson.databind.JsonNode; +import org.gcube.com.fasterxml.jackson.databind.ObjectMapper; import org.gcube.common.gxhttp.reference.GXConnection; import org.gcube.common.gxhttp.request.GXHTTPStringRequest; import org.gcube.informationsystem.base.reference.Direction; @@ -18,6 +22,7 @@ import org.gcube.informationsystem.model.reference.entities.Resource; import org.gcube.informationsystem.model.reference.relations.ConsistsOf; import org.gcube.informationsystem.model.reference.relations.IsRelatedTo; import org.gcube.informationsystem.model.reference.relations.Relation; +import org.gcube.informationsystem.queries.templates.reference.entities.QueryTemplate; import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache; import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCacheRenewal; import org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility; @@ -26,8 +31,10 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundExcep import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.queries.InvalidQueryException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.queries.templates.QueryTemplateNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaNotFoundException; import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath; +import org.gcube.informationsystem.resourceregistry.api.rest.QueryTemplatePath; import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPUtility; import org.gcube.informationsystem.resourceregistry.api.utils.Utility; import org.gcube.informationsystem.types.TypeMapper; @@ -124,7 +131,7 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { return contextCache.getContexts(); } - protected Context getContextFromServer(String id) throws ContextNotFoundException, ResourceRegistryException { + protected Context getContextFromServer(String uuid) throws ContextNotFoundException, ResourceRegistryException { try { // TODO use cache @@ -134,7 +141,7 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART); gxHTTPStringRequest.path(AccessPath.CONTEXTS_PATH_PART); - gxHTTPStringRequest.path(id); + gxHTTPStringRequest.path(uuid); HttpURLConnection httpURLConnection = gxHTTPStringRequest.get(); Context context = HTTPUtility.getResponse(Context.class, httpURLConnection); @@ -154,9 +161,42 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { } } + @Override + public boolean existContext(String uuid) throws ResourceRegistryException { + return existContext(UUID.fromString(uuid)); + } + + @Override + public boolean existContext(UUID uuid) throws ResourceRegistryException { + try { + getContext(uuid); + return true; + }catch (ContextNotFoundException e) { + return false; + } + } + + @Override + public Context getContext(String uuid) throws ContextNotFoundException, ResourceRegistryException { + return getContext(UUID.fromString(uuid)); + } + @Override public Context getContext(UUID uuid) throws ContextNotFoundException, ResourceRegistryException { - return ContextCache.getInstance().getContextByUUID(uuid); + ContextCache contextCache = ContextCache.getInstance(); + Context context = ContextCache.getInstance().getContextByUUID(uuid);; + if(context == null) { + context = getContextFromServer(AccessPath.CURRENT_CONTEXT); + contextCache.cleanCache(); + contextCache.refreshContextsIfNeeded(); + Context c = contextCache.getContextByUUID(context.getHeader().getUUID()); + if(c!=null){ + context = c; + }else { + logger.error("Context with UUID {} is {}. It is possibile to get it from the server but not from the cache. This is very strange and should not occur.", uuid, context); + } + } + return context; } @Override @@ -182,17 +222,67 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { } @Override - public List getSchema(Class clazz, Boolean polymorphic) - throws SchemaNotFoundException, ResourceRegistryException { - String type = Utility.getTypeName(clazz); + public boolean existType(Class clazz) throws ResourceRegistryException { + return existType(Utility.getTypeName(clazz)); + } + + @Override + public boolean existType(String typeName) throws ResourceRegistryException { try { - logger.info("Going to get {} schema", type); + logger.info("Going to get {} schema", typeName); GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName()); gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART); gxHTTPStringRequest.path(AccessPath.TYPES_PATH_PART); - gxHTTPStringRequest.path(type); + gxHTTPStringRequest.path(typeName); + + Map parameters = new HashMap<>(); + parameters.put(AccessPath._POLYMORPHIC_PARAM, Boolean.FALSE.toString()); + gxHTTPStringRequest.queryParams(parameters); + + HttpURLConnection httpURLConnection = gxHTTPStringRequest.head(); + HTTPUtility.getResponse(String.class, httpURLConnection); + + return true; + } catch (NotFoundException e) { + return false; + } catch(ResourceRegistryException e) { + // logger.trace("Error while getting {} schema for {}", polymorphic ? AccessPath.POLYMORPHIC_PARAM + " " : "", + // type, e); + throw e; + } catch(Exception e) { + // logger.trace("Error while getting {}schema for {}", polymorphic ? + // AccessPath.POLYMORPHIC_PARAM + " " : "", + // type, e); + throw new RuntimeException(e); + } + } + + @Override + public List getType(Class clazz, Boolean polymorphic) + throws SchemaNotFoundException, ResourceRegistryException { + try { + String json = getType(Utility.getTypeName(clazz), polymorphic); + return TypeMapper.deserializeTypeDefinitions(json); + } catch(ResourceRegistryException e) { + throw e; + } catch(Exception e) { + throw new RuntimeException(e); + } + } + + @Override + public String getType(String typeName, Boolean polymorphic) + throws SchemaNotFoundException, ResourceRegistryException { + try { + logger.info("Going to get {} schema", typeName); + GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); + gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName()); + gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); + gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART); + gxHTTPStringRequest.path(AccessPath.TYPES_PATH_PART); + gxHTTPStringRequest.path(typeName); Map parameters = new HashMap<>(); parameters.put(AccessPath._POLYMORPHIC_PARAM, polymorphic.toString()); @@ -201,8 +291,8 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { HttpURLConnection httpURLConnection = gxHTTPStringRequest.get(); String json = HTTPUtility.getResponse(String.class, httpURLConnection); - logger.debug("Got schema for {} is {}", type, json); - return TypeMapper.deserializeTypeDefinitions(json); + logger.debug("Got schema for {} is {}", typeName, json); + return json; } catch(ResourceRegistryException e) { // logger.trace("Error while getting {} schema for {}", polymorphic ? AccessPath.POLYMORPHIC_PARAM + " " : "", // type, e); @@ -259,15 +349,15 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { } @Override - public boolean exists(Class clazz, UUID uuid) - throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { + public boolean existInstance(Class clazz, UUID uuid) + throws AvailableInAnotherContextException, ResourceRegistryException { String type = Utility.getTypeName(clazz); - return exists(type, uuid); + return existInstance(type, uuid); } @Override - public boolean exists(String type, UUID uuid) - throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { + public boolean existInstance(String type, UUID uuid) + throws AvailableInAnotherContextException, ResourceRegistryException { try { logger.info("Going to check if {} with UUID {} exists", type, uuid); GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); @@ -285,6 +375,8 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { logger.debug("{} with UUID {} exists", type, uuid); return true; + } catch (NotFoundException e) { + return false; } catch(ResourceRegistryException e) { // logger.trace("Error while checking if {} with UUID {} exists.", type, uuid, // e); @@ -337,13 +429,13 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { } @Override - public String query(String query, int limit, String fetchPlan) + public String rawQuery(String query, int limit, String fetchPlan) throws InvalidQueryException, ResourceRegistryException { - return query(query, limit, fetchPlan, false); + return rawQuery(query, limit, fetchPlan, false); } @Override - public String query(String query, int limit, String fetchPlan, boolean raw) + public String rawQuery(String query, int limit, String fetchPlan, boolean raw) throws InvalidQueryException, ResourceRegistryException { try { @@ -643,5 +735,225 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { throw new RuntimeException(e); } } + + @Override + public List getAllQueryTemplates() throws ResourceRegistryException { + try { + logger.trace("Going to list {}s", QueryTemplate.NAME); + GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); + gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName()); + gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); + gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART); + gxHTTPStringRequest.path(QueryTemplatePath.QUERY_TEMPLATES_PATH_PART); + + HttpURLConnection httpURLConnection = gxHTTPStringRequest.get(); + String all = HTTPUtility.getResponse(String.class, httpURLConnection); + + logger.debug("Got {}s are {}", QueryTemplate.NAME, all); + + JavaType type = ElementMapper.getObjectMapper().getTypeFactory().constructCollectionType(ArrayList.class, QueryTemplate.class); + return ElementMapper.getObjectMapper().readValue(all, type); + + } catch(ResourceRegistryException e) { + // logger.trace("Error Creating {}", facet, e); + throw e; + } catch(Exception e) { + // logger.trace("Error Creating {}", facet, e); + throw new RuntimeException(e); + } + } + + @Override + public boolean existQueryTemplate(QueryTemplate queryTemplate) + throws QueryTemplateNotFoundException, ResourceRegistryException { + return existQueryTemplate(queryTemplate.getName()); + } + + @Override + public boolean existQueryTemplate(String queryTemplateName) + throws QueryTemplateNotFoundException, ResourceRegistryException { + try { + logger.trace("Going to read {} with name {}", QueryTemplate.NAME, queryTemplateName); + GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); + gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName()); + gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); + gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART); + gxHTTPStringRequest.path(QueryTemplatePath.QUERY_TEMPLATES_PATH_PART); + gxHTTPStringRequest.path(queryTemplateName); + + HttpURLConnection httpURLConnection = gxHTTPStringRequest.head(); + HTTPUtility.getResponse(String.class, httpURLConnection); + return true; + } catch (NotFoundException e) { + return false; + } catch(ResourceRegistryException e) { + throw e; + } catch(Exception e) { + throw new RuntimeException(e); + } + } + + @Override + public QueryTemplate readQueryTemplate(QueryTemplate queryTemplate) + throws QueryTemplateNotFoundException, ResourceRegistryException { + // TODO Auto-generated method stub + return readQueryTemplate(queryTemplate.getName()); + } + + @Override + public QueryTemplate readQueryTemplate(String queryTemplateName) + throws QueryTemplateNotFoundException, ResourceRegistryException { + try { + String queryTemplate = readQueryTemplateAsString(queryTemplateName); + return ElementMapper.unmarshal(QueryTemplate.class, queryTemplate); + } catch(ResourceRegistryException e) { + throw e; + } catch(Exception e) { + throw new RuntimeException(e); + } + } + + @Override + public String readQueryTemplateAsString(String queryTemplateName) + throws QueryTemplateNotFoundException, ResourceRegistryException { + try { + logger.trace("Going to read {} with name {}", QueryTemplate.NAME, queryTemplateName); + GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); + gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName()); + gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); + gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART); + gxHTTPStringRequest.path(QueryTemplatePath.QUERY_TEMPLATES_PATH_PART); + gxHTTPStringRequest.path(queryTemplateName); + + HttpURLConnection httpURLConnection = gxHTTPStringRequest.get(); + String c = HTTPUtility.getResponse(String.class, httpURLConnection); + + logger.debug("Got {} is {}", QueryTemplate.NAME, c); + return c; + } catch(ResourceRegistryException e) { + // logger.trace("Error Creating {}", facet, e); + throw e; + } catch(Exception e) { + // logger.trace("Error Creating {}", facet, e); + throw new RuntimeException(e); + } + } + + @Override + public String runQueryTemplateGetString(String name) + throws QueryTemplateNotFoundException, ResourceRegistryException { + return runQueryTemplate(name, ""); + } + + @Override + public List runQueryTemplate(String name) + throws QueryTemplateNotFoundException, ResourceRegistryException { + try { + String ret = runQueryTemplateGetString(name); + JavaType type = ElementMapper.getObjectMapper().getTypeFactory().constructCollectionType(ArrayList.class, Entity.class); + return ElementMapper.getObjectMapper().readValue(ret, type); + } catch(ResourceRegistryException e) { + throw e; + } catch(Exception e) { + throw new RuntimeException(e); + } + } + + @Override + public List runQueryTemplate(QueryTemplate queryTemplate) + throws QueryTemplateNotFoundException, ResourceRegistryException { + return runQueryTemplate(queryTemplate.getName()); + } + + @Override + public String runQueryTemplate(String name, String params) + throws QueryTemplateNotFoundException, ResourceRegistryException { + try { + if(params==null || params.compareTo("")==0) { + logger.trace("Going to run {} using default parameters", QueryTemplate.NAME); + params = null; + }else { + logger.trace("Going to run {} with the following parameters {}", QueryTemplate.NAME, params); + } + + GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); + gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName()); + gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); + gxHTTPStringRequest.header("Content-type", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); + gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART); + gxHTTPStringRequest.path(QueryTemplatePath.QUERY_TEMPLATES_PATH_PART); + gxHTTPStringRequest.path(name); + + HttpURLConnection httpURLConnection = gxHTTPStringRequest.post(params); + String c = HTTPUtility.getResponse(String.class, httpURLConnection); + + logger.trace("The result of the query is {}", c); + return c; + } catch(ResourceRegistryException e) { + throw e; + } catch(Exception e) { + throw new RuntimeException(e); + } + } + + @Override + public List runQueryTemplate(String name, JsonNode jsonNode) + throws QueryTemplateNotFoundException, ResourceRegistryException { + try { + ObjectMapper objectMapper = new ObjectMapper(); + String ret = runQueryTemplate(name, objectMapper.writeValueAsString(jsonNode)); + JavaType type = ElementMapper.getObjectMapper().getTypeFactory().constructCollectionType(ArrayList.class, Entity.class); + return ElementMapper.getObjectMapper().readValue(ret, type); + } catch(ResourceRegistryException e) { + throw e; + } catch(Exception e) { + throw new RuntimeException(e); + } + } + + @Override + public List runQueryTemplate(QueryTemplate queryTemplate, JsonNode jsonNode) + throws QueryTemplateNotFoundException, ResourceRegistryException { + return runQueryTemplate(queryTemplate.getName(), jsonNode); + } + + @Override + public String jsonQuery(String query) throws InvalidQueryException, ResourceRegistryException { + try { + logger.trace("Going to run the following JSON Query {}", query); + + GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); + gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName()); + gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); + gxHTTPStringRequest.header("Content-type", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); + gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART); + gxHTTPStringRequest.path(AccessPath.QUERY_PATH_PART); + + HttpURLConnection httpURLConnection = gxHTTPStringRequest.post(query); + String c = HTTPUtility.getResponse(String.class, httpURLConnection); + + logger.trace("The result of the query is {}", c); + return c; + } catch(ResourceRegistryException e) { + throw e; + } catch(Exception e) { + throw new RuntimeException(e); + } + } + + @Override + public List jsonQuery(JsonNode jsonNode) + throws InvalidQueryException, ResourceRegistryException { + try { + ObjectMapper objectMapper = new ObjectMapper(); + String ret = jsonQuery(objectMapper.writeValueAsString(jsonNode)); + JavaType type = ElementMapper.getObjectMapper().getTypeFactory().constructCollectionType(ArrayList.class, Entity.class); + return ElementMapper.getObjectMapper().readValue(ret, type); + } catch(ResourceRegistryException e) { + throw e; + } catch(Exception e) { + throw new RuntimeException(e); + } + } } 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 7654380..404324c 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientTest.java @@ -49,19 +49,19 @@ public class ResourceRegistryClientTest extends ContextTest { @Test public void testQuery() throws ResourceRegistryException { - String res = resourceRegistryClient.query("SELECT FROM V", 0, null); + String res = resourceRegistryClient.rawQuery("SELECT FROM V", 0, null); logger.trace(res); } @Test public void testGetFacetSchema() throws SchemaNotFoundException, ResourceRegistryException { - List typeDefinitions = resourceRegistryClient.getSchema(ContactFacet.class, true); + List typeDefinitions = resourceRegistryClient.getType(ContactFacet.class, true); logger.trace("{}", typeDefinitions); } @Test public void testGetResourceSchema() throws SchemaNotFoundException, ResourceRegistryException { - List typeDefinitions = resourceRegistryClient.getSchema(HostingNode.class, true); + List typeDefinitions = resourceRegistryClient.getType(HostingNode.class, true); logger.trace("{}", typeDefinitions); } @@ -71,7 +71,7 @@ public class ResourceRegistryClientTest extends ContextTest { @Test(expected = SchemaNotFoundException.class) public void testException() throws SchemaNotFoundException, ResourceRegistryException { - resourceRegistryClient.getSchema(Aux.class, true); + resourceRegistryClient.getType(Aux.class, true); } /* The following tests are commented because we need to create the instances for tests. this is done in @@ -81,13 +81,13 @@ public class ResourceRegistryClientTest extends ContextTest { // @Test public void testExists() throws ResourceRegistryException { UUID uuid = UUID.fromString("bdaccb35-7f27-45a6-8ca9-11d467cb9233"); - resourceRegistryClient.exists(EService.NAME, uuid); + resourceRegistryClient.existInstance(EService.NAME, uuid); } // @Test public void testExistsByClass() throws ResourceRegistryException { UUID uuid = UUID.fromString("bdaccb35-7f27-45a6-8ca9-11d467cb9233"); - resourceRegistryClient.exists(EService.class, uuid); + resourceRegistryClient.existInstance(EService.class, uuid); } // @Test diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientTestWikiExamples.java b/src/test/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientTestWikiExamples.java index a576a7c..b3d9a21 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientTestWikiExamples.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientTestWikiExamples.java @@ -193,13 +193,13 @@ public class ResourceRegistryClientTestWikiExamples extends ContextTest { @Test public void rawQueryExample1() throws ResourceRegistryException, Exception{ - String jsonString = resourceRegistryClient.query("SELECT FROM SoftwareFacet", 1, null, true); + String jsonString = resourceRegistryClient.rawQuery("SELECT FROM SoftwareFacet", 1, null, true); logger.debug("{}", jsonString); } @Test public void rawQueryExample2() throws ResourceRegistryException, Exception{ - String jsonString = resourceRegistryClient.query("SELECT FROM SoftwareFacet", 1, null); + String jsonString = resourceRegistryClient.rawQuery("SELECT FROM SoftwareFacet", 1, null); List list = ElementMapper.unmarshalList(SoftwareFacet.class, jsonString); logger.debug("{}", list); }