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 java.io.IOException;
22  
23  import javax.servlet.ServletConfig;
24  import javax.servlet.ServletException;
25  import javax.servlet.ServletOutputStream;
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  import org.apache.log4j.Logger;
30  import org.itracker.model.Report;
31  import org.itracker.services.ReportService;
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          if(! isLoggedInWithRedirect(request, response)) {
51              return;
52          }
53          
54          // TODO: the 2-3 lines are most propably not used; commented, task added
55          // Commented more not used lines, but added a task for testing. 
56          // HttpSession session = request.getSession();
57          // UserModel user = (UserModel) session.getAttribute("user");
58          // User user = (session == null ? null : (User) session.getAttribute(Constants.USER_KEY));
59  
60          try {
61              ReportService reportService = getITrackerServices(getServletContext()).getReportService();
62  
63              Integer reportId = null;
64              Report report = null;
65  
66              try {
67                  reportId = new Integer((request.getParameter("id") == null ? "-1" : (request.getParameter("id"))));
68                  report = reportService.getReportDAO().findByPrimaryKey(reportId);
69              } catch(NumberFormatException nfe) {
70                  if (logger.isDebugEnabled()) {
71                      logger.debug("Invalid reportId " + request.getParameter("id") + " specified.");
72                  }
73              }
74  
75              if(report == null) {
76                  forward("/error.jsp", request, response);
77                  return;
78              }
79  
80              response.setHeader("Content-Disposition", "attachment; filename=report" + report.getId() + "\"");
81              ServletOutputStream out = response.getOutputStream();
82              //out.write(reportService.getReportFile(reportId));
83              out.close();
84          } catch(IOException ioe) {
85              logger.info("Unable to display report.", ioe);
86          }
87  
88          return;
89      }
90  }