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.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  	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 }