View Javadoc
1   /*
2    * This software was designed and created by Jason Carroll.
3    * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4    * The author can be reached at jcarroll@cowsultants.com
5    * ITracker website: http://www.cowsultants.com
6    * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7    *
8    * This program is free software; you can redistribute it and/or modify
9    * it only under the terms of the GNU General Public License as published by
10   * the Free Software Foundation; either version 2 of the License, or
11   * (at your option) any later version.
12   *
13   * This program is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Public License for more details.
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