KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > web > servlets > ReportDownloadController


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 cowsultants.itracker.web.servlets;
20
21 import java.io.*;
22 import java.rmi.*;
23 import java.util.*;
24 import javax.ejb.*;
25 import javax.rmi.*;
26 import javax.naming.*;
27 import javax.servlet.*;
28 import javax.servlet.http.*;
29
30 import cowsultants.itracker.ejb.client.interfaces.*;
31 import cowsultants.itracker.ejb.client.models.*;
32 import cowsultants.itracker.ejb.client.util.*;
33
34 public class ReportDownloadController extends GenericController {
35     InitialContext ic;
36
37     public ReportDownloadController() {
38     }
39
40     public void init(ServletConfig config) {
41         try {
42             ic = new InitialContext();
43         } catch(NamingException ne) {
44             Logger.logError("Could not locate session EJB during initalization.", ne);
45         }
46     }
47
48     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
49         if(! isLoggedInWithRedirect(request, response)) {
50             return;
51         }
52
53         HttpSession session = request.getSession();
54         UserModel user = (UserModel) session.getAttribute("user");
55         try {
56             Object JavaDoc rhRef = ic.lookup("java:comp/env/" + ReportHandler.JNDI_NAME);
57             ReportHandlerHome rhHome = (ReportHandlerHome) PortableRemoteObject.narrow(rhRef, ReportHandlerHome.class);
58             ReportHandler rh = rhHome.create();
59
60             Integer JavaDoc reportId = null;
61             ReportModel report = null;
62
63             try {
64                 reportId = new Integer JavaDoc((request.getParameter("id") == null ? "-1" : (request.getParameter("id"))));
65                 report = rh.getReport(reportId);
66             } catch(NumberFormatException JavaDoc nfe) {
67                 if(Logger.isLoggingDebug()) {
68                     Logger.logDebug("Invalid reportId " + request.getParameter("id") + " specified.");
69                 }
70             }
71
72             if(report == null) {
73                 forward("/error.jsp", request, response);
74                 return;
75             }
76
77             response.setHeader("Content-Disposition", "attachment; filename=report" + report.getId() + "\"");
78             ServletOutputStream out = response.getOutputStream();
79             out.write(rh.getReportFile(reportId));
80             out.close();
81         } catch(CreateException ce) {
82         } catch(NamingException ne) {
83         } catch(IOException ioe) {
84             Logger.logInfo("Unable to display report.", ioe);
85         }
86
87         return;
88     }
89 }
Popular Tags