2016-12-22 17:27:26 +01:00
|
|
|
package org.gcube.informationsystem.resourceregistry.rest;
|
2016-07-18 11:57:12 +02:00
|
|
|
|
2016-10-27 17:46:09 +02:00
|
|
|
import java.util.UUID;
|
|
|
|
|
2018-06-08 09:30:02 +02:00
|
|
|
import javax.ws.rs.Consumes;
|
2016-07-18 11:57:12 +02:00
|
|
|
import javax.ws.rs.DELETE;
|
2017-11-07 16:50:59 +01:00
|
|
|
import javax.ws.rs.GET;
|
2016-07-18 11:57:12 +02:00
|
|
|
import javax.ws.rs.PUT;
|
|
|
|
import javax.ws.rs.Path;
|
|
|
|
import javax.ws.rs.PathParam;
|
2016-10-27 17:46:09 +02:00
|
|
|
import javax.ws.rs.Produces;
|
2018-06-08 09:30:02 +02:00
|
|
|
import javax.ws.rs.core.MediaType;
|
2019-02-13 12:33:29 +01:00
|
|
|
import javax.ws.rs.core.Response;
|
|
|
|
import javax.ws.rs.core.Response.Status;
|
2016-07-18 11:57:12 +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;
|
2017-03-24 17:04:09 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.ResourceInitializer;
|
2016-12-19 14:59:27 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
2016-07-18 11:57:12 +02:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException;
|
2018-06-08 09:30:02 +02:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath;
|
2016-09-01 17:11:44 +02:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.rest.ContextPath;
|
2020-01-27 17:07:37 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.contexts.entities.ContextManagement;
|
2016-07-18 11:57:12 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
/**
|
2016-10-12 14:33:23 +02:00
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
2016-07-18 11:57:12 +02:00
|
|
|
*/
|
2018-06-04 12:31:09 +02:00
|
|
|
@Path(ContextPath.CONTEXTS_PATH_PART)
|
2016-07-18 11:57:12 +02:00
|
|
|
public class ContextManager {
|
2018-06-04 12:31:09 +02:00
|
|
|
|
2016-07-18 11:57:12 +02:00
|
|
|
/**
|
|
|
|
* Logger
|
|
|
|
*/
|
2018-06-04 12:31:09 +02:00
|
|
|
private static Logger logger = LoggerFactory.getLogger(ContextManager.class);
|
|
|
|
|
2020-03-16 14:51:13 +01:00
|
|
|
/*
|
|
|
|
protected void setRESTCalledMethod(HTTPMETHOD httpMethod) {
|
|
|
|
setRESTCalledMethod(httpMethod, null);
|
2018-06-06 19:26:19 +02:00
|
|
|
}
|
|
|
|
|
2020-03-16 14:51:13 +01:00
|
|
|
protected void setRESTCalledMethod(HTTPMETHOD httpMethod, String uuid) {
|
2018-06-07 18:12:06 +02:00
|
|
|
List<String> list = new ArrayList<>();
|
|
|
|
list.add(ContextPath.CONTEXTS_PATH_PART);
|
|
|
|
if(uuid != null) {
|
|
|
|
list.add(uuid);
|
|
|
|
}
|
2020-03-16 14:51:13 +01:00
|
|
|
Access.setRESTCalledMethod(httpMethod, list, null);
|
2018-06-07 18:12:06 +02:00
|
|
|
}
|
2020-03-16 14:51:13 +01:00
|
|
|
*/
|
2018-06-06 19:26:19 +02:00
|
|
|
|
2018-06-07 18:12:06 +02:00
|
|
|
/*
|
|
|
|
* GET /contexts
|
|
|
|
*
|
|
|
|
*/
|
2018-06-04 12:31:09 +02:00
|
|
|
@GET
|
2018-06-08 09:30:02 +02:00
|
|
|
@Consumes({MediaType.TEXT_PLAIN, ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8})
|
2018-06-04 12:31:09 +02:00
|
|
|
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
|
|
|
|
public String all() throws ContextNotFoundException, ResourceRegistryException {
|
|
|
|
logger.info("Requested to read all {}s", Context.NAME);
|
2020-03-16 14:51:13 +01:00
|
|
|
// setRESTCalledMethod(HTTPMETHOD.GET);
|
|
|
|
CalledMethodProvider.instance.set("listContexts");
|
2018-06-07 18:12:06 +02:00
|
|
|
|
|
|
|
ContextManagement contextManagement = new ContextManagement();
|
2018-06-04 12:31:09 +02:00
|
|
|
return contextManagement.all(false);
|
|
|
|
}
|
|
|
|
|
2018-06-07 18:12:06 +02:00
|
|
|
/*
|
|
|
|
* GET /contexts/{UUID}
|
|
|
|
* e.g. GET /contexts/c0f314e7-2807-4241-a792-2a6c79ed4fd0
|
|
|
|
*
|
2017-11-07 16:50:59 +01:00
|
|
|
*/
|
|
|
|
@GET
|
2018-06-08 09:30:02 +02:00
|
|
|
@Path("{" + AccessPath.CONTEXT_UUID_PATH_PARAM + "}")
|
|
|
|
@Consumes({MediaType.TEXT_PLAIN, ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8})
|
2017-11-07 16:50:59 +01:00
|
|
|
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
|
2018-06-08 09:30:02 +02:00
|
|
|
public String read(@PathParam(AccessPath.CONTEXT_UUID_PATH_PARAM) String uuid)
|
2017-11-23 09:39:21 +01:00
|
|
|
throws ContextNotFoundException, ResourceRegistryException {
|
2018-06-12 11:23:15 +02:00
|
|
|
if(uuid.compareTo(AccessPath.CURRENT_CONTEXT)==0){
|
|
|
|
uuid = ContextUtility.getCurrentSecurityContext().getUUID().toString();
|
|
|
|
}
|
2018-06-12 12:22:58 +02:00
|
|
|
logger.info("Requested to read {} with id {} ", Context.NAME, uuid);
|
2020-03-16 14:51:13 +01:00
|
|
|
//setRESTCalledMethod(HTTPMETHOD.GET, uuid);
|
|
|
|
CalledMethodProvider.instance.set("readContext");
|
2018-06-12 11:23:15 +02:00
|
|
|
|
2018-06-12 12:22:58 +02:00
|
|
|
ContextManagement contextManagement = new ContextManagement();
|
2018-06-04 12:31:09 +02:00
|
|
|
contextManagement.setUUID(UUID.fromString(uuid));
|
2021-02-05 17:50:16 +01:00
|
|
|
return contextManagement.readAsString();
|
2017-11-07 16:50:59 +01:00
|
|
|
}
|
|
|
|
|
2018-06-07 18:12:06 +02:00
|
|
|
/*
|
|
|
|
* PUT /contexts/{UUID}
|
|
|
|
* e.g. PUT /contexts/c0f314e7-2807-4241-a792-2a6c79ed4fd0
|
2017-11-23 15:10:08 +01:00
|
|
|
*
|
|
|
|
* BODY: {...}
|
|
|
|
*
|
|
|
|
*/
|
2018-06-04 12:31:09 +02:00
|
|
|
@PUT
|
2018-06-08 09:30:02 +02:00
|
|
|
@Path("{" + AccessPath.CONTEXT_UUID_PATH_PARAM + "}")
|
|
|
|
@Consumes({MediaType.TEXT_PLAIN, ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8})
|
2017-11-23 15:10:08 +01:00
|
|
|
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
|
2018-06-08 09:30:02 +02:00
|
|
|
public String updateCreate(@PathParam(AccessPath.CONTEXT_UUID_PATH_PARAM) String uuid, String json)
|
|
|
|
throws ResourceRegistryException {
|
2018-06-07 18:12:06 +02:00
|
|
|
logger.info("Requested to update/create {} with json {} ", Context.NAME, json);
|
2020-03-16 14:51:13 +01:00
|
|
|
//setRESTCalledMethod(HTTPMETHOD.PUT, uuid);
|
|
|
|
CalledMethodProvider.instance.set("updateContext");
|
2018-06-06 19:26:19 +02:00
|
|
|
|
2017-11-23 15:10:08 +01:00
|
|
|
ContextManagement contextManagement = new ContextManagement();
|
2018-06-04 12:31:09 +02:00
|
|
|
contextManagement.setUUID(UUID.fromString(uuid));
|
2019-02-13 12:33:29 +01:00
|
|
|
contextManagement.setJson(json);
|
2018-06-12 20:04:54 +02:00
|
|
|
return contextManagement.createOrUpdate();
|
2017-11-23 15:10:08 +01:00
|
|
|
}
|
|
|
|
|
2018-06-07 18:12:06 +02:00
|
|
|
/*
|
|
|
|
* DELETE /contexts/{UUID}
|
|
|
|
* e.g. DELETE /contexts/c0f314e7-2807-4241-a792-2a6c79ed4fd0
|
2016-09-01 18:36:41 +02:00
|
|
|
*/
|
2016-09-01 17:11:44 +02:00
|
|
|
@DELETE
|
2018-06-08 09:30:02 +02:00
|
|
|
@Consumes({MediaType.TEXT_PLAIN, ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8})
|
|
|
|
@Path("{" + AccessPath.CONTEXT_UUID_PATH_PARAM + "}")
|
2019-02-13 12:33:29 +01:00
|
|
|
public Response delete(@PathParam(AccessPath.CONTEXT_UUID_PATH_PARAM) String uuid)
|
2017-11-23 09:39:21 +01:00
|
|
|
throws ContextNotFoundException, ResourceRegistryException {
|
2018-06-07 18:12:06 +02:00
|
|
|
logger.info("Requested to delete {} with id {} ", Context.NAME, uuid);
|
2020-03-16 14:51:13 +01:00
|
|
|
// setRESTCalledMethod(HTTPMETHOD.DELETE, uuid);
|
|
|
|
CalledMethodProvider.instance.set("deleteContext");
|
2018-06-06 19:26:19 +02:00
|
|
|
|
2017-11-23 09:39:21 +01:00
|
|
|
ContextManagement contextManagement = new ContextManagement();
|
|
|
|
contextManagement.setUUID(UUID.fromString(uuid));
|
2019-02-13 12:33:29 +01:00
|
|
|
contextManagement.delete();
|
|
|
|
|
|
|
|
return Response.status(Status.NO_CONTENT).build();
|
2016-07-18 11:57:12 +02:00
|
|
|
}
|
2017-12-07 15:22:22 +01:00
|
|
|
|
2016-07-18 11:57:12 +02:00
|
|
|
}
|