resource-registry/src/main/java/org/gcube/informationsystem/resourceregistry/rest/SharingManagement.java

111 lines
3.7 KiB
Java
Raw Normal View History

package org.gcube.informationsystem.resourceregistry.rest;
import java.util.UUID;
import javax.ws.rs.DELETE;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import org.gcube.common.authorization.library.provider.CalledMethodProvider;
import org.gcube.informationsystem.model.entity.Context;
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.SharingPath;
import org.gcube.informationsystem.resourceregistry.er.ERManagement;
import org.gcube.informationsystem.resourceregistry.er.ERManagementUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Path(SharingPath.SHARING_PATH_PART)
public class SharingManagement {
private static Logger logger = LoggerFactory.getLogger(SharingManagement.class);
public static final String CONTEXT_ID_PATH_PARAM = "contextId";
public static final String TYPE_PATH_PARAM = "type";
public static final String ID_PATH_PARAM = "id";
/**
* e.g PUT
* /resource-registry/sharing/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
*
*/
@PUT
@Path("/{" + CONTEXT_ID_PATH_PARAM + "}" + "/" + TYPE_PATH_PARAM + "/{" + ID_PATH_PARAM + "}")
public boolean add(
@PathParam(CONTEXT_ID_PATH_PARAM) String contextId,
@PathParam(TYPE_PATH_PARAM) String type,
@PathParam(ID_PATH_PARAM) String id)
throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
CalledMethodProvider.instance.set("");
logger.info("Requested to add {} with UUID {} to {} with UUID {}", type, id, Context.NAME, contextId);
@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);
UUID contextUUID = null;
try {
contextUUID = UUID.fromString(contextId);
} catch(Exception e) {
throw new ResourceRegistryException(e);
}
return erManagement.addToContext(contextUUID);
}
/**
* e.g DELETE
* /resource-registry/sharing/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("/{" + CONTEXT_ID_PATH_PARAM + "}" + "/" + TYPE_PATH_PARAM + "/{" + ID_PATH_PARAM + "}")
public boolean remove(
@PathParam(CONTEXT_ID_PATH_PARAM) String contextId,
@PathParam(TYPE_PATH_PARAM) String type,
@PathParam(ID_PATH_PARAM) String id)
throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
CalledMethodProvider.instance.set("");
logger.info("Requested to remove {} with UUID {} to {} with UUID {}", type, id, Context.NAME, contextId);
@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);
UUID contextUUID = null;
try {
contextUUID = UUID.fromString(contextId);
} catch(Exception e) {
throw new ResourceRegistryException(e);
}
return erManagement.removeFromContext(contextUUID);
}
}