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