From 78919bce5a54042fa71e188f72624a0b6e7d35b1 Mon Sep 17 00:00:00 2001 From: "manuele.simi" Date: Wed, 7 Feb 2018 04:48:30 +0000 Subject: [PATCH] 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 --- .../manager/webapp/rs/RMContext.java | 51 +++++++++---------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/webapp/src/main/java/org/gcube/resourcemanagement/manager/webapp/rs/RMContext.java b/webapp/src/main/java/org/gcube/resourcemanagement/manager/webapp/rs/RMContext.java index 87fcf82..28d4771 100644 --- a/webapp/src/main/java/org/gcube/resourcemanagement/manager/webapp/rs/RMContext.java +++ b/webapp/src/main/java/org/gcube/resourcemanagement/manager/webapp/rs/RMContext.java @@ -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(); } }