1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.itracker.web.actions.admin.report;
20
21 import org.apache.log4j.Logger;
22 import org.apache.struts.action.*;
23 import org.itracker.model.Report;
24 import org.itracker.model.util.UserUtilities;
25 import org.itracker.services.ReportService;
26 import org.itracker.web.actions.base.ItrackerBaseAction;
27 import org.itracker.web.util.LoginUtilities;
28 import org.itracker.web.util.ServletContextUtils;
29
30 import javax.servlet.ServletException;
31 import javax.servlet.ServletOutputStream;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34 import java.io.IOException;
35
36
37 public class DownloadReportAction extends ItrackerBaseAction {
38 private static final Logger log = Logger.getLogger(DownloadReportAction.class);
39
40 public DownloadReportAction() {
41 }
42
43 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
44 ActionMessages errors = new ActionMessages();
45
46 String pageTitleKey;
47 String pageTitleArg = "";
48
49 if (!LoginUtilities.hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request, response)) {
50 return mapping.findForward("unauthorized");
51 }
52
53 try {
54 Integer reportId = new Integer((request.getParameter("id") == null ? "-1" : request.getParameter("id")));
55 if (reportId == null || reportId.intValue() < 0) {
56 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidreport"));
57 } else {
58 ReportService reportService = ServletContextUtils.getItrackerServices().getReportService();
59
60 Report report = reportService.getReportDAO().findByPrimaryKey(reportId);
61 if (report != null) {
62 response.setContentType("application/x-itracker-report-export");
63 response.setHeader("Content-Disposition", "attachment; filename=\"ITracker_report_" + report.getId() + ".def\"");
64 ServletOutputStream out = response.getOutputStream();
65 log.debug("Attempting export for: " + report);
66 out.write(report.getFileData());
67 out.flush();
68 out.close();
69 return null;
70 }
71 log.debug("Unknown report " + reportId + " specified for export");
72 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidreport"));
73 }
74 } catch (Exception e) {
75 pageTitleKey = "itracker.web.error.title";
76 request.setAttribute("pageTitleKey", pageTitleKey);
77 request.setAttribute("pageTitleArg", pageTitleArg);
78 log.error("Exception while exporting a report.", e);
79 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
80 }
81 if (!errors.isEmpty()) {
82 saveErrors(request, errors);
83 }
84
85 return mapping.findForward("error");
86 }
87
88 }
89