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 { public class FetchPDF {
private final int MAX_RETRY = 10; private final int MAX_RETRY = 20;
private FileReference fileRef; private FileReference fileRef;
private String code; private String code;
@ -41,8 +41,8 @@ public class FetchPDF {
this.fileRef = fileRef; this.fileRef = fileRef;
} }
public synchronized void incrementAttempt() { public synchronized int incrementAttempt() {
attempt++; 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" + " let isJsonResponse = isJsonString(response_object);\n"
+ " if (isJsonResponse) {\n" + " if (isJsonResponse) {\n"
+ " console.log(\"json response \" + response_object);\n" + " console.log(\"json response \" + response_object);\n"
+ " if (!(response_object.url === null || response_object.url === undefined)) {\n" + " if (response_object.state === \"OK\") {\n"
+ " window.location.replace(response_object.url);\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" + " } else {\n"
+ " setTimeout(() => {\n" + " showError(response_object.messagge);\n"
+ " playPDFPoll();\n"
+ " }, 1000);\n"
+ " }\n" + " }\n"
+ " } else {\n" + " } else {\n"
+ " console.log(\"no json response \" + response_object);\n" + " console.log(\"no json response \" + response_object);\n"
+ " let inner_div = document.getElementById(\"inner-content\");\n" + " showError(\"Error when exporting PDF :-(\");\n"
+ " inner_div.innerHTML = response_object;\n"
+ " }\n" + " }\n"
+ "\n" + "\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" + " function isJsonString(str) {\n"
+ " try {\n" + " try {\n"
+ " JSON.parse(str);\n" + " JSON.parse(str);\n"
@ -84,11 +92,9 @@ public class HTML_Page {
+ " }\n" + " }\n"
+ " return true;\n" + " return true;\n"
+ " }\n" + " }\n"
+ "\n"
+ " </script>\n" + " </script>\n"
+ " <title>D4Science Geoportal - Action</title>\n" + " <title>D4Science Geoportal - Action</title>\n"
+ " </head>\n" + " </head>\n"
+ "\n"
+ " <body onload=\"playPDFPoll()\">"; + " <body onload=\"playPDFPoll()\">";
newHTML += "<img alt=\"D4Science Logo\" src=\"https://services.d4science.org/image/layout_set_logo?img_id=32727\"><br />"; 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.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.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;
@ -236,41 +237,52 @@ public class GeoportalExporter {
FetchPDF fetchedPDF = map.get(pdfCode); FetchPDF fetchedPDF = map.get(pdfCode);
LOG.info("viewPDF FileReference at code {} is {}", pdfCode, fetchedPDF); LOG.info("viewPDF FileReference at code {} is {}", pdfCode, fetchedPDF);
JSONObject theJson = new JSONObject(); String theURL = null;
theJson.put("state", Status.NOT_FOUND.toString()); String messagge = null;
theJson.put("url", ""); String state = null;
GeoportalViewJsonResponse theJson = new GeoportalViewJsonResponse();
theJson.setState(state);
theJson.setUrl(theURL);
theJson.setMessage(messagge);
if (fetchedPDF == null) { if (fetchedPDF == null) {
theJson.setState(Status.NOT_FOUND.getReasonPhrase());
theJson.setMessage("No job found");
String json = theJson.toString(); String json = theJson.toString();
LOG.info("viewPDF returning not found: "+json); LOG.info("viewPDF returning not found: " + json);
return Response.status(Status.OK).entity(json).build(); return Response.status(Status.NOT_FOUND).entity(json).build();
} }
try { try {
String theURL = null;
// File PDF is not available // File PDF is not available
if (fetchedPDF.getFileRef() == null) { if (fetchedPDF.getFileRef() == null) {
fetchedPDF.incrementAttempt(); int attempt = fetchedPDF.incrementAttempt();
if (fetchedPDF.getMAX_RETRY() < fetchedPDF.getAttempt()) { if (fetchedPDF.getAttempt() < fetchedPDF.getMAX_RETRY()) {
theURL = null; state = "OK";
messagge = "Attempt #" + attempt;
} else { } else {
theURL = HTML_Page.getErrorPage("Geoportal Error", state = "ERROR";
"Sorry, an error occurred tryng to create the PDF"); messagge = "Sorry, an error occurred tryng to create the PDF. Max retries reached";
theJson.setState(state);
} }
// updating map status // updating map status
map.put(pdfCode, fetchedPDF); map.put(pdfCode, fetchedPDF);
} else { } else {
// File PDF is available // File PDF is available
state = "OK";
theURL = fetchedPDF.getFileRef().getStorageVolatileURL().toString(); theURL = fetchedPDF.getFileRef().getStorageVolatileURL().toString();
messagge = "PDF created correclty";
// removing from map // removing from map
map.put(pdfCode, null); map.put(pdfCode, null);
} }
theJson.put("state", "OK"); theJson.setState(state);
theJson.put("url", theURL); theJson.setUrl(theURL);
theJson.setMessage(messagge);
String json = theJson.toString(); String json = theJson.toString();
LOG.info("viewPDF returning OK: "+json); LOG.info("viewPDF returning OK: " + json);
return Response.ok(json).build(); return Response.ok(json).build();
} catch (Exception e) { } catch (Exception e) {