SanityCheck on Resource for non safe operation made on Facet/IsRealtedTo

This commit is contained in:
Luca Frosini 2021-03-02 10:58:23 +01:00
parent f36ff52dcf
commit 94388d9a36
3 changed files with 119 additions and 7 deletions

View File

@ -11,7 +11,7 @@ public enum Operation {
REMOVE_FROM_CONTEXT,
QUERY(true),
// GET_METADATA e.g. getinstanceContexts
GET_METADATA;
GET_METADATA(true);
private final boolean safe;

View File

@ -9,8 +9,12 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaViolationException;
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext.PermissionMode;
import org.gcube.informationsystem.resourceregistry.instances.model.Operation;
import org.gcube.informationsystem.types.reference.entities.FacetType;
import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
import com.orientechnologies.orient.core.record.ODirection;
import com.orientechnologies.orient.core.record.OVertex;
/**
@ -61,13 +65,61 @@ public class FacetManagement extends EntityManagement<Facet, FacetType> {
return true;
}
protected void checkResource() throws SchemaViolationException, ResourceRegistryException {
if(!entryPoint) {
return;
}
if(operation.isSafe()) {
/* You should not be here.
* The sanity check should not be triggered for a safety operation.
* Anyway, using this code as guard.
*/
logger.warn("sanityCheck should not be triggered for a safe method (i.e. {}). It is not an error but it slow down the performace. Please contact the developer", operation.toString());
return;
}
if(operation == Operation.UPDATE) {
// an update to the Facet only modify the Facet properties, no need to check the source resource
return;
}
ODatabaseDocument targetSecurityContextODatabaseDocument = null;
try {
OVertex oVertex = getElement().getVertices(ODirection.IN).iterator().next();
ResourceManagement resourceManagement = new ResourceManagement();
resourceManagement.setElement(oVertex);
switch (operation) {
case CREATE: case DELETE:
resourceManagement.setWorkingContext(getWorkingContext());
resourceManagement.setODatabaseDocument(oDatabaseDocument);
break;
case ADD_TO_CONTEXT: case REMOVE_FROM_CONTEXT:
resourceManagement.setWorkingContext(targetSecurityContext);
targetSecurityContextODatabaseDocument = targetSecurityContext.getDatabaseDocument(PermissionMode.READER);
resourceManagement.setODatabaseDocument(targetSecurityContextODatabaseDocument);
break;
default:
// You should not be here
return;
}
resourceManagement.sanityCheck();
}catch (Exception e) {
throw new ResourceRegistryException(e);
}finally {
if(targetSecurityContextODatabaseDocument!=null) {
targetSecurityContextODatabaseDocument.close();
oDatabaseDocument.activateOnCurrentThread();
}
}
}
@Override
public void sanityCheck() throws SchemaViolationException, ResourceRegistryException {
super.sanityCheck();
if(entryPoint) {
// We need to check the Resource
}
checkResource();
}
}

View File

@ -12,9 +12,16 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.cons
import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.consistsOf.ConsistsOfAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.consistsOf.ConsistsOfNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaViolationException;
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext.PermissionMode;
import org.gcube.informationsystem.resourceregistry.instances.model.Operation;
import org.gcube.informationsystem.resourceregistry.instances.model.entities.FacetManagement;
import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement;
import org.gcube.informationsystem.types.reference.entities.FacetType;
import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
import com.orientechnologies.orient.core.record.ODirection;
import com.orientechnologies.orient.core.record.OVertex;
/**
* @author Luca Frosini (ISTI - CNR)
*/
@ -56,10 +63,63 @@ public class ConsistsOfManagement extends RelationManagement<FacetManagement, Fa
return facetManagement;
}
protected void checkResource() throws SchemaViolationException, ResourceRegistryException {
if(!entryPoint) {
return;
}
if(operation.isSafe()) {
/* You should not be here.
* The sanity check should not be triggered for a safety operation.
* Anyway, using this code as guard.
*/
logger.warn("sanityCheck should not be triggered for a safe method (i.e. {}). It is not an error but it slow down the performace. Please contact the developer", operation.toString());
return;
}
if(operation == Operation.UPDATE) {
// an update to the Facet only modify the Facet properties, no need to check the source resource
return;
}
ODatabaseDocument targetSecurityContextODatabaseDocument = null;
try {
OVertex oVertex = getElement().getVertex(ODirection.IN);
ResourceManagement resourceManagement = new ResourceManagement();
resourceManagement.setElement(oVertex);
switch (operation) {
case CREATE: case DELETE:
resourceManagement.setWorkingContext(getWorkingContext());
resourceManagement.setODatabaseDocument(oDatabaseDocument);
break;
case ADD_TO_CONTEXT: case REMOVE_FROM_CONTEXT:
resourceManagement.setWorkingContext(targetSecurityContext);
targetSecurityContextODatabaseDocument = targetSecurityContext.getDatabaseDocument(PermissionMode.READER);
resourceManagement.setODatabaseDocument(targetSecurityContextODatabaseDocument);
break;
default:
// You should not be here
return;
}
resourceManagement.sanityCheck();
}catch (Exception e) {
throw new ResourceRegistryException(e);
}finally {
if(targetSecurityContextODatabaseDocument!=null) {
targetSecurityContextODatabaseDocument.close();
oDatabaseDocument.activateOnCurrentThread();
}
}
}
@Override
public void sanityCheck() throws SchemaViolationException, ResourceRegistryException {
// TODO Auto-generated method stub
super.sanityCheck();
checkResource();
}
}