From 8d46d0610b09219f918e2e421d023324322435b9 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Mon, 22 Apr 2024 17:59:16 +0200 Subject: [PATCH] updated healthcheck and GeoportalJsonResponse --- ...sponse.java => GeoportalJsonResponse.java} | 2 +- .../resolver/services/GeoportalExporter.java | 33 ++++++++++++------- 2 files changed, 22 insertions(+), 13 deletions(-) rename src/main/java/org/gcube/datatransfer/resolver/geoportal/exporter/{GeoportalViewJsonResponse.java => GeoportalJsonResponse.java} (80%) diff --git a/src/main/java/org/gcube/datatransfer/resolver/geoportal/exporter/GeoportalViewJsonResponse.java b/src/main/java/org/gcube/datatransfer/resolver/geoportal/exporter/GeoportalJsonResponse.java similarity index 80% rename from src/main/java/org/gcube/datatransfer/resolver/geoportal/exporter/GeoportalViewJsonResponse.java rename to src/main/java/org/gcube/datatransfer/resolver/geoportal/exporter/GeoportalJsonResponse.java index b122bbf..ff65c5f 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/geoportal/exporter/GeoportalViewJsonResponse.java +++ b/src/main/java/org/gcube/datatransfer/resolver/geoportal/exporter/GeoportalJsonResponse.java @@ -3,7 +3,7 @@ package org.gcube.datatransfer.resolver.geoportal.exporter; import lombok.Data; @Data -public class GeoportalViewJsonResponse { +public class GeoportalJsonResponse { //State can be "OK" or "ERROR" String state; diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/GeoportalExporter.java b/src/main/java/org/gcube/datatransfer/resolver/services/GeoportalExporter.java index f971d02..cbe99e6 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/GeoportalExporter.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/GeoportalExporter.java @@ -31,7 +31,7 @@ import org.gcube.common.authorization.utils.manager.SecretManagerProvider; import org.gcube.common.authorization.utils.user.User; import org.gcube.datatransfer.resolver.ConstantsResolver; import org.gcube.datatransfer.resolver.geoportal.exporter.FetchPDF; -import org.gcube.datatransfer.resolver.geoportal.exporter.GeoportalViewJsonResponse; +import org.gcube.datatransfer.resolver.geoportal.exporter.GeoportalJsonResponse; import org.gcube.datatransfer.resolver.geoportal.exporter.HTML_Page; import org.gcube.datatransfer.resolver.services.error.ExceptionManager; import org.gcube.datatransfer.resolver.services.exceptions.BadRequestException; @@ -218,7 +218,7 @@ public class GeoportalExporter { */ @GET @Path("/export/{type}/healthcheck") - @Produces({ MediaType.TEXT_HTML, MediaType.TEXT_PLAIN }) + @Produces({ MediaType.TEXT_HTML, MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON }) public Response healthcheck(@Context HttpServletRequest req, @PathParam(EXPORT_TYPE) String export_type) throws WebApplicationException { @@ -242,12 +242,18 @@ public class GeoportalExporter { Geoportal_PDF_Exporter pdfExporter = new Geoportal_PDF_Exporter(); boolean checked = pdfExporter.checkConfig(); - + GeoportalJsonResponse theJson = new GeoportalJsonResponse(); if (checked) { - return Response.ok(GeoportalExporter.class.getSimpleName() + " Config OK").build(); + theJson.setState("OK"); + theJson.setMessage(GeoportalExporter.class.getSimpleName() + " Config OK in the context: " + context); + String jsonResponse = responseToString(theJson); + return Response.ok(jsonResponse).type(MediaType.APPLICATION_JSON).build(); } else { - return Response.status(Status.NOT_FOUND).entity(GeoportalExporter.class.getSimpleName() + " Config KO") - .type(MediaType.TEXT_PLAIN).build(); + theJson.setState("KO"); + theJson.setMessage(GeoportalExporter.class.getSimpleName() + " Config " + + Status.NOT_FOUND.getReasonPhrase() + " in the context: " + context); + String jsonResponse = responseToString(theJson); + return Response.status(Status.NOT_FOUND).entity(jsonResponse).type(MediaType.APPLICATION_JSON).build(); } } catch (Exception e) { @@ -268,7 +274,7 @@ public class GeoportalExporter { */ @GET @Path("/view/{jobCode}") - @Produces({ MediaType.TEXT_PLAIN, MediaType.TEXT_HTML }) + @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_HTML, MediaType.TEXT_PLAIN }) public Response viewJob(@Context HttpServletRequest req, @PathParam(JOB_CODE) String jobCode) throws WebApplicationException { @@ -282,20 +288,18 @@ public class GeoportalExporter { String messagge = null; String state = null; - GeoportalViewJsonResponse theJson = new GeoportalViewJsonResponse(); + GeoportalJsonResponse theJson = new GeoportalJsonResponse(); theJson.setState(state); theJson.setUrl(theURL); theJson.setMessage(messagge); String jsonReponse = null; - ObjectMapper mapper = new ObjectMapper(); - if (fetchedPDF == null) { theJson.setState(Status.NOT_FOUND.getReasonPhrase()); theJson.setMessage("No job found"); try { - jsonReponse = mapper.writeValueAsString(theJson); + jsonReponse = responseToString(theJson); LOG.info("viewJob returning not found: " + jsonReponse); return Response.status(Status.NOT_FOUND).entity(jsonReponse).build(); } catch (JsonProcessingException e) { @@ -333,7 +337,7 @@ public class GeoportalExporter { theJson.setUrl(theURL); theJson.setMessage(messagge); try { - jsonReponse = mapper.writeValueAsString(theJson); + jsonReponse = responseToString(theJson); LOG.info("viewJob returning OK: " + jsonReponse); return Response.ok(jsonReponse).build(); } catch (JsonProcessingException e) { @@ -351,6 +355,11 @@ public class GeoportalExporter { } + public String responseToString(GeoportalJsonResponse jsonResponse) throws JsonProcessingException { + ObjectMapper mapper = new ObjectMapper(); + return mapper.writeValueAsString(jsonResponse); + } + /** * Check path parameter not null. *