Clover Coverage Report - itracker
Coverage timestamp: Tue May 1 2012 16:42:12 CEST
41   120   12   13.67
14   77   0.29   3
3     4  
1    
 
 
  AttachmentDownloadController       Line # 42 41 12 0% 0.0
 
No Tests
 
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.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    * @deprecated Use org.itracker.web.actions.admin.attachment.DownloadAttachmentAction instead.
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  0 toggle public AttachmentDownloadController() {
51    }
52   
 
53  0 toggle public void init(ServletConfig config) {
54    }
55   
 
56  0 toggle public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
57  0 ServletOutputStream out = null;
58   
59  0 if(! isLoggedInWithRedirect(request, response)) {
60  0 return;
61    }
62   
63  0 HttpSession session = request.getSession();
64  0 User user = (User) session.getAttribute("user");
65  0 try {
66  0 IssueService issueService = getITrackerServices(request.getSession().getServletContext() ).getIssueService();
67   
68  0 Integer attachmentId = null;
69  0 IssueAttachment attachment = null;
70   
71  0 try {
72  0 attachmentId = new Integer((request.getParameter("id") == null ? "-1" : (request.getParameter("id"))));
73  0 attachment = issueService.getIssueAttachment(attachmentId);
74    } catch(NumberFormatException nfe) {
75  0 if(logger.isDebugEnabled()) {
76  0 logger.debug("Invalid attachmentId " + request.getParameter("id") + " specified.");
77    }
78    }
79   
80  0 if(attachment == null) {
81  0 ActionErrors errors = new ActionErrors();
82  0 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidattachment"));
83  0 saveMessages(request, errors);
84  0 forward("/themes/defaulttheme/error.jsp", request, response);
85  0 return;
86    }
87   
88  0 if(! issueService.canViewIssue(attachment.getIssue().getId(), user)) {
89  0 forward("/themes/defaulttheme/unauthorized.jsp", request, response);
90  0 return;
91    }
92   
93  0 byte[] fileData = issueService.getIssueAttachmentData(attachmentId);
94  0 if(fileData == null) {
95  0 ActionErrors errors = new ActionErrors();
96  0 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.missingattachmentdata"));
97  0 saveMessages(request, errors);
98  0 forward("/themes/defaulttheme/error.jsp", request, response);
99  0 return;
100    }
101   
102  0 response.setContentType(attachment.getType());
103  0 response.setHeader("Content-Disposition", "inline; filename=" + attachment.getOriginalFileName() + "");
104  0 out = response.getOutputStream();
105  0 logger.debug("Displaying attachment " + attachment.getId() + " of type " + attachment.getType() + " to client. Attachment size: " + fileData.length);
106  0 out.write(fileData);
107    } catch(IOException ioe) {
108  0 logger.info("Unable to display attachment.", ioe);
109    } catch(Exception e) {
110  0 logger.error( e.getMessage(), e );
111    } finally {
112  0 if(out != null) {
113  0 out.flush();
114  0 out.close();
115    }
116    }
117   
118  0 return;
119    }
120    }