export_to_pdf #8

Merged
francesco.mangiacrapa merged 84 commits from export_to_pdf into master 2024-06-28 15:17:39 +02:00
1 changed files with 48 additions and 31 deletions
Showing only changes of commit e58935d489 - Show all commits

View File

@ -93,30 +93,39 @@ public class GeoportalExporter {
checkExportType(req, export_type); checkExportType(req, export_type);
try { boolean checked = false;
SecretManager cm = SecretManagerProvider.instance.get(); SecretManager cm = SecretManagerProvider.instance.get();
String context = cm.getContext(); final String context = cm.getContext();
if (context == null) { if (context == null) {
throw ExceptionManager.forbiddenException(req, "Cannot determine context (the scope)", this.getClass(), throw ExceptionManager.forbiddenException(req, "Cannot determine context (the scope)", this.getClass(),
helpURI); helpURI);
} }
org.gcube.common.authorization.utils.user.User user = cm.getUser();
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); LOG.info("Identified caller {} in context {}", user.getUsername(), context);
try {
Geoportal_PDF_Exporter pdfExporter = new Geoportal_PDF_Exporter(); 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) { if (userAgentName != null) {
LOG.info("Serving request as User-Agent {}", userAgentName); LOG.info("Serving request as User-Agent {}", userAgentName);
final String pollingCode = ucdID + "_" + projectID + "_" + System.currentTimeMillis(); 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 { try {
Thread t = new Thread() { Thread t = new Thread() {
@Override @Override
@ -127,6 +136,7 @@ public class GeoportalExporter {
map.put(pollingCode, fetchPDF); map.put(pollingCode, fetchPDF);
pdfRef = exportAsPDF(req, ucdID, projectID, null, context, user); pdfRef = exportAsPDF(req, ucdID, projectID, null, context, user);
LOG.info("exportAsPDF setFileRef in thread for code: " + pollingCode);
fetchPDF.setFileRef(pdfRef); fetchPDF.setFileRef(pdfRef);
map.put(pollingCode, fetchPDF); map.put(pollingCode, fetchPDF);
} }
@ -134,9 +144,15 @@ public class GeoportalExporter {
}; };
t.start(); t.start();
} catch (Exception e) { } 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") return Response.ok(entity).encoding("UTF-8").header(ConstantsResolver.CONTENT_TYPE, "text/html")
.build(); .build();
} else { } else {
@ -152,17 +168,17 @@ public class GeoportalExporter {
.header("Content-Type", pdfRef.getContentType()); .header("Content-Type", pdfRef.getContentType());
return response.build(); 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) { } catch (Exception e) {
LOG.error("Error on performing export", e); LOG.error("Error on performing export", e);
throw ExceptionManager.internalErrorException(req, "Sorry, error occurred when exporting the project", throw ExceptionManager.internalErrorException(req, "Sorry, error occurred when exporting the project",
this.getClass(), helpURI); 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 @GET
@Path("/view/{pdfCode}") @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) public Response viewPDF(@Context HttpServletRequest req, @PathParam(PDF_CODE) String pdfCode)
throws WebApplicationException { throws WebApplicationException {
LOG.info(this.getClass().getSimpleName() + " viewPDF - GET starts..."); 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); FetchPDF fetchedPDF = map.get(pdfCode);
LOG.info("FileReference at code {} is {}", pdfCode, fetchedPDF); LOG.info("FileReference at code {} is {}", pdfCode, fetchedPDF);
if (fetchedPDF == null) { if (fetchedPDF == null) {
LOG.info("returning not found"); 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(); .type(MediaType.TEXT_HTML).build();
} }