From fc020c3f9bc75ff60b269e47b9cd7582d3c1d32d Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 6 Apr 2017 13:05:14 +0000 Subject: [PATCH] Added exceptions git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry-client@146645 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../client/ResourceRegistryClient.java | 5 +- .../client/ResourceRegistryClientImpl.java | 73 ++++++++----------- 2 files changed, 34 insertions(+), 44 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 0a3b8f7..404bd48 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClient.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClient.java @@ -8,6 +8,7 @@ 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.ResourceRegistryException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERAvailableInAnotherContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.query.InvalidQueryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException; @@ -20,11 +21,11 @@ public interface ResourceRegistryClient { public void exists( Class clazz, UUID uuid) throws ERNotFoundException, - ResourceRegistryException; + ERAvailableInAnotherContextException, ResourceRegistryException; public ERType getInstance( Class clazz, UUID uuid) throws ERNotFoundException, - ResourceRegistryException; + ERAvailableInAnotherContextException, ResourceRegistryException; public List getInstances( String type, Boolean polymorphic) throws 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 d01cce2..f5d7119 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientImpl.java @@ -16,6 +16,7 @@ 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.ResourceRegistryException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERAvailableInAnotherContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.query.InvalidQueryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException; @@ -32,29 +33,28 @@ import org.slf4j.LoggerFactory; */ public class ResourceRegistryClientImpl implements ResourceRegistryClient { - private static final Logger logger = LoggerFactory - .getLogger(ResourceRegistryClientImpl.class); + private static final Logger logger = LoggerFactory.getLogger(ResourceRegistryClientImpl.class); public static final String PATH_SEPARATOR = "/"; - + protected final String address; - protected HTTPCall httpCall; - + protected HTTPCall httpCall; + public ResourceRegistryClientImpl(String address) { this.address = address; - + } private HTTPCall getHTTPCall() throws MalformedURLException { - if(httpCall==null){ + if (httpCall == null) { httpCall = new HTTPCall(address, ResourceRegistryClient.class.getSimpleName()); } return httpCall; } - + @Override public void exists(Class clazz, UUID uuid) - throws ERNotFoundException, ResourceRegistryException { + throws ERNotFoundException, ERAvailableInAnotherContextException, ResourceRegistryException { String type = clazz.getSimpleName(); try { logger.info("Going to check if {} with UUID {} exists", type, uuid); @@ -70,7 +70,7 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { HTTPCall httpCall = getHTTPCall(); httpCall.call(clazz, stringWriter.toString(), HTTPMETHOD.HEAD); - + logger.debug("{} with UUID {} exists", type, uuid); } catch (ResourceRegistryException e) { throw e; @@ -78,10 +78,10 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { throw new RuntimeException(e); } } - + @Override public ERType getInstance(Class clazz, UUID uuid) - throws ERNotFoundException, ResourceRegistryException { + throws ERNotFoundException, ERAvailableInAnotherContextException, ResourceRegistryException { String type = clazz.getSimpleName(); try { logger.info("Going to get {} with UUID {}", type, uuid); @@ -96,8 +96,7 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { stringWriter.append(uuid.toString()); HTTPCall httpCall = getHTTPCall(); - ERType erType = httpCall.call(clazz, stringWriter.toString(), - HTTPMETHOD.GET); + ERType erType = httpCall.call(clazz, stringWriter.toString(), HTTPMETHOD.GET); logger.debug("Got {} with UUID {} is {}", type, uuid, erType); return erType; @@ -111,8 +110,7 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { } @Override - public List getInstances(String type, - Boolean polymorphic) throws ResourceRegistryException { + public List getInstances(String type, Boolean polymorphic) throws ResourceRegistryException { try { logger.info("Going to get all instances of {} ", type); StringWriter stringWriter = new StringWriter(); @@ -125,15 +123,14 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { Map parameters = new HashMap<>(); parameters.put(AccessPath.POLYMORPHIC_PARAM, polymorphic.toString()); - + HTTPCall httpCall = getHTTPCall(); - String ret = httpCall.call(String.class, stringWriter.toString(), - HTTPMETHOD.GET, parameters); + String ret = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.GET, parameters); logger.debug("Got instances of {} are {}", type, ret); return ISMapper.unmarshalList(Entity.class, ret); - + } catch (ResourceRegistryException e) { logger.error("Error while getting {} instances", type, e); throw e; @@ -144,12 +141,10 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { } @Override - public List getInstancesFromEntity( - String relationType, Boolean polymorphic, UUID reference, + public List getInstancesFromEntity(String relationType, Boolean polymorphic, UUID reference, Direction direction) throws ResourceRegistryException { try { - logger.info("Going to get all instances of {} from/to {}", relationType, - reference.toString()); + logger.info("Going to get all instances of {} from/to {}", relationType, reference.toString()); StringWriter stringWriter = new StringWriter(); stringWriter.append(PATH_SEPARATOR); stringWriter.append(AccessPath.ACCESS_PATH_PART); @@ -163,15 +158,13 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { parameters.put(AccessPath.REFERENCE, reference.toString()); parameters.put(AccessPath.DIRECTION, direction.toString()); - HTTPCall httpCall = getHTTPCall(); String ret = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.GET, parameters); - logger.debug("Got instances of {} from/to {} are {}", relationType, - reference.toString(), ret); + logger.debug("Got instances of {} from/to {} are {}", relationType, reference.toString(), ret); return ISMapper.unmarshalList(Resource.class, ret); - + } catch (ResourceRegistryException e) { logger.error("Error while getting instances of {} from/to {}", relationType, e); throw e; @@ -182,8 +175,7 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { } @Override - public List getSchema( - Class clazz, Boolean polymorphic) + public List getSchema(Class clazz, Boolean polymorphic) throws SchemaNotFoundException, ResourceRegistryException { String type = clazz.getSimpleName(); @@ -200,22 +192,19 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { Map parameters = new HashMap<>(); parameters.put(AccessPath.POLYMORPHIC_PARAM, polymorphic.toString()); - HTTPCall httpCall = getHTTPCall(); String schema = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.GET, parameters); logger.debug("Got schema for {} is {}", type, schema); return TypeBinder.deserializeTypeDefinitions(schema); - + } catch (ResourceRegistryException e) { - logger.error("Error while getting {}schema for {}", - polymorphic ? AccessPath.POLYMORPHIC_PARAM + " " : "", + logger.error("Error while getting {}schema for {}", polymorphic ? AccessPath.POLYMORPHIC_PARAM + " " : "", type, e); throw e; } catch (Exception e) { - logger.error("Error while getting {}schema for {}", - polymorphic ? AccessPath.POLYMORPHIC_PARAM + " " : "", + logger.error("Error while getting {}schema for {}", polymorphic ? AccessPath.POLYMORPHIC_PARAM + " " : "", type, e); throw new RuntimeException(e); } @@ -224,31 +213,31 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { @Override public String query(String query, int limit, String fetchPlan) throws InvalidQueryException, ResourceRegistryException { - + try { logger.debug("Going to query. {}", query); StringWriter stringWriter = new StringWriter(); stringWriter.append(PATH_SEPARATOR); stringWriter.append(AccessPath.ACCESS_PATH_PART); - + Map parameters = new HashMap<>(); parameters.put(AccessPath.QUERY_PARAM, query); - if(limit<=0){ + if (limit <= 0) { limit = AccessPath.DEFAULT_LIMIT; } parameters.put(AccessPath.LIMIT_PARAM, Integer.toString(limit)); - + if (fetchPlan != null) { parameters.put(AccessPath.FETCH_PLAN_PARAM, fetchPlan); } - + HTTPCall httpCall = getHTTPCall(); String ret = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.GET, parameters); logger.debug("Query result is {}", ret); return ret; - + } catch (ResourceRegistryException e) { logger.error("Error while querying", e); throw e;