113 lines
4.5 KiB
Java
113 lines
4.5 KiB
Java
package org.gcube.resourcemanagement.manager.webapp.rs;
|
|
|
|
import javax.ws.rs.DELETE;
|
|
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.model.entity.Context;
|
|
import static org.gcube.resourcemanagement.manager.io.rs.RMContextPath.*;
|
|
|
|
import java.util.UUID;
|
|
|
|
import org.gcube.resourcemanagement.manager.io.codeexceptions.SerializableErrorEntity;
|
|
import org.gcube.resourcemanagement.manager.io.codeexceptions.WebCodeException;
|
|
import org.gcube.resourcemanagement.manager.io.rs.RMContextDoesNotExistException;
|
|
import org.gcube.resourcemanagement.manager.webapp.ResourceInitializer;
|
|
import org.gcube.resourcemanagement.manager.webapp.context.CreateRequest;
|
|
import org.gcube.resourcemanagement.manager.webapp.context.DeleteRequest;
|
|
import org.gcube.resourcemanagement.manager.webapp.context.ResponseFromResourceRegistry;
|
|
import org.gcube.resourcemanagement.manager.webapp.context.ContextHolder;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
/**
|
|
* Resource methods for {@link Context}.
|
|
*
|
|
* @author Manuele Simi (ISTI-CNR)
|
|
*
|
|
*/
|
|
@Path(CONTEXT_ROOT)
|
|
public class RMContext {
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(RMContext.class);
|
|
|
|
/*
|
|
* e.g. POST /resource-manager/context?rrURL=http://registry:port//resource-registry
|
|
*/
|
|
@POST
|
|
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
|
|
public Response create(String json, @QueryParam(FORCE_RRURL_PARAM) String rrURL) {
|
|
CalledMethodProvider.instance.set(String.format("POST /%s/%s", APPLICATION_PATH, CONTEXT_ROOT));
|
|
logger.info("Requested to create context {} with json {}", Context.NAME, json);
|
|
logger.info("Force URL: " + rrURL);
|
|
ContextHolder holder = new ContextHolder(json);
|
|
ResponseFromResourceRegistry returned = CreateRequest.fromHolder(holder).forceURL(rrURL).submit();
|
|
/* if (!returned.wasSuccessful()) {
|
|
throw new WebCodeException(new RMContextDoesNotExistException("too bad"));
|
|
}*/
|
|
return this.buildResponse(returned,Status.CREATED,"Context successfully created.","Failed to create the context.");
|
|
}
|
|
|
|
/*
|
|
* e.g. DELETE /resource-manager/context/UUID?rrURL=http://registry:port//resource-registry
|
|
*/
|
|
@DELETE
|
|
@Path("{" + UUID_PARAM + "}")
|
|
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
|
|
public Response delete(@PathParam(UUID_PARAM) String uuid, @QueryParam(FORCE_RRURL_PARAM) String rrURL) {
|
|
CalledMethodProvider.instance.set(String.format("DELETE /%s/%s/ID", APPLICATION_PATH, CONTEXT_ROOT));
|
|
logger.info("Requested to delete context with id {}", uuid);
|
|
logger.info("Force URL: " + rrURL);
|
|
ResponseFromResourceRegistry returned = DeleteRequest.fromUUID(UUID.fromString(uuid)).forceURL(rrURL).submit();
|
|
/* if (!returned.wasSuccessful()) {
|
|
throw new WebCodeException(new RMContextDoesNotExistException());
|
|
}*/
|
|
return this.buildResponse(returned,Status.OK,"Context successfully deleted.","Failed to delete the context.");
|
|
}
|
|
|
|
/**
|
|
* Builds a response for the client.
|
|
* @param returned
|
|
* @param successMessage
|
|
* @param failedMessage
|
|
* @return the response
|
|
*/
|
|
private Response buildResponse(ResponseFromResourceRegistry returned, Status successStatus, String successMessage, String failedMessage) {
|
|
if (returned.wasSuccessful()) {
|
|
return Response.status(successStatus)
|
|
.entity(returned.getMessage().map(m -> m).orElse(successMessage))
|
|
.type(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8).build();
|
|
} else {
|
|
if (returned.getException().isPresent())
|
|
throw returned.getException().get();
|
|
return Response.status(Status.BAD_REQUEST)
|
|
.entity(returned.getMessage().map(m -> m).orElse(failedMessage)).build();
|
|
}
|
|
}
|
|
/**
|
|
* Builds a response for the client.
|
|
* @param returned
|
|
* @param successMessage
|
|
* @param failedMessage
|
|
* @return the response
|
|
*/
|
|
private Response buildResponseWithException(ResponseFromResourceRegistry returned, Status successStatus, String successMessage, String failedMessage) {
|
|
if (returned.wasSuccessful()) {
|
|
return Response.status(successStatus)
|
|
.entity(returned.getMessage().map(m -> m).orElse(successMessage))
|
|
.type(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8).build();
|
|
} else {
|
|
if (returned.getException().isPresent())
|
|
throw returned.getException().get();
|
|
return Response.status(Status.BAD_REQUEST)
|
|
.entity(returned.getMessage().map(m -> m).orElse(failedMessage)).build();
|
|
}
|
|
}
|
|
}
|