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  import org.apache.log4j.Logger;
22  import org.itracker.model.Report;
23  import org.itracker.services.ReportService;
24  import org.itracker.web.util.ServletContextUtils;
25  
26  import javax.servlet.ServletConfig;
27  import javax.servlet.ServletException;
28  import javax.servlet.ServletOutputStream;
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  import java.io.IOException;
32  
33  
34  public class ReportDownloadController extends GenericController {
35  
36      private static final Logger logger = Logger.getLogger(ReportDownloadController.class);
37      /**
38       *
39       */
40      private static final long serialVersionUID = 1L;
41  
42      public ReportDownloadController() {
43      }
44  
45      public void init(ServletConfig config) {
46  
47      }
48  
49      public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
50  
51          try {
52              ReportService reportService = ServletContextUtils.getItrackerServices().getReportService();
53  
54              Integer reportId;
55              Report report = null;
56  
57              try {
58                  reportId = new Integer((request.getParameter("id") == null ? "-1" : (request.getParameter("id"))));
59                  report = reportService.getReportDAO().findByPrimaryKey(reportId);
60              } catch (NumberFormatException nfe) {
61                  if (logger.isDebugEnabled()) {
62                      logger.debug("Invalid reportId " + request.getParameter("id") + " specified.");
63                  }
64              }
65  
66              if (report == null) {
67                  forward("/error.do", request, response);
68                  return;
69              }
70  
71              response.setHeader("Content-Disposition", "attachment; filename=report" + report.getId() + "\"");
72              ServletOutputStream out = response.getOutputStream();
73              out.write(report.getFileData());
74              out.close();
75          } catch (IOException ioe) {
76              logger.info("Unable to display report.", ioe);
77          }
78      }
79  }