Skip to content

Commit

Permalink
implemented new download resource (Issue #76)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsoika committed Feb 11, 2025
1 parent 3ccdc06 commit 55fa0f7
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,7 +39,8 @@
*
*/
@Named
@ConversationScoped
// @ConversationScoped
@SessionScoped
public class ConnectionController implements Serializable {

private static final long serialVersionUID = 7027147503119012594L;
Expand Down
20 changes: 17 additions & 3 deletions src/main/java/org/imixs/application/admin/DocumentController.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,17 @@ 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();
ItemCollection downloadDocument = workflowClient.getDocument(id);
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());
Expand All @@ -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;
}

}
79 changes: 79 additions & 0 deletions src/main/java/org/imixs/application/admin/DownloadResource.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
19 changes: 5 additions & 14 deletions src/main/webapp/reports.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -36,38 +36,32 @@
disabled="#{!connectionController.connected}">
</h:commandLink>
</td>

<td>#{report.item['contenttype']}</td>

<td>#{report.item['encoding']}</td>

<td>#{report.item['$modified']}</td>

<!-- download/delete -->
<td>
<ui:param name="downloadName"
value="#{report.getItemValueString('txtname').concat('.xml')}" />
<h:commandLink value="" title="#{downloadName}"
action="#{documentController.downloadDocument(report.item['$uniqueid'],downloadName)}">
value="#{report.getItemValueString('txtname').concat('.imixs-report')}" />
<h:outputLink value="#{documentController.getDownloadUrl(report.item['$uniqueid'], downloadName)}"
title="#{downloadName}">
<span class="typcn typcn-download"></span>
</h:commandLink>
</h:outputLink>
<h:commandLink
actionListener="#{reportController.deleteReport(report.item['$uniqueid'])}"
disabled="#{!connectionController.connected}"
onclick="return (confirm('Delete Report #{report.item['txtname']} ?'))">
<span class="typcn typcn-trash error"></span>
</h:commandLink>
</h:commandLink>
</td>


</tr>
</ui:repeat>
</tbody>
</table>

<h:commandButton actionListener="#{reportController.createReport()}" value="Create new Report"
disabled="#{!connectionController.connected}" action="report">

</h:commandButton>

<div class="imixs-form-section">
Expand All @@ -82,9 +76,6 @@
</dl>
</div>




</div>
</h:form>
</f:view>
Expand Down

0 comments on commit 55fa0f7

Please sign in to comment.