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 java.io.IOException;
23
24 import javax.servlet.ServletConfig;
25 import javax.servlet.ServletException;
26 import javax.servlet.ServletOutputStream;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 import javax.servlet.http.HttpSession;
30
31 import org.apache.log4j.Logger;
32 import org.apache.struts.action.ActionErrors;
33 import org.apache.struts.action.ActionMessage;
34 import org.apache.struts.action.ActionMessages;
35 import org.itracker.model.IssueAttachment;
36 import org.itracker.model.User;
37 import org.itracker.services.IssueService;
38
39
40
41
42 public class AttachmentDownloadController extends GenericController {
43
44 private static final Logger logger = Logger.getLogger(AttachmentDownloadController.class);
45
46
47
48 private static final long serialVersionUID = 1L;
49
50 public AttachmentDownloadController() {
51 }
52
53 public void init(ServletConfig config) {
54 }
55
56 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
57 ServletOutputStream out = null;
58
59 if(! isLoggedInWithRedirect(request, response)) {
60 return;
61 }
62
63 HttpSession session = request.getSession();
64 User user = (User) session.getAttribute("user");
65 try {
66 IssueService issueService = getITrackerServices(request.getSession().getServletContext() ).getIssueService();
67
68 Integer attachmentId = null;
69 IssueAttachment attachment = null;
70
71 try {
72 attachmentId = new Integer((request.getParameter("id") == null ? "-1" : (request.getParameter("id"))));
73 attachment = issueService.getIssueAttachment(attachmentId);
74 } catch(NumberFormatException nfe) {
75 if(logger.isDebugEnabled()) {
76 logger.debug("Invalid attachmentId " + request.getParameter("id") + " specified.");
77 }
78 }
79
80 if(attachment == null) {
81 ActionErrors errors = new ActionErrors();
82 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidattachment"));
83 saveMessages(request, errors);
84 forward("/themes/defaulttheme/error.jsp", request, response);
85 return;
86 }
87
88 if(! issueService.canViewIssue(attachment.getIssue().getId(), user)) {
89 forward("/themes/defaulttheme/unauthorized.jsp", request, response);
90 return;
91 }
92
93 byte[] fileData = issueService.getIssueAttachmentData(attachmentId);
94 if(fileData == null) {
95 ActionErrors errors = new ActionErrors();
96 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.missingattachmentdata"));
97 saveMessages(request, errors);
98 forward("/themes/defaulttheme/error.jsp", request, response);
99 return;
100 }
101
102 response.setContentType(attachment.getType());
103 response.setHeader("Content-Disposition", "inline; filename=" + attachment.getOriginalFileName() + "");
104 out = response.getOutputStream();
105 logger.debug("Displaying attachment " + attachment.getId() + " of type " + attachment.getType() + " to client. Attachment size: " + fileData.length);
106 out.write(fileData);
107 } catch(IOException ioe) {
108 logger.info("Unable to display attachment.", ioe);
109 } catch(Exception e) {
110 logger.error( e.getMessage(), e );
111 } finally {
112 if(out != null) {
113 out.flush();
114 out.close();
115 }
116 }
117
118 return;
119 }
120 }