From 4796e807efd525f607b810096a22270e9ee42298 Mon Sep 17 00:00:00 2001 From: "luca.frosini" Date: Mon, 4 Jun 2018 10:31:09 +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@167856 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../context/ContextManagement.java | 4 ++ .../resourceregistry/rest/Access.java | 29 ++++++++---- .../resourceregistry/rest/ContextManager.java | 47 ++++++++++++------- .../resourceregistry/rest/SchemaManager.java | 32 +++++++------ .../schema/SchemaManagementImpl.java | 19 ++++++-- 5 files changed, 87 insertions(+), 44 deletions(-) diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextManagement.java index 4366c8e..5956669 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextManagement.java @@ -60,6 +60,10 @@ public class ContextManagement extends EntityManagement { getWorkingContext(); } + public UUID getUUID() { + return uuid; + } + public String getName() { if(name == null) { if(element == null) { diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/Access.java b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/Access.java index 0f77a86..4271ecb 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/Access.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/Access.java @@ -266,22 +266,31 @@ public class Access { } /* - * e.g. GET /resource-registry/access/context/c0f314e7-2807-4241-a792-2a6c79ed4fd0 + * e.g. GET /resource-registry/access/contexts/c0f314e7-2807-4241-a792-2a6c79ed4fd0 */ @GET - @Path(AccessPath.CONTEXT_PATH_PART + "{" + ID_PATH_PARAM + "}") + @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(); - if(uuid.compareTo(AccessPath.ALL_PATH_PART)==0) { - logger.info("Requested to read all {}s", org.gcube.informationsystem.model.entity.Context.NAME); - return contextManagement.all(false); - }else { - logger.info("Requested to read {} with id {} ", org.gcube.informationsystem.model.entity.Context.NAME, uuid); - contextManagement.setUUID(UUID.fromString(uuid)); - return contextManagement.read(); - } + 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 c5cd91d..a197a6d 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/ContextManager.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/ContextManager.java @@ -2,6 +2,7 @@ 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; @@ -16,6 +17,7 @@ import org.gcube.informationsystem.model.entity.Context; import org.gcube.informationsystem.resourceregistry.ResourceInitializer; 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; @@ -26,30 +28,44 @@ import org.slf4j.LoggerFactory; /** * @author Luca Frosini (ISTI - CNR) */ -@Path(ContextPath.CONTEXT_PATH_PART) +@Path(ContextPath.CONTEXTS_PATH_PART) public class ContextManager { - + /** * Logger */ - private static Logger logger = LoggerFactory - .getLogger(ContextManager.class); - + private static Logger logger = LoggerFactory.getLogger(ContextManager.class); + public static final String ID_PATH_PARAM = "id"; - + + @GET + @Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) + public String all() throws ContextNotFoundException, ResourceRegistryException { + ContextManagement contextManagement = new ContextManagement(); + logger.info("Requested to read all {}s", Context.NAME); + return contextManagement.all(false); + } + /** * e.g. PUT /resource-registry/context * * BODY: {...} * */ - @PUT + @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(); } @@ -66,14 +82,9 @@ public class ContextManager { public String read(@PathParam(ID_PATH_PARAM) String uuid) throws ContextNotFoundException, ResourceRegistryException { ContextManagement contextManagement = new ContextManagement(); - if(uuid.compareTo(ContextPath.ALL_PATH_PART)==0) { - logger.info("Requested to read all {}s", Context.NAME); - return contextManagement.all(false); - }else { - logger.info("Requested to read {} with id {} ", Context.NAME, uuid); - contextManagement.setUUID(UUID.fromString(uuid)); - return contextManagement.read(); - } + logger.info("Requested to read {} with id {} ", Context.NAME, uuid); + contextManagement.setUUID(UUID.fromString(uuid)); + return contextManagement.read(); } /** @@ -82,12 +93,14 @@ public class ContextManager { * BODY: {...} * */ - @POST + @PUT + @Path("{" + ID_PATH_PARAM + "}") @Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) - public String update(String json) + public String update(@PathParam(ID_PATH_PARAM) String uuid, String json) throws ContextNotFoundException, ResourceRegistryException { logger.info("Requested to update {} with json {} ", Context.NAME, json); ContextManagement contextManagement = new ContextManagement(); + contextManagement.setUUID(UUID.fromString(uuid)); contextManagement.setJSON(json); return contextManagement.update(); } 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 838e29f..4a1c528 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/SchemaManager.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/SchemaManager.java @@ -26,33 +26,34 @@ import org.slf4j.LoggerFactory; /** * @author Luca Frosini (ISTI - CNR) */ -@Path(SchemaPath.SCHEMA_PATH_PART) +@Path(SchemaPath.SCHEMAS_PATH_PART) public class SchemaManager { private static Logger logger = LoggerFactory.getLogger(SchemaManager.class); - public static final String TYPE_PATH_PARAM = "type"; + + public static final String TYPE_NAME_PATH_PARAM = "typeName"; /** - * e.g. PUT /resource-registry/schema/{E-R} + * e.g. PUT /resource-registry/schemas/ContactFacet * * BODY: {...} * - * @param type + * @param baseType * @param json * @return * @throws SchemaException */ @PUT - @Path("{" + TYPE_PATH_PARAM + "}") + @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_PATH_PARAM) String type, String json) + public Response create(@PathParam(TYPE_NAME_PATH_PARAM) String typeName, @QueryParam(SchemaPath.BASE_TYPE_PATH_PARAM) String baseType, String json) throws SchemaException, ResourceRegistryException { - logger.info("Requested {} registration with schema {}", type, json); + logger.info("Requested {} registration with schema {}", baseType, json); AccessType accessType = null; try { - accessType = AccessType.valueOf(type.toUpperCase()); + accessType = AccessType.valueOf(baseType.toUpperCase()); switch(accessType) { case EMBEDDED: break; @@ -74,28 +75,31 @@ public class SchemaManager { } } catch(Exception e) { - String error = String.format("Cannot register %s schema", type); + String error = String.format("Cannot register %s schema", baseType); throw new ResourceRegistryException(error); } SchemaManagement schemaManagement = new SchemaManagementImpl(); + ((SchemaManagementImpl) schemaManagement).setTypeName(typeName); String ret = schemaManagement.create(json, accessType); return Response.status(Status.CREATED).entity(ret).type(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) .build(); } /* - * e.g. GET /resource-registry/schema/ContactFacet?polymorphic=true + * e.g. GET /resource-registry/schemas/ContactFacet?polymorphic=true */ @GET - @Path("{" + TYPE_PATH_PARAM + "}") + @Path("{" + TYPE_NAME_PATH_PARAM + "}") @Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) - public String read(@PathParam(TYPE_PATH_PARAM) String type, + public String read(@PathParam(TYPE_NAME_PATH_PARAM) String typeName, @QueryParam(SchemaPath.POLYMORPHIC_PARAM) @DefaultValue("false") Boolean polymorphic) throws SchemaNotFoundException, ResourceRegistryException { - logger.info("Requested Schema for type {}", type); + logger.info("Requested Schema for type {}", typeName); SchemaManagement schemaManagement = new SchemaManagementImpl(); - return schemaManagement.read(type, polymorphic); + return schemaManagement.read(typeName, polymorphic); } + + } 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 e60b1c3..e243ce6 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/schema/SchemaManagementImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/schema/SchemaManagementImpl.java @@ -17,6 +17,7 @@ import org.gcube.informationsystem.model.entity.Resource; import org.gcube.informationsystem.model.relation.Relation; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaAlreadyPresentException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaCreationException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException; import org.gcube.informationsystem.resourceregistry.context.ContextUtility; @@ -52,10 +53,16 @@ public class SchemaManagementImpl implements SchemaManagement { private static Logger logger = LoggerFactory.getLogger(SchemaManagementImpl.class); + protected String typeName; + protected static OClass getOClass(OSchema oSchema, String type) throws SchemaException { return oSchema.getClass(type); } + public void setTypeName(String typeName) { + this.typeName = typeName; + } + public static OClass getTypeSchema(OrientBaseGraph orientBaseGraph, String type, AccessType accessType) throws SchemaException { OMetadata oMetadata = orientBaseGraph.getRawGraph().getMetadata(); @@ -165,6 +172,12 @@ public class SchemaManagementImpl implements SchemaManagement { ObjectMapper mapper = new ObjectMapper(); 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()); + throw new SchemaCreationException(error); + } + + AdminSecurityContext adminSecurityContext = ContextUtility.getAdminSecurityContext(); orientGraphNoTx = adminSecurityContext.getGraphNoTx(PermissionMode.WRITER); @@ -189,7 +202,7 @@ public class SchemaManagementImpl implements SchemaManagement { } else { String error = String.format("Allowed superclass are %s, %s, %s, or any subclasses of them.", Entity.NAME, Relation.NAME, Embedded.NAME); - throw new ResourceRegistryException(error); + throw new SchemaCreationException(error); } try { @@ -222,7 +235,7 @@ public class SchemaManagementImpl implements SchemaManagement { if(Resource.class.isAssignableFrom(baseType.getTypeClass())) { Set properties = typeDefinition.getProperties(); if(properties != null && properties.size() > 0) { - throw new Exception("A Resource cannot contains any properties."); + throw new SchemaCreationException("A Resource cannot contains any properties."); } } else { for(Property property : typeDefinition.getProperties()) { @@ -300,7 +313,7 @@ public class SchemaManagementImpl implements SchemaManagement { } catch(SchemaException e) { throw e; } catch(Exception ex) { - throw new SchemaException(ex); + throw new SchemaCreationException(ex); } finally { if(orientGraphNoTx != null) { orientGraphNoTx.shutdown();