More work on delete method.
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/resource-management/resource-manager@162207 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
b4faa918a5
commit
60a49651a5
|
@ -14,6 +14,9 @@ public class RMContextPath {
|
||||||
|
|
||||||
public static final String CREATE_PATH_PART = "create";
|
public static final String CREATE_PATH_PART = "create";
|
||||||
|
|
||||||
|
public static final String DELETE_PATH_PART = "delete";
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Force the service to use the ResourceRegistry instance at the given URL.
|
* Force the service to use the ResourceRegistry instance at the given URL.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -3,7 +3,6 @@ package org.gcube.resourcemanagement.manager.webapp.context;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import org.gcube.informationsystem.model.entity.Context;
|
|
||||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -20,7 +19,7 @@ public final class DeleteRequest extends RequestToResourceRegistry {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DeleteRequest fromHolder(UUID context) {
|
public static DeleteRequest fromUUID(UUID context) {
|
||||||
Objects.requireNonNull(context);
|
Objects.requireNonNull(context);
|
||||||
return new DeleteRequest(context);
|
return new DeleteRequest(context);
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,8 +11,12 @@ import javax.ws.rs.core.Response.Status;
|
||||||
import org.gcube.common.authorization.library.provider.CalledMethodProvider;
|
import org.gcube.common.authorization.library.provider.CalledMethodProvider;
|
||||||
import org.gcube.informationsystem.model.entity.Context;
|
import org.gcube.informationsystem.model.entity.Context;
|
||||||
import static org.gcube.resourcemanagement.manager.io.rs.RMContextPath.*;
|
import static org.gcube.resourcemanagement.manager.io.rs.RMContextPath.*;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
import org.gcube.resourcemanagement.manager.webapp.ResourceInitializer;
|
import org.gcube.resourcemanagement.manager.webapp.ResourceInitializer;
|
||||||
import org.gcube.resourcemanagement.manager.webapp.context.CreateRequest;
|
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.ResponseFromResourceRegistry;
|
||||||
import org.gcube.resourcemanagement.manager.webapp.context.ContextHolder;
|
import org.gcube.resourcemanagement.manager.webapp.context.ContextHolder;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
@ -30,7 +34,7 @@ public class RMContext {
|
||||||
private static Logger logger = LoggerFactory.getLogger(RMContext.class);
|
private static Logger logger = LoggerFactory.getLogger(RMContext.class);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* e.g. POST /resource-manager/context/
|
* e.g. POST /resource-manager/context?rrURL=http://registry:port//resource-registry
|
||||||
*/
|
*/
|
||||||
@POST
|
@POST
|
||||||
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
|
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
|
||||||
|
@ -38,31 +42,42 @@ public class RMContext {
|
||||||
CalledMethodProvider.instance.set(String.format("POST /%s/%s", APPLICATION_PATH, CONTEXT_ROOT));
|
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("Requested to create context {} with json {}", Context.NAME, json);
|
||||||
logger.info("Force URL: " + rrURL);
|
logger.info("Force URL: " + rrURL);
|
||||||
Response response;
|
|
||||||
ContextHolder holder = new ContextHolder(json);
|
ContextHolder holder = new ContextHolder(json);
|
||||||
ResponseFromResourceRegistry returned = CreateRequest.fromHolder(holder).forceURL(rrURL).submit();
|
ResponseFromResourceRegistry returned = CreateRequest.fromHolder(holder).forceURL(rrURL).submit();
|
||||||
logger.debug("Returned " + returned.wasSuccessful());
|
return this.buildResponse(returned,"Context successfully created.","Failed to create the context.");
|
||||||
if (returned.wasSuccessful()) {
|
|
||||||
response = Response.status(Status.CREATED)
|
|
||||||
.entity(returned.getMessage().map(m -> m).orElse("Context successfully created."))
|
|
||||||
.type(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8).build();
|
|
||||||
} else {
|
|
||||||
if (returned.getException().isPresent())
|
|
||||||
throw returned.getException().get();
|
|
||||||
response = Response.status(Status.BAD_REQUEST)
|
|
||||||
.entity(returned.getMessage().map(m -> m).orElse("Failed to create the context.")).build();
|
|
||||||
}
|
|
||||||
return response;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* e.g. DELETE /resource-manager/context/UUID?rrURL=http://registry:port//resource-registry
|
||||||
|
*/
|
||||||
@DELETE
|
@DELETE
|
||||||
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
|
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
|
||||||
public Response delete(String uuid, @QueryParam(FORCE_RRURL_PARAM) String rrURL) {
|
public Response delete(String uuid, @QueryParam(FORCE_RRURL_PARAM) String rrURL) {
|
||||||
CalledMethodProvider.instance.set(String.format("DELETE /%s/%s/ID", APPLICATION_PATH, CONTEXT_ROOT));
|
CalledMethodProvider.instance.set(String.format("DELETE /%s/%s/ID", APPLICATION_PATH, CONTEXT_ROOT));
|
||||||
logger.info("Requested to delete context {} with id {}", Context.NAME, uuid);
|
logger.info("Requested to delete context {} with id {}", Context.NAME, uuid);
|
||||||
logger.info("Force URL: " + rrURL);
|
logger.info("Force URL: " + rrURL);
|
||||||
Response response = null;
|
ResponseFromResourceRegistry returned = DeleteRequest.fromUUID(UUID.fromString(uuid)).forceURL(rrURL).submit();
|
||||||
|
return this.buildResponse(returned,"Context successfully deleted.","Failed to delete the context.");
|
||||||
return response;
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds a response for the client.
|
||||||
|
* @param returned
|
||||||
|
* @param successMessage
|
||||||
|
* @param failedMessage
|
||||||
|
* @return the response
|
||||||
|
*/
|
||||||
|
private Response buildResponse(ResponseFromResourceRegistry returned, String successMessage, String failedMessage) {
|
||||||
|
if (returned.wasSuccessful()) {
|
||||||
|
return Response.status(Status.CREATED)
|
||||||
|
.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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue