Refs #11288: Made resource-registry more RESTful

Task-Url: https://support.d4science.org/issues/11288

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@169087 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2018-06-12 16:09:36 +00:00
parent f25aa078de
commit 8b40881f77
2 changed files with 82 additions and 64 deletions

View File

@ -165,14 +165,15 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
} }
public void setElementType(String erType) throws ResourceRegistryException { public void setElementType(String erType) throws ResourceRegistryException {
if(this.elementType==null) { if(this.elementType == null) {
if(erType == null || erType.compareTo("") == 0) { if(erType == null || erType.compareTo("") == 0) {
erType = accessType.getName(); erType = accessType.getName();
} }
this.elementType = erType; this.elementType = erType;
}else { } else {
if(elementType.compareTo(erType)!=0) { if(elementType.compareTo(erType) != 0) {
throw new ResourceRegistryException("Provided type " + erType + " does not match with the one already known " + this.accessType); throw new ResourceRegistryException(
"Provided type " + erType + " does not match with the one already known " + this.accessType);
} }
} }
@ -185,7 +186,6 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
return elementType; return elementType;
} }
protected void checkJSON() throws ResourceRegistryException { protected void checkJSON() throws ResourceRegistryException {
if(uuid == null) { if(uuid == null) {
try { try {
@ -307,9 +307,11 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
return reallyDelete(); return reallyDelete();
} }
protected abstract boolean reallyAddToContext(SecurityContext targetSecurityContext) throws ContextException, ResourceRegistryException; protected abstract boolean reallyAddToContext(SecurityContext targetSecurityContext)
throws ContextException, ResourceRegistryException;
public boolean internalAddToContext(SecurityContext targetSecurityContext) throws ContextException, ResourceRegistryException { public boolean internalAddToContext(SecurityContext targetSecurityContext)
throws ContextException, ResourceRegistryException {
try { try {
boolean ret = reallyAddToContext(targetSecurityContext); boolean ret = reallyAddToContext(targetSecurityContext);
HeaderUtility.updateModifiedByAndLastUpdate(element); HeaderUtility.updateModifiedByAndLastUpdate(element);
@ -318,13 +320,16 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
} catch(ResourceRegistryException e) { } catch(ResourceRegistryException e) {
throw e; throw e;
} catch(Exception e) { } catch(Exception e) {
throw new ResourceRegistryException("Error Adding " + elementType + " to " + targetSecurityContext.toString(), e.getCause()); throw new ResourceRegistryException(
"Error Adding " + elementType + " to " + targetSecurityContext.toString(), e.getCause());
} }
} }
protected abstract boolean reallyRemoveFromContext(SecurityContext targetSecurityContext) throws ContextException, ResourceRegistryException; protected abstract boolean reallyRemoveFromContext(SecurityContext targetSecurityContext)
throws ContextException, ResourceRegistryException;
public boolean internalRemoveFromContext(SecurityContext targetSecurityContext) throws ContextException, ResourceRegistryException { public boolean internalRemoveFromContext(SecurityContext targetSecurityContext)
throws ContextException, ResourceRegistryException {
try { try {
boolean ret = reallyRemoveFromContext(targetSecurityContext); boolean ret = reallyRemoveFromContext(targetSecurityContext);
HeaderUtility.updateModifiedByAndLastUpdate(element); HeaderUtility.updateModifiedByAndLastUpdate(element);
@ -333,7 +338,8 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
} catch(ResourceRegistryException e) { } catch(ResourceRegistryException e) {
throw e; throw e;
} catch(Exception e) { } catch(Exception e) {
throw new ResourceRegistryException("Error Removing " + elementType + " from " + targetSecurityContext.toString(), e.getCause()); throw new ResourceRegistryException(
"Error Removing " + elementType + " from " + targetSecurityContext.toString(), e.getCause());
} }
} }
@ -400,7 +406,8 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
public El retrieveElementFromAnyContext() throws NotFoundException, ResourceRegistryException { public El retrieveElementFromAnyContext() throws NotFoundException, ResourceRegistryException {
try { try {
return Utility.getElementByUUIDAsAdmin(elementType == null ? accessType.getName() : elementType, uuid, elementClass); return Utility.getElementByUUIDAsAdmin(elementType == null ? accessType.getName() : elementType, uuid,
elementClass);
} catch(NotFoundException e) { } catch(NotFoundException e) {
throw getSpecificElementNotFoundException(e); throw getSpecificElementNotFoundException(e);
} catch(ResourceRegistryException e) { } catch(ResourceRegistryException e) {
@ -429,23 +436,9 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
} }
} }
public boolean exists() public boolean exists() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
return exists(false);
}
public boolean exists(boolean writer)
throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
try { try {
if(writer) { orientGraph = getWorkingContext().getGraph(PermissionMode.READER);
// Exists is used to understand if it is a create or update.
// In any case the we need writing right on this instance
orientGraph = getWorkingContext().getGraph(PermissionMode.WRITER);
orientGraph.setAutoStartTx(false);
orientGraph.begin();
}else {
orientGraph = getWorkingContext().getGraph(PermissionMode.READER);
}
getElement(); getElement();
@ -463,18 +456,58 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
} }
} }
public String create() throws AlreadyPresentException, ResourceRegistryException { public String createOrUpdate() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
return create(false); try {
orientGraph = getWorkingContext().getGraph(PermissionMode.WRITER);
orientGraph.setAutoStartTx(false);
orientGraph.begin();
boolean update = false;
try {
getElement();
update = true;
element = internalUpdate();
}catch (NotFoundException e) {
element = internalCreate();
}
orientGraph.commit();
if(update) {
setReload(true);
}
// TODO Notify to subscriptionNotification
return serialize();
} catch(ResourceRegistryException e) {
logger.error("Unable to update {} with UUID {}", accessType.getName(), uuid);
if(orientGraph != null) {
orientGraph.rollback();
}
throw e;
} catch(Exception e) {
logger.error("Unable to update {} with UUID {}", accessType.getName(), uuid, e);
if(orientGraph != null) {
orientGraph.rollback();
}
throw new ResourceRegistryException(e);
} finally {
if(orientGraph != null) {
orientGraph.shutdown();
}
}
} }
public String create(boolean transactionStarted) throws AlreadyPresentException, ResourceRegistryException {
public String create() throws AlreadyPresentException, ResourceRegistryException {
try { try {
if(!transactionStarted) { orientGraph = getWorkingContext().getGraph(PermissionMode.WRITER);
orientGraph = getWorkingContext().getGraph(PermissionMode.WRITER); orientGraph.setAutoStartTx(false);
orientGraph.setAutoStartTx(false); orientGraph.begin();
orientGraph.begin();
}
element = internalCreate(); element = internalCreate();
@ -524,16 +557,10 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
} }
public String update() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { public String update() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
return update(false);
}
public String update(boolean transactionStarted) throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
try { try {
if(!transactionStarted) { orientGraph = getWorkingContext().getGraph(PermissionMode.WRITER);
orientGraph = getWorkingContext().getGraph(PermissionMode.WRITER); orientGraph.setAutoStartTx(false);
orientGraph.setAutoStartTx(false); orientGraph.begin();
orientGraph.begin();
}
element = internalUpdate(); element = internalUpdate();
@ -564,8 +591,7 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
} }
} }
public boolean delete() public boolean delete() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
logger.debug("Going to delete {} with UUID {}", accessType.getName(), uuid); logger.debug("Going to delete {} with UUID {}", accessType.getName(), uuid);
try { try {
@ -605,7 +631,8 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
} }
} }
public boolean addToContext(UUID contextUUID) throws NotFoundException, ContextException, ResourceRegistryException { public boolean addToContext(UUID contextUUID)
throws NotFoundException, ContextException, ResourceRegistryException {
logger.info("Going to add {} with UUID {} to Context with UUID {}", accessType.getName(), uuid, contextUUID); logger.info("Going to add {} with UUID {} to Context with UUID {}", accessType.getName(), uuid, contextUUID);
try { try {
@ -640,7 +667,8 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
} }
} }
public boolean removeFromContext(UUID contextUUID) throws NotFoundException, ContextException, ResourceRegistryException { public boolean removeFromContext(UUID contextUUID)
throws NotFoundException, ContextException, ResourceRegistryException {
logger.debug("Going to remove {} with UUID {} from Context with UUID {}", elementType, uuid, contextUUID); logger.debug("Going to remove {} with UUID {} from Context with UUID {}", elementType, uuid, contextUUID);
try { try {
@ -654,7 +682,8 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
boolean removed = internalRemoveFromContext(targetSecurityContext); boolean removed = internalRemoveFromContext(targetSecurityContext);
orientGraph.commit(); orientGraph.commit();
logger.info("{} with UUID {} successfully removed from Context with UUID {}", elementType, uuid, contextUUID); logger.info("{} with UUID {} successfully removed from Context with UUID {}", elementType, uuid,
contextUUID);
return removed; return removed;
} catch(ResourceRegistryException e) { } catch(ResourceRegistryException e) {
@ -664,7 +693,8 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
} }
throw e; throw e;
} catch(Exception e) { } catch(Exception e) {
logger.error("Unable to remove {} with UUID {} from Context with UUID {}", elementType, uuid, contextUUID, e); logger.error("Unable to remove {} with UUID {} from Context with UUID {}", elementType, uuid, contextUUID,
e);
if(orientGraph != null) { if(orientGraph != null) {
orientGraph.rollback(); orientGraph.rollback();
} }

View File

@ -157,23 +157,11 @@ public class InstancesManager {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ERManagement erManagement = ERManagementUtility.getERManagement(type); ERManagement erManagement = ERManagementUtility.getERManagement(type);
erManagement.setUUID(UUID.fromString(uuid));
boolean create = false;
try {
erManagement.exists(false);
} catch(NotFoundException e) {
create = true;
}
erManagement.setUUID(UUID.fromString(uuid)); erManagement.setUUID(UUID.fromString(uuid));
erManagement.setElementType(type); erManagement.setElementType(type);
erManagement.setJSON(json); erManagement.setJSON(json);
if(create) {
return erManagement.create(true);
}
return erManagement.update(true); return erManagement.createOrUpdate();
} }
/* /*