Fixing ER deserialization and checks for element in different context

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@146431 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2017-03-29 14:14:03 +00:00
parent f8ce50d91d
commit 5e7bb046c3
5 changed files with 207 additions and 110 deletions

View File

@ -31,9 +31,13 @@ import org.gcube.informationsystem.model.relation.IsRelatedTo;
import org.gcube.informationsystem.model.relation.Relation;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException;
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.entity.resource.ResourceAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.RelationAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.RelationNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException;
import org.gcube.informationsystem.resourceregistry.context.ContextUtility;
@ -57,6 +61,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.orientechnologies.orient.core.db.record.OTrackedList;
import com.orientechnologies.orient.core.metadata.OMetadata;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OProperty;
@ -73,7 +78,6 @@ import com.tinkerpop.blueprints.util.StringFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*
*/
public abstract class ERManagement<ERType extends ER, El extends Element> {
@ -224,10 +228,22 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
checkJSON();
}
protected void getSchema() throws SchemaException {
protected OClass getOClass() throws SchemaException {
if(oClass==null){
if(element!=null){
OrientElement orientElement = (OrientElement) element;
OMetadata oMetadata = orientElement.getGraph().getRawGraph().getMetadata();
OSchema oSchema = oMetadata.getSchema();
String type = orientElement.getRecord().getClassName();
oClass = oSchema.getClass(type);
}else{
oClass = SchemaManagementImpl.getTypeSchema(erType, baseType);
}
}
return oClass;
}
public void setElementType(String erType) throws ResourceRegistryException {
this.erType = erType;
if (erType == null || erType.compareTo("") == 0) {
@ -257,16 +273,14 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
try {
uuid = org.gcube.informationsystem.impl.utils.Utility
.getUUIDFromJsonNode(jsonNode);
} catch (Exception e) {
}
} catch (Exception e) {}
} else {
checkUUIDMatch();
}
if (this.erType == null) {
this.erType = getClassProperty(jsonNode);
getSchema();
getOClass();
} else {
checkERMatch();
}
@ -283,7 +297,7 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
throw new ResourceRegistryException(error);
}
}
getSchema();
getOClass();
}
protected void checkUUIDMatch() throws ResourceRegistryException {
@ -341,8 +355,8 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
this.uuid = HeaderUtility.getHeader(element).getUUID();
}
protected void throwElementNotFoundException(ResourceRegistryException e)
throws ERNotFoundException, ResourceRegistryException {
protected void throwElementNotFoundException(ERNotFoundException e)
throws ERNotFoundException {
if (Resource.class.isAssignableFrom(erTypeClass)) {
throw new ResourceNotFoundException(e);
@ -351,9 +365,21 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
} else if (Relation.class.isAssignableFrom(erTypeClass)) {
throw new RelationNotFoundException(e);
}
throw e;
}
protected void throwERAvailableInAnotherContextException(ERNotFoundException e)
throws ERAvailableInAnotherContextException {
if (Resource.class.isAssignableFrom(erTypeClass)) {
throw new ResourceAvailableInAnotherContextException(e);
} else if (Facet.class.isAssignableFrom(erTypeClass)) {
throw new FacetAvailableInAnotherContextException(e);
} else if (Relation.class.isAssignableFrom(erTypeClass)) {
throw new RelationAvailableInAnotherContextException(e);
}
throw new ERAvailableInAnotherContextException(e);
}
public El getElement() throws ResourceRegistryException {
@ -362,9 +388,24 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
element = Utility.getElementByUUID(orientGraph,
erType == null ? baseType : erType, uuid, elementClass);
}
} catch (ResourceRegistryException e) {
} catch (ERNotFoundException e) {
try{
Utility.getElementByUUIDAsAdmin(erType == null ? baseType : erType, uuid, elementClass);
throwERAvailableInAnotherContextException(e);
}catch (ERNotFoundException e1) {
// Using e not e1
throwElementNotFoundException(e);
} catch (ResourceRegistryException e1) {
throw e1;
} catch (Exception e1) {
throw new ResourceRegistryException(e);
}
} catch (ResourceRegistryException e) {
throw e;
} catch (Exception e) {
throw new ResourceRegistryException(e);
}
return element;
}
@ -393,9 +434,7 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
try {
orientGraph = ContextUtility.getActualSecurityContextGraph(PermissionMode.READER);
getElement();
if(element==null){
if(getElement()==null){
return false;
}else{
return true;
@ -733,51 +772,33 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
return element;
}
public JSONObject toJSONObject() throws ResourceRegistryException {
protected Object getPropertyForJson(String key, Object object) throws ResourceRegistryException {
try {
OrientElement orientElement = (OrientElement) element;
Map<String, Object> properties = orientElement.getProperties();
String type = orientElement.getRecord().getClassName();
OMetadata oMetadata = orientElement.getGraph().getRawGraph().getMetadata();
OSchema oSchema = oMetadata.getSchema();
OClass oClass = oSchema.getClass(type);
OUTER_FOR: for(String key : orientElement.getPropertyKeys()){
Object object = properties.get(key);
if(key.compareTo(ER.HEADER_PROPERTY)==0){
// Keep the header
// Keeping the header
HeaderOrient headerOrient = HeaderUtility.getHeaderOrient((ODocument) object);
JSONObject headerObject = new JSONObject(headerOrient.toJSON("class"));
properties.put(ER.HEADER_PROPERTY, headerObject);
continue;
return headerObject;
}
if (ignoreKeys.contains(key)) {
properties.remove(key);
continue OUTER_FOR;
return null;
}
for (String prefix : ignoreStartWithKeys) {
if (key.startsWith(prefix)) {
properties.remove(key);
continue OUTER_FOR;
return null;
}
}
if(object instanceof ODocument){
String json = ((ODocument) object).toJSON("class");
JSONObject jsonObject = new JSONObject(json);
properties.put(key, jsonObject);
return jsonObject;
}
if(object instanceof Date){
OProperty oProperty = oClass.getProperty(key);
OProperty oProperty = getOClass().getProperty(key);
OType oType = oProperty.getType();
DateFormat dateFormat = ODateHelper.getDateTimeFormatInstance();
switch (oType) {
@ -793,20 +814,32 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
break;
}
properties.put(key, dateFormat.format((Date) object));
return dateFormat.format((Date) object);
}
if(object instanceof OTrackedList){
OTrackedList<?> oTrackedList = (OTrackedList<?>) object;
JSONArray jsonArray = new JSONArray();
for(Object o : oTrackedList){
Object obj = getPropertyForJson("PLACEHOLDER", o);
jsonArray.put(obj);
}
return jsonArray;
}
return object.toString();
}catch(Exception e){
throw new ResourceRegistryException("Error while serializing "
+ key + "=" + object.toString() + " in " + getElement().toString(), e);
}
}
JSONObject jsonObject = new JSONObject(properties);
jsonObject.put(ISManageable.CLASS_PROPERTY, type);
//Collection<String> superClasses = oClass.getSuperClassesNames();
Collection<OClass> allSuperClasses = oClass.getAllSuperClasses();
protected Collection<String> getSuperclasses() throws SchemaException{
Collection<OClass> allSuperClasses = getOClass().getAllSuperClasses();
Collection<String> superClasses = new HashSet<>();
for(OClass oSuperClass : allSuperClasses){
String name = oSuperClass.getName();
@ -818,13 +851,39 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
superClasses.add(name);
}
return superClasses;
}
public JSONObject toJSONObject() throws ResourceRegistryException {
try {
OrientElement orientElement = (OrientElement) getElement();
Map<String, Object> properties = orientElement.getProperties();
for(String key : orientElement.getPropertyKeys()){
Object object = properties.get(key);
object = getPropertyForJson(key, object);
if(object!=null){
properties.put(key, object);
}else{
properties.remove(key);
}
}
JSONObject jsonObject = new JSONObject(properties);
String type = orientElement.getRecord().getClassName();
jsonObject.put(ISManageable.CLASS_PROPERTY, type);
Collection<String> superClasses = getSuperclasses();
JSONArray jsonArray = new JSONArray(superClasses);
jsonObject.put(ISManageable.SUPERCLASSES_PROPERTY, jsonArray);
return jsonObject;
}catch(Exception e){
throw new ResourceRegistryException("Error while serializing " + element.toString(), e);
} catch (ResourceRegistryException e) {
throw e;
} catch(Exception e){
throw new ResourceRegistryException("Error while serializing " + getElement().toString(), e);
}
}

View File

@ -16,6 +16,9 @@ import org.gcube.informationsystem.model.embedded.Header;
import org.gcube.informationsystem.model.entity.Entity;
import org.gcube.informationsystem.model.relation.Relation;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException;
import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper;
import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper.PermissionMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -25,8 +28,10 @@ import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Element;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientBaseGraph;
import com.tinkerpop.blueprints.impls.orient.OrientElement;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import com.tinkerpop.blueprints.impls.orient.OrientGraphFactory;
import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx;
import com.tinkerpop.blueprints.util.io.graphson.GraphSONMode;
import com.tinkerpop.blueprints.util.io.graphson.GraphSONUtility;
@ -84,9 +89,30 @@ public class Utility {
}
}
public static <El extends Element> El getElementByUUIDAsAdmin(String elementType, UUID uuid,
Class<? extends El> clz) throws ERNotFoundException, ResourceRegistryException {
OrientGraphFactory orientGraphFactory = SecurityContextMapper
.getSecurityContextFactory(SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, PermissionMode.READER);
OrientGraphNoTx orientGraphNoTx = null;
try {
orientGraphNoTx = orientGraphFactory.getNoTx();
return Utility.getElementByUUID(orientGraphNoTx, elementType, uuid, clz);
} finally {
if (orientGraphNoTx != null) {
orientGraphNoTx.shutdown();
}
}
}
public static <El extends Element> El getElementByUUID(
OrientGraph orientGraph, String elementType, UUID uuid,
Class<? extends El> clz) throws ResourceRegistryException {
OrientBaseGraph orientBaseGraph, String elementType, UUID uuid,
Class<? extends El> clz) throws ERNotFoundException, ResourceRegistryException {
if (elementType == null || elementType.compareTo("") == 0) {
if (Vertex.class.isAssignableFrom(clz)) {
@ -104,12 +130,12 @@ public class Utility {
OSQLSynchQuery<El> osqlSynchQuery = new OSQLSynchQuery<>(select);
Iterable<El> elements = orientGraph.command(osqlSynchQuery).execute();
Iterable<El> elements = orientBaseGraph.command(osqlSynchQuery).execute();
if (elements == null || !elements.iterator().hasNext()) {
String error = String.format("No %s with UUID %s was found",
elementType, uuid.toString());
logger.info(error);
throw new ResourceRegistryException(error);
throw new ERNotFoundException(error);
}
Iterator<El> iterator = elements.iterator();

View File

@ -59,8 +59,8 @@ public class ScopedTest {
GCUBE_DEVSEC = properties.getProperty(GCUBE_DEVSEC_VARNAME);
GCUBE_DEVSEC_DEVVRE = properties.getProperty(GCUBE_DEVSEC_DEVVRE_VARNAME);
DEFAULT_TEST_SCOPE = GCUBE_DEVNEXT_NEXTNEXT;
ALTERNATIVE_TEST_SCOPE = GCUBE_DEVSEC;
DEFAULT_TEST_SCOPE = GCUBE_DEVSEC;
ALTERNATIVE_TEST_SCOPE = GCUBE_DEVNEXT_NEXTNEXT;
}
public static String getCurrentScope(String token) throws ObjectNotFound, Exception{

View File

@ -49,8 +49,10 @@ import org.gcube.informationsystem.model.relation.isrelatedto.Hosts;
import org.gcube.informationsystem.resourceregistry.ScopedTest;
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.EntityNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.RelationAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.RelationNotFoundException;
import org.gcube.informationsystem.resourceregistry.er.SmartgearResourcesTest;
import org.gcube.informationsystem.resourceregistry.er.entity.FacetManagement;
@ -106,8 +108,8 @@ public class MultiContextTest extends ScopedTest {
uuid);
throw new Exception(
"You should not be able to read Facet with UUID " + uuid);
} catch (ResourceRegistryException e) {
logger.debug("Good the facet created in /gcube/devsec is not visible in /gcube/devNext");
} catch (FacetAvailableInAnotherContextException e) {
logger.debug("Good the facet created in the default context is not visible in an alternative context");
}
cpuFacet.setAdditionalProperty("My", "Test");
@ -121,8 +123,8 @@ public class MultiContextTest extends ScopedTest {
uuid);
throw new Exception(
"You should not be able to read Facet with UUID " + uuid);
} catch (ResourceRegistryException e) {
logger.debug("Good the Facet created in /gcube/devsec cannot be updated in /gcube/devNext");
} catch (FacetAvailableInAnotherContextException e) {
logger.debug("Good the Facet created in the default context cannot be updated in an alternative context");
}
try {
@ -133,8 +135,8 @@ public class MultiContextTest extends ScopedTest {
uuid);
throw new Exception(
"You should not be able to delete Facet with UUID " + uuid);
} catch (ResourceRegistryException e) {
logger.debug("Good the Facet created in /gcube/devsec cannot be deleted in /gcube/devNext");
} catch (FacetAvailableInAnotherContextException e) {
logger.debug("Good the Facet created in the default context cannot be deleted in an alternative context");
}
/* ------------------------------------------------------------------ */
@ -277,7 +279,7 @@ public class MultiContextTest extends ScopedTest {
resourceManagement = new ResourceManagement();
resourceManagement.setUUID(eServiceUUID);
resourceManagement.read();
} catch (ResourceNotFoundException e) {
} catch (ResourceAvailableInAnotherContextException e) {
logger.debug("Resource with {} Not Found as Expected",
eServiceUUID);
}
@ -285,7 +287,7 @@ public class MultiContextTest extends ScopedTest {
resourceManagement = new ResourceManagement();
resourceManagement.setUUID(eServiceUUID);
resourceManagement.delete();
} catch (ResourceNotFoundException e) {
} catch (ResourceAvailableInAnotherContextException e) {
logger.debug("Resource with {} Not Deleted as Expected",
eServiceUUID);
}
@ -429,7 +431,7 @@ public class MultiContextTest extends ScopedTest {
String error = String.format("{} with UUID {} should not be visible.", EService.NAME, eServiceUUID);
logger.trace(error);
throw new Exception(error);
}catch (EntityNotFoundException e) {
}catch (ResourceAvailableInAnotherContextException e) {
// OK
}
@ -440,7 +442,7 @@ public class MultiContextTest extends ScopedTest {
String error = String.format("{} with UUID {} should not be visible.", Hosts.NAME, hostsUUID);
logger.trace(error);
throw new Exception(error);
}catch (RelationNotFoundException e) {
}catch (RelationAvailableInAnotherContextException e) {
// OK
}

View File

@ -121,6 +121,16 @@ public class ERManagementTest extends ScopedTest {
Assert.assertTrue(deleted);
}
//@Test
public void testReadResource() throws Exception {
ResourceManagement resourceManagement = new ResourceManagement();
resourceManagement.setUUID(UUID.fromString("26da57ee-33bd-4c4b-8aef-9206b61c329e"));
String read= resourceManagement.read();
logger.debug(read);
}
//@Test
public void testDeleteResource() throws Exception {
ResourceManagement resourceManagement = new ResourceManagement();