This commit is contained in:
Luca Frosini 2021-01-25 17:38:19 +01:00
parent 60c96b6a91
commit a10323735d
11 changed files with 317 additions and 171 deletions

View File

@ -4,6 +4,7 @@ import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
@ -432,13 +433,13 @@ public class ContextManagement extends EntityElementManagement<Context> {
}
@Override
protected boolean reallyAddToContext(SecurityContext targetSecurityContext)
protected Map<UUID,JsonNode> reallyAddToContext(SecurityContext targetSecurityContext)
throws ContextException, ResourceRegistryException {
throw new UnsupportedOperationException();
}
@Override
protected boolean reallyRemoveFromContext(SecurityContext targetSecurityContext)
protected Map<UUID,JsonNode> reallyRemoveFromContext(SecurityContext targetSecurityContext)
throws ContextException, ResourceRegistryException {
throw new UnsupportedOperationException();
}

View File

@ -1,5 +1,8 @@
package org.gcube.informationsystem.resourceregistry.contexts.relations;
import java.util.Map;
import java.util.UUID;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
import org.gcube.informationsystem.base.reference.AccessType;
@ -101,13 +104,13 @@ public class IsParentOfManagement extends RelationElementManagement<ContextManag
}
@Override
protected boolean reallyAddToContext(SecurityContext targetSecurityContext)
protected Map<UUID,JsonNode> reallyAddToContext(SecurityContext targetSecurityContext)
throws ContextException, ResourceRegistryException {
throw new UnsupportedOperationException();
}
@Override
protected boolean reallyRemoveFromContext(SecurityContext targetSecurityContext)
protected Map<UUID,JsonNode> reallyRemoveFromContext(SecurityContext targetSecurityContext)
throws ContextException, ResourceRegistryException {
throw new UnsupportedOperationException();
}

View File

