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 3d62c29..b57b739 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/GeoportalExporter.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/GeoportalExporter.java @@ -93,30 +93,39 @@ public class GeoportalExporter { checkExportType(req, export_type); + boolean checked = false; + + SecretManager cm = SecretManagerProvider.instance.get(); + final String context = cm.getContext(); + if (context == null) { + throw ExceptionManager.forbiddenException(req, "Cannot determine context (the scope)", this.getClass(), + helpURI); + } + + final org.gcube.common.authorization.utils.user.User user = cm.getUser(); + if (user == null) { + throw ExceptionManager.forbiddenException(req, "Cannot determine user", this.getClass(), helpURI); + } + + LOG.info("Identified caller {} in context {}", user.getUsername(), context); + 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(); + checked = pdfExporter.checkConfig(); + } catch (Exception e) { + LOG.error("Error on performing export", e); + throw ExceptionManager + .internalErrorException(req, + "Error the " + Geoportal_PDF_Exporter.class.getSimpleName() + + " seems to be not configured in the context: " + context, + this.getClass(), helpURI); + } - if (checked) { + if (checked) { + try { if (userAgentName != null) { LOG.info("Serving request as User-Agent {}", userAgentName); final String pollingCode = ucdID + "_" + projectID + "_" + System.currentTimeMillis(); - String serviceViewPDF_URL = String.format("%s/%s/view/%s", Util.getServerURL(req), - "geoportal", pollingCode); - - String entity = HTML_Page.entityHTMLMessage("Exporting as PDF...", - "The project with id: " + projectID, true, serviceViewPDF_URL); - try { Thread t = new Thread() { @Override @@ -127,6 +136,7 @@ public class GeoportalExporter { map.put(pollingCode, fetchPDF); pdfRef = exportAsPDF(req, ucdID, projectID, null, context, user); + LOG.info("exportAsPDF setFileRef in thread for code: " + pollingCode); fetchPDF.setFileRef(pdfRef); map.put(pollingCode, fetchPDF); } @@ -134,9 +144,15 @@ public class GeoportalExporter { }; t.start(); } catch (Exception e) { - // TODO: handle exception + LOG.error("Error on performing export in thread", e); } + String serviceViewPDF_URL = String.format("%s/%s/view/%s", Util.getServerURL(req), "geoportal", + pollingCode); + + String entity = HTML_Page.entityHTMLMessage("Exporting as PDF...", + "The project with id: " + projectID, true, serviceViewPDF_URL); + return Response.ok(entity).encoding("UTF-8").header(ConstantsResolver.CONTENT_TYPE, "text/html") .build(); } else { @@ -152,16 +168,16 @@ public class GeoportalExporter { .header("Content-Type", pdfRef.getContentType()); return response.build(); } - } else { - return Response.status(Status.NOT_FOUND) - .entity(GeoportalExporter.class.getSimpleName() + " Config not found in the context " + context) - .type(MediaType.TEXT_PLAIN).build(); + } catch (Exception e) { + LOG.error("Error on performing export", e); + throw ExceptionManager.internalErrorException(req, "Sorry, error occurred when exporting the project", + 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); + } else { + return Response.status(Status.NOT_FOUND) + .entity(GeoportalExporter.class.getSimpleName() + + "seems to be not configured in the context: " + context) + .type(MediaType.TEXT_PLAIN).build(); } } @@ -210,19 +226,20 @@ public class GeoportalExporter { @GET @Path("/view/{pdfCode}") - @Produces({ MediaType.TEXT_PLAIN, MediaType.TEXT_HTML}) + @Produces({ MediaType.TEXT_PLAIN, MediaType.TEXT_HTML }) public Response viewPDF(@Context HttpServletRequest req, @PathParam(PDF_CODE) String pdfCode) throws WebApplicationException { LOG.info(this.getClass().getSimpleName() + " viewPDF - GET starts..."); - LOG.info("param "+PDF_CODE+": {}", pdfCode); + LOG.info("param " + PDF_CODE + ": {}", pdfCode); FetchPDF fetchedPDF = map.get(pdfCode); LOG.info("FileReference at code {} is {}", pdfCode, fetchedPDF); if (fetchedPDF == null) { LOG.info("returning not found"); - return Response.status(Status.NOT_FOUND).entity(HTML_Page.getErrorPage("Job not created", "No job found with id "+pdfCode)) + return Response.status(Status.NOT_FOUND) + .entity(HTML_Page.getErrorPage("Job not created", "No job found with id " + pdfCode)) .type(MediaType.TEXT_HTML).build(); }