refs #5753: Expose Remove From Context API

https://support.d4science.org/issues/5753

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry-publisher@141324 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2016-12-21 15:29:37 +00:00
parent 2bab42cc87
commit 5699ce9dab
2 changed files with 141 additions and 32 deletions

View File

@ -56,4 +56,19 @@ public interface ResourceRegistryPublisher {
throws FacetNotFoundException, ContextNotFoundException, throws FacetNotFoundException, ContextNotFoundException,
ResourceRegistryException; ResourceRegistryException;
public boolean removeResourceFromContext(UUID uuid)
throws ResourceNotFoundException, ContextNotFoundException,
ResourceRegistryException;
public <R extends Resource> boolean removeResourceFromContext(R resource)
throws ResourceNotFoundException, ContextNotFoundException,
ResourceRegistryException;
public boolean removeFacetFromContext(UUID uuid) throws FacetNotFoundException,
ContextNotFoundException, ResourceRegistryException;
public <F extends Facet> boolean removeFacetFromContext(F facet)
throws FacetNotFoundException, ContextNotFoundException,
ResourceRegistryException;
} }

View File

@ -139,7 +139,6 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
return result.toString(); return result.toString();
} }
public HTTPInputs(String path, HTTPMETHOD method, public HTTPInputs(String path, HTTPMETHOD method,
List<Map.Entry<String, String>> parameters) List<Map.Entry<String, String>> parameters)
throws UnsupportedEncodingException { throws UnsupportedEncodingException {
@ -235,8 +234,10 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
connection.setRequestMethod(method.toString()); connection.setRequestMethod(method.toString());
String body = httpInputs.getBody(); String body = httpInputs.getBody();
if (body!=null && (method == HTTPMETHOD.POST || method == HTTPMETHOD.PUT)) { if (body != null
DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); && (method == HTTPMETHOD.POST || method == HTTPMETHOD.PUT)) {
DataOutputStream wr = new DataOutputStream(
connection.getOutputStream());
wr.writeBytes(body); wr.writeBytes(body);
wr.flush(); wr.flush();
wr.close(); wr.close();
@ -322,7 +323,6 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
stringWriter.append(PATH_SEPARATOR); stringWriter.append(PATH_SEPARATOR);
stringWriter.append(facet.getHeader().getUUID().toString()); stringWriter.append(facet.getHeader().getUUID().toString());
String body = Entities.marshal(facet); String body = Entities.marshal(facet);
HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(),
HTTPMETHOD.POST, null, body); HTTPMETHOD.POST, null, body);
@ -398,7 +398,8 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
} }
@Override @Override
public <R extends Resource> R updateResource(Class<R> resourceClass, R resource) { public <R extends Resource> R updateResource(Class<R> resourceClass,
R resource) {
try { try {
logger.info("Going to update: {}", resource); logger.info("Going to update: {}", resource);
StringWriter stringWriter = new StringWriter(); StringWriter stringWriter = new StringWriter();
@ -612,9 +613,11 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
stringWriter.append(PATH_SEPARATOR); stringWriter.append(PATH_SEPARATOR);
stringWriter.append(uuid.toString()); stringWriter.append(uuid.toString());
HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.POST, null); HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(),
HTTPMETHOD.POST, null);
ResourceRegistryCall<Boolean> call = new ResourceRegistryCall<>(Boolean.class, httpInputs); ResourceRegistryCall<Boolean> call = new ResourceRegistryCall<>(
Boolean.class, httpInputs);
boolean added = delegate.make(call); boolean added = delegate.make(call);
logger.info("{} with UUID {} was {} added to current {} : {}", logger.info("{} with UUID {} was {} added to current {} : {}",
@ -632,7 +635,7 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
public <R extends Resource> boolean addResourceToContext(R resource) public <R extends Resource> boolean addResourceToContext(R resource)
throws ResourceNotFoundException, ContextNotFoundException, throws ResourceNotFoundException, ContextNotFoundException,
ResourceRegistryException { ResourceRegistryException {
return addFacetToContext(resource.getHeader().getUUID()); return addResourceToContext(resource.getHeader().getUUID());
} }
@Override @Override
@ -653,9 +656,11 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
stringWriter.append(PATH_SEPARATOR); stringWriter.append(PATH_SEPARATOR);
stringWriter.append(uuid.toString()); stringWriter.append(uuid.toString());
HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.POST, null); HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(),
HTTPMETHOD.POST, null);
ResourceRegistryCall<Boolean> call = new ResourceRegistryCall<>(Boolean.class, httpInputs); ResourceRegistryCall<Boolean> call = new ResourceRegistryCall<>(
Boolean.class, httpInputs);
boolean added = delegate.make(call); boolean added = delegate.make(call);
logger.info("{} with UUID {} was {} added to current {} : {}", logger.info("{} with UUID {} was {} added to current {} : {}",
@ -676,4 +681,93 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
return addFacetToContext(facet.getHeader().getUUID()); 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<Boolean> 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 <R extends Resource> 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<Boolean> 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 <F extends Facet> boolean removeFacetFromContext(F facet)
throws FacetNotFoundException, ContextNotFoundException,
ResourceRegistryException {
return removeFacetFromContext(facet.getHeader().getUUID());
}
} }