This commit is contained in:
Francesco Mangiacrapa 2024-04-19 16:10:56 +02:00
parent 4aed42d515
commit 1f2baf3ef7
3 changed files with 234 additions and 136 deletions

View File

@ -0,0 +1,65 @@
package org.gcube.datatransfer.resolver.geoportal.exporter;
import org.gcube.application.geoportaldatamapper.shared.FileReference;
public class FetchPDF {
private final int MAX_RETRY = 10;
private FileReference fileRef;
private String code;
private int attempt = 0;
public FetchPDF() {
}
public FetchPDF(FileReference fileRef, String code, int attempt) {
super();
this.fileRef = fileRef;
this.code = code;
this.attempt = attempt;
}
public int getMAX_RETRY() {
return MAX_RETRY;
}
public FileReference getFileRef() {
return fileRef;
}
public String getCode() {
return code;
}
public int getAttempt() {
return attempt;
}
public void setFileRef(FileReference fileRef) {
this.fileRef = fileRef;
}
public synchronized void incrementAttempt() {
attempt++;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("FetchPDF [MAX_RETRY=");
builder.append(MAX_RETRY);
builder.append(", fileRef=");
builder.append(fileRef);
builder.append(", code=");
builder.append(code);
builder.append(", attempt=");
builder.append(attempt);
builder.append("]");
return builder.toString();
}
}

View File

@ -0,0 +1,133 @@
package org.gcube.datatransfer.resolver.geoportal.exporter;
public class HTML_Page {
/**
* Sets the HTML message.
*
* @param action the action
* @param message the message
* @param waiting the waiting
* @return the string
*/
public static String entityHTMLMessage(String action, String message, boolean waiting, String viewPdfURL) {
String newHTML = "<html>\n"
+ "\n"
+ " <head>\n"
+ " <meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">\n"
+ " <style>\n"
+ " html,\n"
+ " body {\n"
+ " margin: 10px;\n"
+ " width: 100%;\n"
+ " height: 100%;\n"
+ " display: table\n"
+ " }\n"
+ "\n"
+ " #content {\n"
+ " position: absolute;\n"
+ " left: 50%;\n"
+ " top: 50%;\n"
+ " -webkit-transform: translate(-50%, -50%);\n"
+ " transform: translate(-50%, -50%);\n"
+ " text-align: center;\n"
+ " }\n"
+ "\n"
+ " #message {\n"
+ " color: gray;\n"
+ " font-size: 24px;\n"
+ " }\n"
+ "\n"
+ " </style>\n"
+ " <script type=\"text/javascript\">\n"
+ " async function fetchPDF_URL() {\n"
+ " const response = await fetch('"+viewPdfURL+"');\n"
+ " console.log(\"polling response\");\n"
+ " return response.json();\n"
+ " }\n"
+ " async function playPDFPoll() {\n"
+ " const pdf_json = await fetchPDF_URL();\n"
+ " if (!(pdf_json.url === null || pdf_json.url === undefined)) {\n"
+ " window.location.replace(pdf_json.url);\n"
+ " } else {\n"
+ " setTimeout(() => {\n"
+ " playPDFPoll();\n"
+ " }, 1000);\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 />";
newHTML += "<div id=\"content\">";
newHTML += "<p style=\"font-size: 18px;\">" + action + "</p>";
if (waiting) {
newHTML += "<img alt=\"D4Science Geoportal Loading...\" src=\"uri-resolver/img/loading-gears.gif\"><br />";
}
newHTML += "<br/><p id=\"message\">" + message + "</p>";
newHTML += "</div>/body></html>";
return newHTML;
}
/**
* Sets the HTML message.
*
* @param action the action
* @param message the message
* @param waiting the waiting
* @return the string
*/
public static String getErrorPage(String action, String message) {
String newHTML = "<html>\n"
+ "\n"
+ " <head>\n"
+ " <meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">\n"
+ " <style>\n"
+ " html,\n"
+ " body {\n"
+ " margin: 10px;\n"
+ " width: 100%;\n"
+ " height: 100%;\n"
+ " display: table\n"
+ " }\n"
+ "\n"
+ " #content {\n"
+ " position: absolute;\n"
+ " left: 50%;\n"
+ " top: 50%;\n"
+ " -webkit-transform: translate(-50%, -50%);\n"
+ " transform: translate(-50%, -50%);\n"
+ " text-align: center;\n"
+ " }\n"
+ "\n"
+ " #message {\n"
+ " color: gray;\n"
+ " font-size: 24px;\n"
+ " }\n"
+ "\n"
+ " </style>\n"
+ "</head>";
newHTML += "<img alt=\"D4Science Logo\" src=\"https://services.d4science.org/image/layout_set_logo?img_id=32727\"><br />";
newHTML += "<div id=\"content\">";
newHTML += "<p style=\"font-size: 18px;\">" + action + "</p>";
newHTML += "<br/><p id=\"message\">" + message + "</p>";
newHTML += "</div>/body></html>";
return newHTML;
}
}

