updated healthcheck and GeoportalJsonResponse

This commit is contained in:
Francesco Mangiacrapa 2024-04-22 17:59:16 +02:00
parent 40f417c90b
commit 8d46d0610b
2 changed files with 22 additions and 13 deletions

View File

@ -3,7 +3,7 @@ package org.gcube.datatransfer.resolver.geoportal.exporter;
import lombok.Data; import lombok.Data;
@Data @Data
public class GeoportalViewJsonResponse { public class GeoportalJsonResponse {
//State can be "OK" or "ERROR" //State can be "OK" or "ERROR"
String state; String state;

View File

@ -31,7 +31,7 @@ import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.common.authorization.utils.user.User; import org.gcube.common.authorization.utils.user.User;
import org.gcube.datatransfer.resolver.ConstantsResolver; import org.gcube.datatransfer.resolver.ConstantsResolver;
import org.gcube.datatransfer.resolver.geoportal.exporter.FetchPDF; 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.geoportal.exporter.HTML_Page;
import org.gcube.datatransfer.resolver.services.error.ExceptionManager; import org.gcube.datatransfer.resolver.services.error.ExceptionManager;
import org.gcube.datatransfer.resolver.services.exceptions.BadRequestException; import org.gcube.datatransfer.resolver.services.exceptions.BadRequestException;
@ -218,7 +218,7 @@ public class GeoportalExporter {
*/ */
@GET @GET
@Path("/export/{type}/healthcheck") @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) public Response healthcheck(@Context HttpServletRequest req, @PathParam(EXPORT_TYPE) String export_type)
throws WebApplicationException { throws WebApplicationException {
@ -242,12 +242,18 @@ public class GeoportalExporter {
Geoportal_PDF_Exporter pdfExporter = new Geoportal_PDF_Exporter(); Geoportal_PDF_Exporter pdfExporter = new Geoportal_PDF_Exporter();
boolean checked = pdfExporter.checkConfig(); boolean checked = pdfExporter.checkConfig();
GeoportalJsonResponse theJson = new GeoportalJsonResponse();
if (checked) { 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 { } else {
return Response.status(Status.NOT_FOUND).entity(GeoportalExporter.class.getSimpleName() + " Config KO") theJson.setState("KO");
.type(MediaType.TEXT_PLAIN).build(); 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) { } catch (Exception e) {
@ -268,7 +274,7 @@ public class GeoportalExporter {
*/ */
@GET @GET
@Path("/view/{jobCode}") @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) public Response viewJob(@Context HttpServletRequest req, @PathParam(JOB_CODE) String jobCode)
throws WebApplicationException { throws WebApplicationException {
@ -282,20 +288,18 @@ public class GeoportalExporter {
String messagge = null; String messagge = null;
String state = null; String state = null;
GeoportalViewJsonResponse theJson = new GeoportalViewJsonResponse(); GeoportalJsonResponse theJson = new GeoportalJsonResponse();
theJson.setState(state); theJson.setState(state);
theJson.setUrl(theURL); theJson.setUrl(theURL);
theJson.setMessage(messagge); theJson.setMessage(messagge);
String jsonReponse = null; String jsonReponse = null;
ObjectMapper mapper = new ObjectMapper();
if (fetchedPDF == null) { if (fetchedPDF == null) {
theJson.setState(Status.NOT_FOUND.getReasonPhrase()); theJson.setState(Status.NOT_FOUND.getReasonPhrase());
theJson.setMessage("No job found"); theJson.setMessage("No job found");
try { try {
jsonReponse = mapper.writeValueAsString(theJson); jsonReponse = responseToString(theJson);
LOG.info("viewJob returning not found: " + jsonReponse); LOG.info("viewJob returning not found: " + jsonReponse);
return Response.status(Status.NOT_FOUND).entity(jsonReponse).build(); return Response.status(Status.NOT_FOUND).entity(jsonReponse).build();
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
@ -333,7 +337,7 @@ public class GeoportalExporter {
theJson.setUrl(theURL); theJson.setUrl(theURL);
theJson.setMessage(messagge); theJson.setMessage(messagge);
try { try {
jsonReponse = mapper.writeValueAsString(theJson); jsonReponse = responseToString(theJson);
LOG.info("viewJob returning OK: " + jsonReponse); LOG.info("viewJob returning OK: " + jsonReponse);
return Response.ok(jsonReponse).build(); return Response.ok(jsonReponse).build();
} catch (JsonProcessingException e) { } 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. * Check path parameter not null.
* *