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
This commit is contained in:
Luca Frosini 2018-06-04 10:31:09 +00:00
parent a238a4baa2
commit 4796e807ef
5 changed files with 87 additions and 44 deletions

View File

@ -60,6 +60,10 @@ public class ContextManagement extends EntityManagement<Context> {
getWorkingContext(); getWorkingContext();
} }
public UUID getUUID() {
return uuid;
}
public String getName() { public String getName() {
if(name == null) { if(name == null) {
if(element == null) { if(element == null) {

View File

@ -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 @GET
@Path(AccessPath.CONTEXT_PATH_PART + "{" + ID_PATH_PARAM + "}") @Path(AccessPath.CONTEXTS_PATH_PART + "{" + ID_PATH_PARAM + "}")
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) @Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
public String getContext(@PathParam(ID_PATH_PARAM) String uuid) public String getContext(@PathParam(ID_PATH_PARAM) String uuid)
throws ContextNotFoundException, ResourceRegistryException { throws ContextNotFoundException, ResourceRegistryException {
ContextManagement contextManagement = new ContextManagement(); ContextManagement contextManagement = new ContextManagement();
if(uuid.compareTo(AccessPath.ALL_PATH_PART)==0) { logger.info("Requested to read {} with id {} ", org.gcube.informationsystem.model.entity.Context.NAME, uuid);
logger.info("Requested to read all {}s", org.gcube.informationsystem.model.entity.Context.NAME); contextManagement.setUUID(UUID.fromString(uuid));
return contextManagement.all(false); return contextManagement.read();
}else {
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);
}
} }

View File

@ -2,6 +2,7 @@ package org.gcube.informationsystem.resourceregistry.rest;
import java.util.UUID; import java.util.UUID;
import javax.mail.Header;
import javax.ws.rs.DELETE; import javax.ws.rs.DELETE;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.POST; 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.ResourceInitializer;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; 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.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.ContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.rest.ContextPath; import org.gcube.informationsystem.resourceregistry.api.rest.ContextPath;
@ -26,30 +28,44 @@ import org.slf4j.LoggerFactory;
/** /**
* @author Luca Frosini (ISTI - CNR) * @author Luca Frosini (ISTI - CNR)
*/ */
@Path(ContextPath.CONTEXT_PATH_PART) @Path(ContextPath.CONTEXTS_PATH_PART)
public class ContextManager { public class ContextManager {
/** /**
* Logger * Logger
*/ */
private static Logger logger = LoggerFactory private static Logger logger = LoggerFactory.getLogger(ContextManager.class);
.getLogger(ContextManager.class);
public static final String ID_PATH_PARAM = "id"; 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 * e.g. PUT /resource-registry/context
* *
* BODY: {...} * BODY: {...}
* *
*/ */
@PUT @POST
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) @Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
public Response create(String json) public Response create(String json)
throws ContextAlreadyPresentException, ResourceRegistryException { throws ContextAlreadyPresentException, ResourceRegistryException {
logger.info("Requested to create {} with json {}", Context.NAME, json); logger.info("Requested to create {} with json {}", Context.NAME, json);
ContextManagement contextManagement = new ContextManagement(); ContextManagement contextManagement = new ContextManagement();
contextManagement.setJSON(json); 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(); String ret = contextManagement.create();
return Response.status(Status.CREATED).entity(ret).type(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8).build(); 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) public String read(@PathParam(ID_PATH_PARAM) String uuid)
throws ContextNotFoundException, ResourceRegistryException { throws ContextNotFoundException, ResourceRegistryException {
ContextManagement contextManagement = new ContextManagement(); ContextManagement contextManagement = new ContextManagement();
if(uuid.compareTo(ContextPath.ALL_PATH_PART)==0) { logger.info("Requested to read {} with id {} ", Context.NAME, uuid);
logger.info("Requested to read all {}s", Context.NAME); contextManagement.setUUID(UUID.fromString(uuid));
return contextManagement.all(false); return contextManagement.read();
}else {
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: {...} * BODY: {...}
* *
*/ */
@POST @PUT
@Path("{" + ID_PATH_PARAM + "}")
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) @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 { throws ContextNotFoundException, ResourceRegistryException {
logger.info("Requested to update {} with json {} ", Context.NAME, json); logger.info("Requested to update {} with json {} ", Context.NAME, json);
ContextManagement contextManagement = new ContextManagement(); ContextManagement contextManagement = new ContextManagement();
contextManagement.setUUID(UUID.fromString(uuid));
contextManagement.setJSON(json); contextManagement.setJSON(json);
return contextManagement.update(); return contextManagement.update();
} }

View File

@ -26,33 +26,34 @@ import org.slf4j.LoggerFactory;
/** /**
* @author Luca Frosini (ISTI - CNR) * @author Luca Frosini (ISTI - CNR)
*/ */
@Path(SchemaPath.SCHEMA_PATH_PART) @Path(SchemaPath.SCHEMAS_PATH_PART)
public class SchemaManager { public class SchemaManager {
private static Logger logger = LoggerFactory.getLogger(SchemaManager.class); 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: {...} * BODY: {...}
* *
* @param type * @param baseType
* @param json * @param json
* @return * @return
* @throws SchemaException * @throws SchemaException
*/ */
@PUT @PUT
@Path("{" + TYPE_PATH_PARAM + "}") @Path("{" + TYPE_NAME_PATH_PARAM + "}")
@Consumes({MediaType.TEXT_PLAIN, ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8}) @Consumes({MediaType.TEXT_PLAIN, ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8})
@Produces(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 { throws SchemaException, ResourceRegistryException {
logger.info("Requested {} registration with schema {}", type, json); logger.info("Requested {} registration with schema {}", baseType, json);
AccessType accessType = null; AccessType accessType = null;
try { try {
accessType = AccessType.valueOf(type.toUpperCase()); accessType = AccessType.valueOf(baseType.toUpperCase());
switch(accessType) { switch(accessType) {
case EMBEDDED: case EMBEDDED:
break; break;
@ -74,28 +75,31 @@ public class SchemaManager {
} }
} catch(Exception e) { } 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); throw new ResourceRegistryException(error);
} }
SchemaManagement schemaManagement = new SchemaManagementImpl(); SchemaManagement schemaManagement = new SchemaManagementImpl();
((SchemaManagementImpl) schemaManagement).setTypeName(typeName);
String ret = schemaManagement.create(json, accessType); String ret = schemaManagement.create(json, accessType);
return Response.status(Status.CREATED).entity(ret).type(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) return Response.status(Status.CREATED).entity(ret).type(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
.build(); .build();
} }
/* /*
* e.g. GET /resource-registry/schema/ContactFacet?polymorphic=true * e.g. GET /resource-registry/schemas/ContactFacet?polymorphic=true
*/ */
@GET @GET
@Path("{" + TYPE_PATH_PARAM + "}") @Path("{" + TYPE_NAME_PATH_PARAM + "}")
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) @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) @QueryParam(SchemaPath.POLYMORPHIC_PARAM) @DefaultValue("false") Boolean polymorphic)
throws SchemaNotFoundException, ResourceRegistryException { throws SchemaNotFoundException, ResourceRegistryException {
logger.info("Requested Schema for type {}", type); logger.info("Requested Schema for type {}", typeName);
SchemaManagement schemaManagement = new SchemaManagementImpl(); SchemaManagement schemaManagement = new SchemaManagementImpl();
return schemaManagement.read(type, polymorphic); return schemaManagement.read(typeName, polymorphic);
} }
} }

View File

@ -17,6 +17,7 @@ import org.gcube.informationsystem.model.entity.Resource;
import org.gcube.informationsystem.model.relation.Relation; import org.gcube.informationsystem.model.relation.Relation;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; 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.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.SchemaException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
import org.gcube.informationsystem.resourceregistry.context.ContextUtility; import org.gcube.informationsystem.resourceregistry.context.ContextUtility;
@ -52,10 +53,16 @@ public class SchemaManagementImpl implements SchemaManagement {
private static Logger logger = LoggerFactory.getLogger(SchemaManagementImpl.class); private static Logger logger = LoggerFactory.getLogger(SchemaManagementImpl.class);
protected String typeName;
protected static OClass getOClass(OSchema oSchema, String type) throws SchemaException { protected static OClass getOClass(OSchema oSchema, String type) throws SchemaException {
return oSchema.getClass(type); return oSchema.getClass(type);
} }
public void setTypeName(String typeName) {
this.typeName = typeName;
}
public static OClass getTypeSchema(OrientBaseGraph orientBaseGraph, String type, AccessType accessType) public static OClass getTypeSchema(OrientBaseGraph orientBaseGraph, String type, AccessType accessType)
throws SchemaException { throws SchemaException {
OMetadata oMetadata = orientBaseGraph.getRawGraph().getMetadata(); OMetadata oMetadata = orientBaseGraph.getRawGraph().getMetadata();
@ -165,6 +172,12 @@ public class SchemaManagementImpl implements SchemaManagement {
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
TypeDefinition typeDefinition = mapper.readValue(jsonSchema, TypeDefinition.class); 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(); AdminSecurityContext adminSecurityContext = ContextUtility.getAdminSecurityContext();
orientGraphNoTx = adminSecurityContext.getGraphNoTx(PermissionMode.WRITER); orientGraphNoTx = adminSecurityContext.getGraphNoTx(PermissionMode.WRITER);
@ -189,7 +202,7 @@ public class SchemaManagementImpl implements SchemaManagement {
} else { } else {
String error = String.format("Allowed superclass are %s, %s, %s, or any subclasses of them.", String error = String.format("Allowed superclass are %s, %s, %s, or any subclasses of them.",
Entity.NAME, Relation.NAME, Embedded.NAME); Entity.NAME, Relation.NAME, Embedded.NAME);
throw new ResourceRegistryException(error); throw new SchemaCreationException(error);
} }
try { try {
@ -222,7 +235,7 @@ public class SchemaManagementImpl implements SchemaManagement {
if(Resource.class.isAssignableFrom(baseType.getTypeClass())) { if(Resource.class.isAssignableFrom(baseType.getTypeClass())) {
Set<Property> properties = typeDefinition.getProperties(); Set<Property> properties = typeDefinition.getProperties();
if(properties != null && properties.size() > 0) { 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 { } else {
for(Property property : typeDefinition.getProperties()) { for(Property property : typeDefinition.getProperties()) {
@ -300,7 +313,7 @@ public class SchemaManagementImpl implements SchemaManagement {
} catch(SchemaException e) { } catch(SchemaException e) {
throw e; throw e;
} catch(Exception ex) { } catch(Exception ex) {
throw new SchemaException(ex); throw new SchemaCreationException(ex);
} finally { } finally {
if(orientGraphNoTx != null) { if(orientGraphNoTx != null) {
orientGraphNoTx.shutdown(); orientGraphNoTx.shutdown();