@ -87,6 +87,69 @@ public abstract class ElementManagement<El extends OElement> {
protected El element;
protected boolean reload;
/**
* By the default the system execute the the operation of
* context sharing so this variable is initialised as false.
*
* Setting this variable to false the system just simulate
* the operation of context sharing
* i.e. AddToContext, RemoveFromContext.
*
* This option can also be used in conjunction with
* {@link ElementManagement#honourPropagationConstraintsInContextSharing}=false.
* This allow to simulate a sharing operation which requires
* do not honour the propagation constraints.
*/
protected boolean dryRunContextSharing;
/**
* By the default the system honour the propagation constraints
* so this variable is initialised as true.
*
* To revert a previous operation or for particular a maintenance
* we could request to the service do not honour the propagation
* constraints but under certain conditions and with some limitation
* only.
* These limitation are required to keep the system in a consistent
* state.
* In fact, this directive is only valid for Resources and IsRelatedTo
* relations. We need to take in account that to add an
* IsRelatedTo to a context always the source and target
* Resources must be in such a Context.
* Please also take into account that adding a Resource
* to a context always imply to honour the propagation constraints
* of ConsistsOf relations. In fact, a resource must be characterised
* least by one facet in any context it belongs. Giving that we
* cannot made assumption on which facets must be used.
* A way could be to consider just the facets are mandatory for such a
* Resource Type, but the type could not have mandatory facets
* (even every Resource Type in the gCube Model has one mandatory facet).
* As counterpart, when a Resource is removed from a Context all the facets
* charactering it must be removed.
*
* This option can also be used in conjunction with
* {@link ElementManagement#dryRunContextSharing}=true.
* This allow to simulate a sharing operation which requires
* do not honour the propagation constraints.
*/
protected boolean honourPropagationConstraintsInContextSharing;
public boolean isDryRunContextSharing() {
return dryRunContextSharing;
}
public void setDryRunContextSharing(boolean dryRunContextSharing) {
this.dryRunContextSharing = dryRunContextSharing;
}
public boolean isHonourPropagationConstraintsInContextSharing() {
return honourPropagationConstraintsInContextSharing;
}
public void setHonourPropagationConstraintsInContextSharing(boolean honourPropagationConstraintsInContextSharing) {
this.honourPropagationConstraintsInContextSharing = honourPropagationConstraintsInContextSharing;
}
public UUID getUUID() {
return uuid;
}
@ -128,6 +191,19 @@ public abstract class ElementManagement<El extends OElement> {
this.reload = false;
/*
* By the default the system execute the the operation of
* context sharing so this variable is initialised as false.
*/
this.dryRunContextSharing = false;
/*
* By the default the system honour the propagation constraints
* so this variable is initialised as true.
*/
this.honourPropagationConstraintsInContextSharing = true;
}
public void setUUID(UUID uuid) throws ResourceRegistryException {
@ -327,16 +403,19 @@ public abstract class ElementManagement<El extends OElement> {
return reallyDelete();
}
protected abstract boolean reallyAddToContext(SecurityContext targetSecurityContext)
protected abstract Map<UUID,JsonNode> reallyAddToContext(SecurityContext targetSecurityContext)
throws ContextException, ResourceRegistryException;
public boolean internalAddToContext(SecurityContext targetSecurityContext)
public Map<UUID,JsonNode> internalAddToContext(SecurityContext targetSecurityContext)
throws ContextException, ResourceRegistryException {
try {
boolean ret = reallyAddToContext(targetSecurityContext);
HeaderUtility.updateModifiedByAndLastUpdate(element);
element.save();
return ret && true;
Map<UUID,JsonNode> affectedInstances = reallyAddToContext(targetSecurityContext);
if(!dryRunContextSharing) {
HeaderUtility.updateModifiedByAndLastUpdate(element);
element.save();
}
affectedInstances.put(uuid, serializeSelfOnly());
return affectedInstances;
} catch(ResourceRegistryException e) {
throw e;
} catch(Exception e) {
@ -345,16 +424,19 @@ public abstract class ElementManagement<El extends OElement> {
}
}
protected abstract boolean reallyRemoveFromContext(SecurityContext targetSecurityContext)
protected abstract Map<UUID,JsonNode> reallyRemoveFromContext(SecurityContext targetSecurityContext)
throws ContextException, ResourceRegistryException;
public boolean internalRemoveFromContext(SecurityContext targetSecurityContext)
public Map<UUID,JsonNode> internalRemoveFromContext(SecurityContext targetSecurityContext)
throws ContextException, ResourceRegistryException {
try {
boolean ret = reallyRemoveFromContext(targetSecurityContext);
HeaderUtility.updateModifiedByAndLastUpdate(element);
element.save();
return ret && true;
Map<UUID,JsonNode> affectedInstances = reallyRemoveFromContext(targetSecurityContext);
if(!dryRunContextSharing) {
HeaderUtility.updateModifiedByAndLastUpdate(element);
element.save();
}
affectedInstances.put(uuid, serializeSelfOnly());
return affectedInstances;
} catch(ResourceRegistryException e) {
throw e;
} catch(Exception e) {
@ -681,7 +763,7 @@ public abstract class ElementManagement<El extends OElement> {
}
}
public boolean addToContext(UUID contextUUID)
public Map<UUID,JsonNode> addToContext(UUID contextUUID)
throws NotFoundException, ContextException, ResourceRegistryException {
logger.info("Going to add {} with UUID {} to Context with UUID {}", accessType.getName(), uuid, contextUUID);
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
@ -691,12 +773,12 @@ public abstract class ElementManagement<El extends OElement> {
SecurityContext targetSecurityContext = ContextUtility.getInstance().getSecurityContextByUUID(contextUUID);
boolean added = internalAddToContext(targetSecurityContext);
Map<UUID,JsonNode> affecteElements = internalAddToContext(targetSecurityContext);
oDatabaseDocument.commit();
logger.info("{} with UUID {} successfully added to Context with UUID {}", elementType, uuid, contextUUID);
return added;
return affecteElements;
} catch(ResourceRegistryException e) {
logger.error("Unable to add {} with UUID {} to Context with UUID {}", elementType, uuid, contextUUID);
if(oDatabaseDocument != null) {
@ -720,7 +802,7 @@ public abstract class ElementManagement<El extends OElement> {
}
}
public boolean removeFromContext(UUID contextUUID)
public Map<UUID,JsonNode> removeFromContext(UUID contextUUID)
throws NotFoundException, ContextException, ResourceRegistryException {
logger.debug("Going to remove {} with UUID {} from Context with UUID {}", elementType, uuid, contextUUID);
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
@ -730,13 +812,12 @@ public abstract class ElementManagement<El extends OElement> {
SecurityContext targetSecurityContext = ContextUtility.getInstance().getSecurityContextByUUID(contextUUID);
boolean removed = internalRemoveFromContext(targetSecurityContext);
Map<UUID,JsonNode> affecteElements = internalRemoveFromContext(targetSecurityContext);
oDatabaseDocument.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 affecteElements;
} catch(ResourceRegistryException e) {
logger.error("Unable to remove {} with UUID {} from Context with UUID {}", elementType, uuid, contextUUID);
if(oDatabaseDocument != null) {

View File

@ -191,37 +191,53 @@ public abstract class EntityManagement<E extends EntityElement> extends EntityEl
}
@Override
protected boolean reallyAddToContext(SecurityContext targetSecurityContext)
protected Map<UUID,JsonNode> reallyAddToContext(SecurityContext targetSecurityContext)
throws ContextException, ResourceRegistryException {
Map<UUID,JsonNode> affectedInstances = new HashMap<>();
targetSecurityContext.addElement(getElement(), oDatabaseDocument);
/*
* DO NOT UNCOMMENT
* the instance is added internalAddToContext() function after
* the update of Header metadata i.e. modifiedBy, lastUpdateTime
* affectedInstances.put(uuid, serializeSelfOnly());
*/
Iterable<OEdge> edges = getElement().getEdges(ODirection.OUT);
for(OEdge edge : edges) {
@SuppressWarnings("rawtypes")
RelationManagement relationManagement = getRelationManagement(edge);
relationManagement.internalAddToContext(targetSecurityContext);
affectedInstances.putAll(relationManagement.internalAddToContext(targetSecurityContext));
}
return true;
return affectedInstances;
}
@Override
protected boolean reallyRemoveFromContext(SecurityContext targetSecurityContext)
protected Map<UUID,JsonNode> reallyRemoveFromContext(SecurityContext targetSecurityContext)
throws ContextException, ResourceRegistryException {
Map<UUID,JsonNode> affectedInstances = new HashMap<>();
Iterable<OEdge> edges = getElement().getEdges(ODirection.OUT);
for(OEdge edge : edges) {
@SuppressWarnings("rawtypes")
RelationManagement relationManagement = getRelationManagement(edge);
relationManagement.internalRemoveFromContext(targetSecurityContext);
affectedInstances.putAll(relationManagement.internalRemoveFromContext(targetSecurityContext));
}
targetSecurityContext.removeElement(getElement(), oDatabaseDocument);
/*
* DO NOT UNCOMMENT
* the instance is added internalAddToContext() function after
* the update of Header metadata i.e. modifiedBy, lastUpdateTime
* affectedInstances.put(uuid, serializeSelfOnly());
*/
return true;
return affectedInstances;
}
@Override

View File

@ -47,11 +47,21 @@ import com.orientechnologies.orient.core.record.impl.ODocument;
public abstract class RelationManagement<T extends EntityManagement<? extends Entity>>
extends RelationElementManagement<ResourceManagement, T>{
/**
* Used in AddToContext. The Relation must add/check also the source entity
*/
public boolean checkContextOfSourceEntity;
public void setCheckContextOfSourceEntity(boolean checkContextOfSourceEntity) {
this.checkContextOfSourceEntity = checkContextOfSourceEntity;
}
public final PropagationConstraint defaultPropagationConstraint;
protected RelationManagement(AccessType accessType, Class<? extends Entity> targetEntityClass, PropagationConstraint defaultPropagationConstraint) {
super(accessType, Resource.class, targetEntityClass);
this.defaultPropagationConstraint = defaultPropagationConstraint;
this.checkContextOfSourceEntity = false;
}
protected RelationManagement(AccessType accessType, Class<? extends Entity> targetEntityClass, SecurityContext workingContext, ODatabaseDocument orientGraph,
@ -260,10 +270,12 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
}
@Override
protected boolean reallyAddToContext(SecurityContext targetSecurityContext)
protected Map<UUID,JsonNode> reallyAddToContext(SecurityContext targetSecurityContext)
throws ContextException, ResourceRegistryException {
getElement();
Map<UUID,JsonNode> affectedInstances = new HashMap<>();
AddConstraint addConstraint = AddConstraint.unpropagate;
try {
@ -292,9 +304,15 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
* The relation must be added only in the case the target vertex must be added.
* Otherwise we have a relation which point to an entity outside of the context.
*/
getTargetEntityManagement().internalAddToContext(targetSecurityContext);
affectedInstances.putAll(getTargetEntityManagement().internalAddToContext(targetSecurityContext));
targetSecurityContext.addElement(getElement(), oDatabaseDocument);
/*
* DO NOT UNCOMMENT
* the instance is added internalAddToContext() function after
* the update of Header metadata i.e. modifiedBy, lastUpdateTime
* affectedInstances.put(uuid, serializeSelfOnly());
*/
break;
@ -305,30 +323,34 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
break;
}
return true;
return affectedInstances;
}
public boolean forcedAddToContext(SecurityContext targetSecurityContext)
public Map<UUID,JsonNode> forcedAddToContext(SecurityContext targetSecurityContext)
throws ContextException, ResourceRegistryException {
getElement();
Map<UUID,JsonNode> affectedInstances = new HashMap<>();
/* Adding source to Context */
getSourceEntityManagement().internalAddToContext(targetSecurityContext);
affectedInstances.putAll(getSourceEntityManagement().internalAddToContext(targetSecurityContext));
/* Adding target to Context */
getTargetEntityManagement().internalAddToContext(targetSecurityContext);
affectedInstances.putAll(getTargetEntityManagement().internalAddToContext(targetSecurityContext));
targetSecurityContext.addElement(getElement(), oDatabaseDocument);
affectedInstances.put(uuid, serializeSelfOnly());
return true;
return affectedInstances;
}
@Override
protected boolean reallyRemoveFromContext(SecurityContext targetSecurityContext)
protected Map<UUID,JsonNode> reallyRemoveFromContext(SecurityContext targetSecurityContext)
throws ContextException, ResourceRegistryException {
getElement();
Map<UUID,JsonNode> affectedInstances = new HashMap<>();
RemoveConstraint removeConstraint = RemoveConstraint.keep;
try {
@ -357,10 +379,11 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
* avoid to have edge having a source outside of the context.
*/
targetSecurityContext.removeElement(getElement(), oDatabaseDocument);
affectedInstances.put(uuid, serializeSelfOnly());
switch(removeConstraint) {
case cascade:
getTargetEntityManagement().internalRemoveFromContext(targetSecurityContext);
affectedInstances.putAll(getTargetEntityManagement().internalRemoveFromContext(targetSecurityContext));
break;
case cascadeWhenOrphan:
@ -386,10 +409,10 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
if(count > 0) {
logger.trace(
"{} point to {} which is not orphan ({} exists). Giving {} directive, it will be not remove from .",
"{} point to {} which is not orphan ({} exists). Giving {} directive, it will be not remove from {}.",
element, target, edge, removeConstraint, targetSecurityContext);
} else {
getTargetEntityManagement().internalRemoveFromContext(targetSecurityContext);
affectedInstances.putAll(getTargetEntityManagement().internalRemoveFromContext(targetSecurityContext));
}
break;
@ -400,7 +423,7 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
break;
}
return true;
return affectedInstances;
}
@Override
@ -495,7 +518,7 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
}
@Override
public boolean addToContext(UUID contextUUID) throws NotFoundException, ContextException {
public Map<UUID,JsonNode> addToContext(UUID contextUUID) throws NotFoundException, ContextException {
logger.debug("Going to add {} with UUID {} to Context with UUID {}", accessType.getName(), uuid, contextUUID);
try {
@ -503,7 +526,7 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
SecurityContext targetSecurityContext = ContextUtility.getInstance().getSecurityContextByUUID(contextUUID);
boolean added = forcedAddToContext(targetSecurityContext);
Map<UUID,JsonNode> added = forcedAddToContext(targetSecurityContext);
oDatabaseDocument.commit();
logger.info("{} with UUID {} successfully added to Context with UUID {}", accessType.getName(), uuid,

View File

@ -1,18 +1,23 @@
package org.gcube.informationsystem.resourceregistry.rest;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
import org.gcube.common.authorization.library.provider.CalledMethodProvider;
import org.gcube.informationsystem.base.reference.Element;
import org.gcube.informationsystem.base.reference.IdentifiableElement;
import org.gcube.informationsystem.context.reference.entities.Context;
import org.gcube.informationsystem.model.reference.properties.Header;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException;
@ -21,7 +26,6 @@ import org.gcube.informationsystem.resourceregistry.api.rest.SharingPath;
import org.gcube.informationsystem.resourceregistry.api.rest.SharingPath.SharingOperation;
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagement;
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagementUtility;
import org.gcube.informationsystem.resourceregistry.rest.extrahttpmethods.PATCH;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -33,6 +37,19 @@ public class SharingManager {
private static Logger logger = LoggerFactory.getLogger(SharingManager.class);
protected String serializeAffectedInstaces(ObjectMapper objectMapper, Map<UUID,JsonNode> affectedInstances) throws ResourceRegistryException {
ArrayNode arrayNode = objectMapper.createArrayNode();
for(JsonNode jsonNode : affectedInstances.values()) {
arrayNode.add(jsonNode);
}
try {
return objectMapper.writeValueAsString(arrayNode);
} catch (JsonProcessingException e) {
throw new ResourceRegistryException(e);
}
}
/**
* Add/Remove to/from the context identified by CONTEXT_UUID path parameter the list of instances contained in the body of the request.
* The the body is the following
@ -55,143 +72,133 @@ public class SharingManager {
* where
* 67062c11-9c3a-4906-870d-7df6a43408b0/ is the Context UUID
*
* The body contains the list of instances to add/remvoe to/from the context identified by CONTEXT_UUID
* The body contains the list of instances to add/remove to/from the context identified by CONTEXT_UUID
*
*/
@PATCH
@POST
@Path("{" + SharingPath.CONTEXTS_PATH_PART + "/{" + AccessPath.CONTEXT_UUID_PATH_PARAM + "}")
public boolean addRemoveToFromContextNoPropagationConstraint(
public String addRemoveToFromContextNoPropagationConstraint(
@PathParam(AccessPath.CONTEXT_UUID_PATH_PARAM) String contextId,
@QueryParam(SharingPath.OPERATION_QUERY_PARAMETER) SharingOperation operation,
@QueryParam(SharingPath.DRY_RUN_QUERY_PARAMETER) Boolean dryRun,
String body)
throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
logger.info("Requested to add to {} with UUID {} the following instances {}", Context.NAME, contextId, body);
if(operation == SharingOperation.ADD) {
CalledMethodProvider.instance.set("addToContextNoPropagationConstraint");
}else {
CalledMethodProvider.instance.set("removeFromContextNoPropagationConstraint");
try {
StringBuffer calledMethod = new StringBuffer();
if(dryRun==null) {
dryRun = false;
}
if(dryRun) {
calledMethod.append("dryRun");
}
if(operation == SharingOperation.ADD) {
logger.info("Requested {} {} to {} with UUID {}", dryRun? "a dry run for adding": "to add", body, Context.NAME, contextId);
calledMethod.append("AddToContext");
}else {
logger.info("Requested {} {} from {} with UUID {}", dryRun? "a dry run for removing": "to remove", body, Context.NAME, contextId);
calledMethod.append("RemoveFromContext");
}
calledMethod.append("NoPropagationConstraint");
CalledMethodProvider.instance.set(calledMethod.toString());
ObjectMapper objectMapper = new ObjectMapper();
Map<UUID,JsonNode> affectedInstances = new HashMap<>();
ArrayNode arrayNode = (ArrayNode) objectMapper.readTree(body);
for(JsonNode node : arrayNode) {
String type = node.get(Element.CLASS_PROPERTY).asText();
String instanceId = node.get(IdentifiableElement.HEADER_PROPERTY).get(Header.UUID_PROPERTY).asText();
@SuppressWarnings("rawtypes")
ElementManagement elementManagement = ElementManagementUtility.getERManagement(type);
elementManagement.setUUID(UUID.fromString(instanceId));
elementManagement.setDryRunContextSharing(dryRun);
ArrayNode ret = null;
if(operation == SharingOperation.ADD) {
affectedInstances.putAll(elementManagement.addToContext(UUID.fromString(contextId)));
}else {
affectedInstances.putAll(elementManagement.removeFromContext(UUID.fromString(contextId)));
}
}
return serializeAffectedInstaces(objectMapper, affectedInstances);
}catch (ResourceRegistryException e) {
throw e;
}catch (Exception e) {
throw new ResourceRegistryException(e);
}
/*
boolean success = true;
for(any element in body) {
String type = ""; // get type
UUID instanceUUID = UUID.fromString("");
@SuppressWarnings("rawtypes")
ElementManagement elementManagement = ElementManagementUtility.getERManagement(type);
elementManagement.setUUID(instanceUUID);
// success = success && return elementManagement.addToContextNoPropagationConstraint(UUID.fromString(contextId), body);
}
*/
return true;
}
/**
* Add/Remove an instance from context. The operation can have a cascade effect due to propagation constraint.
*
* Get the list of instances affected by an add/remove to/from context the target instance identified by UUID path parameter
* Return the list of instances affected by an add/remove to/from context the target instance identified by UUID path parameter
*
* GET /sharing/contexts/{CONTEXT_UUID}/{TYPE_NAME}/{UUID}?operation=(ADD|REMOVE)
* POST /sharing/contexts/{CONTEXT_UUID}/{TYPE_NAME}/{UUID}?operation=(ADD|REMOVE)&[dryRun=true]
*
* e.g
* GET /resource-registry/sharing/contexts/67062c11-9c3a-4906-870d-7df6a43408b0/HostingNode/16032d09-3823-444e-a1ff-a67de4f350a8?operation=ADD
* GET /resource-registry/sharing/contexts/67062c11-9c3a-4906-870d-7df6a43408b0/HostingNode/16032d09-3823-444e-a1ff-a67de4f350a8?operation=REMOVE
* POST /resource-registry/sharing/contexts/67062c11-9c3a-4906-870d-7df6a43408b0/HostingNode/16032d09-3823-444e-a1ff-a67de4f350a8?operation=ADD&dryRun=true
* POST /resource-registry/sharing/contexts/67062c11-9c3a-4906-870d-7df6a43408b0/HostingNode/16032d09-3823-444e-a1ff-a67de4f350a8?operation=REMOVE&dryRun=true
*
* where
* 67062c11-9c3a-4906-870d-7df6a43408b0 is the Context UUID and
* 16032d09-3823-444e-a1ff-a67de4f350a8 is the HostingNode UUID
*
*/
@GET
@Path("{" + SharingPath.CONTEXTS_PATH_PART + "/{" + AccessPath.CONTEXT_UUID_PATH_PARAM + "}/"
+ AccessPath.TYPE_PATH_PARAM + "}" + "/{" + AccessPath.UUID_PATH_PARAM + "}")
public String checkAddRemove(
@PathParam(AccessPath.CONTEXT_UUID_PATH_PARAM) String contextId,
@PathParam(AccessPath.TYPE_PATH_PARAM) String type,
@PathParam(AccessPath.UUID_PATH_PARAM) String instanceId,
@QueryParam(SharingPath.OPERATION_QUERY_PARAMETER) SharingOperation operation)
throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
logger.info("Requested to add {} with UUID {} to {} with UUID {}", type, instanceId, Context.NAME, contextId);
if(operation == SharingOperation.ADD) {
CalledMethodProvider.instance.set("dryRunAddToContext");
}else {
CalledMethodProvider.instance.set("dryRunRemoveFromContext");
}
@SuppressWarnings("rawtypes")
ElementManagement elementManagement = ElementManagementUtility.getERManagement(type);
elementManagement.setUUID(UUID.fromString(instanceId));
if(operation == SharingOperation.ADD) {
// elementManagement.dryRunAddToContext(UUID.fromString(contextId));
}else {
// elementManagement.dryRunRemoveFromContext(UUID.fromString(contextId));
}
return "";
}
/**
* POST /sharing/contexts/{CONTEXT_UUID}/{TYPE_NAME}/{UUID}
* e.g
* POST /resource-registry/sharing/contexts/67062c11-9c3a-4906-870d-7df6a43408b0/HostingNode/16032d09-3823-444e-a1ff-a67de4f350a8
* Where
* 67062c11-9c3a-4906-870d-7df6a43408b0 is the Context UUID and
* 16032d09-3823-444e-a1ff-a67de4f350a8 is the HostingNode UUID
*
*/
@POST
@Path("{" + SharingPath.CONTEXTS_PATH_PART + "/{" + AccessPath.CONTEXT_UUID_PATH_PARAM + "}/"
+ AccessPath.TYPE_PATH_PARAM + "}" + "/{" + AccessPath.UUID_PATH_PARAM + "}")
public boolean add(
public String addRemove(
@PathParam(AccessPath.CONTEXT_UUID_PATH_PARAM) String contextId,
@PathParam(AccessPath.TYPE_PATH_PARAM) String type,
@PathParam(AccessPath.UUID_PATH_PARAM) String instanceId)
@PathParam(AccessPath.UUID_PATH_PARAM) String instanceId,
@QueryParam(SharingPath.OPERATION_QUERY_PARAMETER) SharingOperation operation,
@QueryParam(SharingPath.DRY_RUN_QUERY_PARAMETER) Boolean dryRun)
throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
logger.info("Requested to add {} with UUID {} to {} with UUID {}", type, instanceId, Context.NAME, contextId);
// setRESTCalledMethod(HTTPMETHOD.PUT, type);
CalledMethodProvider.instance.set("addToContext");
@SuppressWarnings("rawtypes")
ElementManagement elementManagement = ElementManagementUtility.getERManagement(type);
elementManagement.setUUID(UUID.fromString(instanceId));
return elementManagement.addToContext(UUID.fromString(contextId));
}
/*
* DELETE /sharing/contexts/{CONTEXT_UUID}/{TYPE_NAME}/{UUID}
* e.g
* DELETE /resource-registry/sharing/contexts/67062c11-9c3a-4906-870d-7df6a43408b0/HostingNode/16032d09-3823-444e-a1ff-a67de4f350a8
* Where
* 67062c11-9c3a-4906-870d-7df6a43408b0 is the Context UUID and
* 16032d09-3823-444e-a1ff-a67de4f350a8 is the HostingNode UUID
*
*
*/
@DELETE
@Path("{" + SharingPath.CONTEXTS_PATH_PART + "/{" + AccessPath.CONTEXT_UUID_PATH_PARAM + "}/"
+ AccessPath.TYPE_PATH_PARAM + "}" + "/{" + AccessPath.UUID_PATH_PARAM + "}")
public Response remove(
@PathParam(AccessPath.CONTEXT_UUID_PATH_PARAM) String contextId,
@PathParam(AccessPath.TYPE_PATH_PARAM) String type,
@PathParam(AccessPath.UUID_PATH_PARAM) String instanceId)
throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
logger.info("Requested to remove {} with UUID {} to {} with UUID {}", type, instanceId, Context.NAME,
contextId);
CalledMethodProvider.instance.set("removeFromContext");
StringBuffer calledMethod = new StringBuffer();
if(dryRun==null) {
dryRun = false;
}
if(dryRun) {
calledMethod.append("dryRun");
}
if(operation == SharingOperation.ADD) {
logger.info("Requested {} {} with UUID {} to {} with UUID {}", dryRun? "a dry run for adding": "to add", type, instanceId, Context.NAME, contextId);
calledMethod.append("AddToContext");
}else {
logger.info("Requested {} {} with UUID {} from {} with UUID {}", dryRun? "a dry run for removing": "to remove", type, instanceId, Context.NAME, contextId);
calledMethod.append("RemoveFromContext");
}
CalledMethodProvider.instance.set(calledMethod.toString());
@SuppressWarnings("rawtypes")
ElementManagement elementManagement = ElementManagementUtility.getERManagement(type);
elementManagement.setUUID(UUID.fromString(instanceId));
elementManagement.removeFromContext(UUID.fromString(contextId));
return Response.status(Status.NO_CONTENT).build();
elementManagement.setDryRunContextSharing(dryRun);
ObjectMapper objectMapper = new ObjectMapper();
Map<UUID,JsonNode> affectedInstances = null;
if(operation == SharingOperation.ADD) {
affectedInstances = elementManagement.addToContext(UUID.fromString(contextId));
}else {
affectedInstances = elementManagement.removeFromContext(UUID.fromString(contextId));
}
try {
return serializeAffectedInstaces(objectMapper, affectedInstances);
} catch (Exception e) {
throw new ResourceRegistryException(e);
}
}
}

View File

@ -1,6 +1,8 @@
package org.gcube.informationsystem.resourceregistry.types.entities;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.informationsystem.base.reference.AccessType;
@ -196,13 +198,13 @@ public abstract class EntityTypeDefinitionManagement<E extends EntityType> exten
}
@Override
protected boolean reallyAddToContext(SecurityContext targetSecurityContext)
protected Map<UUID,JsonNode> reallyAddToContext(SecurityContext targetSecurityContext)
throws ContextException, ResourceRegistryException {
throw new UnsupportedOperationException();
}
@Override
protected boolean reallyRemoveFromContext(SecurityContext targetSecurityContext)
protected Map<UUID,JsonNode> reallyRemoveFromContext(SecurityContext targetSecurityContext)
throws ContextException, ResourceRegistryException {
throw new UnsupportedOperationException();
}

View File

@ -1,6 +1,8 @@
package org.gcube.informationsystem.resourceregistry.types.properties;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.informationsystem.base.reference.AccessType;
@ -188,13 +190,13 @@ public class PropertyTypeDefinitionManagement extends ElementManagement<OElement
}
@Override
protected boolean reallyAddToContext(SecurityContext targetSecurityContext)
protected Map<UUID,JsonNode> reallyAddToContext(SecurityContext targetSecurityContext)
throws ContextException, ResourceRegistryException {
throw new UnsupportedOperationException();
}
@Override
protected boolean reallyRemoveFromContext(SecurityContext targetSecurityContext)
protected Map<UUID,JsonNode> reallyRemoveFromContext(SecurityContext targetSecurityContext)
throws ContextException, ResourceRegistryException {
throw new UnsupportedOperationException();
}

View File

@ -1,7 +1,10 @@
package org.gcube.informationsystem.resourceregistry.types.relations;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.model.reference.relations.Relation;
import org.gcube.informationsystem.resourceregistry.api.exceptions.AlreadyPresentException;
@ -194,13 +197,13 @@ public abstract class RelationTypeDefinitionManagement<T extends EntityTypeDefin
}
@Override
protected boolean reallyAddToContext(SecurityContext targetSecurityContext)
protected Map<UUID,JsonNode> reallyAddToContext(SecurityContext targetSecurityContext)
throws ContextException, ResourceRegistryException {
throw new UnsupportedOperationException();
}
@Override
protected boolean reallyRemoveFromContext(SecurityContext targetSecurityContext)
protected Map<UUID,JsonNode> reallyRemoveFromContext(SecurityContext targetSecurityContext)
throws ContextException, ResourceRegistryException {
throw new UnsupportedOperationException();
}

View File

@ -1,7 +1,9 @@
package org.gcube.informationsystem.resourceregistry.instances.model.entity;
import java.util.Map;
import java.util.UUID;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.informationsystem.base.reference.IdentifiableElement;
import org.gcube.informationsystem.model.reference.entities.Facet;
import org.gcube.informationsystem.resourceregistry.ContextTest;
@ -145,28 +147,30 @@ public class FacetManagementTest extends ContextTest {
return deleted;
}
protected <F extends Facet> boolean addToContext(F facet) throws Exception {
protected <F extends Facet> Map<UUID,JsonNode> addToContext(F facet) throws Exception {
FacetManagement facetManagement = new FacetManagement();
String facetType = Utility.getTypeName(facet);
facetManagement.setElementType(facetType);
facetManagement.setUUID(facet.getHeader().getUUID());
boolean added = facetManagement.addToContext(ContextUtility.getCurrentSecurityContext().getUUID());
Assert.assertTrue(added);
Map<UUID,JsonNode> affectedInstances = facetManagement.addToContext(ContextUtility.getCurrentSecurityContext().getUUID());
Assert.assertTrue(affectedInstances.containsKey(facet.getHeader().getUUID()));
Assert.assertTrue(affectedInstances.size()==1);
return added;
return affectedInstances;
}
protected <F extends Facet> boolean removeFromContext(F facet) throws Exception {
protected <F extends Facet> Map<UUID,JsonNode> removeFromContext(F facet) throws Exception {
FacetManagement facetManagement = new FacetManagement();
String facetType = Utility.getTypeName(facet);
facetManagement.setElementType(facetType);
facetManagement.setUUID(facet.getHeader().getUUID());
boolean added = facetManagement.removeFromContext(ContextUtility.getCurrentSecurityContext().getUUID());
Assert.assertTrue(added);
Map<UUID,JsonNode> affectedInstances = facetManagement.removeFromContext(ContextUtility.getCurrentSecurityContext().getUUID());
Assert.assertTrue(affectedInstances.containsKey(facet.getHeader().getUUID()));
Assert.assertTrue(affectedInstances.size()==1);
return added;
return affectedInstances;
}
interface ActionFunction<F extends Facet> {

View File

@ -9,9 +9,12 @@ import java.net.URL;
import java.util.Calendar;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
import org.gcube.informationsystem.context.reference.entities.Context;
import org.gcube.informationsystem.model.impl.properties.PropagationConstraintImpl;
@ -281,8 +284,9 @@ public class BasicTest extends ContextTest {
resourceManagement = new ResourceManagement();
resourceManagement.setUUID(uuid);
boolean addedToContext = resourceManagement.addToContext(ContextUtility.getCurrentSecurityContext().getUUID());
Assert.assertTrue(addedToContext);
Map<UUID, JsonNode> affectedInstaces = resourceManagement.addToContext(ContextUtility.getCurrentSecurityContext().getUUID());
Assert.assertTrue(affectedInstaces);
resourceManagement = new ResourceManagement();
resourceManagement.setUUID(uuid);
@ -439,8 +443,8 @@ public class BasicTest extends ContextTest {
resourceManagement = new ResourceManagement();
resourceManagement.setUUID(hnUUID);
boolean removed = resourceManagement.removeFromContext(ContextUtility.getCurrentSecurityContext().getUUID());
Assert.assertTrue(removed);
Map<String,JsonNode> affectedInstaces = resourceManagement.removeFromContext(ContextUtility.getCurrentSecurityContext().getUUID());
Assert.assertTrue(affectedInstaces);
/* The cascading MUST remove the relation and the target so that
* I MUST not be able to read Activates relation and EService