From 5590c79548d6934616a26498ee66f238a0dba5fa Mon Sep 17 00:00:00 2001 From: "luca.frosini" Date: Wed, 6 Jun 2018 17:26:19 +0000 Subject: [PATCH] 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@167900 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../context/ContextUtility.java | 2 +- .../context/IsParentOfManagement.java | 6 +- .../resourceregistry/er/ERManagement.java | 47 +++--- .../er/ERManagementUtility.java | 22 +++ .../er/entity/EntityManagement.java | 12 +- .../er/relation/RelationManagement.java | 38 +++-- .../resourceregistry/rest/Access.java | 159 ++++++++++-------- .../resourceregistry/rest/ContextManager.java | 48 ++---- .../resourceregistry/rest/ERManager.java | 10 +- .../rest/InstancesManager.java | 125 ++++++++++++++ .../resourceregistry/rest/SchemaManager.java | 49 +++--- .../rest/SharingManagement.java | 110 ++++++++++++ .../schema/SchemaManagementImpl.java | 2 +- .../er/entity/FacetManagementTest.java | 4 +- .../er/multicontext/BasicTest.java | 8 +- 15 files changed, 448 insertions(+), 194 deletions(-) create mode 100644 src/main/java/org/gcube/informationsystem/resourceregistry/rest/InstancesManager.java create mode 100644 src/main/java/org/gcube/informationsystem/resourceregistry/rest/SharingManagement.java diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextUtility.java b/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextUtility.java index 1818255..bcea054 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextUtility.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextUtility.java @@ -143,7 +143,7 @@ public class ContextUtility { } } - protected SecurityContext getSecurityContextByUUID(UUID uuid) throws ResourceRegistryException { + public SecurityContext getSecurityContextByUUID(UUID uuid) throws ResourceRegistryException { return getSecurityContextByUUID(uuid, null); } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/context/IsParentOfManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/context/IsParentOfManagement.java index 968749d..ab2e05e 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/context/IsParentOfManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/context/IsParentOfManagement.java @@ -1,5 +1,7 @@ package org.gcube.informationsystem.resourceregistry.context; +import java.util.UUID; + import org.codehaus.jettison.json.JSONObject; import org.gcube.informationsystem.model.AccessType; import org.gcube.informationsystem.model.embedded.PropagationConstraint; @@ -140,12 +142,12 @@ public class IsParentOfManagement extends RelationManagement { if(jsonNode != null) { String type = getClassProperty(jsonNode); if(type != null && type.compareTo(erType) != 0) { - String error = String.format("Declared resourceType does not match with json representation %s!=%s", + String error = String.format("Requested type does not match with json representation %s!=%s", erType, type); logger.trace(error); throw new ResourceRegistryException(error); @@ -296,33 +296,33 @@ public abstract class ERManagement { return reallyDelete(); } - protected abstract boolean reallyAddToContext() throws ContextException, ResourceRegistryException; + protected abstract boolean reallyAddToContext(SecurityContext targetSecurityContext) throws ContextException, ResourceRegistryException; - public boolean internalAddToContext() throws ContextException, ResourceRegistryException { + public boolean internalAddToContext(SecurityContext targetSecurityContext) throws ContextException, ResourceRegistryException { try { - boolean ret = reallyAddToContext(); + boolean ret = reallyAddToContext(targetSecurityContext); HeaderUtility.updateModifiedByAndLastUpdate(element); ((OrientElement) element).save(); return ret && true; } catch(ResourceRegistryException e) { throw e; } catch(Exception e) { - throw new ResourceRegistryException("Error Adding " + erType + " to Current Context ", e.getCause()); + throw new ResourceRegistryException("Error Adding " + erType + " to " + targetSecurityContext.toString(), e.getCause()); } } - protected abstract boolean reallyRemoveFromContext() throws ContextException, ResourceRegistryException; + protected abstract boolean reallyRemoveFromContext(SecurityContext targetSecurityContext) throws ContextException, ResourceRegistryException; - public boolean internalRemoveFromContext() throws ContextException, ResourceRegistryException { + public boolean internalRemoveFromContext(SecurityContext targetSecurityContext) throws ContextException, ResourceRegistryException { try { - boolean ret = reallyRemoveFromContext(); + boolean ret = reallyRemoveFromContext(targetSecurityContext); HeaderUtility.updateModifiedByAndLastUpdate(element); ((OrientElement) element).save(); return ret && true; } catch(ResourceRegistryException e) { throw e; } catch(Exception e) { - throw new ResourceRegistryException("Error Removing " + erType + " from Current Context ", e.getCause()); + throw new ResourceRegistryException("Error Removing " + erType + " from " + targetSecurityContext.toString(), e.getCause()); } } @@ -568,29 +568,30 @@ public abstract class ERManagement { } } - public boolean addToContext() throws NotFoundException, ContextException, ResourceRegistryException { - logger.info("Going to add {} with UUID {} to Context {}", accessType.getName(), uuid, - getWorkingContext().toString()); + public boolean addToContext(UUID contextUUID) throws NotFoundException, ContextException, ResourceRegistryException { + logger.info("Going to add {} with UUID {} to Context with UUID {}", accessType.getName(), uuid, contextUUID); try { orientGraph = ContextUtility.getAdminSecurityContext().getGraph(PermissionMode.WRITER); orientGraph.setAutoStartTx(false); orientGraph.begin(); - boolean added = internalAddToContext(); + SecurityContext targetSecurityContext = ContextUtility.getInstance().getSecurityContextByUUID(contextUUID); + + boolean added = internalAddToContext(targetSecurityContext); orientGraph.commit(); - logger.info("{} with UUID {} successfully added to actual Context", accessType.getName(), uuid); + logger.info("{} with UUID {} successfully added to Context with UUID {}", erType, uuid, contextUUID); return added; } catch(ResourceRegistryException e) { - logger.error("Unable to add {} with UUID {} to actual Context", accessType.getName(), uuid); + logger.error("Unable to add {} with UUID {} to Context with UUID {}", erType, uuid, contextUUID); if(orientGraph != null) { orientGraph.rollback(); } throw e; } catch(Exception e) { - logger.error("Unable to add {} with UUID {} to actual Context", accessType.getName(), uuid, e); + logger.error("Unable to add {} with UUID {} to Context with UUID {}", erType, uuid, contextUUID, e); if(orientGraph != null) { orientGraph.rollback(); } @@ -602,8 +603,8 @@ public abstract class ERManagement { } } - public boolean removeFromContext() throws NotFoundException, ContextException, ResourceRegistryException { - logger.debug("Going to remove {} with UUID {} from actual Context", accessType.getName(), uuid); + public boolean removeFromContext(UUID contextUUID) throws NotFoundException, ContextException, ResourceRegistryException { + logger.debug("Going to remove {} with UUID {} from Context with UUID {}", erType, uuid, contextUUID); try { @@ -611,20 +612,22 @@ public abstract class ERManagement { orientGraph.setAutoStartTx(false); orientGraph.begin(); - boolean removed = internalRemoveFromContext(); + SecurityContext targetSecurityContext = ContextUtility.getInstance().getSecurityContextByUUID(contextUUID); + + boolean removed = internalRemoveFromContext(targetSecurityContext); orientGraph.commit(); - logger.info("{} with UUID {} successfully removed from actual Context", accessType.getName(), uuid); + logger.info("{} with UUID {} successfully removed from Context with UUID {}", erType, uuid, contextUUID); return removed; } catch(ResourceRegistryException e) { - logger.error("Unable to remove {} with UUID {} from actual Context", accessType.getName(), uuid); + logger.error("Unable to remove {} with UUID {} from Context with UUID {}", erType, uuid, contextUUID); if(orientGraph != null) { orientGraph.rollback(); } throw e; } catch(Exception e) { - logger.error("Unable to remove {} with UUID {} from actual Context", accessType.getName(), uuid, e); + logger.error("Unable to remove {} with UUID {} from Context with UUID {}", erType, uuid, contextUUID, e); if(orientGraph != null) { orientGraph.rollback(); } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/er/ERManagementUtility.java b/src/main/java/org/gcube/informationsystem/resourceregistry/er/ERManagementUtility.java index f7f6c7b..edd8980 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/er/ERManagementUtility.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/er/ERManagementUtility.java @@ -2,6 +2,8 @@ package org.gcube.informationsystem.resourceregistry.er; import java.util.UUID; +import org.gcube.informationsystem.model.AccessType; +import org.gcube.informationsystem.model.embedded.Embedded; import org.gcube.informationsystem.model.entity.Entity; import org.gcube.informationsystem.model.entity.Facet; import org.gcube.informationsystem.model.entity.Resource; @@ -39,6 +41,26 @@ public class ERManagementUtility { private static Logger logger = LoggerFactory.getLogger(EntityManagement.class); + public static AccessType getBaseAccessType(String type) throws ResourceRegistryException { + OClass oClass = SchemaManagementImpl.getTypeSchema(type, null); + + if(oClass.isSubClassOf(Resource.NAME)) { + return AccessType.RESOURCE; + } else if(oClass.isSubClassOf(Facet.NAME)) { + return AccessType.FACET; + } else if(oClass.isSubClassOf(ConsistsOf.NAME)) { + return AccessType.CONSISTS_OF; + } else if(oClass.isSubClassOf(IsRelatedTo.NAME)) { + return AccessType.IS_RELATED_TO; + } else if(oClass.isSubClassOf(Embedded.NAME)) { + return AccessType.EMBEDDED; + } + + throw new ResourceRegistryException(type + "is not a base type"); + + } + + @SuppressWarnings("rawtypes") public static ERManagement getERManagement(String type) throws ResourceRegistryException { diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/EntityManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/EntityManagement.java index 9406ef2..d93dd93 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/EntityManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/EntityManagement.java @@ -171,33 +171,33 @@ public abstract class EntityManagement extends ERManagement edges = getElement().getEdges(Direction.OUT); for(Edge edge : edges) { @SuppressWarnings("rawtypes") RelationManagement relationManagement = getRelationManagement(edge); - relationManagement.internalAddToContext(); + relationManagement.internalAddToContext(targetSecurityContext); } return true; } @Override - protected boolean reallyRemoveFromContext() throws ContextException, ResourceRegistryException { + protected boolean reallyRemoveFromContext(SecurityContext targetSecurityContext) throws ContextException, ResourceRegistryException { Iterable edges = getElement().getEdges(Direction.OUT); for(Edge edge : edges) { @SuppressWarnings("rawtypes") RelationManagement relationManagement = getRelationManagement(edge); - relationManagement.internalRemoveFromContext(); + relationManagement.internalRemoveFromContext(targetSecurityContext); } - getWorkingContext().removeElement(getElement(), orientGraph); + targetSecurityContext.removeElement(getElement(), orientGraph); return true; } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/RelationManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/RelationManagement.java index e72f161..1915b69 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/RelationManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/RelationManagement.java @@ -290,7 +290,7 @@ public abstract class RelationManagement 0) { logger.trace( - "{} point to {} which is not orphan ({} exists). Giving {} directive, it will be not remove from current context.", - element, target, edge, removeConstraint); + "{} point to {} which is not orphan ({} exists). Giving {} directive, it will be not remove from .", + element, target, edge, removeConstraint, targetSecurityContext); } else { - getTargetEntityManagement().internalRemoveFromContext(); + getTargetEntityManagement().internalRemoveFromContext(targetSecurityContext); } break; @@ -552,20 +552,22 @@ public abstract class RelationManagement - * It accepts idempotent query only..
- *
- * For query syntax please refer to
- * - * - * https://orientdb.com/docs/last/SQL-Syntax.html
- *
- * - * e.g. GET /resource-registry/access?query=SELECT FROM V - * - * @param query - * Defines the query to send to the backend. - * @param limit - * Defines the number of results you want returned, defaults to includeSubtypes results. - * @param fetchPlan - * Defines the fetching strategy you want to use. See - * - * https://orientdb.com/docs/last/Fetching-Strategies.html - * - * @return The JSON representation of the result - * @throws InvalidQueryException - * if the query is invalid or no idempotent + /* + * e.g. GET /resource-registry/access/contexts */ @GET + @Path(AccessPath.CONTEXTS_PATH_PART) @Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) - public String query(@QueryParam(AccessPath.QUERY_PARAM) String query, - @QueryParam(AccessPath.LIMIT_PARAM) Integer limit, - @QueryParam(AccessPath.FETCH_PLAN_PARAM) String fetchPlan) throws InvalidQueryException { - logger.info("Requested query (fetch plan {}, limit : {}):\n{}", fetchPlan, limit, query); - Query queryManager = new QueryImpl(); - return queryManager.query(query, limit, fetchPlan); + public String getContexts() + throws ContextNotFoundException, ResourceRegistryException { + ContextManagement contextManagement = new ContextManagement(); + logger.info("Requested to read all {}s", org.gcube.informationsystem.model.entity.Context.NAME); + return contextManagement.all(false); } + /* + * e.g. GET /resource-registry/access/contexts/c0f314e7-2807-4241-a792-2a6c79ed4fd0 + */ + @GET + @Path(AccessPath.CONTEXTS_PATH_PART + "{" + ID_PATH_PARAM + "}") + @Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) + public String getContext(@PathParam(ID_PATH_PARAM) String uuid) + throws ContextNotFoundException, ResourceRegistryException { + ContextManagement contextManagement = new ContextManagement(); + logger.info("Requested to read {} with id {} ", org.gcube.informationsystem.model.entity.Context.NAME, uuid); + contextManagement.setUUID(UUID.fromString(uuid)); + return contextManagement.read(); + } + + + /* + * e.g. GET /resource-registry/access/types/ContactFacet?polymorphic=true + */ + @GET + @Path(AccessPath.TYPES_PATH_PART + "/{" + TYPE_PATH_PARAM + "}") + @Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) + public String getType(@PathParam(TYPE_PATH_PARAM) String type, + @QueryParam(AccessPath.POLYMORPHIC_PARAM) @DefaultValue("false") Boolean polymorphic) + throws SchemaNotFoundException, ResourceRegistryException { + logger.info("Requested Schema for type {}", type); + SchemaManagement schemaManagement = new SchemaManagementImpl(); + return schemaManagement.read(type, polymorphic); + } + + @HEAD - @Path(AccessPath.INSTANCE_PATH_PART + "/" + "{" + TYPE_PATH_PARAM + "}" + "/{" + ID_PATH_PARAM + "}") + @Path(AccessPath.INSTANCES_PATH_PART + "/" + "{" + TYPE_PATH_PARAM + "}" + "/{" + ID_PATH_PARAM + "}") public Response exists(@PathParam(TYPE_PATH_PARAM) String type, @PathParam(ID_PATH_PARAM) String id) throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { CalledMethodProvider.instance.set(HTTPMETHOD.HEAD.name() + " /" + AccessPath.ACCESS_PATH_PART + "/" - + AccessPath.INSTANCE_PATH_PART + "/" + type + "/{" + ID_PATH_PARAM + "}"); + + AccessPath.INSTANCES_PATH_PART + "/" + type + "/{" + ID_PATH_PARAM + "}"); logger.info("Requested to check if {} with id {} exists", type, id); @SuppressWarnings("rawtypes") @@ -126,18 +134,19 @@ public class Access { } } + /* * e.g. GET - * /resource-registry/access/instance/ContactFacet/4d28077b-566d-4132-b073-f4edaf61dcb9 + * /resource-registry/access/instances/ContactFacet/4d28077b-566d-4132-b073-f4edaf61dcb9 */ @GET - @Path(AccessPath.INSTANCE_PATH_PART + "/" + "{" + TYPE_PATH_PARAM + "}" + "/{" + ID_PATH_PARAM + "}") + @Path(AccessPath.INSTANCES_PATH_PART + "/" + "{" + TYPE_PATH_PARAM + "}" + "/{" + ID_PATH_PARAM + "}") @Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) public String getInstance(@PathParam(TYPE_PATH_PARAM) String type, @PathParam(ID_PATH_PARAM) String id) throws NotFoundException, ResourceRegistryException { CalledMethodProvider.instance.set(HTTPMETHOD.GET.name() + " /" + AccessPath.ACCESS_PATH_PART + "/" - + AccessPath.INSTANCE_PATH_PART + "/" + type + "/{" + ID_PATH_PARAM + "}"); + + AccessPath.INSTANCES_PATH_PART + "/" + type + "/{" + ID_PATH_PARAM + "}"); logger.info("Requested {} with id {}", type, id); @@ -153,6 +162,47 @@ public class Access { return erManagement.read(); } + + + + /** + * It includeSubtypes to query Entities and Relations in the current Context.
+ * It accepts idempotent query only..
+ *
+ * For query syntax please refer to
+ * + * + * https://orientdb.com/docs/last/SQL-Syntax.html
+ *
+ * + * e.g. GET /resource-registry/access?query=SELECT FROM V + * + * @param query + * Defines the query to send to the backend. + * @param limit + * Defines the number of results you want returned, defaults to includeSubtypes results. + * @param fetchPlan + * Defines the fetching strategy you want to use. See + * + * https://orientdb.com/docs/last/Fetching-Strategies.html + * + * @return The JSON representation of the result + * @throws InvalidQueryException + * if the query is invalid or no idempotent + */ + @GET + @Path(AccessPath.QUERY_PATH_PART) + @Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) + public String query(@QueryParam(AccessPath.QUERY_PARAM) String query, + @QueryParam(AccessPath.LIMIT_PARAM) Integer limit, + @QueryParam(AccessPath.FETCH_PLAN_PARAM) String fetchPlan) throws InvalidQueryException { + logger.info("Requested query (fetch plan {}, limit : {}):\n{}", fetchPlan, limit, query); + Query queryManager = new QueryImpl(); + return queryManager.query(query, limit, fetchPlan); + } + + + /* * e.g. * GET /resource-registry/access/instances/EService?polymorphic=true @@ -251,46 +301,9 @@ public class Access { throw new ResourceRegistryException("Invalid Request"); } - /* - * e.g. GET /resource-registry/access/schema/ContactFacet?polymorphic=true - */ - @GET - @Path(AccessPath.SCHEMA_PATH_PART + "/{" + TYPE_PATH_PARAM + "}") - @Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) - public String getSchema(@PathParam(TYPE_PATH_PARAM) String type, - @QueryParam(AccessPath.POLYMORPHIC_PARAM) @DefaultValue("false") Boolean polymorphic) - throws SchemaNotFoundException, ResourceRegistryException { - logger.info("Requested Schema for type {}", type); - SchemaManagement schemaManagement = new SchemaManagementImpl(); - return schemaManagement.read(type, polymorphic); - } - /* - * e.g. GET /resource-registry/access/contexts/c0f314e7-2807-4241-a792-2a6c79ed4fd0 - */ - @GET - @Path(AccessPath.CONTEXTS_PATH_PART + "{" + ID_PATH_PARAM + "}") - @Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) - public String getContext(@PathParam(ID_PATH_PARAM) String uuid) - throws ContextNotFoundException, ResourceRegistryException { - ContextManagement contextManagement = new ContextManagement(); - logger.info("Requested to read {} with id {} ", org.gcube.informationsystem.model.entity.Context.NAME, uuid); - contextManagement.setUUID(UUID.fromString(uuid)); - return contextManagement.read(); - } - /* - * e.g. GET /resource-registry/access/contexts/c0f314e7-2807-4241-a792-2a6c79ed4fd0 - */ - @GET - @Path(AccessPath.CONTEXTS_PATH_PART) - @Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) - public String getContexts() - throws ContextNotFoundException, ResourceRegistryException { - ContextManagement contextManagement = new ContextManagement(); - logger.info("Requested to read all {}s", org.gcube.informationsystem.model.entity.Context.NAME); - return contextManagement.all(false); - } + } \ No newline at end of file diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/ContextManager.java b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/ContextManager.java index b3c2722..5b9e549 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/ContextManager.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/ContextManager.java @@ -2,26 +2,22 @@ package org.gcube.informationsystem.resourceregistry.rest; import java.util.UUID; -import javax.mail.Header; import javax.ws.rs.DELETE; import javax.ws.rs.GET; -import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.Response.Status; +import org.gcube.common.authorization.library.provider.CalledMethodProvider; import org.gcube.informationsystem.model.entity.Context; import org.gcube.informationsystem.resourceregistry.ResourceInitializer; import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; -import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextAlreadyPresentException; -import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextCreationException; import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException; import org.gcube.informationsystem.resourceregistry.api.rest.ContextPath; +import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPCall.HTTPMETHOD; import org.gcube.informationsystem.resourceregistry.context.ContextManagement; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -39,6 +35,11 @@ public class ContextManager { public static final String ID_PATH_PARAM = "id"; + protected void setCalledMethod(HTTPMETHOD httpMethod, String uuid) { + CalledMethodProvider.instance.set(httpMethod.name() + " /" + ContextPath.CONTEXTS_PATH_PART + "/" + uuid); + } + + @GET @Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) public String all() throws ContextNotFoundException, ResourceRegistryException { @@ -47,30 +48,6 @@ public class ContextManager { return contextManagement.all(false); } - /** - * e.g. POST /resource-registry/contexts - * - * BODY: {...} - * - */ - @POST - @Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) - public Response create(String json) - throws ContextAlreadyPresentException, ResourceRegistryException { - logger.info("Requested to create {} with json {}", Context.NAME, json); - ContextManagement contextManagement = new ContextManagement(); - contextManagement.setJSON(json); - UUID uuid = contextManagement.getUUID(); - if(uuid!=null) { - String error = String.format("Could not specify an UUID in % to create a %s using POST. Please use PUT instead and specify the UUID as path argument.", - Header.class.getSimpleName(), Context.NAME); - logger.info(error); - throw new ContextCreationException(error); - } - String ret = contextManagement.create(); - return Response.status(Status.CREATED).entity(ret).type(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8).build(); - } - /** * e.g. GET /resource-registry/contexts/c0f314e7-2807-4241-a792-2a6c79ed4fd0 * @param uuid @@ -82,6 +59,9 @@ public class ContextManager { @Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) public String read(@PathParam(ID_PATH_PARAM) String uuid) throws ContextNotFoundException, ResourceRegistryException { + + setCalledMethod(HTTPMETHOD.GET, uuid); + ContextManagement contextManagement = new ContextManagement(); logger.info("Requested to read {} with id {} ", Context.NAME, uuid); contextManagement.setUUID(UUID.fromString(uuid)); @@ -97,8 +77,11 @@ public class ContextManager { @PUT @Path("{" + ID_PATH_PARAM + "}") @Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) - public String update(@PathParam(ID_PATH_PARAM) String uuid, String json) + public String updateCreate(@PathParam(ID_PATH_PARAM) String uuid, String json) throws ResourceRegistryException { + + setCalledMethod(HTTPMETHOD.PUT, uuid); + logger.info("Requested to update {} with json {} ", Context.NAME, json); ContextManagement contextManagement = new ContextManagement(); contextManagement.setUUID(UUID.fromString(uuid)); @@ -127,6 +110,9 @@ public class ContextManager { @Path("{" + ID_PATH_PARAM + "}") public boolean delete(@PathParam(ID_PATH_PARAM) String uuid) throws ContextNotFoundException, ResourceRegistryException { + + setCalledMethod(HTTPMETHOD.DELETE, uuid); + logger.info("Requested to delete {} with id {} ", Context.NAME, uuid); ContextManagement contextManagement = new ContextManagement(); contextManagement.setUUID(UUID.fromString(uuid)); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/ERManager.java b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/ERManager.java index b7ccc22..76ebb00 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/ERManager.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/ERManager.java @@ -30,6 +30,7 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resour import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException; import org.gcube.informationsystem.resourceregistry.api.rest.ERPath; import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPCall.HTTPMETHOD; +import org.gcube.informationsystem.resourceregistry.context.ContextUtility; import org.gcube.informationsystem.resourceregistry.er.entity.FacetManagement; import org.gcube.informationsystem.resourceregistry.er.entity.ResourceManagement; import org.gcube.informationsystem.resourceregistry.er.relation.ConsistsOfManagement; @@ -40,6 +41,7 @@ import org.slf4j.LoggerFactory; /** * @author Luca Frosini (ISTI - CNR) */ +@Deprecated @Path(ERPath.ER_PATH_PART) public class ERManager { @@ -280,7 +282,7 @@ public class ERManager { logger.info("Requested to add {} with UUID {} to current {}", Resource.NAME, uuid, Context.NAME); ResourceManagement resourceManagement = new ResourceManagement(); resourceManagement.setUUID(UUID.fromString(uuid)); - return resourceManagement.addToContext(); + return resourceManagement.addToContext(ContextUtility.getCurrentSecurityContext().getUUID()); } /** @@ -296,7 +298,7 @@ public class ERManager { logger.info("Requested to add {} with UUID {} to current {}", Facet.NAME, uuid, Context.NAME); FacetManagement facetManagement = new FacetManagement(); facetManagement.setUUID(UUID.fromString(uuid)); - return facetManagement.addToContext(); + return facetManagement.addToContext(ContextUtility.getCurrentSecurityContext().getUUID()); } /** @@ -313,7 +315,7 @@ public class ERManager { logger.info("Requested to remove {} with UUID {} from current {}", Resource.NAME, uuid, Context.NAME); ResourceManagement resourceManagement = new ResourceManagement(); resourceManagement.setUUID(UUID.fromString(uuid)); - return resourceManagement.removeFromContext(); + return resourceManagement.removeFromContext(ContextUtility.getCurrentSecurityContext().getUUID()); } /** @@ -330,7 +332,7 @@ public class ERManager { logger.info("Requested to remove {} with UUID {} from current {}", Facet.NAME, uuid, Context.NAME); FacetManagement facetManagement = new FacetManagement(); facetManagement.setUUID(UUID.fromString(uuid)); - return facetManagement.removeFromContext(); + return facetManagement.removeFromContext(ContextUtility.getCurrentSecurityContext().getUUID()); } } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/InstancesManager.java b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/InstancesManager.java new file mode 100644 index 0000000..b32daa5 --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/InstancesManager.java @@ -0,0 +1,125 @@ +package org.gcube.informationsystem.resourceregistry.rest; + +import java.util.UUID; + +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +import org.gcube.common.authorization.library.provider.CalledMethodProvider; +import org.gcube.informationsystem.resourceregistry.ResourceInitializer; +import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; +import org.gcube.informationsystem.resourceregistry.api.rest.InstancePath; +import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPCall.HTTPMETHOD; +import org.gcube.informationsystem.resourceregistry.er.ERManagement; +import org.gcube.informationsystem.resourceregistry.er.ERManagementUtility; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Luca Frosini (ISTI - CNR) + */ +@Path(InstancePath.INSTANCES_PATH_PART) +public class InstancesManager { + + private static Logger logger = LoggerFactory.getLogger(InstancesManager.class); + + public static final String ID_PATH_PARAM = "id"; + public static final String TYPE_PATH_PARAM = "type"; + + protected void setCalledMethod(HTTPMETHOD httpMethod, String type) { + CalledMethodProvider.instance.set(httpMethod.name() + " /" + InstancePath.INSTANCES_PATH_PART + "/" + + type + "/{" + ID_PATH_PARAM + "}"); + } + + + /** + * e.g. GET /resource-registry/instances/{type}/4023d5b2-8601-47a5-83ef-49ffcbfc7d86 + * + */ + @GET + @Path("/{" + TYPE_PATH_PARAM + "}" + "/{" + ID_PATH_PARAM + "}") + @Consumes({MediaType.TEXT_PLAIN, ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8}) + @Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) + public String read(@PathParam(TYPE_PATH_PARAM) String type, @PathParam(ID_PATH_PARAM) String uuid, String json) + throws NotFoundException, ResourceRegistryException { + + setCalledMethod(HTTPMETHOD.GET, type); + + logger.info("Requested to read {} with id {}", type, uuid); + logger.trace("Requested to read {} with id {} with json {}", type, uuid, json); + + @SuppressWarnings("rawtypes") + ERManagement erManagement = ERManagementUtility.getERManagement(type); + + erManagement.setElementType(type); + erManagement.setUUID(UUID.fromString(uuid)); + return erManagement.read(); + } + + /** + * e.g. PUT /resource-registry/instances/{type}/4023d5b2-8601-47a5-83ef-49ffcbfc7d86 + * + * BODY: {...} + * + */ + @PUT + @Path("/{" + TYPE_PATH_PARAM + "}" + "/{" + ID_PATH_PARAM + "}") + @Consumes({MediaType.TEXT_PLAIN, ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8}) + @Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) + public String updateOrCreate(@PathParam(TYPE_PATH_PARAM) String type, @PathParam(ID_PATH_PARAM) String uuid, String json) + throws ResourceRegistryException { + + setCalledMethod(HTTPMETHOD.PUT, type); + + logger.info("Requested to update/create {} with id {}", type, uuid); + logger.trace("Requested to update/create {} with id {} with json {}", type, uuid, json); + + @SuppressWarnings("rawtypes") + ERManagement erManagement = ERManagementUtility.getERManagement(type); + + erManagement.setUUID(UUID.fromString(uuid)); + + boolean create = false; + try { + erManagement.read(); + } catch(NotFoundException e) { + create = true; + } + + erManagement.setElementType(type); + erManagement.setJSON(json); + if(create) { + return erManagement.create(); + } + + return erManagement.update(); + } + + /** + * e.g. DELETE /resource-registry/instances/{type}/4023d5b2-8601-47a5-83ef-49ffcbfc7d86 + * + */ + @DELETE + @Path("/{" + TYPE_PATH_PARAM + "}" + "/{" + ID_PATH_PARAM + "}") + public boolean delete(@PathParam(TYPE_PATH_PARAM) String type, @PathParam(ID_PATH_PARAM) String uuid) + throws ResourceRegistryException { + + setCalledMethod(HTTPMETHOD.DELETE, type); + + logger.info("Requested to delete {} with id {}", type, uuid); + + @SuppressWarnings("rawtypes") + ERManagement erManagement = ERManagementUtility.getERManagement(type); + + erManagement.setUUID(UUID.fromString(uuid)); + return erManagement.delete(); + } + +} diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/SchemaManager.java b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/SchemaManager.java index 4a1c528..9420eee 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/SchemaManager.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/SchemaManager.java @@ -17,7 +17,8 @@ import org.gcube.informationsystem.resourceregistry.ResourceInitializer; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException; -import org.gcube.informationsystem.resourceregistry.api.rest.SchemaPath; +import org.gcube.informationsystem.resourceregistry.api.rest.TypePath; +import org.gcube.informationsystem.resourceregistry.er.ERManagementUtility; import org.gcube.informationsystem.resourceregistry.schema.SchemaManagement; import org.gcube.informationsystem.resourceregistry.schema.SchemaManagementImpl; import org.slf4j.Logger; @@ -26,7 +27,7 @@ import org.slf4j.LoggerFactory; /** * @author Luca Frosini (ISTI - CNR) */ -@Path(SchemaPath.SCHEMAS_PATH_PART) +@Path(TypePath.TYPES_PATH_PART) public class SchemaManager { private static Logger logger = LoggerFactory.getLogger(SchemaManager.class); @@ -34,48 +35,29 @@ public class SchemaManager { public static final String TYPE_NAME_PATH_PARAM = "typeName"; /** - * e.g. PUT /resource-registry/schemas/ContactFacet + * e.g. PUT /resource-registry/types/ContactFacet * * BODY: {...} * - * @param baseType + * @param typeName * @param json * @return * @throws SchemaException + * @throws ResourceRegistryException */ @PUT @Path("{" + TYPE_NAME_PATH_PARAM + "}") @Consumes({MediaType.TEXT_PLAIN, ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8}) @Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) - public Response create(@PathParam(TYPE_NAME_PATH_PARAM) String typeName, @QueryParam(SchemaPath.BASE_TYPE_PATH_PARAM) String baseType, String json) + public Response create(@PathParam(TYPE_NAME_PATH_PARAM) String typeName, String json) throws SchemaException, ResourceRegistryException { - logger.info("Requested {} registration with schema {}", baseType, json); + logger.info("Requested {} registration with schema {}", typeName, json); AccessType accessType = null; try { - accessType = AccessType.valueOf(baseType.toUpperCase()); - switch(accessType) { - case EMBEDDED: - break; - - case FACET: - break; - - case RESOURCE: - break; - - case IS_RELATED_TO: - break; - - case CONSISTS_OF: - break; - - default: - throw new Exception(); - - } + accessType = ERManagementUtility.getBaseAccessType(typeName); } catch(Exception e) { - String error = String.format("Cannot register %s schema", baseType); + String error = String.format("Cannot register %s schema", typeName); throw new ResourceRegistryException(error); } @@ -86,14 +68,21 @@ public class SchemaManager { .build(); } - /* + /** * e.g. GET /resource-registry/schemas/ContactFacet?polymorphic=true + * + * + * @param typeName + * @param polymorphic + * @return + * @throws SchemaNotFoundException + * @throws ResourceRegistryException */ @GET @Path("{" + TYPE_NAME_PATH_PARAM + "}") @Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) public String read(@PathParam(TYPE_NAME_PATH_PARAM) String typeName, - @QueryParam(SchemaPath.POLYMORPHIC_PARAM) @DefaultValue("false") Boolean polymorphic) + @QueryParam(TypePath.POLYMORPHIC_PARAM) @DefaultValue("false") Boolean polymorphic) throws SchemaNotFoundException, ResourceRegistryException { logger.info("Requested Schema for type {}", typeName); SchemaManagement schemaManagement = new SchemaManagementImpl(); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/SharingManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/SharingManagement.java new file mode 100644 index 0000000..7afb022 --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/SharingManagement.java @@ -0,0 +1,110 @@ +package org.gcube.informationsystem.resourceregistry.rest; + +import java.util.UUID; + +import javax.ws.rs.DELETE; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; + +import org.gcube.common.authorization.library.provider.CalledMethodProvider; +import org.gcube.informationsystem.model.entity.Context; +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; +import org.gcube.informationsystem.resourceregistry.api.rest.SharingPath; +import org.gcube.informationsystem.resourceregistry.er.ERManagement; +import org.gcube.informationsystem.resourceregistry.er.ERManagementUtility; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Path(SharingPath.SHARING_PATH_PART) +public class SharingManagement { + + private static Logger logger = LoggerFactory.getLogger(SharingManagement.class); + + public static final String CONTEXT_ID_PATH_PARAM = "contextId"; + public static final String TYPE_PATH_PARAM = "type"; + public static final String ID_PATH_PARAM = "id"; + + /** + * e.g PUT + * /resource-registry/sharing/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 + * + */ + @PUT + @Path("/{" + CONTEXT_ID_PATH_PARAM + "}" + "/" + TYPE_PATH_PARAM + "/{" + ID_PATH_PARAM + "}") + public boolean add( + @PathParam(CONTEXT_ID_PATH_PARAM) String contextId, + @PathParam(TYPE_PATH_PARAM) String type, + @PathParam(ID_PATH_PARAM) String id) + throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { + + CalledMethodProvider.instance.set(""); + + logger.info("Requested to add {} with UUID {} to {} with UUID {}", type, id, Context.NAME, contextId); + + @SuppressWarnings("rawtypes") + ERManagement erManagement = ERManagementUtility.getERManagement(type); + UUID uuid = null; + try { + uuid = UUID.fromString(id); + } catch(Exception e) { + throw new ResourceRegistryException(e); + } + erManagement.setUUID(uuid); + + UUID contextUUID = null; + try { + contextUUID = UUID.fromString(contextId); + } catch(Exception e) { + throw new ResourceRegistryException(e); + } + + return erManagement.addToContext(contextUUID); + } + + + /** + * e.g DELETE + * /resource-registry/sharing/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("/{" + CONTEXT_ID_PATH_PARAM + "}" + "/" + TYPE_PATH_PARAM + "/{" + ID_PATH_PARAM + "}") + public boolean remove( + @PathParam(CONTEXT_ID_PATH_PARAM) String contextId, + @PathParam(TYPE_PATH_PARAM) String type, + @PathParam(ID_PATH_PARAM) String id) + throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { + + CalledMethodProvider.instance.set(""); + + logger.info("Requested to remove {} with UUID {} to {} with UUID {}", type, id, Context.NAME, contextId); + + @SuppressWarnings("rawtypes") + ERManagement erManagement = ERManagementUtility.getERManagement(type); + UUID uuid = null; + try { + uuid = UUID.fromString(id); + } catch(Exception e) { + throw new ResourceRegistryException(e); + } + erManagement.setUUID(uuid); + + UUID contextUUID = null; + try { + contextUUID = UUID.fromString(contextId); + } catch(Exception e) { + throw new ResourceRegistryException(e); + } + + return erManagement.removeFromContext(contextUUID); + } + + +} diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/schema/SchemaManagementImpl.java b/src/main/java/org/gcube/informationsystem/resourceregistry/schema/SchemaManagementImpl.java index e243ce6..63fc0aa 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/schema/SchemaManagementImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/schema/SchemaManagementImpl.java @@ -173,7 +173,7 @@ public class SchemaManagementImpl implements SchemaManagement { TypeDefinition typeDefinition = mapper.readValue(jsonSchema, TypeDefinition.class); if(typeName.compareTo(typeDefinition.getName())!=0) { - String error = String.format("Provided typename path argument %s does not match with the type name in the definition %S. Please be coherent.", typeName, typeDefinition.getName()); + String error = String.format("Provided type name path argument %s does not match with the type name in the definition %S. Please be coherent.", typeName, typeDefinition.getName()); throw new SchemaCreationException(error); } diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/er/entity/FacetManagementTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/er/entity/FacetManagementTest.java index 5335d28..061fdfd 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/er/entity/FacetManagementTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/er/entity/FacetManagementTest.java @@ -150,7 +150,7 @@ public class FacetManagementTest extends ScopedTest { facetManagement.setElementType(facetType); facetManagement.setUUID(facet.getHeader().getUUID()); - boolean added = facetManagement.addToContext(); + boolean added = facetManagement.addToContext(ContextUtility.getCurrentSecurityContext().getUUID()); Assert.assertTrue(added); return added; @@ -162,7 +162,7 @@ public class FacetManagementTest extends ScopedTest { facetManagement.setElementType(facetType); facetManagement.setUUID(facet.getHeader().getUUID()); - boolean added = facetManagement.removeFromContext(); + boolean added = facetManagement.removeFromContext(ContextUtility.getCurrentSecurityContext().getUUID()); Assert.assertTrue(added); return added; diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/er/multicontext/BasicTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/er/multicontext/BasicTest.java index d0c0d78..60c43df 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/er/multicontext/BasicTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/er/multicontext/BasicTest.java @@ -262,7 +262,7 @@ public class BasicTest extends ScopedTest { resourceManagement = new ResourceManagement(); resourceManagement.setUUID(uuid); - boolean addedToContext = resourceManagement.addToContext(); + boolean addedToContext = resourceManagement.addToContext(ContextUtility.getCurrentSecurityContext().getUUID()); Assert.assertTrue(addedToContext); resourceManagement = new ResourceManagement(); @@ -319,7 +319,7 @@ public class BasicTest extends ScopedTest { ContextNotFoundException, ResourceRegistryException { ResourceManagement resourceManagement = new ResourceManagement(); resourceManagement.setUUID(UUID.fromString("")); - resourceManagement.addToContext(); + resourceManagement.addToContext(ContextUtility.getCurrentSecurityContext().getUUID()); } @@ -400,7 +400,7 @@ public class BasicTest extends ScopedTest { isRelatedToManagement = new IsRelatedToManagement(); isRelatedToManagement.setUUID(hostsUUID); - isRelatedToManagement.addToContext(); + isRelatedToManagement.addToContext(ContextUtility.getCurrentSecurityContext().getUUID()); /* The addTocontext on the relation adds the source and target too. * So that I MUST be able to read HostinNode and EService @@ -420,7 +420,7 @@ public class BasicTest extends ScopedTest { resourceManagement = new ResourceManagement(); resourceManagement.setUUID(hnUUID); - boolean removed = resourceManagement.removeFromContext(); + boolean removed = resourceManagement.removeFromContext(ContextUtility.getCurrentSecurityContext().getUUID()); Assert.assertTrue(removed); /* The cascading MUST remove the relation and the target so that