This commit is contained in:
Francesco Mangiacrapa 2024-04-19 15:34:31 +02:00
parent 069b631bc8
commit 4aed42d515
1 changed files with 132 additions and 101 deletions

View File

@ -1,6 +1,8 @@
package org.gcube.datatransfer.resolver.services;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedHashMap;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
@ -54,6 +56,8 @@ 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>();
public enum ACCEPTED_EXPORT_TYPE {
pdf
}
@ -104,9 +108,25 @@ public class GeoportalExporter {
if (checked) {
if (userAgentName != null) {
LOG.info("Serving request as User-Agent {}", userAgentName);
String pollingCode = ucdID+"_"+projectID+"_"+System.currentTimeMillis();
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);
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);
}
};
t.start();
}catch (Exception e) {
// TODO: handle exception
}
return Response.ok(entity).encoding("UTF-8").header(ConstantsResolver.CONTENT_TYPE, "text/html")
.build();
} else {
@ -136,67 +156,6 @@ public class GeoportalExporter {
}
// /**
// * Resolve geoportal no app def.
// *
// * @param req the req
// * @param vreName the vre name
// * @param exportType the export type
// * @param ucdID the ucd ID
// * @param projectID the project ID
// * @return the response
// * @throws WebApplicationException the web application exception
// */
// @GET
// @Path("/export/{type}/view/{usecase_id}/{project_id}")
// @Produces({ MediaType.TEXT_HTML })
// public Response exportView(@Context HttpServletRequest req, @PathParam(EXPORT_TYPE) String export_type,
// @PathParam(PATH_USECASE_ID) String ucdID, @PathParam(PATH_PROJECT_ID) String projectID)
// throws WebApplicationException {
//
// String userAgentName = req.getHeader("User-Agent");
//
// LOG.info(this.getClass().getSimpleName() + " export view - GET starts...");
// LOG.debug("params are: exportType: {}, ucdID: {}, projectID: {}", export_type, ucdID, projectID);
//
// checkPathParameterNotNull(req, EXPORT_TYPE, export_type);
// checkPathParameterNotNull(req, PATH_USECASE_ID, ucdID);
// checkPathParameterNotNull(req, PATH_PROJECT_ID, projectID);
//
// checkExportType(req, export_type);
//
// try {
//
// SecretManager cm = SecretManagerProvider.instance.get();
// String context = cm.getContext();
// if (context == null) {
// throw ExceptionManager.forbiddenException(req, "Cannot determine context (the scope)", this.getClass(),
// helpURI);
// }
// org.gcube.common.authorization.utils.user.User user = cm.getUser();
// LOG.info("Identified caller {} in context {}", user.getUsername(), context);
//
// Geoportal_PDF_Exporter pdfExporter = new Geoportal_PDF_Exporter();
// boolean checked = pdfExporter.checkConfig();
//
// if (checked) {
// LOG.info("Serving request as User-Agent {}", userAgentName);
// String entity = entityHTMLMessage("Exporting as PDF...", "The project with " + projectID, true);
// return Response.ok(entity).encoding("UTF-8").header(ConstantsResolver.CONTENT_TYPE, "text/html")
// .build();
// } else {
// throw ExceptionManager.notFoundException(req, this.getClass().getSimpleName()+ " not found in the context: "+context,
// this.getClass(), helpURI);
// }
//
// } catch (Exception e) {
// LOG.error("Error on performing export", e);
// throw ExceptionManager.internalErrorException(req, "Sorry, error occurred when exporting the project",
// this.getClass(), helpURI);
// }
//
// }
@GET
@Path("/export/{type}/healthcheck")
@Produces({ MediaType.TEXT_HTML, MediaType.TEXT_PLAIN })
@ -249,10 +208,21 @@ public class GeoportalExporter {
throws WebApplicationException {
LOG.info(this.getClass().getSimpleName() + " viewPDF - GET starts...");
LOG.debug("param is: pdfCode: {}", pdfCode);
LOG.info("param is: pdfCode: {}", pdfCode);
try {
JsonObject jsonObject = new JsonObject();
FileReference thePdfURL = map.get(pdfCode);
LOG.info("FileReference at code {} is {}", pdfCode, thePdfURL);
String theURL = null;
if(thePdfURL!=null)
theURL = thePdfURL.getStorageVolatileURL().toString();
jsonObject.addProperty("url", theURL);
String json = jsonObject.toString();
return Response.ok(json).build();
} catch (Exception e) {
LOG.error("Error on performing healthcheck", e);
@ -261,10 +231,7 @@ public class GeoportalExporter {
this.getClass(), helpURI);
}
JsonObject jsonObject = new JsonObject();
jsonObject.add("url", null);
String json = jsonObject.toString();
return Response.ok(json).build();
}
public void checkPathParameterNotNull(HttpServletRequest req, String parameter, String value)
@ -362,45 +329,109 @@ public class GeoportalExporter {
*/
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>";
// 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>";
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>"
+ ""
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"
+ " const response = await fetch('"+viewPdfURL+"');\n"
+ " console.log(\"polling response\");\n"
+ " return response.json();\n"
+ "}"
+ "async function playPDFPoll() { "
+ "const pdf_json = await fetchPDF_URL(); "
+ " }\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"
+ " }, 2000);\n"
+ " }, 1000);\n"
+ " }\n"
+ "}"
+ "</script></body></html>";
+ " }\n"
+ " </script>\n"
+ " <title>D4Science Geoportal - Action</title>\n"
+ " </head>\n"
+ "\n"
+ " <body onload=\"playPDFPoll()\">";
return html;
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;
}
}