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