updated exporter behaviour

This commit is contained in:
Francesco Mangiacrapa 2024-04-22 14:26:11 +02:00
parent f3ccc3b134
commit c61bac50ed
4 changed files with 58 additions and 27 deletions

View File

@ -4,7 +4,7 @@ import org.gcube.application.geoportaldatamapper.shared.FileReference;
public class FetchPDF {
private final int MAX_RETRY = 10;
private final int MAX_RETRY = 20;
private FileReference fileRef;
private String code;
@ -41,8 +41,8 @@ public class FetchPDF {
this.fileRef = fileRef;
}
public synchronized void incrementAttempt() {
attempt++;
public synchronized int incrementAttempt() {
return attempt++;
}

View File

@ -0,0 +1,13 @@
package org.gcube.datatransfer.resolver.geoportal.exporter;
import lombok.Data;
@Data
public class GeoportalViewJsonResponse {
//State cona be "OK" or "ERROR"
String state;
String url;
String message;
}

View File

@ -61,21 +61,29 @@ public class HTML_Page {
+ " let isJsonResponse = isJsonString(response_object);\n"
+ " if (isJsonResponse) {\n"
+ " console.log(\"json response \" + response_object);\n"
+ " if (!(response_object.url === null || response_object.url === undefined)) {\n"
+ " window.location.replace(response_object.url);\n"
+ " if (response_object.state === \"OK\") {\n"
+ " if (!(response_object.url === null || response_object.url === undefined)) {\n"
+ " window.location.replace(response_object.url);\n"
+ " } else {\n"
+ " setTimeout(() => {\n"
+ " playPDFPoll();\n"
+ " }, 1000);\n"
+ " }\n"
+ " } else {\n"
+ " setTimeout(() => {\n"
+ " playPDFPoll();\n"
+ " }, 1000);\n"
+ " showError(response_object.messagge);\n"
+ " }\n"
+ " } else {\n"
+ " console.log(\"no json response \" + response_object);\n"
+ " let inner_div = document.getElementById(\"inner-content\");\n"
+ " inner_div.innerHTML = response_object;\n"
+ " showError(\"Error when exporting PDF :-(\");\n"
+ " }\n"
+ "\n"
+ " }\n"
+ "\n"
+ " function showError(error_msg) {\n"
+ " let inner_div = document.getElementById(\"inner-content\");\n"
+ " inner_div.innerHTML = error_msg;\n"
+ " }\n"
+ "\n"
+ " function isJsonString(str) {\n"
+ " try {\n"
+ " JSON.parse(str);\n"
@ -84,11 +92,9 @@ public class HTML_Page {
+ " }\n"
+ " return true;\n"
+ " }\n"
+ "\n"
+ " </script>\n"
+ " <title>D4Science Geoportal - Action</title>\n"
+ " </head>\n"
+ "\n"
+ " <body onload=\"playPDFPoll()\">";
newHTML += "<img alt=\"D4Science Logo\" src=\"https://services.d4science.org/image/layout_set_logo?img_id=32727\"><br />";

View File

@ -27,6 +27,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.HTML_Page;
import org.gcube.datatransfer.resolver.services.error.ExceptionManager;
import org.gcube.datatransfer.resolver.services.exceptions.BadRequestException;
@ -236,41 +237,52 @@ public class GeoportalExporter {
FetchPDF fetchedPDF = map.get(pdfCode);
LOG.info("viewPDF FileReference at code {} is {}", pdfCode, fetchedPDF);
JSONObject theJson = new JSONObject();
theJson.put("state", Status.NOT_FOUND.toString());
theJson.put("url", "");
String theURL = null;
String messagge = null;
String state = null;
GeoportalViewJsonResponse theJson = new GeoportalViewJsonResponse();
theJson.setState(state);
theJson.setUrl(theURL);
theJson.setMessage(messagge);
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.OK).entity(json).build();
LOG.info("viewPDF returning not found: " + json);
return Response.status(Status.NOT_FOUND).entity(json).build();
}
try {
String theURL = null;
// File PDF is not available
if (fetchedPDF.getFileRef() == null) {
fetchedPDF.incrementAttempt();
if (fetchedPDF.getMAX_RETRY() < fetchedPDF.getAttempt()) {
theURL = null;
int attempt = fetchedPDF.incrementAttempt();
if (fetchedPDF.getAttempt() < fetchedPDF.getMAX_RETRY()) {
state = "OK";
messagge = "Attempt #" + attempt;
} else {
theURL = HTML_Page.getErrorPage("Geoportal Error",
"Sorry, an error occurred tryng to create the PDF");
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);
} else {
// File PDF is available
state = "OK";
theURL = fetchedPDF.getFileRef().getStorageVolatileURL().toString();
messagge = "PDF created correclty";
// removing from map
map.put(pdfCode, null);
}
theJson.put("state", "OK");
theJson.put("url", theURL);
theJson.setState(state);
theJson.setUrl(theURL);
theJson.setMessage(messagge);
String json = theJson.toString();
LOG.info("viewPDF returning OK: "+json);
LOG.info("viewPDF returning OK: " + json);
return Response.ok(json).build();
} catch (Exception e) {