diff --git a/webapp/src/main/java/org/gcube/resourcemanagement/manager/webapp/context/ResponseFromResourceRegistry.java b/webapp/src/main/java/org/gcube/resourcemanagement/manager/webapp/context/ResponseFromResourceRegistry.java index 13bd323..be25aeb 100644 --- a/webapp/src/main/java/org/gcube/resourcemanagement/manager/webapp/context/ResponseFromResourceRegistry.java +++ b/webapp/src/main/java/org/gcube/resourcemanagement/manager/webapp/context/ResponseFromResourceRegistry.java @@ -62,6 +62,14 @@ public class ResponseFromResourceRegistry { return response; } + /** + * The {@link ErrorCode} occurred when preparing the response. + * @return the error code or null + */ + public Optional getErrorCode() { + return Optional.ofNullable(success? null: this.code); + } + /** * Creates a response from an error code. * @param e 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 cc83d42..87fcf82 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 @@ -16,7 +16,6 @@ import static org.gcube.resourcemanagement.manager.io.rs.RMContextPath.*; import java.util.UUID; -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; @@ -47,9 +46,6 @@ public class RMContext { 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."); } @@ -64,9 +60,6 @@ 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(); -/* if (!returned.wasSuccessful()) { - throw new WebCodeException(new RMContextDoesNotExistException()); - }*/ return this.buildResponse(returned,Status.OK,"Context successfully deleted.","Failed to delete the context."); } @@ -84,26 +77,9 @@ public class RMContext { .type(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8).build(); } else { if (returned.getException().isPresent()) - GXErrorResponse.throwException(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()) - GXErrorResponse.throwException(returned.getException().get()); + GXErrorResponse.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(); }