2018-06-06 19:26:19 +02:00
|
|
|
package org.gcube.informationsystem.resourceregistry.rest;
|
|
|
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
import javax.ws.rs.DELETE;
|
2020-11-03 16:30:24 +01:00
|
|
|
import javax.ws.rs.GET;
|
2018-06-06 19:26:19 +02:00
|
|
|
import javax.ws.rs.PUT;
|
|
|
|
import javax.ws.rs.Path;
|
|
|
|
import javax.ws.rs.PathParam;
|
2020-11-03 16:30:24 +01:00
|
|
|
import javax.ws.rs.Produces;
|
2019-02-13 12:33:29 +01: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;
|
2019-10-25 17:45:59 +02:00
|
|
|
import org.gcube.informationsystem.context.reference.entities.Context;
|
2020-11-03 16:30:24 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.ResourceInitializer;
|
2018-06-06 19:26:19 +02:00
|
|
|
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;
|
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.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
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
@Path(SharingPath.SHARING_PATH_PART)
|
|
|
|
public class SharingManagement {
|
2020-11-24 15:39:09 +01:00
|
|
|
|
2018-06-06 19:26:19 +02:00
|
|
|
private static Logger logger = LoggerFactory.getLogger(SharingManagement.class);
|
2020-11-24 15:39:09 +01:00
|
|
|
|
2020-03-16 14:51:13 +01:00
|
|
|
/*
|
2020-11-24 15:39:09 +01:00
|
|
|
* protected void setRESTCalledMethod(HTTPMETHOD httpMethod, String type) {
|
|
|
|
* List<String> 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); }
|
|
|
|
*/
|
|
|
|
|
2020-11-03 16:30:24 +01:00
|
|
|
/*
|
2020-11-24 15:39:09 +01:00
|
|
|
* 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
|
2020-11-03 16:30:24 +01:00
|
|
|
*
|
|
|
|
* Return a list of UUID identifying the context the instance belongs to.
|
|
|
|
*/
|
|
|
|
@GET
|
2020-11-24 15:39:09 +01:00
|
|
|
@Path("{" + AccessPath.TYPE_PATH_PARAM + "}" + "/{" + AccessPath.UUID_PATH_PARAM + "}/"
|
|
|
|
+ SharingPath.CONTEXTS_PATH_PART)
|
2020-11-03 16:30:24 +01:00
|
|
|
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
|
2020-11-24 15:39:09 +01:00
|
|
|
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)
|
2020-11-03 16:30:24 +01:00
|
|
|
throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
|
2020-11-11 14:17:28 +01:00
|
|
|
logger.info("Requested to get contexts of {} with UUID {}", type, instanceId);
|
|
|
|
CalledMethodProvider.instance.set("getInstanceContexts");
|
2020-11-24 15:39:09 +01:00
|
|
|
|
2020-11-03 16:30:24 +01:00
|
|
|
@SuppressWarnings("rawtypes")
|
|
|
|
ElementManagement erManagement = ElementManagementUtility.getERManagement(type);
|
2020-11-11 14:17:28 +01:00
|
|
|
erManagement.setUUID(UUID.fromString(instanceId));
|
2020-11-03 16:30:24 +01:00
|
|
|
return erManagement.getContexts();
|
|
|
|
}
|
2020-11-24 15:39:09 +01:00
|
|
|
|
2018-06-08 09:30:02 +02:00
|
|
|
/*
|
2020-11-24 15:39:09 +01:00
|
|
|
* 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
|
|
|
|
* 67062c11-9c3a-4906-870d-7df6a43408b0/ is the Context UUID
|
2018-06-06 19:26:19 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
@PUT
|
2020-11-24 15:39:09 +01:00
|
|
|
@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,
|
|
|
|
@PathParam(AccessPath.UUID_PATH_PARAM) String instanceId,
|
2020-11-03 16:30:24 +01:00
|
|
|
@PathParam(AccessPath.CONTEXT_UUID_PATH_PARAM) String contextId)
|
2018-06-06 19:26:19 +02:00
|
|
|
throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
|
2020-11-24 15:39:09 +01:00
|
|
|
|
2020-11-03 16:30:24 +01:00
|
|
|
logger.info("Requested to add {} with UUID {} to {} with UUID {}", type, instanceId, Context.NAME, contextId);
|
2020-03-16 14:51:13 +01:00
|
|
|
// setRESTCalledMethod(HTTPMETHOD.PUT, type);
|
|
|
|
CalledMethodProvider.instance.set("addToContext");
|
2020-11-24 15:39:09 +01:00
|
|
|
|
2018-06-06 19:26:19 +02:00
|
|
|
@SuppressWarnings("rawtypes")
|
2020-11-03 16:30:24 +01:00
|
|
|
ElementManagement elementManagement = ElementManagementUtility.getERManagement(type);
|
2020-11-11 14:17:28 +01:00
|
|
|
elementManagement.setUUID(UUID.fromString(instanceId));
|
|
|
|
return elementManagement.addToContext(UUID.fromString(contextId));
|
2018-06-06 19:26:19 +02:00
|
|
|
}
|
2020-11-24 15:39:09 +01:00
|
|
|
|
2018-06-08 09:30:02 +02:00
|
|
|
/*
|
2020-11-24 15:39:09 +01:00
|
|
|
* 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
|
2018-06-06 19:26:19 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
@DELETE
|
2020-11-24 15:39:09 +01:00
|
|
|
@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,
|
2020-11-03 16:30:24 +01:00
|
|
|
@PathParam(AccessPath.CONTEXT_UUID_PATH_PARAM) String contextId)
|
2018-06-06 19:26:19 +02:00
|
|
|
throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
|
2020-11-24 15:39:09 +01:00
|
|
|
logger.info("Requested to remove {} with UUID {} to {} with UUID {}", type, instanceId, Context.NAME,
|
|
|
|
contextId);
|
2020-03-16 14:51:13 +01:00
|
|
|
// setRESTCalledMethod(HTTPMETHOD.DELETE, type);
|
|
|
|
CalledMethodProvider.instance.set("removeFromContext");
|
2020-11-24 15:39:09 +01:00
|
|
|
|
2018-06-06 19:26:19 +02:00
|
|
|
@SuppressWarnings("rawtypes")
|
2020-11-03 16:30:24 +01:00
|
|
|
ElementManagement elementManagement = ElementManagementUtility.getERManagement(type);
|
2020-11-11 14:17:28 +01:00
|
|
|
elementManagement.setUUID(UUID.fromString(instanceId));
|
|
|
|
elementManagement.removeFromContext(UUID.fromString(contextId));
|
2020-11-24 15:39:09 +01:00
|
|
|
|
2019-02-13 12:33:29 +01:00
|
|
|
return Response.status(Status.NO_CONTENT).build();
|
2018-06-06 19:26:19 +02:00
|
|
|
}
|
2020-11-24 15:39:09 +01:00
|
|
|
|
2018-06-06 19:26:19 +02:00
|
|
|
}
|