fixed json response

This commit is contained in:
Francesco Mangiacrapa 2024-04-22 14:43:29 +02:00
parent c61bac50ed
commit 9ec1dcf9a0
2 changed files with 26 additions and 12 deletions

View File

@ -5,9 +5,8 @@ import lombok.Data;
@Data
public class GeoportalViewJsonResponse {
//State cona be "OK" or "ERROR"
//State can be "OK" or "ERROR"
String state;
String url;
String message;
}

View File

@ -22,6 +22,8 @@ import org.gcube.application.geoportalcommon.shared.GeoportalItemReferences.SHAR
import org.gcube.application.geoportaldatamapper.exporter.Geoportal_PDF_Exporter;
import org.gcube.application.geoportaldatamapper.shared.ExporterProjectSource;
import org.gcube.application.geoportaldatamapper.shared.FileReference;
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.common.authorization.utils.user.User;
@ -33,12 +35,9 @@ import org.gcube.datatransfer.resolver.services.error.ExceptionManager;
import org.gcube.datatransfer.resolver.services.exceptions.BadRequestException;
import org.gcube.datatransfer.resolver.util.SingleFileStreamingOutput;
import org.gcube.datatransfer.resolver.util.Util;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.JsonObject;
/**
* The Class GeoportalExporter.
*
@ -246,12 +245,23 @@ public class GeoportalExporter {
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");
String json = theJson.toString();
LOG.info("viewPDF returning not found: " + json);
return Response.status(Status.NOT_FOUND).entity(json).build();
try {
jsonReponse = mapper.writeValueAsString(theJson);
LOG.info("viewPDF returning not found: " + jsonReponse);
return Response.status(Status.NOT_FOUND).entity(jsonReponse).build();
} catch (JsonProcessingException e) {
throw ExceptionManager.internalErrorException(req, "Error when returning "
+ GeoportalExporter.class.getSimpleName() + " not found job for " + pdfCode, this.getClass(),
helpURI);
}
}
try {
@ -265,7 +275,6 @@ public class GeoportalExporter {
state = "ERROR";
messagge = "Sorry, an error occurred tryng to create the PDF. Max retries reached";
theJson.setState(state);
}
// updating map status
map.put(pdfCode, fetchedPDF);
@ -281,9 +290,15 @@ public class GeoportalExporter {
theJson.setState(state);
theJson.setUrl(theURL);
theJson.setMessage(messagge);
String json = theJson.toString();
LOG.info("viewPDF returning OK: " + json);
return Response.ok(json).build();
try {
jsonReponse = mapper.writeValueAsString(theJson);
LOG.info("viewPDF returning OK: " + jsonReponse);
return Response.ok(jsonReponse).build();
} catch (JsonProcessingException e) {
throw ExceptionManager.internalErrorException(req,
"Error when returning " + GeoportalExporter.class.getSimpleName() + " response for " + pdfCode,
this.getClass(), helpURI);
}
} catch (Exception e) {
LOG.error("Error on checking job", e);