Added missing APIs and aligned API names

This commit is contained in:
Luca Frosini 2022-02-09 13:20:48 +01:00
parent 8ead38f561
commit 4d2f87e83c
4 changed files with 460 additions and 81 deletions

View File

@ -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<Context> 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 <ERElem extends ERElement> boolean existType(Class<ERElem> clazz) throws ResourceRegistryException;
public String getType(String typeName, Boolean polymorphic)
throws SchemaNotFoundException, ResourceRegistryException;
public <ERElem extends ERElement> List<Type> getType(Class<ERElem> clazz, Boolean polymorphic)
throws SchemaNotFoundException, ResourceRegistryException;
/* ---------------------------------------------------------------------- */
public <ERElem extends ERElement> List<ERElem> getInstances(Class<ERElem> clazz, Boolean polymorphic)
throws ResourceRegistryException;
public String getInstances(String type, Boolean polymorphic) throws ResourceRegistryException;
public <ERElem extends ERElement> boolean exists(Class<ERElem> clazz, UUID uuid)
throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException;
public boolean exists(String type, UUID uuid)
throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException;
public <ERElem extends ERElement> boolean existInstance(Class<ERElem> clazz, UUID uuid)
throws AvailableInAnotherContextException, ResourceRegistryException;
public boolean existInstance(String type, UUID uuid)
throws AvailableInAnotherContextException, ResourceRegistryException;
public <ERElem extends ERElement> ERElem getInstance(Class<ERElem> clazz, UUID uuid)
throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException;
public String getInstance(String type, UUID uuid)
throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException;
public <R extends Resource, C extends ConsistsOf<?,?>, F extends Facet> List<R> getResourcesFromReferenceFacet(
Class<R> resourceClass, Class<C> consistsOfClass, F referenceFacet,
boolean polymorphic) throws ResourceRegistryException;
public <R extends Resource, C extends ConsistsOf<?,?>, F extends Facet> List<R> getResourcesFromReferenceFacet(
/* ---------------------------------------------------------------------- */
public <ERElem extends ERElement> Map<UUID, String> getInstanceContexts(Class<ERElem> clazz, UUID uuid)
throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException;
public Map<UUID, String> getInstanceContexts(String type, UUID uuid)
throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException;
/* ---------------------------------------------------------------------- */
public List<QueryTemplate> 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 <E extends Entity> List<E> runQueryTemplate(String name)
throws QueryTemplateNotFoundException, ResourceRegistryException;
public <E extends Entity> List<E> runQueryTemplate(QueryTemplate queryTemplate)
throws QueryTemplateNotFoundException, ResourceRegistryException;
public String runQueryTemplate(String name, String params)
throws QueryTemplateNotFoundException, ResourceRegistryException;
public <E extends Entity> List<E> runQueryTemplate(String name, JsonNode jsonNode)
throws QueryTemplateNotFoundException, ResourceRegistryException;
public <E extends Entity> List<E> runQueryTemplate(QueryTemplate queryTemplate, JsonNode jsonNode)
throws QueryTemplateNotFoundException, ResourceRegistryException;
/* ---------------------------------------------------------------------- */
public <R extends Resource, C extends ConsistsOf<?, ?>, F extends Facet> List<R> getResourcesFromReferenceFacet(
Class<R> resourceClass, Class<C> consistsOfClass, F referenceFacet, boolean polymorphic)
throws ResourceRegistryException;
public <R extends Resource, C extends ConsistsOf<?, ?>, F extends Facet> List<R> getResourcesFromReferenceFacet(
Class<R> resourceClass, Class<C> consistsOfClass, Class<F> facetClass, UUID referenceFacetUUID,
boolean polymorphic) throws ResourceRegistryException;
public String getResourcesFromReferenceFacet(String resourceType, String consistsOfType, String facetType,
UUID referenceFacetUUID, boolean polymorphic) throws ResourceRegistryException;
public <R extends Resource, C extends ConsistsOf<?,?>, F extends Facet> List<R> getFilteredResources(
public <R extends Resource, C extends ConsistsOf<?, ?>, F extends Facet> List<R> getFilteredResources(
Class<R> resourceClass, Class<C> consistsOfClass, Class<F> facetClass, boolean polymorphic,
Map<String,String> facetConstraint) throws ResourceRegistryException;
Map<String, String> facetConstraint) throws ResourceRegistryException;
public String getFilteredResources(String resourceType, String consistsOfType, String facetType,
boolean polymorphic, Map<String,String> facetConstraint) throws ResourceRegistryException;
public <R extends Resource, I extends IsRelatedTo<?,?>, RR extends Resource> List<R> getRelatedResourcesFromReferenceResource(
Class<R> resourceClass, Class<I> isRelatedToClass, RR referenceResource,
Direction direction, boolean polymorphic) throws ResourceRegistryException;
public <R extends Resource, I extends IsRelatedTo<?,?>, RR extends Resource> List<R> getRelatedResourcesFromReferenceResource(
Class<R> resourceClass, Class<I> isRelatedToClass, Class<RR> referenceResourceClass, UUID referenceResourceUUID,
Direction direction, boolean polymorphic) throws ResourceRegistryException;
boolean polymorphic, Map<String, String> facetConstraint) throws ResourceRegistryException;
public <R extends Resource, I extends IsRelatedTo<?, ?>, RR extends Resource> List<R> getRelatedResourcesFromReferenceResource(
Class<R> resourceClass, Class<I> isRelatedToClass, RR referenceResource, Direction direction,
boolean polymorphic) throws ResourceRegistryException;
public <R extends Resource, I extends IsRelatedTo<?, ?>, RR extends Resource> List<R> getRelatedResourcesFromReferenceResource(
Class<R> resourceClass, Class<I> isRelatedToClass, Class<RR> 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 <R extends Resource, I extends IsRelatedTo<?,?>, RR extends Resource> List<R> getRelatedResources(
public <R extends Resource, I extends IsRelatedTo<?, ?>, RR extends Resource> List<R> getRelatedResources(
Class<R> resourceClass, Class<I> isRelatedToClass, Class<RR> 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 <E extends Entity> List<E> 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 <ERElem extends ERElement> List<Type> getSchema(Class<ERElem> clazz, Boolean polymorphic)
throws SchemaNotFoundException, ResourceRegistryException;
public Context getContext(UUID uuid) throws ContextNotFoundException, ResourceRegistryException;
public Context getCurrentContext() throws ContextNotFoundException, ResourceRegistryException;
public List<Context> getAllContext() throws ResourceRegistryException;
public <ERElem extends ERElement> Map<UUID, String> getInstanceContexts(Class<ERElem> clazz, UUID uuid)
throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException;
public Map<UUID, String> 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;
}

View File

@ -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 <ERElem extends ERElement> List<Type> getSchema(Class<ERElem> clazz, Boolean polymorphic)
throws SchemaNotFoundException, ResourceRegistryException {
String type = Utility.getTypeName(clazz);
public <ERElem extends ERElement> boolean existType(Class<ERElem> 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<String,String> 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 <ERElem extends ERElement> List<Type> getType(Class<ERElem> 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<String,String> 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 <ERElem extends ERElement> boolean exists(Class<ERElem> clazz, UUID uuid)
throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
public <ERElem extends ERElement> boolean existInstance(Class<ERElem> 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<QueryTemplate> 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 <E extends Entity> List<E> 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 <E extends Entity> List<E> 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 <E extends Entity> List<E> 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 <E extends Entity> List<E> 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 <E extends Entity> List<E> 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);
}
}
}

View File

@ -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<Type> typeDefinitions = resourceRegistryClient.getSchema(ContactFacet.class, true);
List<Type> typeDefinitions = resourceRegistryClient.getType(ContactFacet.class, true);
logger.trace("{}", typeDefinitions);
}
@Test
public void testGetResourceSchema() throws SchemaNotFoundException, ResourceRegistryException {
List<Type> typeDefinitions = resourceRegistryClient.getSchema(HostingNode.class, true);
List<Type> 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

View File

@ -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<SoftwareFacet> list = ElementMapper.unmarshalList(SoftwareFacet.class, jsonString);
logger.debug("{}", list);
}