diff --git a/src/main/java/org/damap/base/rest/DmpDocumentResource.java b/src/main/java/org/damap/base/rest/DmpDocumentResource.java index 3f314811..cbfee2c8 100644 --- a/src/main/java/org/damap/base/rest/DmpDocumentResource.java +++ b/src/main/java/org/damap/base/rest/DmpDocumentResource.java @@ -44,67 +44,64 @@ public class DmpDocumentResource { */ @GET @Path("/{dmpId}") + @Deprecated(since = "4.3.0") public Response exportTemplate( @PathParam("dmpId") long dmpId, @QueryParam("template") ETemplateType template) { log.info("Return DMP document file for DMP with id=" + dmpId); - String personId = this.getPersonId(); - if (!accessValidator.canViewDmp(dmpId, personId)) { - throw new ForbiddenException("Not authorized to access dmp with id " + dmpId); - } - - String filename = dmpService.getDefaultFileName(dmpId); - - XWPFDocument document; - if (template != null) document = exportTemplateBroker.exportTemplateByType(dmpId, template); - else document = exportTemplateBroker.exportTemplate(dmpId); - - StreamingOutput streamingOutput = - new StreamingOutput() { - @Override - public void write(OutputStream os) throws IOException, WebApplicationException { - document.write(os); - document.close(); - } - }; - - return Response.ok(streamingOutput) - .header("Content-Disposition", "attachment;filename=" + filename + ".docx") - .header("Access-Control-Expose-Headers", "Content-Disposition") - .build(); + return this.export(dmpId, template, true, "docx"); } @GET - @Path("/preview/{dmpId}") + @Path("/{dmpId}/export") @Produces(MediaType.APPLICATION_OCTET_STREAM) - public Response getPreviewPDF( - @PathParam("dmpId") long dmpId, @QueryParam("template") ETemplateType template) { + public Response export( + @PathParam("dmpId") long dmpId, + @QueryParam("template") ETemplateType template, + @QueryParam("download") Boolean download, + @QueryParam("filetype") String filetype + ) { + + if (download == null) { + download = true; + } - log.info("Returning DMP PDF file for DMP with id=" + dmpId); + if (filetype == null || (!filetype.equals("pdf") && !filetype.equals("docx"))) { + filetype = "docx"; + } + + log.info("Returning DMP document file for DMP with id=" + dmpId); String personId = this.getPersonId(); if (!accessValidator.canViewDmp(dmpId, personId)) { throw new ForbiddenException("Not authorized to access dmp with id " + dmpId); } - StreamingOutput pdfDocument; + StreamingOutput document; try { - pdfDocument = documentService.getPreviewPDF(dmpId, template); + document = documentService.getExportDocument(dmpId, template, download, filetype); } catch (Exception e) { - log.error("Error creating PDF document", e); - return Response.serverError().entity("Error creating PDF document").build(); + log.error("Error exporting DMP document", e); + return Response.serverError().entity("Error exporting DMP document").build(); } + String filename = dmpService.getDefaultFileName(dmpId); + // Return the PDF file in the response - return Response.ok(pdfDocument) - .header("Content-Disposition", "attachment; filename=\"dmp_" + dmpId + ".pdf\"") - .header("Content-Type", "application/pdf") // Set the content type + Response response = Response.ok(document) + .header("Content-Disposition", "attachment;filename=" + filename + "." + filetype) .header("Access-Control-Expose-Headers", "Content-Disposition") .build(); + + if (filetype.equals("pdf")) { + response.getHeaders().add("Content-Type", "application/pdf"); + } + + return response; } @GET - @Path("/template_type/{dmpId}") + @Path("/{dmpId}/template_type") @Produces(MediaType.APPLICATION_JSON) public ETemplateType getTemplateType(@PathParam("dmpId") long dmpId) { log.info("Return template type for DMP with id=" + dmpId); diff --git a/src/main/java/org/damap/base/rest/document/service/DocumentService.java b/src/main/java/org/damap/base/rest/document/service/DocumentService.java index 732a1e56..8e10600d 100644 --- a/src/main/java/org/damap/base/rest/document/service/DocumentService.java +++ b/src/main/java/org/damap/base/rest/document/service/DocumentService.java @@ -2,10 +2,13 @@ import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; +import jakarta.ws.rs.WebApplicationException; import jakarta.ws.rs.core.StreamingOutput; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStream; + import lombok.extern.jbosslog.JBossLog; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.damap.base.conversion.ExportTemplateBroker; @@ -27,19 +30,28 @@ public class DocumentService { @Inject @RestClient GotenbergRestService gotenbergRestService; - public StreamingOutput getPreviewPDF(long dmpId, ETemplateType template) { + public StreamingOutput getExportDocument(long dmpId, ETemplateType template, boolean download, String filetype) { // Fetch the document based on the template XWPFDocument document = (template != null) ? exportTemplateBroker.exportTemplateByType(dmpId, template) : exportTemplateBroker.exportTemplate(dmpId); - // Create a temporary Word document + if (filetype.equals("pdf")) { + return getPdfOf(document); + } else if (filetype.equals("docx")) { + return getWordDocumentOf(document); + } + + throw new WebApplicationException("Invalid file type: " + filetype); + } + + private StreamingOutput getPdfOf(XWPFDocument xwpfDocument) { File tempFile; try { tempFile = File.createTempFile("dmp", ".docx"); try (FileOutputStream out = new FileOutputStream(tempFile)) { - document.write(out); + xwpfDocument.write(out); } } catch (IOException e) { log.error("Error creating temporary file", e); @@ -71,6 +83,13 @@ public StreamingOutput getPreviewPDF(long dmpId, ETemplateType template) { }; } + private StreamingOutput getWordDocumentOf(XWPFDocument xwpfDocument) { + return os -> { + xwpfDocument.write(os); + xwpfDocument.close(); + }; + } + public ETemplateType getTemplateType(long dmpId) { return templateSelectorService.selectTemplate(dmpService.getDmpById(dmpId)); }