View Javadoc
1   package org.itracker.web.actions.admin.attachment;
2   
3   import org.apache.log4j.Logger;
4   import org.apache.struts.action.*;
5   import org.itracker.model.IssueAttachment;
6   import org.itracker.services.IssueService;
7   import org.itracker.web.actions.base.ItrackerBaseAction;
8   import org.itracker.web.util.ServletContextUtils;
9   import org.springframework.web.bind.ServletRequestUtils;
10  
11  import javax.servlet.ServletOutputStream;
12  import javax.servlet.http.HttpServletRequest;
13  import javax.servlet.http.HttpServletResponse;
14  
15  public class DownloadAttachmentAction extends ItrackerBaseAction {
16  
17      private static final Logger log = Logger.getLogger(DownloadAttachmentAction.class);
18  
19      public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request,
20                                   HttpServletResponse response) throws Exception {
21  
22          Integer attachmentID = ServletRequestUtils.getIntParameter(request, "id");
23  
24          IssueService issueService = ServletContextUtils.getItrackerServices().getIssueService();
25  
26          IssueAttachment attachment = issueService.getIssueAttachment(attachmentID);
27  
28          if (attachment.getFileData() == null) {
29              ActionMessages errors = new ActionMessages();
30  
31              errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.missingattachmentdata"));
32  
33              saveErrors(request, errors);
34  
35              return actionMapping.findForward("error_page");
36          }
37  
38          response.setContentType(attachment.getType());
39          response.setHeader("Content-Disposition", "attachment; filename=" + attachment.getOriginalFileName() + "");
40          ServletOutputStream outputStream = response.getOutputStream();
41          log.debug("Displaying attachment " + attachment.getId() + " of type " + attachment.getType()
42                  + " to client.  Attachment size: " + attachment.getFileData().length);
43  
44          outputStream.write(attachment.getFileData());
45  
46          return null;
47  
48      }
49  
50  }