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

@ -138,14 +138,13 @@ 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 {
this(path, method, parameters, null); this(path, method, parameters, null);
} }
/** /**
* @param path * @param path
* @param method * @param method
@ -182,8 +181,8 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
public String getUrlParameters() { public String getUrlParameters() {
return this.urlParameters; return this.urlParameters;
} }
public String getBody(){ public String getBody() {
return this.body; return this.body;
} }
@ -208,8 +207,8 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
protected HttpURLConnection getConnection(URL url, HTTPMETHOD method) protected HttpURLConnection getConnection(URL url, HTTPMETHOD method)
throws Exception { throws Exception {
if(httpInputs.getUrlParameters()!=null){ if (httpInputs.getUrlParameters() != null) {
url = new URL(url + "?" + httpInputs.getUrlParameters()); url = new URL(url + "?" + httpInputs.getUrlParameters());
} }
@ -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);
@ -396,9 +396,10 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
throw new ServiceException(e); throw new ServiceException(e);
} }
} }
@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();
@ -476,7 +477,7 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
.toString()); .toString());
stringWriter.append(PATH_SEPARATOR); stringWriter.append(PATH_SEPARATOR);
stringWriter.append(consistsOfClass.getSimpleName()); stringWriter.append(consistsOfClass.getSimpleName());
String body = Entities.marshal(consistsOf); String body = Entities.marshal(consistsOf);
HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(),
@ -599,9 +600,9 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
ResourceRegistryException { ResourceRegistryException {
String context = getCurrentContext(); String context = getCurrentContext();
try { 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); Resource.NAME, uuid, Context.NAME, context);
StringWriter stringWriter = new StringWriter(); StringWriter stringWriter = new StringWriter();
stringWriter.append(PATH_SEPARATOR); stringWriter.append(PATH_SEPARATOR);
stringWriter.append(EntityPath.ENTITY_PATH_PART); stringWriter.append(EntityPath.ENTITY_PATH_PART);
@ -612,17 +613,19 @@ 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 {} : {}",
Resource.NAME, uuid, added ? "successfully": "NOT", Resource.NAME, uuid, added ? "successfully" : "NOT",
Context.NAME, context); Context.NAME, context);
return added; return added;
} catch (Exception e) { } 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); Resource.NAME, uuid, Context.NAME, context, e);
throw new ServiceException(e); throw new ServiceException(e);
} }
@ -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
@ -640,9 +643,9 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
ContextNotFoundException, ResourceRegistryException { ContextNotFoundException, ResourceRegistryException {
String context = getCurrentContext(); String context = getCurrentContext();
try { 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); Facet.NAME, uuid, Context.NAME, context);
StringWriter stringWriter = new StringWriter(); StringWriter stringWriter = new StringWriter();
stringWriter.append(PATH_SEPARATOR); stringWriter.append(PATH_SEPARATOR);
stringWriter.append(EntityPath.ENTITY_PATH_PART); stringWriter.append(EntityPath.ENTITY_PATH_PART);
@ -653,17 +656,19 @@ 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 {} : {}",
Facet.NAME, uuid, added ? "successfully": "NOT", Facet.NAME, uuid, added ? "successfully" : "NOT",
Context.NAME, context); Context.NAME, context);
return added; return added;
} catch (Exception e) { } 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); Facet.NAME, uuid, Context.NAME, context, e);
throw new ServiceException(e); throw new ServiceException(e);
} }
@ -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());
}
} }