diff --git a/src/main/java/org/imixs/application/admin/ConnectionController.java b/src/main/java/org/imixs/application/admin/ConnectionController.java index 9d7f4da..c7b70d0 100644 --- a/src/main/java/org/imixs/application/admin/ConnectionController.java +++ b/src/main/java/org/imixs/application/admin/ConnectionController.java @@ -15,7 +15,7 @@ import org.imixs.workflow.ItemCollection; import jakarta.enterprise.context.Conversation; -import jakarta.enterprise.context.ConversationScoped; +import jakarta.enterprise.context.SessionScoped; import jakarta.faces.context.FacesContext; import jakarta.inject.Inject; import jakarta.inject.Named; @@ -39,7 +39,8 @@ * */ @Named -@ConversationScoped +// @ConversationScoped +@SessionScoped public class ConnectionController implements Serializable { private static final long serialVersionUID = 7027147503119012594L; diff --git a/src/main/java/org/imixs/application/admin/DocumentController.java b/src/main/java/org/imixs/application/admin/DocumentController.java index 04a8d74..1e4138d 100644 --- a/src/main/java/org/imixs/application/admin/DocumentController.java +++ b/src/main/java/org/imixs/application/admin/DocumentController.java @@ -146,6 +146,8 @@ public void delete(String id) { * in the browser. */ public void downloadDocument(String id, String filename) { + FacesContext facesContext = FacesContext.getCurrentInstance(); + HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse(); try { // load report WorkflowClient workflowClient = connectionController.getWorkflowClient(); @@ -153,10 +155,8 @@ public void downloadDocument(String id, String filename) { XMLDocument xmlDocument = XMLDocumentAdapter.getDocument(downloadDocument); // set Content-Type and Header for Download - FacesContext facesContext = FacesContext.getCurrentInstance(); - HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse(); response.setContentType("application/octet-stream"); - response.setHeader("Content-Disposition", "attachment;filename=" + filename); + response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\""); // marshal xmlObject... JAXBContext jaxbContext = JAXBContext.newInstance(xmlDocument.getClass()); @@ -175,7 +175,21 @@ public void downloadDocument(String id, String filename) { facesContext.responseComplete(); } catch (IOException | JAXBException | RestAPIException e) { e.printStackTrace(); + } finally { + // Ensure the output stream is closed + try { + if (response.getOutputStream() != null) { + response.getOutputStream().close(); + } + } catch (IOException e) { + e.printStackTrace(); + } } } + public String getDownloadUrl(String id, String filename) { + // Beispiel: Erstellen Sie die REST-URL für den Download + return "/api/download?fileId=" + id + "&filename=" + filename; + } + } \ No newline at end of file diff --git a/src/main/java/org/imixs/application/admin/DownloadResource.java b/src/main/java/org/imixs/application/admin/DownloadResource.java new file mode 100644 index 0000000..d2359ec --- /dev/null +++ b/src/main/java/org/imixs/application/admin/DownloadResource.java @@ -0,0 +1,79 @@ +package org.imixs.application.admin; + +import org.imixs.melman.RestAPIException; +import org.imixs.melman.WorkflowClient; +import org.imixs.workflow.ItemCollection; +import org.imixs.workflow.xml.XMLDocument; +import org.imixs.workflow.xml.XMLDocumentAdapter; + +import jakarta.inject.Inject; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.QueryParam; +import jakarta.ws.rs.core.Response; +import jakarta.ws.rs.core.StreamingOutput; +import jakarta.xml.bind.JAXBContext; +import jakarta.xml.bind.JAXBException; +import jakarta.xml.bind.Marshaller; + +/** + * Rest API Endpoint to load a xml document from the remote instance This + * endpoint is used for download links + * + */ +@Path("/download") +public class DownloadResource { + + @Inject + ConnectionController connectionController; + + @GET + public Response downloadFile(@QueryParam("fileId") String fileId, @QueryParam("filename") String filename) { + try { + // Holen Sie sich das XML-Dokument + XMLDocument xmlDocument = getXmlDocument(fileId); + + // Erstellen Sie einen StreamingOutput, um die Datei zu schreiben + StreamingOutput streamingOutput = output -> { + JAXBContext jaxbContext; + try { + jaxbContext = JAXBContext.newInstance(XMLDocument.class); + Marshaller marshaller = jaxbContext.createMarshaller(); + marshaller.marshal(xmlDocument, output); + } catch (JAXBException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + }; + + // Setzen Sie die HTTP-Header für den Download + return Response.ok(streamingOutput) + .header("Content-Disposition", "attachment; filename=\"" + filename + "\"").type("application/xml") + .build(); + } catch (Exception e) { + e.printStackTrace(); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR) + .entity("Fehler beim Generieren der Datei: " + e.getMessage()).build(); + } + } + + /** + * This method loads a document by its UniqueID and initialzes a download + * stream. Used in the search view and in reports.xhtml to download a Document + * objects as an XML file. The filename is the name the download will be shown + * in the browser. + */ + public XMLDocument getXmlDocument(String id) { + XMLDocument xmlDocument = null; + try { + // load document + WorkflowClient workflowClient = connectionController.getWorkflowClient(); + ItemCollection downloadDocument = workflowClient.getDocument(id); + xmlDocument = XMLDocumentAdapter.getDocument(downloadDocument); + } catch (RestAPIException e) { + e.printStackTrace(); + } + return xmlDocument; + } + +} \ No newline at end of file diff --git a/src/main/webapp/reports.xhtml b/src/main/webapp/reports.xhtml index 8bd26b7..93c7521 100644 --- a/src/main/webapp/reports.xhtml +++ b/src/main/webapp/reports.xhtml @@ -36,30 +36,25 @@ disabled="#{!connectionController.connected}"> -