1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.itracker.web.actions.admin.attachment;
20
21 import org.apache.log4j.Logger;
22 import org.apache.struts.action.*;
23 import org.itracker.model.IssueAttachment;
24 import org.itracker.services.IssueService;
25 import org.itracker.web.actions.base.ItrackerBaseAction;
26 import org.itracker.web.util.ServletContextUtils;
27
28 import javax.servlet.ServletException;
29 import javax.servlet.ServletOutputStream;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32 import java.io.IOException;
33 import java.util.List;
34 import java.util.zip.ZipEntry;
35 import java.util.zip.ZipOutputStream;
36
37
38 public class ExportAttachmentsAction extends ItrackerBaseAction {
39 private static final Logger log = Logger.getLogger(DownloadAttachmentAction.class);
40
41 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
42 ActionMessages errors = new ActionMessages();
43
44
45 try {
46 IssueService issueService = ServletContextUtils.getItrackerServices().getIssueService();
47
48 List<IssueAttachment> attachments = issueService.getAllIssueAttachments();
49 if (attachments.size() > 0) {
50 response.setContentType("application/zip");
51 response.setHeader("Content-Disposition", "attachment; filename=\"ITracker_attachments.zip\"");
52 ServletOutputStream out = response.getOutputStream();
53 ZipOutputStream zipOut = new ZipOutputStream(out);
54 try {
55 for (IssueAttachment attachment : attachments) {
56 log.debug("Attempting export for: " + attachment);
57 byte[] attachmentData = issueService.getIssueAttachmentData(attachment.getId());
58 if (attachmentData.length > 0) {
59 ZipEntry zipEntry = new ZipEntry(attachment.getFileName());
60 zipEntry.setSize(attachmentData.length);
61 zipEntry.setTime(attachment.getLastModifiedDate().getTime());
62 zipOut.putNextEntry(zipEntry);
63 zipOut.write(attachmentData, 0, attachmentData.length);
64 zipOut.closeEntry();
65 }
66 }
67 zipOut.close();
68 out.flush();
69 out.close();
70 } catch (Exception e) {
71 log.error("Exception while exporting attachments.", e);
72 }
73 return null;
74 }
75 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.noattachments"));
76 } catch (Exception e) {
77 log.error("Exception while exporting attachments.", e);
78 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
79 }
80
81 if (!errors.isEmpty()) {
82 saveErrors(request, errors);
83
84 }
85
86 return mapping.findForward("error");
87 }
88
89 }
90