Use GXErrorOutboundResponse and GXOutboundSuccessResponse from gxRest to return a response in the create and delete methods.

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/resource-management/resource-manager@163003 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Manuele Simi 2018-02-07 04:48:30 +00:00
parent 9654a44e67
commit 78919bce5a
1 changed files with 25 additions and 26 deletions

View File

@ -7,10 +7,10 @@ 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.common.gxrest.response.error.GXErrorResponse;
import org.gcube.common.gxrest.response.error.GXErrorOutboundResponse;
import org.gcube.common.gxrest.response.success.GXOutboundSuccessResponse;
import org.gcube.informationsystem.model.entity.Context;
import static org.gcube.resourcemanagement.manager.io.rs.RMContextPath.*;
@ -36,7 +36,8 @@ public class RMContext {
private static Logger logger = LoggerFactory.getLogger(RMContext.class);
/*
* e.g. POST /resource-manager/context?rrURL=http://registry:port//resource-registry
* e.g. POST
* /resource-manager/context?rrURL=http://registry:port//resource-registry
*/
@POST
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
@ -46,11 +47,22 @@ public class RMContext {
logger.info("Force URL: " + rrURL);
ContextHolder holder = new ContextHolder(json);
ResponseFromResourceRegistry returned = CreateRequest.fromHolder(holder).forceURL(rrURL).submit();
return this.buildResponse(returned,Status.CREATED,"Context successfully created.","Failed to create the context.");
if (!returned.wasSuccessful()) {
if (returned.getException().isPresent())
GXErrorOutboundResponse.throwException(returned.getException().get());
else if (returned.getErrorCode().isPresent())
GXErrorOutboundResponse.throwErrorCode(returned.getErrorCode().get());
else
GXErrorOutboundResponse.throwException(new Exception("Failed to create the context."));
}
return GXOutboundSuccessResponse.newCREATEResponse(null).withMessage("Context successfully created.")
.ofType(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8).build();
}
/*
* e.g. DELETE /resource-manager/context/UUID?rrURL=http://registry:port//resource-registry
* e.g. DELETE
* /resource-manager/context/UUID?rrURL=http://registry:port//resource-
* registry
*/
@DELETE
@Path("{" + UUID_PARAM + "}")
@ -60,28 +72,15 @@ public class RMContext {
logger.info("Requested to delete context with id {}", uuid);
logger.info("Force URL: " + rrURL);
ResponseFromResourceRegistry returned = DeleteRequest.fromUUID(UUID.fromString(uuid)).forceURL(rrURL).submit();
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.wasSuccessful()) {
if (returned.getException().isPresent())
GXErrorResponse.throwException(returned.getException().get());
GXErrorOutboundResponse.throwException(returned.getException().get());
else if (returned.getErrorCode().isPresent())
GXErrorResponse.returnErrorCode(returned.getErrorCode().get());
return Response.status(Status.BAD_REQUEST)
.entity(returned.getMessage().map(m -> m).orElse(failedMessage)).build();
GXErrorOutboundResponse.throwErrorCode(returned.getErrorCode().get());
else
GXErrorOutboundResponse.throwException(new Exception("Failed to delete the context."));
}
return GXOutboundSuccessResponse.newOKResponse().withMessage("Context successfully deleted.")
.ofType(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8).build();
}
}