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@168972 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2018-06-07 16:12:06 +00:00
parent 5590c79548
commit 5a8812a5a0
4 changed files with 317 additions and 112 deletions

View File

@ -1,10 +1,14 @@
package org.gcube.informationsystem.resourceregistry.rest;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.HEAD;
@ -13,6 +17,7 @@ import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
@ -29,6 +34,7 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.context.Conte
import org.gcube.informationsystem.resourceregistry.api.exceptions.query.InvalidQueryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath;
import org.gcube.informationsystem.resourceregistry.api.rest.InstancePath;
import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPCall.HTTPMETHOD;
import org.gcube.informationsystem.resourceregistry.context.ContextManagement;
import org.gcube.informationsystem.resourceregistry.er.ERManagement;
@ -56,21 +62,64 @@ public class Access {
public static final String ID_PATH_PARAM = "id";
public static final String TYPE_PATH_PARAM = "type";
public static void setCalledMethod(HTTPMETHOD httpMethod, List<String> pathValues, Map<String, String> map) {
StringWriter stringWriter = new StringWriter();
stringWriter.append(httpMethod.name());
boolean first = true;
for(String value : pathValues){
stringWriter.append(first ? " /" : "/");
stringWriter.append(value);
}
first = true;
if(map!=null) {
for(String key : map.keySet()) {
stringWriter.append(first ? "?" : "&");
stringWriter.append(key);
stringWriter.append("=");
stringWriter.append(map.get(key));
}
}
CalledMethodProvider.instance.set(stringWriter.toString());
}
protected void setCalledMethodLocal(HTTPMETHOD httpMethod, String path) {
setCalledMethodLocal(httpMethod, path, null);
}
protected void setCalledMethodLocal(HTTPMETHOD httpMethod, String path, Map<String, String> map) {
List<String> list = new ArrayList<>();
list.add(path);
setCalledMethodLocal(httpMethod, list, map);
}
protected void setCalledMethodLocal(HTTPMETHOD httpMethod, List<String> pathValues) {
setCalledMethodLocal(httpMethod, pathValues, null);
}
protected void setCalledMethodLocal(HTTPMETHOD httpMethod, List<String> pathValues, Map<String, String> map) {
List<String> list = new ArrayList<>();
list.add(AccessPath.ACCESS_PATH_PART);
list.addAll(pathValues);
Access.setCalledMethod(httpMethod, list, map);
}
/*
* e.g. GET /resource-registry/access/contexts
* e.g. GET /access/contexts
*/
@GET
@Path(AccessPath.CONTEXTS_PATH_PART)
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
public String getContexts()
throws ContextNotFoundException, ResourceRegistryException {
ContextManagement contextManagement = new ContextManagement();
public String getAllContexts()
throws ResourceRegistryException {
logger.info("Requested to read all {}s", org.gcube.informationsystem.model.entity.Context.NAME);
setCalledMethodLocal(HTTPMETHOD.GET, AccessPath.CONTEXTS_PATH_PART);
ContextManagement contextManagement = new ContextManagement();
return contextManagement.all(false);
}
/*
* e.g. GET /resource-registry/access/contexts/c0f314e7-2807-4241-a792-2a6c79ed4fd0
* GET /access/contexts/{UUID}
* e.g. GET /access/contexts/c0f314e7-2807-4241-a792-2a6c79ed4fd0
*/
@GET
@Path(AccessPath.CONTEXTS_PATH_PART + "{" + ID_PATH_PARAM + "}")
@ -79,13 +128,18 @@ public class Access {
throws ContextNotFoundException, ResourceRegistryException {
ContextManagement contextManagement = new ContextManagement();
logger.info("Requested to read {} with id {} ", org.gcube.informationsystem.model.entity.Context.NAME, uuid);
List<String> pathValues = new ArrayList<>();
pathValues.add(AccessPath.CONTEXTS_PATH_PART);
pathValues.add(uuid);
setCalledMethodLocal(HTTPMETHOD.GET, pathValues);
contextManagement.setUUID(UUID.fromString(uuid));
return contextManagement.read();
}
/*
* e.g. GET /resource-registry/access/types/ContactFacet?polymorphic=true
* GET /access/types/{TYPE_NAME}[?polymorphic=false]
* e.g. GET /access/types/ContactFacet?polymorphic=true
*/
@GET
@Path(AccessPath.TYPES_PATH_PART + "/{" + TYPE_PATH_PARAM + "}")
@ -94,29 +148,72 @@ public class Access {
@QueryParam(AccessPath.POLYMORPHIC_PARAM) @DefaultValue("false") Boolean polymorphic)
throws SchemaNotFoundException, ResourceRegistryException {
logger.info("Requested Schema for type {}", type);
List<String> pathValues = new ArrayList<>();
pathValues.add(AccessPath.TYPES_PATH_PART);
pathValues.add(type);
Map<String, String> map = new HashMap<String, String>();
map.put(InstancePath.POLYMORPHIC_PARAM, polymorphic.toString());
setCalledMethodLocal(HTTPMETHOD.GET, pathValues, map);
SchemaManagement schemaManagement = new SchemaManagementImpl();
return schemaManagement.read(type, polymorphic);
}
@HEAD
@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.INSTANCES_PATH_PART + "/" + type + "/{" + ID_PATH_PARAM + "}");
/*
* GET /access/instances/{TYPE_NAME}[?polymorphic=true]
* e.g. GET /access/instances/ContactFacet?polymorphic=true
*
*/
@GET
@Path(AccessPath.INSTANCES_PATH_PART + "/{" + TYPE_PATH_PARAM + "}")
@Consumes({MediaType.TEXT_PLAIN, ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8})
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
public String getAllInstances(@PathParam(TYPE_PATH_PARAM) String type,
@QueryParam(InstancePath.POLYMORPHIC_PARAM) @DefaultValue("true") Boolean polymorphic)
throws NotFoundException, ResourceRegistryException {
logger.info("Requested all {}instances of {}", polymorphic ? InstancePath.POLYMORPHIC_PARAM + " ": "", type);
List<String> pathValues = new ArrayList<>();
pathValues.add(AccessPath.INSTANCES_PATH_PART);
pathValues.add(type);
Map<String, String> map = new HashMap<String, String>();
map.put(InstancePath.POLYMORPHIC_PARAM, polymorphic.toString());
setCalledMethodLocal(HTTPMETHOD.GET, pathValues, map);
logger.info("Requested to check if {} with id {} exists", type, id);
@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);
return erManagement.all(polymorphic);
}
/*
* HEAD /access/instances/{TYPE_NAME}/{UUID}
* e.g. HEAD /access/instances/ContactFacet/4023d5b2-8601-47a5-83ef-49ffcbfc7d86
*
*/
@HEAD
@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 Response instanceExists(@PathParam(TYPE_PATH_PARAM) String type, @PathParam(ID_PATH_PARAM) String uuid)
throws NotFoundException, ResourceRegistryException {
logger.info("Requested to check if {} with id {} exists", type, uuid);
List<String> pathValues = new ArrayList<>();
pathValues.add(AccessPath.INSTANCES_PATH_PART);
pathValues.add(type);
pathValues.add("{" + ID_PATH_PARAM + "}");
setCalledMethodLocal(HTTPMETHOD.HEAD, pathValues);
@SuppressWarnings("rawtypes")
ERManagement erManagement = ERManagementUtility.getERManagement(type);
try {
erManagement.setUUID(UUID.fromString(uuid));
boolean found = erManagement.exists();
if(found) {
return Response.status(Status.NO_CONTENT).build();
@ -134,37 +231,46 @@ public class Access {
}
}
/*
* e.g. GET
* /resource-registry/access/instances/ContactFacet/4d28077b-566d-4132-b073-f4edaf61dcb9
* GET /access/instances/{TYPE_NAME}/{UUID}
* e.g. GET /access/instances/ContactFacet/4023d5b2-8601-47a5-83ef-49ffcbfc7d86
*
*/
@GET
@Path(AccessPath.INSTANCES_PATH_PART + "/" + "{" + TYPE_PATH_PARAM + "}" + "/{" + ID_PATH_PARAM + "}")
@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 getInstance(@PathParam(TYPE_PATH_PARAM) String type, @PathParam(ID_PATH_PARAM) String id)
public String getInstance(@PathParam(TYPE_PATH_PARAM) String type, @PathParam(ID_PATH_PARAM) String uuid)
throws NotFoundException, ResourceRegistryException {
CalledMethodProvider.instance.set(HTTPMETHOD.GET.name() + " /" + AccessPath.ACCESS_PATH_PART + "/"
+ AccessPath.INSTANCES_PATH_PART + "/" + type + "/{" + ID_PATH_PARAM + "}");
logger.info("Requested {} with id {}", type, id);
logger.info("Requested to read {} with id {}", type, uuid);
List<String> pathValues = new ArrayList<>();
pathValues.add(AccessPath.INSTANCES_PATH_PART);
pathValues.add(type);
pathValues.add("{" + ID_PATH_PARAM + "}");
setCalledMethodLocal(HTTPMETHOD.GET, pathValues);
@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);
erManagement.setElementType(type);
erManagement.setUUID(UUID.fromString(uuid));
return erManagement.read();
}
/**
* It includeSubtypes to query Entities and Relations in the current Context.<br />
* It accepts idempotent query only.. <br />

View File

@ -1,5 +1,7 @@
package org.gcube.informationsystem.resourceregistry.rest;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.ws.rs.DELETE;
@ -9,12 +11,10 @@ import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
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.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;
@ -35,41 +35,54 @@ 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);
protected void setCalledMethod(HTTPMETHOD httpMethod) {
setCalledMethod(httpMethod, null);
}
protected void setCalledMethod(HTTPMETHOD httpMethod, String uuid) {
List<String> list = new ArrayList<>();
list.add(ContextPath.CONTEXTS_PATH_PART);
if(uuid != null) {
list.add(uuid);
}
Access.setCalledMethod(httpMethod, list, null);
}
/*
* GET /contexts
*
*/
@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);
setCalledMethod(HTTPMETHOD.GET);
ContextManagement contextManagement = new ContextManagement();
return contextManagement.all(false);
}
/**
* e.g. GET /resource-registry/contexts/c0f314e7-2807-4241-a792-2a6c79ed4fd0
* @param uuid
* @return
* @throws ContextException
/*
* GET /contexts/{UUID}
* e.g. GET /contexts/c0f314e7-2807-4241-a792-2a6c79ed4fd0
*
*/
@GET
@Path("{" + ID_PATH_PARAM + "}")
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
public String read(@PathParam(ID_PATH_PARAM) String uuid)
throws ContextNotFoundException, ResourceRegistryException {
logger.info("Requested to read {} with id {} ", Context.NAME, uuid);
setCalledMethod(HTTPMETHOD.GET, uuid);
ContextManagement contextManagement = new ContextManagement();
logger.info("Requested to read {} with id {} ", Context.NAME, uuid);
contextManagement.setUUID(UUID.fromString(uuid));
return contextManagement.read();
}
/**
* e.g. PUT /resource-registry/contexts/c0f314e7-2807-4241-a792-2a6c79ed4fd0
/*
* PUT /contexts/{UUID}
* e.g. PUT /contexts/c0f314e7-2807-4241-a792-2a6c79ed4fd0
*
* BODY: {...}
*
@ -77,43 +90,39 @@ public class ContextManager {
@PUT
@Path("{" + ID_PATH_PARAM + "}")
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
public String updateCreate(@PathParam(ID_PATH_PARAM) String uuid, String json)
throws ResourceRegistryException {
public String updateCreate(@PathParam(ID_PATH_PARAM) String uuid, String json) throws ResourceRegistryException {
logger.info("Requested to update/create {} with json {} ", Context.NAME, json);
setCalledMethod(HTTPMETHOD.PUT, uuid);
logger.info("Requested to update {} with json {} ", Context.NAME, json);
ContextManagement contextManagement = new ContextManagement();
contextManagement.setUUID(UUID.fromString(uuid));
boolean create = false;
try {
contextManagement.read();
}catch (NotFoundException e) {
} catch(NotFoundException e) {
create = true;
}
contextManagement.setJSON(json);
if(create){
if(create) {
return contextManagement.create();
}
return contextManagement.update();
}
/**
* e.g. DELETE /resource-registry/contexts/c0f314e7-2807-4241-a792-2a6c79ed4fd0
* @param uuid
* @return
* @throws ContextException
/*
* DELETE /contexts/{UUID}
* e.g. DELETE /contexts/c0f314e7-2807-4241-a792-2a6c79ed4fd0
*
*/
@DELETE
@Path("{" + ID_PATH_PARAM + "}")
public boolean delete(@PathParam(ID_PATH_PARAM) String uuid)
throws ContextNotFoundException, ResourceRegistryException {
logger.info("Requested to delete {} with id {} ", Context.NAME, uuid);
setCalledMethod(HTTPMETHOD.DELETE, uuid);
logger.info("Requested to delete {} with id {} ", Context.NAME, uuid);
ContextManagement contextManagement = new ContextManagement();
contextManagement.setUUID(UUID.fromString(uuid));
return contextManagement.delete();

View File

@ -1,18 +1,27 @@
package org.gcube.informationsystem.resourceregistry.rest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.HEAD;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
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.resourceregistry.ResourceInitializer;
import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.rest.InstancePath;
@ -33,27 +42,95 @@ public class InstancesManager {
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 + "}");
protected void setCalledMethod(HTTPMETHOD httpMethod, String type, Map<String, String> map) {
setCalledMethod(httpMethod, type, false, map);
}
protected void setCalledMethod(HTTPMETHOD httpMethod, String type, boolean uuid) {
setCalledMethod(httpMethod, type, uuid, null);
}
/**
* e.g. GET /resource-registry/instances/{type}/4023d5b2-8601-47a5-83ef-49ffcbfc7d86
protected void setCalledMethod(HTTPMETHOD httpMethod, String type, boolean uuid, Map<String, String> map) {
List<String> list = new ArrayList<>();
list.add(InstancePath.INSTANCES_PATH_PART);
list.add(type);
if(uuid) {
list.add("{" + ID_PATH_PARAM + "}");
}
Access.setCalledMethod(httpMethod, list, map);
}
/*
* GET /instances/{TYPE_NAME}[?polymorphic=true]
* e.g. GET /instances/ContactFacet?polymorphic=true
*
*/
@GET
@Path("/{" + TYPE_PATH_PARAM + "}")
@Consumes({MediaType.TEXT_PLAIN, ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8})
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
public String readAll(@PathParam(TYPE_PATH_PARAM) String type,
@QueryParam(InstancePath.POLYMORPHIC_PARAM) @DefaultValue("true") Boolean polymorphic)
throws NotFoundException, ResourceRegistryException {
logger.info("Requested all {}instances of {}", polymorphic ? InstancePath.POLYMORPHIC_PARAM + " ": "", type);
Map<String, String> map = new HashMap<String, String>();
map.put(InstancePath.POLYMORPHIC_PARAM, polymorphic.toString());
setCalledMethod(HTTPMETHOD.GET, type, map);
@SuppressWarnings("rawtypes")
ERManagement erManagement = ERManagementUtility.getERManagement(type);
return erManagement.all(polymorphic);
}
/*
* HEAD /instances/{TYPE_NAME}/{UUID}
* e.g. HEAD /instances/ContactFacet/4023d5b2-8601-47a5-83ef-49ffcbfc7d86
*
*/
@HEAD
@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 Response exists(@PathParam(TYPE_PATH_PARAM) String type, @PathParam(ID_PATH_PARAM) String uuid)
throws NotFoundException, ResourceRegistryException {
logger.info("Requested to check if {} with id {} exists", type, uuid);
setCalledMethod(HTTPMETHOD.HEAD, type, true);
@SuppressWarnings("rawtypes")
ERManagement erManagement = ERManagementUtility.getERManagement(type);
try {
erManagement.setUUID(UUID.fromString(uuid));
boolean found = erManagement.exists();
if(found) {
return Response.status(Status.NO_CONTENT).build();
} else {
// This code should never be reached due to exception management
// anyway adding it for safety reason
return Response.status(Status.NOT_FOUND).build();
}
} catch(NotFoundException e) {
return Response.status(Status.NOT_FOUND).build();
} catch(AvailableInAnotherContextException e) {
return Response.status(Status.FORBIDDEN).build();
} catch(ResourceRegistryException e) {
throw e;
}
}
/*
* GET /instances/{TYPE_NAME}/{UUID}
* e.g. GET /instances/ContactFacet/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)
public String read(@PathParam(TYPE_PATH_PARAM) String type, @PathParam(ID_PATH_PARAM) String uuid)
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);
setCalledMethod(HTTPMETHOD.GET, type, true);
@SuppressWarnings("rawtypes")
ERManagement erManagement = ERManagementUtility.getERManagement(type);
@ -63,8 +140,9 @@ public class InstancesManager {
return erManagement.read();
}
/**
* e.g. PUT /resource-registry/instances/{type}/4023d5b2-8601-47a5-83ef-49ffcbfc7d86
/*
* PUT /instances/{TYPE_NAME}/{UUID}
* e.g. PUT /instances/ContactFacet/4023d5b2-8601-47a5-83ef-49ffcbfc7d86
*
* BODY: {...}
*
@ -75,11 +153,9 @@ public class InstancesManager {
@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);
setCalledMethod(HTTPMETHOD.PUT, type, true);
@SuppressWarnings("rawtypes")
ERManagement erManagement = ERManagementUtility.getERManagement(type);
@ -102,22 +178,20 @@ public class InstancesManager {
return erManagement.update();
}
/**
* e.g. DELETE /resource-registry/instances/{type}/4023d5b2-8601-47a5-83ef-49ffcbfc7d86
/*
* DELETE /instances/{TYPE_NAME}/{UUID}
* e.g. DELETE /instances/ContactFacet/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);
setCalledMethod(HTTPMETHOD.DELETE, type, true);
@SuppressWarnings("rawtypes")
ERManagement erManagement = ERManagementUtility.getERManagement(type);
erManagement.setUUID(UUID.fromString(uuid));
return erManagement.delete();
}

View File

@ -1,5 +1,10 @@
package org.gcube.informationsystem.resourceregistry.rest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
@ -17,7 +22,9 @@ 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.InstancePath;
import org.gcube.informationsystem.resourceregistry.api.rest.TypePath;
import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPCall.HTTPMETHOD;
import org.gcube.informationsystem.resourceregistry.er.ERManagementUtility;
import org.gcube.informationsystem.resourceregistry.schema.SchemaManagement;
import org.gcube.informationsystem.resourceregistry.schema.SchemaManagementImpl;
@ -34,61 +41,70 @@ public class SchemaManager {
public static final String TYPE_NAME_PATH_PARAM = "typeName";
/**
* e.g. PUT /resource-registry/types/ContactFacet
protected void setCalledMethod(HTTPMETHOD httpMethod, String type) {
setCalledMethod(httpMethod, type, null);
}
protected void setCalledMethod(HTTPMETHOD httpMethod, String type, Boolean polymorphic) {
List<String> list = new ArrayList<>();
list.add(TypePath.TYPES_PATH_PART);
list.add(type);
Map<String, String> map = null;
if(polymorphic!=null) {
map = new HashMap<String, String>();
map.put(InstancePath.POLYMORPHIC_PARAM, polymorphic.toString());
}
Access.setCalledMethod(httpMethod, list, map);
}
/*
* PUT /types/{TYPE_NAME}
* e.g. PUT /types/ContactFacet
*
* BODY: {...}
*
* @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, String json)
public Response create(@PathParam(TYPE_NAME_PATH_PARAM) String type, String json)
throws SchemaException, ResourceRegistryException {
logger.info("Requested {} registration with schema {}", typeName, json);
logger.info("Requested {} creation with schema {}", type, json);
setCalledMethod(HTTPMETHOD.PUT, type);
AccessType accessType = null;
try {
accessType = ERManagementUtility.getBaseAccessType(typeName);
accessType = ERManagementUtility.getBaseAccessType(type);
} catch(Exception e) {
String error = String.format("Cannot register %s schema", typeName);
String error = String.format("Cannot register %s schema", type);
throw new ResourceRegistryException(error);
}
SchemaManagement schemaManagement = new SchemaManagementImpl();
((SchemaManagementImpl) schemaManagement).setTypeName(typeName);
((SchemaManagementImpl) schemaManagement).setTypeName(type);
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/schemas/ContactFacet?polymorphic=true
/*
* GET /types/{TYPE_NAME}
* e.g. GET /types/ContactFacet?polymorphic=false
*
*
* @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,
public String read(@PathParam(TYPE_NAME_PATH_PARAM) String type,
@QueryParam(TypePath.POLYMORPHIC_PARAM) @DefaultValue("false") Boolean polymorphic)
throws SchemaNotFoundException, ResourceRegistryException {
logger.info("Requested Schema for type {}", typeName);
logger.info("Requested Schema for type {}", type);
setCalledMethod(HTTPMETHOD.PUT, type, polymorphic);
SchemaManagement schemaManagement = new SchemaManagementImpl();
return schemaManagement.read(typeName, polymorphic);
return schemaManagement.read(type, polymorphic);
}
}