diff --git a/src/main/java/org/gcube/datatransfer/resolver/geoportal/exporter/FetchPDF.java b/src/main/java/org/gcube/datatransfer/resolver/geoportal/exporter/FetchPDF.java new file mode 100644 index 0000000..227e5ed --- /dev/null +++ b/src/main/java/org/gcube/datatransfer/resolver/geoportal/exporter/FetchPDF.java @@ -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(); + } + + +} diff --git a/src/main/java/org/gcube/datatransfer/resolver/geoportal/exporter/HTML_Page.java b/src/main/java/org/gcube/datatransfer/resolver/geoportal/exporter/HTML_Page.java new file mode 100644 index 0000000..c4c958d --- /dev/null +++ b/src/main/java/org/gcube/datatransfer/resolver/geoportal/exporter/HTML_Page.java @@ -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 = "\n" + + "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " D4Science Geoportal - Action\n" + + " \n" + + "\n" + + " "; + + newHTML += "\"D4Science
"; + + newHTML += "
"; + newHTML += "

" + action + "

"; + + if (waiting) { + newHTML += "\"D4Science
"; + } + + newHTML += "

" + message + "

"; + newHTML += "
/body>"; + + 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 = "\n" + + "\n" + + " \n" + + " \n" + + " \n" + + ""; + + newHTML += "\"D4Science
"; + + newHTML += "
"; + newHTML += "

" + action + "

"; + newHTML += "

" + message + "

"; + newHTML += "
/body>"; + + return newHTML; + } + + +} diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/GeoportalExporter.java b/src/main/java/org/gcube/datatransfer/resolver/services/GeoportalExporter.java index f1d4666..737d58c 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/GeoportalExporter.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/GeoportalExporter.java @@ -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; @@ -55,8 +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 map = new LinkedHashMap(); + + private static LinkedHashMap map = new LinkedHashMap(); public enum ACCEPTED_EXPORT_TYPE { pdf @@ -108,25 +109,32 @@ public class GeoportalExporter { if (checked) { if (userAgentName != null) { 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); - + final String pollingCode = ucdID + "_" + projectID + "_" + System.currentTimeMillis(); + String viewPdfURL = req.getServletPath() + "/view/" + pollingCode; + + 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); } - + }; t.start(); - }catch (Exception e) { + } catch (Exception e) { // TODO: handle exception } - + return Response.ok(entity).encoding("UTF-8").header(ConstantsResolver.CONTENT_TYPE, "text/html") .build(); } else { @@ -200,7 +208,7 @@ public class GeoportalExporter { this.getClass(), helpURI); } } - + @GET @Path("/view/{pdfCode}") @Produces({ MediaType.TEXT_PLAIN }) @@ -211,26 +219,33 @@ public class GeoportalExporter { LOG.info("param is: pdfCode: {}", pdfCode); 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()" + "" -// + "" -// + "D4Science Geoportal - Action" + "" + ""; -// -// html += "\"D4Science
"; -// -// html += "
"; -// html += "

" + action + "

"; -// -// if (waiting) { -// html += "\"D4Science
"; -// } -// -// html += "

" + message + "

"; -// html += "
" -// + "" -// + ""; - - - - String newHTML = "\n" - + "\n" - + " \n" - + " \n" - + " \n" - + " \n" - + " D4Science Geoportal - Action\n" - + " \n" - + "\n" - + " "; - - newHTML += "\"D4Science
"; - - newHTML += "
"; - newHTML += "

" + action + "

"; - - if (waiting) { - newHTML += "\"D4Science
"; - } - - newHTML += "

" + message + "

"; - newHTML += "
/body>"; - - return newHTML; - } - }