diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/InstancesManager.java b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/InstancesManager.java index 3a5e3d8..1358171 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/InstancesManager.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/InstancesManager.java @@ -21,8 +21,11 @@ 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.exceptions.context.ContextNotFoundException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException; import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath; import org.gcube.informationsystem.resourceregistry.api.rest.InstancePath; +import org.gcube.informationsystem.resourceregistry.api.rest.SharingPath; import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagement; import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagementUtility; @@ -193,4 +196,30 @@ public class InstancesManager extends BaseRest { return Response.status(Status.NO_CONTENT).build(); } + + /* + * 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(); + } + } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/SharingManager.java b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/SharingManager.java index a7f800b..539f769 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/SharingManager.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/SharingManager.java @@ -4,23 +4,24 @@ import java.util.UUID; import javax.ws.rs.DELETE; import javax.ws.rs.GET; -import javax.ws.rs.PUT; +import javax.ws.rs.POST; 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.Response; import javax.ws.rs.core.Response.Status; import org.gcube.common.authorization.library.provider.CalledMethodProvider; import org.gcube.informationsystem.context.reference.entities.Context; -import org.gcube.informationsystem.resourceregistry.ResourceInitializer; 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.AccessPath; import org.gcube.informationsystem.resourceregistry.api.rest.SharingPath; +import org.gcube.informationsystem.resourceregistry.api.rest.SharingPath.SharingOperation; import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagement; import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagementUtility; +import org.gcube.informationsystem.resourceregistry.rest.extrahttpmethods.PATCH; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -32,53 +33,124 @@ public class SharingManager { private static Logger logger = LoggerFactory.getLogger(SharingManager.class); - /* - * protected void setRESTCalledMethod(HTTPMETHOD httpMethod, String type) { - * List list = new ArrayList<>(); - * list.add(SharingPath.SHARING_PATH_PART); list.add(type); list.add("{" + - * AccessPath.UUID_PATH_PARAM + "}"); list.add(SharingPath.CONTEXTS_PATH_PART); - * list.add("{" + AccessPath.CONTEXT_UUID_PATH_PARAM + "}"); - * Access.setRESTCalledMethod(httpMethod, list, null); } - */ - - /* - * GET /sharing/{TYPE_NAME}/{UUID}/contexts/ e.g GET - * /resource-registry/sharing/HostingNode/16032d09-3823-444e-a1ff-a67de4f350a8/ - * contexts Where 16032d09-3823-444e-a1ff-a67de4f350a8 is the HostingNode UUID + /** + * Add/Remove to/from the context identified by CONTEXT_UUID path parameter the list of instances contained in the body of the request. + * The the body is the following * - * 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(); - } - - /* - * PUT /sharing/{TYPE_NAME}/{UUID}/contexts/{CONTEXT_UUID} e.g PUT - * /resource-registry/sharing/HostingNode/16032d09-3823-444e-a1ff-a67de4f350a8/ - * contexts/67062c11-9c3a-4906-870d-7df6a43408b0 Where - * 16032d09-3823-444e-a1ff-a67de4f350a8 is the HostingNode UUID and + * [ + * {"@class" : "HostingNode", header : { "uuid" : "16032d09-3823-444e-a1ff-a67de4f350a8"}}, + * {"@class" : "Hosts", header : { "uuid" : "97ab8a6b-6b1b-4868-b8fc-ba48d0439ba9"}}, + * {"@class" : "EService", header : { "uuid" : "d3b1a29b-aa70-4a5a-be83-361a4209dd3e"}} + * ] + * + * + * Each instance is managed without considering the propagation constraint of relations. + * + * PATCH /sharing/contexts/{CONTEXT_UUID}?operation=(ADD|REMOVE) + * + * e.g + * PATCH /resource-registry/contexts/67062c11-9c3a-4906-870d-7df6a43408b0?operation=ADD + * PATCH /resource-registry/contexts/67062c11-9c3a-4906-870d-7df6a43408b0?operation=REMOVE + * + * where * 67062c11-9c3a-4906-870d-7df6a43408b0/ is the Context UUID * + * The body contains the list of instances to add/remvoe to/from the context identified by CONTEXT_UUID + * */ - @PUT - @Path("{" + AccessPath.TYPE_PATH_PARAM + "}" + "/{" + AccessPath.UUID_PATH_PARAM + "}/" - + SharingPath.CONTEXTS_PATH_PART + "/{" + AccessPath.CONTEXT_UUID_PATH_PARAM + "}") - public boolean add(@PathParam(AccessPath.TYPE_PATH_PARAM) String type, + @PATCH + @Path("{" + SharingPath.CONTEXTS_PATH_PART + "/{" + AccessPath.CONTEXT_UUID_PATH_PARAM + "}") + public boolean addRemoveToFromContextNoPropagationConstraint( + @PathParam(AccessPath.CONTEXT_UUID_PATH_PARAM) String contextId, + @QueryParam(SharingPath.OPERATION_QUERY_PARAMETER) SharingOperation operation, + String body) + throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { + + logger.info("Requested to add to {} with UUID {} the following instances {}", Context.NAME, contextId, body); + + if(operation == SharingOperation.ADD) { + CalledMethodProvider.instance.set("addToContextNoPropagationConstraint"); + }else { + CalledMethodProvider.instance.set("removeFromContextNoPropagationConstraint"); + } + + /* + boolean success = true; + for(any element in body) { + String type = ""; // get type + UUID instanceUUID = UUID.fromString(""); + @SuppressWarnings("rawtypes") + ElementManagement elementManagement = ElementManagementUtility.getERManagement(type); + elementManagement.setUUID(instanceUUID); + // success = success && return elementManagement.addToContextNoPropagationConstraint(UUID.fromString(contextId), body); + } + */ + return true; + } + + /** + * + * Get the list of instances affected by an add/remove to/from context the target instance identified by UUID path parameter + * + * GET /sharing/contexts/{CONTEXT_UUID}/{TYPE_NAME}/{UUID}?operation=(ADD|REMOVE) + * + * e.g + * GET /resource-registry/sharing/contexts/67062c11-9c3a-4906-870d-7df6a43408b0/HostingNode/16032d09-3823-444e-a1ff-a67de4f350a8?operation=ADD + * GET /resource-registry/sharing/contexts/67062c11-9c3a-4906-870d-7df6a43408b0/HostingNode/16032d09-3823-444e-a1ff-a67de4f350a8?operation=REMOVE + * + * where + * 67062c11-9c3a-4906-870d-7df6a43408b0 is the Context UUID and + * 16032d09-3823-444e-a1ff-a67de4f350a8 is the HostingNode UUID + * + */ + @GET + @Path("{" + SharingPath.CONTEXTS_PATH_PART + "/{" + AccessPath.CONTEXT_UUID_PATH_PARAM + "}/" + + AccessPath.TYPE_PATH_PARAM + "}" + "/{" + AccessPath.UUID_PATH_PARAM + "}") + public String checkAddRemove( + @PathParam(AccessPath.CONTEXT_UUID_PATH_PARAM) String contextId, + @PathParam(AccessPath.TYPE_PATH_PARAM) String type, @PathParam(AccessPath.UUID_PATH_PARAM) String instanceId, - @PathParam(AccessPath.CONTEXT_UUID_PATH_PARAM) String contextId) + @QueryParam(SharingPath.OPERATION_QUERY_PARAMETER) SharingOperation operation) + throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { + + logger.info("Requested to add {} with UUID {} to {} with UUID {}", type, instanceId, Context.NAME, contextId); + + if(operation == SharingOperation.ADD) { + CalledMethodProvider.instance.set("dryRunAddToContext"); + }else { + CalledMethodProvider.instance.set("dryRunRemoveFromContext"); + } + + + @SuppressWarnings("rawtypes") + ElementManagement elementManagement = ElementManagementUtility.getERManagement(type); + elementManagement.setUUID(UUID.fromString(instanceId)); + + if(operation == SharingOperation.ADD) { + // elementManagement.dryRunAddToContext(UUID.fromString(contextId)); + }else { + // elementManagement.dryRunRemoveFromContext(UUID.fromString(contextId)); + } + + return ""; + } + + /** + * POST /sharing/contexts/{CONTEXT_UUID}/{TYPE_NAME}/{UUID} + * e.g + * POST /resource-registry/sharing/contexts/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 + * + */ + @POST + @Path("{" + SharingPath.CONTEXTS_PATH_PART + "/{" + AccessPath.CONTEXT_UUID_PATH_PARAM + "}/" + + AccessPath.TYPE_PATH_PARAM + "}" + "/{" + AccessPath.UUID_PATH_PARAM + "}") + public boolean add( + @PathParam(AccessPath.CONTEXT_UUID_PATH_PARAM) String contextId, + @PathParam(AccessPath.TYPE_PATH_PARAM) String type, + @PathParam(AccessPath.UUID_PATH_PARAM) String instanceId) throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { logger.info("Requested to add {} with UUID {} to {} with UUID {}", type, instanceId, Context.NAME, contextId); @@ -92,23 +164,26 @@ public class SharingManager { } /* - * DELETE /sharing/{TYPE_NAME}/{UUID}/contexts/{CONTEXT_UUID} e.g DELETE - * /resource-registry/sharing/HostingNode/16032d09-3823-444e-a1ff-a67de4f350a8/ - * contexts/67062c11-9c3a-4906-870d-7df6a43408b0 Where - * 16032d09-3823-444e-a1ff-a67de4f350a8 is the HostingNode UUID and - * 67062c11-9c3a-4906-870d-7df6a43408b0 is the Context UUID + * DELETE /sharing/contexts/{CONTEXT_UUID}/{TYPE_NAME}/{UUID} + * e.g + * DELETE /resource-registry/sharing/contexts/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("{" + AccessPath.TYPE_PATH_PARAM + "}" + "/{" + AccessPath.UUID_PATH_PARAM + "}/" - + SharingPath.CONTEXTS_PATH_PART + "/{" + AccessPath.CONTEXT_UUID_PATH_PARAM + "}") - public Response remove(@PathParam(AccessPath.TYPE_PATH_PARAM) String type, - @PathParam(AccessPath.UUID_PATH_PARAM) String instanceId, - @PathParam(AccessPath.CONTEXT_UUID_PATH_PARAM) String contextId) + @Path("{" + SharingPath.CONTEXTS_PATH_PART + "/{" + AccessPath.CONTEXT_UUID_PATH_PARAM + "}/" + + AccessPath.TYPE_PATH_PARAM + "}" + "/{" + AccessPath.UUID_PATH_PARAM + "}") + public Response remove( + @PathParam(AccessPath.CONTEXT_UUID_PATH_PARAM) String contextId, + @PathParam(AccessPath.TYPE_PATH_PARAM) String type, + @PathParam(AccessPath.UUID_PATH_PARAM) String instanceId) throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { logger.info("Requested to remove {} with UUID {} to {} with UUID {}", type, instanceId, Context.NAME, contextId); - // setRESTCalledMethod(HTTPMETHOD.DELETE, type); + CalledMethodProvider.instance.set("removeFromContext"); @SuppressWarnings("rawtypes") diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/extrahttpmethods/PATCH.java b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/extrahttpmethods/PATCH.java new file mode 100644 index 0000000..d41cf5c --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/extrahttpmethods/PATCH.java @@ -0,0 +1,14 @@ +package org.gcube.informationsystem.resourceregistry.rest.extrahttpmethods; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import javax.ws.rs.HttpMethod; + +@Target({ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +@HttpMethod("PATCH") +public @interface PATCH { +} \ No newline at end of file