diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/publisher/proxy/ResourceRegistryPublisher.java b/src/main/java/org/gcube/informationsystem/resourceregistry/publisher/proxy/ResourceRegistryPublisher.java index 9f66e44..66bbae0 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/publisher/proxy/ResourceRegistryPublisher.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/publisher/proxy/ResourceRegistryPublisher.java @@ -56,4 +56,19 @@ public interface ResourceRegistryPublisher { throws FacetNotFoundException, ContextNotFoundException, ResourceRegistryException; + + public boolean removeResourceFromContext(UUID uuid) + throws ResourceNotFoundException, ContextNotFoundException, + ResourceRegistryException; + + public boolean removeResourceFromContext(R resource) + throws ResourceNotFoundException, ContextNotFoundException, + ResourceRegistryException; + + public boolean removeFacetFromContext(UUID uuid) throws FacetNotFoundException, + ContextNotFoundException, ResourceRegistryException; + + public boolean removeFacetFromContext(F facet) + throws FacetNotFoundException, ContextNotFoundException, + ResourceRegistryException; } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/publisher/proxy/ResourceRegistryPublisherImpl.java b/src/main/java/org/gcube/informationsystem/resourceregistry/publisher/proxy/ResourceRegistryPublisherImpl.java index 6f6a5f9..ae5e884 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/publisher/proxy/ResourceRegistryPublisherImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/publisher/proxy/ResourceRegistryPublisherImpl.java @@ -138,14 +138,13 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher return result.toString(); } - - + public HTTPInputs(String path, HTTPMETHOD method, List> parameters) throws UnsupportedEncodingException { this(path, method, parameters, null); } - + /** * @param path * @param method @@ -182,8 +181,8 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher public String getUrlParameters() { return this.urlParameters; } - - public String getBody(){ + + public String getBody() { return this.body; } @@ -208,8 +207,8 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher protected HttpURLConnection getConnection(URL url, HTTPMETHOD method) throws Exception { - - if(httpInputs.getUrlParameters()!=null){ + + if (httpInputs.getUrlParameters() != null) { url = new URL(url + "?" + httpInputs.getUrlParameters()); } @@ -235,8 +234,10 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher connection.setRequestMethod(method.toString()); String body = httpInputs.getBody(); - if (body!=null && (method == HTTPMETHOD.POST || method == HTTPMETHOD.PUT)) { - DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); + if (body != null + && (method == HTTPMETHOD.POST || method == HTTPMETHOD.PUT)) { + DataOutputStream wr = new DataOutputStream( + connection.getOutputStream()); wr.writeBytes(body); wr.flush(); wr.close(); @@ -322,7 +323,6 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher stringWriter.append(PATH_SEPARATOR); stringWriter.append(facet.getHeader().getUUID().toString()); - String body = Entities.marshal(facet); HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.POST, null, body); @@ -396,9 +396,10 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher throw new ServiceException(e); } } - + @Override - public R updateResource(Class resourceClass, R resource) { + public R updateResource(Class resourceClass, + R resource) { try { logger.info("Going to update: {}", resource); StringWriter stringWriter = new StringWriter(); @@ -476,7 +477,7 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher .toString()); stringWriter.append(PATH_SEPARATOR); stringWriter.append(consistsOfClass.getSimpleName()); - + String body = Entities.marshal(consistsOf); HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), @@ -599,9 +600,9 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher ResourceRegistryException { String context = getCurrentContext(); try { - logger.info("Going to add {} with UUID {} to current {} : {}", + logger.info("Going to add {} with UUID {} to current {} : {}", Resource.NAME, uuid, Context.NAME, context); - + StringWriter stringWriter = new StringWriter(); stringWriter.append(PATH_SEPARATOR); stringWriter.append(EntityPath.ENTITY_PATH_PART); @@ -612,17 +613,19 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher stringWriter.append(PATH_SEPARATOR); stringWriter.append(uuid.toString()); - HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.POST, null); - - ResourceRegistryCall call = new ResourceRegistryCall<>(Boolean.class, httpInputs); - + HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), + HTTPMETHOD.POST, null); + + ResourceRegistryCall call = new ResourceRegistryCall<>( + Boolean.class, httpInputs); + boolean added = delegate.make(call); logger.info("{} with UUID {} was {} added to current {} : {}", - Resource.NAME, uuid, added ? "successfully": "NOT", - Context.NAME, context); + Resource.NAME, uuid, added ? "successfully" : "NOT", + Context.NAME, context); return added; } catch (Exception e) { - logger.error("Error Adding {} with UUID {} to current {} : {}", + logger.error("Error Adding {} with UUID {} to current {} : {}", Resource.NAME, uuid, Context.NAME, context, e); throw new ServiceException(e); } @@ -632,7 +635,7 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher public boolean addResourceToContext(R resource) throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { - return addFacetToContext(resource.getHeader().getUUID()); + return addResourceToContext(resource.getHeader().getUUID()); } @Override @@ -640,9 +643,9 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher ContextNotFoundException, ResourceRegistryException { String context = getCurrentContext(); try { - logger.info("Going to add {} with UUID {} to current {} : {}", + logger.info("Going to add {} with UUID {} to current {} : {}", Facet.NAME, uuid, Context.NAME, context); - + StringWriter stringWriter = new StringWriter(); stringWriter.append(PATH_SEPARATOR); stringWriter.append(EntityPath.ENTITY_PATH_PART); @@ -653,17 +656,19 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher stringWriter.append(PATH_SEPARATOR); stringWriter.append(uuid.toString()); - HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.POST, null); - - ResourceRegistryCall call = new ResourceRegistryCall<>(Boolean.class, httpInputs); - + HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), + HTTPMETHOD.POST, null); + + ResourceRegistryCall call = new ResourceRegistryCall<>( + Boolean.class, httpInputs); + boolean added = delegate.make(call); logger.info("{} with UUID {} was {} added to current {} : {}", - Facet.NAME, uuid, added ? "successfully": "NOT", - Context.NAME, context); + Facet.NAME, uuid, added ? "successfully" : "NOT", + Context.NAME, context); return added; } catch (Exception e) { - logger.error("Error Adding {} with UUID {} to current {} : {}", + logger.error("Error Adding {} with UUID {} to current {} : {}", Facet.NAME, uuid, Context.NAME, context, e); throw new ServiceException(e); } @@ -676,4 +681,93 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher return addFacetToContext(facet.getHeader().getUUID()); } + /* -------------------------------------------------------------------- */ + @Override + public boolean removeResourceFromContext(UUID uuid) + throws ResourceNotFoundException, ContextNotFoundException, + ResourceRegistryException { + String context = getCurrentContext(); + try { + logger.info("Going to add {} with UUID {} to current {} : {}", + Resource.NAME, uuid, Context.NAME, context); + + StringWriter stringWriter = new StringWriter(); + stringWriter.append(PATH_SEPARATOR); + stringWriter.append(EntityPath.ENTITY_PATH_PART); + stringWriter.append(PATH_SEPARATOR); + stringWriter.append(EntityPath.REMOVE_PATH_PART); + stringWriter.append(PATH_SEPARATOR); + stringWriter.append(EntityPath.RESOURCE_PATH_PART); + stringWriter.append(PATH_SEPARATOR); + stringWriter.append(uuid.toString()); + + HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), + HTTPMETHOD.POST, null); + + ResourceRegistryCall call = new ResourceRegistryCall<>( + Boolean.class, httpInputs); + + boolean removed = delegate.make(call); + logger.info("{} with UUID {} was {} removed from current {} : {}", + Resource.NAME, uuid, removed ? "successfully" : "NOT", + Context.NAME, context); + return removed; + } catch (Exception e) { + logger.error("Error Adding {} with UUID {} to current {} : {}", + Resource.NAME, uuid, Context.NAME, context, e); + throw new ServiceException(e); + } + } + + @Override + public boolean removeResourceFromContext(R resource) + throws ResourceNotFoundException, ContextNotFoundException, + ResourceRegistryException { + return removeResourceFromContext(resource.getHeader().getUUID()); + } + + @Override + public boolean removeFacetFromContext(UUID uuid) + throws FacetNotFoundException, ContextNotFoundException, + ResourceRegistryException { + String context = getCurrentContext(); + try { + logger.info("Going to add {} with UUID {} to current {} : {}", + Facet.NAME, uuid, Context.NAME, context); + + StringWriter stringWriter = new StringWriter(); + stringWriter.append(PATH_SEPARATOR); + stringWriter.append(EntityPath.ENTITY_PATH_PART); + stringWriter.append(PATH_SEPARATOR); + stringWriter.append(EntityPath.REMOVE_PATH_PART); + stringWriter.append(PATH_SEPARATOR); + stringWriter.append(EntityPath.FACET_PATH_PART); + stringWriter.append(PATH_SEPARATOR); + stringWriter.append(uuid.toString()); + + HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), + HTTPMETHOD.POST, null); + + ResourceRegistryCall call = new ResourceRegistryCall<>( + Boolean.class, httpInputs); + + boolean removed = delegate.make(call); + logger.info("{} with UUID {} was {} removed from current {} : {}", + Facet.NAME, uuid, removed ? "successfully" : "NOT", + Context.NAME, context); + return removed; + } catch (Exception e) { + logger.error("Error Adding {} with UUID {} to current {} : {}", + Facet.NAME, uuid, Context.NAME, context, e); + throw new ServiceException(e); + } + } + + @Override + public boolean removeFacetFromContext(F facet) + throws FacetNotFoundException, ContextNotFoundException, + ResourceRegistryException { + return removeFacetFromContext(facet.getHeader().getUUID()); + } + }