2018-06-06 19:26:19 +02:00
|
|
|
package org.gcube.informationsystem.resourceregistry.rest;
|
|
|
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
import javax.ws.rs.Consumes;
|
|
|
|
import javax.ws.rs.DELETE;
|
2018-06-07 18:12:06 +02:00
|
|
|
import javax.ws.rs.DefaultValue;
|
2018-06-06 19:26:19 +02:00
|
|
|
import javax.ws.rs.GET;
|
2018-06-07 18:12:06 +02:00
|
|
|
import javax.ws.rs.HEAD;
|
2018-06-06 19:26:19 +02:00
|
|
|
import javax.ws.rs.PUT;
|
|
|
|
import javax.ws.rs.Path;
|
|
|
|
import javax.ws.rs.PathParam;
|
|
|
|
import javax.ws.rs.Produces;
|
2018-06-07 18:12:06 +02:00
|
|
|
import javax.ws.rs.QueryParam;
|
2018-06-06 19:26:19 +02:00
|
|
|
import javax.ws.rs.core.MediaType;
|
2018-06-07 18:12:06 +02:00
|
|
|
import javax.ws.rs.core.Response;
|
|
|
|
import javax.ws.rs.core.Response.Status;
|
2018-06-06 19:26:19 +02:00
|
|
|
|
2020-03-16 14:51:13 +01:00
|
|
|
import org.gcube.common.authorization.library.provider.CalledMethodProvider;
|
2018-06-06 19:26:19 +02:00
|
|
|
import org.gcube.informationsystem.resourceregistry.ResourceInitializer;
|
2018-06-07 18:12:06 +02:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException;
|
2018-06-06 19:26:19 +02:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
2021-01-24 23:33:06 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException;
|
2018-06-08 09:30:02 +02:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath;
|
2018-06-06 19:26:19 +02:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.rest.InstancePath;
|
2021-01-24 23:33:06 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.rest.SharingPath;
|
2020-01-30 11:04:16 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagement;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagementUtility;
|
2018-06-06 19:26:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
|
|
*/
|
|
|
|
@Path(InstancePath.INSTANCES_PATH_PART)
|
2020-10-27 15:36:28 +01:00
|
|
|
public class InstancesManager extends BaseRest {
|
2018-06-06 19:26:19 +02:00
|
|
|
|
2018-06-07 18:12:06 +02:00
|
|
|
/*
|
|
|
|
* GET /instances/{TYPE_NAME}[?polymorphic=true]
|
|
|
|
* e.g. GET /instances/ContactFacet?polymorphic=true
|
2018-06-06 19:26:19 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
@GET
|
2018-06-08 09:30:02 +02:00
|
|
|
@Path("/{" + AccessPath.TYPE_PATH_PARAM + "}")
|
2018-06-07 18:12:06 +02:00
|
|
|
@Consumes({MediaType.TEXT_PLAIN, ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8})
|
|
|
|
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
|
2018-06-08 09:30:02 +02:00
|
|
|
public String readAll(@PathParam(AccessPath.TYPE_PATH_PARAM) String type,
|
2018-06-07 18:12:06 +02:00
|
|
|
@QueryParam(InstancePath.POLYMORPHIC_PARAM) @DefaultValue("true") Boolean polymorphic)
|
|
|
|
throws NotFoundException, ResourceRegistryException {
|
2018-06-08 09:30:02 +02:00
|
|
|
logger.info("Requested all {}instances of {}", polymorphic ? InstancePath.POLYMORPHIC_PARAM + " " : "", type);
|
2021-03-02 09:13:53 +01:00
|
|
|
CalledMethodProvider.instance.set("listInstances");
|
|
|
|
|
2020-11-09 15:45:07 +01:00
|
|
|
checkHierarchicalMode();
|
|
|
|
checkIncludeInstancesContexts();
|
2018-06-07 18:12:06 +02:00
|
|
|
|
|
|
|
@SuppressWarnings("rawtypes")
|
2020-01-30 11:04:16 +01:00
|
|
|
ElementManagement erManagement = ElementManagementUtility.getERManagement(type);
|
2018-06-07 18:12:06 +02:00
|
|
|
return erManagement.all(polymorphic);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* HEAD /instances/{TYPE_NAME}/{UUID}
|
|
|
|
* e.g. HEAD /instances/ContactFacet/4023d5b2-8601-47a5-83ef-49ffcbfc7d86
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
@HEAD
|
2018-06-08 09:30:02 +02:00
|
|
|
@Path("/{" + AccessPath.TYPE_PATH_PARAM + "}" + "/{" + AccessPath.UUID_PATH_PARAM + "}")
|
2018-06-06 19:26:19 +02:00
|
|
|
@Consumes({MediaType.TEXT_PLAIN, ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8})
|
|
|
|
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
|
2018-06-08 09:30:02 +02:00
|
|
|
public Response exists(@PathParam(AccessPath.TYPE_PATH_PARAM) String type,
|
|
|
|
@PathParam(AccessPath.UUID_PATH_PARAM) String uuid) throws NotFoundException, ResourceRegistryException {
|
2018-06-07 18:12:06 +02:00
|
|
|
logger.info("Requested to check if {} with id {} exists", type, uuid);
|
2021-03-02 09:13:53 +01:00
|
|
|
CalledMethodProvider.instance.set("existInstance");
|
|
|
|
|
2020-11-09 15:45:07 +01:00
|
|
|
checkHierarchicalMode();
|
|
|
|
checkIncludeInstancesContexts();
|
2018-06-06 19:26:19 +02:00
|
|
|
|
2018-06-07 18:12:06 +02:00
|
|
|
@SuppressWarnings("rawtypes")
|
2020-01-30 11:04:16 +01:00
|
|
|
ElementManagement erManagement = ElementManagementUtility.getERManagement(type);
|
2018-06-07 18:12:06 +02:00
|
|
|
try {
|
|
|
|
erManagement.setUUID(UUID.fromString(uuid));
|
2018-06-12 17:24:19 +02:00
|
|
|
boolean found = erManagement.exists();
|
2018-06-07 18:12:06 +02:00
|
|
|
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
|
2018-06-08 09:30:02 +02:00
|
|
|
@Path("/{" + AccessPath.TYPE_PATH_PARAM + "}" + "/{" + AccessPath.UUID_PATH_PARAM + "}")
|
2018-06-07 18:12:06 +02:00
|
|
|
@Consumes({MediaType.TEXT_PLAIN, ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8})
|
|
|
|
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
|
2018-06-08 09:30:02 +02:00
|
|
|
public String read(@PathParam(AccessPath.TYPE_PATH_PARAM) String type,
|
|
|
|
@PathParam(AccessPath.UUID_PATH_PARAM) String uuid) throws NotFoundException, ResourceRegistryException {
|
2018-06-06 19:26:19 +02:00
|
|
|
logger.info("Requested to read {} with id {}", type, uuid);
|
2021-03-02 09:13:53 +01:00
|
|
|
CalledMethodProvider.instance.set("readInstance");
|
|
|
|
|
2020-11-09 15:45:07 +01:00
|
|
|
checkHierarchicalMode();
|
|
|
|
checkIncludeInstancesContexts();
|
2018-06-06 19:26:19 +02:00
|
|
|
|
|
|
|
@SuppressWarnings("rawtypes")
|
2020-01-30 11:04:16 +01:00
|
|
|
ElementManagement erManagement = ElementManagementUtility.getERManagement(type);
|
2018-06-06 19:26:19 +02:00
|
|
|
erManagement.setElementType(type);
|
|
|
|
erManagement.setUUID(UUID.fromString(uuid));
|
2021-02-05 17:50:16 +01:00
|
|
|
return erManagement.read().toString();
|
2018-06-06 19:26:19 +02:00
|
|
|
}
|
|
|
|
|
2018-06-07 18:12:06 +02:00
|
|
|
/*
|
|
|
|
* PUT /instances/{TYPE_NAME}/{UUID}
|
|
|
|
* e.g. PUT /instances/ContactFacet/4023d5b2-8601-47a5-83ef-49ffcbfc7d86
|
2018-06-06 19:26:19 +02:00
|
|
|
*
|
|
|
|
* BODY: {...}
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
@PUT
|
2018-06-08 09:30:02 +02:00
|
|
|
@Path("/{" + AccessPath.TYPE_PATH_PARAM + "}" + "/{" + AccessPath.UUID_PATH_PARAM + "}")
|
2018-06-06 19:26:19 +02:00
|
|
|
@Consumes({MediaType.TEXT_PLAIN, ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8})
|
|
|
|
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
|
2018-06-08 09:30:02 +02:00
|
|
|
public String updateOrCreate(@PathParam(AccessPath.TYPE_PATH_PARAM) String type,
|
|
|
|
@PathParam(AccessPath.UUID_PATH_PARAM) String uuid, String json) throws ResourceRegistryException {
|
2018-06-06 19:26:19 +02:00
|
|
|
logger.info("Requested to update/create {} with id {}", type, uuid);
|
|
|
|
logger.trace("Requested to update/create {} with id {} with json {}", type, uuid, json);
|
2020-03-16 14:51:13 +01:00
|
|
|
CalledMethodProvider.instance.set("updateInstance");
|
2018-06-06 19:26:19 +02:00
|
|
|
|
2018-06-12 17:55:46 +02:00
|
|
|
@SuppressWarnings("rawtypes")
|
2020-01-30 11:04:16 +01:00
|
|
|
ElementManagement erManagement = ElementManagementUtility.getERManagement(type);
|
2018-06-12 17:24:19 +02:00
|
|
|
erManagement.setUUID(UUID.fromString(uuid));
|
2018-06-06 19:26:19 +02:00
|
|
|
erManagement.setElementType(type);
|
2019-02-13 12:33:29 +01:00
|
|
|
erManagement.setJson(json);
|
2018-06-12 18:09:36 +02:00
|
|
|
return erManagement.createOrUpdate();
|
2018-06-06 19:26:19 +02:00
|
|
|
}
|
|
|
|
|
2018-06-07 18:12:06 +02:00
|
|
|
/*
|
|
|
|
* DELETE /instances/{TYPE_NAME}/{UUID}
|
|
|
|
* e.g. DELETE /instances/ContactFacet/4023d5b2-8601-47a5-83ef-49ffcbfc7d86
|
2018-06-06 19:26:19 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
@DELETE
|
2018-06-08 09:30:02 +02:00
|
|
|
@Path("/{" + AccessPath.TYPE_PATH_PARAM + "}" + "/{" + AccessPath.UUID_PATH_PARAM + "}")
|
2019-02-13 12:33:29 +01:00
|
|
|
public Response delete(@PathParam(AccessPath.TYPE_PATH_PARAM) String type,
|
2018-06-08 09:30:02 +02:00
|
|
|
@PathParam(AccessPath.UUID_PATH_PARAM) String uuid) throws ResourceRegistryException {
|
2018-06-06 19:26:19 +02:00
|
|
|
logger.info("Requested to delete {} with id {}", type, uuid);
|
2020-03-16 14:51:13 +01:00
|
|
|
CalledMethodProvider.instance.set("deleteInstance");
|
2018-06-07 18:12:06 +02:00
|
|
|
|
2018-06-06 19:26:19 +02:00
|
|
|
@SuppressWarnings("rawtypes")
|
2020-01-30 11:04:16 +01:00
|
|
|
ElementManagement erManagement = ElementManagementUtility.getERManagement(type);
|
2018-06-06 19:26:19 +02:00
|
|
|
erManagement.setUUID(UUID.fromString(uuid));
|
2019-02-13 12:33:29 +01:00
|
|
|
erManagement.delete();
|
|
|
|
|
|
|
|
return Response.status(Status.NO_CONTENT).build();
|
2018-06-06 19:26:19 +02:00
|
|
|
}
|
|
|
|
|
2021-01-24 23:33:06 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* GET /instances/{TYPE_NAME}/{UUID}/contexts/ e.g GET
|
|
|
|
* /resource-registry/instances/HostingNode/16032d09-3823-444e-a1ff-a67de4f350a8/contexts
|
|
|
|
*
|
|
|
|
* Where 16032d09-3823-444e-a1ff-a67de4f350a8 is the HostingNode UUID
|
|
|
|
*
|
|
|
|
* Return a list of UUID identifying the context the instance belongs to.
|
|
|
|
*/
|
|
|
|
@GET
|
|
|
|
@Path("{" + AccessPath.TYPE_PATH_PARAM + "}" + "/{" + AccessPath.UUID_PATH_PARAM + "}/"
|
|
|
|
+ SharingPath.CONTEXTS_PATH_PART)
|
|
|
|
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
|
|
|
|
public String getInstanceContexts(@PathParam(AccessPath.TYPE_PATH_PARAM) String type,
|
|
|
|
@PathParam(AccessPath.UUID_PATH_PARAM) String instanceId,
|
|
|
|
@PathParam(AccessPath.CONTEXT_UUID_PATH_PARAM) String contextId)
|
|
|
|
throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
|
|
|
|
logger.info("Requested to get contexts of {} with UUID {}", type, instanceId);
|
|
|
|
CalledMethodProvider.instance.set("getInstanceContexts");
|
|
|
|
|
|
|
|
@SuppressWarnings("rawtypes")
|
|
|
|
ElementManagement erManagement = ElementManagementUtility.getERManagement(type);
|
|
|
|
erManagement.setUUID(UUID.fromString(instanceId));
|
|
|
|
return erManagement.getContexts();
|
|
|
|
}
|
|
|
|
|
2018-06-06 19:26:19 +02:00
|
|
|
}
|