View File

@ -1,7 +1,6 @@
package org.gcube.datatransfer.resolver.services;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedHashMap;
import javax.servlet.http.HttpServletRequest;
@ -27,6 +26,8 @@ import org.gcube.common.authorization.utils.manager.SecretManager;
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.HTML_Page;
import org.gcube.datatransfer.resolver.services.error.ExceptionManager;
import org.gcube.datatransfer.resolver.services.exceptions.BadRequestException;
import org.gcube.datatransfer.resolver.util.SingleFileStreamingOutput;
@ -56,7 +57,7 @@ public class GeoportalExporter {
private static final Logger LOG = LoggerFactory.getLogger(GeoportalExporter.class);
private static String helpURI = "https://wiki.gcube-system.org/gcube/URI_Resolver#Geoportal_Resolver";
private static LinkedHashMap<String, FileReference> map = new LinkedHashMap<String, FileReference>();
private static LinkedHashMap<String, FetchPDF> map = new LinkedHashMap<String, FetchPDF>();
public enum ACCEPTED_EXPORT_TYPE {
pdf
@ -110,15 +111,22 @@ public class GeoportalExporter {
LOG.info("Serving request as User-Agent {}", userAgentName);
final String pollingCode = ucdID + "_" + projectID + "_" + System.currentTimeMillis();
String viewPdfURL = req.getServletPath() + "/view/" + pollingCode;
String entity = entityHTMLMessage("Exporting as PDF...", "The project with " + projectID, true, viewPdfURL);
String entity = HTML_Page.entityHTMLMessage("Exporting as PDF...",
"The project with id: " + projectID, true, viewPdfURL);
try {
Thread t = new Thread() {
@Override
public void run() {
LOG.info("exportAsPDF called in thread...");
FileReference pdfRef = exportAsPDF(req, ucdID, projectID, null, context, user);
map.put(pollingCode, pdfRef);
FileReference pdfRef = null;
FetchPDF fetchPDF = new FetchPDF(pdfRef, pollingCode, 0);
map.put(pollingCode, fetchPDF);
pdfRef = exportAsPDF(req, ucdID, projectID, null, context, user);
fetchPDF.setFileRef(pdfRef);
map.put(pollingCode, fetchPDF);
}
};
@ -213,17 +221,25 @@ public class GeoportalExporter {
try {
JsonObject jsonObject = new JsonObject();
FileReference thePdfURL = map.get(pdfCode);
LOG.info("FileReference at code {} is {}", pdfCode, thePdfURL);
FetchPDF fetchedPDF = map.get(pdfCode);
LOG.info("FileReference at code {} is {}", pdfCode, fetchedPDF.getFileRef());
String theURL = null;
if(thePdfURL!=null)
theURL = thePdfURL.getStorageVolatileURL().toString();
if (fetchedPDF.getFileRef()==null) {
fetchedPDF.incrementAttempt();
if(fetchedPDF.getMAX_RETRY()<fetchedPDF.getAttempt()) {
theURL = null;
}else {
theURL = HTML_Page.getErrorPage("Geoportal Error", "Sorry, an error occurred tryng to create the PDF");
}
}else {
theURL = fetchedPDF.getFileRef().getStorageVolatileURL().toString();
}
map.put(pdfCode, fetchedPDF);
jsonObject.addProperty("url", theURL);
String json = jsonObject.toString();
return Response.ok(json).build();
} catch (Exception e) {
LOG.error("Error on performing healthcheck", e);
throw ExceptionManager.internalErrorException(req,
@ -231,7 +247,6 @@ public class GeoportalExporter {
this.getClass(), helpURI);
}
}
public void checkPathParameterNotNull(HttpServletRequest req, String parameter, String value)
@ -319,119 +334,4 @@ public class GeoportalExporter {
}
}
/**
* Sets the HTML message.
*
* @param action the action
* @param message the message
* @param waiting the waiting
* @return the string
*/
protected String entityHTMLMessage(String action, String message, boolean waiting, String viewPdfURL) {
// String html = "<html>" + "<head>" + "<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">"
// + "<style>" + "html, body {\n" + " margin: 10px;\n" + " width: 100%;\n" + " height: 100%;\n"
// + " display: table\n" + "}\n" + "#content {\n" + " position: absolute;\n" + " left: 50%;\n"
// + " top: 50%;\n" + " -webkit-transform: translate(-50%, -50%);\n"
// + " transform: translate(-50%, -50%);\n" + " text-align: center;\n" + "}" + "#message {\n"
// + " color:gray;" + " font-size: 24px;" + "}" + "</style>"
// + "<title>D4Science Geoportal - Action</title>" + "</head>" + "<body>";
//
// html += "<img alt=\"D4Science Logo\" src=\"https://services.d4science.org/image/layout_set_logo?img_id=32727\"><br />";
//
// html += "<div id=\"content\">";
// html += "<p style=\"font-size: 18px;\">" + action + "</p>";
//
// if (waiting) {
// html += "<img alt=\"D4Science Geoportal Loading...\" src=\"uri-resolver/img/loading-gears.gif\"><br />";
// }
//
// html += "<br/><p id=\"message\">" + message + "</p>";
// html += "</div>"
// + ""
// + "<script>"
// + ""
// + "async function fetchPDF_URL() {\n"
// + " const response = await fetch("+viewPdfURL+");\n"
// + " return response.json();\n"
// + "}"
// + "async function playPDFPoll() { "
// + "const pdf_json = await fetchPDF_URL(); "
// + "if (!(pdf_json.url === null || pdf_json.url === undefined)) {\n"
// + " window.location.replace(pdf_json.url);\n"
// + " } else {\n"
// + " setTimeout(() => {\n"
// + " playPDFPoll();\n"
// + " }, 2000);\n"
// + " }\n"
// + "}"
// + "</script></body></html>";
String newHTML = "<html>\n"
+ "\n"
+ " <head>\n"
+ " <meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">\n"
+ " <style>\n"
+ " html,\n"
+ " body {\n"
+ " margin: 10px;\n"
+ " width: 100%;\n"
+ " height: 100%;\n"
+ " display: table\n"
+ " }\n"
+ "\n"
+ " #content {\n"
+ " position: absolute;\n"
+ " left: 50%;\n"
+ " top: 50%;\n"
+ " -webkit-transform: translate(-50%, -50%);\n"
+ " transform: translate(-50%, -50%);\n"
+ " text-align: center;\n"
+ " }\n"
+ "\n"
+ " #message {\n"
+ " color: gray;\n"
+ " font-size: 24px;\n"
+ " }\n"
+ "\n"
+ " </style>\n"
+ " <script type=\"text/javascript\">\n"
+ " async function fetchPDF_URL() {\n"
+ " const response = await fetch('"+viewPdfURL+"');\n"
+ " console.log(\"polling response\");\n"
+ " return response.json();\n"
+ " }\n"
+ " async function playPDFPoll() {\n"
+ " const pdf_json = await fetchPDF_URL();\n"
+ " if (!(pdf_json.url === null || pdf_json.url === undefined)) {\n"
+ " window.location.replace(pdf_json.url);\n"
+ " } else {\n"
+ " setTimeout(() => {\n"
+ " playPDFPoll();\n"
+ " }, 1000);\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 />";
newHTML += "<div id=\"content\">";
newHTML += "<p style=\"font-size: 18px;\">" + action + "</p>";
if (waiting) {
newHTML += "<img alt=\"D4Science Geoportal Loading...\" src=\"uri-resolver/img/loading-gears.gif\"><br />";
}
newHTML += "<br/><p id=\"message\">" + message + "</p>";
newHTML += "</div>/body></html>";
return newHTML;
}
}