1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.itracker.web.servlets;
20
21
22 import org.apache.log4j.Logger;
23 import org.apache.struts.action.ActionErrors;
24 import org.apache.struts.action.ActionMessage;
25 import org.apache.struts.action.ActionMessages;
26 import org.itracker.model.IssueAttachment;
27 import org.itracker.services.IssueService;
28 import org.itracker.web.util.LoginUtilities;
29 import org.itracker.web.util.ServletContextUtils;
30
31 import javax.servlet.ServletConfig;
32 import javax.servlet.ServletException;
33 import javax.servlet.ServletOutputStream;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpServletResponse;
36 import java.io.IOException;
37
38
39
40
41 public class AttachmentDownloadController extends GenericController {
42
43 private static final Logger logger = Logger.getLogger(AttachmentDownloadController.class);
44
45
46
47 private static final long serialVersionUID = 1L;
48
49 public AttachmentDownloadController() {
50 }
51
52 public void init(ServletConfig config) {
53 }
54
55 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
56 ServletOutputStream out = null;
57
58 try {
59 IssueService issueService = ServletContextUtils.getItrackerServices().getIssueService();
60
61 Integer attachmentId = null;
62 IssueAttachment attachment = null;
63
64 try {
65 attachmentId = new Integer((request.getParameter("id") == null ? "-1" : (request.getParameter("id"))));
66 attachment = issueService.getIssueAttachment(attachmentId);
67 } catch (NumberFormatException nfe) {
68 if (logger.isDebugEnabled()) {
69 logger.debug("Invalid attachmentId " + request.getParameter("id") + " specified.");
70 }
71 }
72
73 if (attachment == null) {
74 ActionErrors errors = new ActionErrors();
75 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidattachment"));
76 saveMessages(request, errors);
77 forward("/error.do", request, response);
78 return;
79 }
80
81 if (!LoginUtilities.canViewIssue(attachment.getIssue())) {
82 forward("/unauthorized.do", request, response);
83 return;
84 }
85
86 byte[] fileData = issueService.getIssueAttachmentData(attachmentId);
87 if (fileData == null) {
88 ActionErrors errors = new ActionErrors();
89 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.missingattachmentdata"));
90 saveMessages(request, errors);
91 forward("/error.do", request, response);
92 return;
93 }
94
95 response.setContentType(attachment.getType());
96 response.setHeader("Content-Disposition", "attachment; filename=" + attachment.getOriginalFileName() + "");
97 out = response.getOutputStream();
98 logger.debug("Displaying attachment " + attachment.getId() + " of type " + attachment.getType() + " to client. Attachment size: " + fileData.length);
99 out.write(fileData);
100 } catch (IOException ioe) {
101 logger.info("Unable to display attachment.", ioe);
102 } catch (Exception e) {
103 logger.error(e.getMessage(), e);
104 } finally {
105 if (out != null) {
106 out.flush();
107 out.close();
108 }
109 }
110 }
111 }