📄 reportservlet.java
字号:
/* CRMS, customer relationship management system Copyright (C) 2003 Service To Youth Council This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA For further information contact the SYC ICT department on GPL@syc.net.au 98 Kermode Street North Adelaide South Australia SA 5006 +61 (0)8 8367 0755 *//* * ReportServlet.java * * Created on 11 June 2003, 16:27 */package crms.servlet;import java.io.*;import java.net.*;import java.util.*;import javax.servlet.*;import javax.servlet.http.*;import javax.xml.transform.*;import javax.xml.transform.sax.*;import javax.xml.transform.stream.*;import org.apache.fop.apps.Driver;import crms.report.*;import crms.vo.*;import org.apache.log4j.Logger;import crms.report.*;/** * * @author Administrator */public class ReportServlet extends HttpServlet { private static Logger logger = Logger.getLogger(ReportServlet.class); private String stylesheetPath = null; public static String STYLESHEET_PATH = "stylesheet-path"; public static String PARAM_REPORT_KEY = "report-key"; public static String REPORT_CONFIG_PATH = "config-path"; /** Initializes the servlet. */ public void init(ServletConfig config) throws ServletException { super.init(config); stylesheetPath = config.getInitParameter(STYLESHEET_PATH); logger.debug("Initialized report servlet with stylsheet path: " + stylesheetPath); String configPath = config.getInitParameter(REPORT_CONFIG_PATH); ReportFactory.getInstance(configPath, true); logger.debug("Initialized ReportFactory servlet with report config path: " + configPath); } /** Destroys the servlet. */ public void destroy() { } /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. * @param request servlet request * @param response servlet response */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { logger.debug("Processing report request"); //response.setContentType("text/html"); BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream()); String reportKey = request.getParameter(PARAM_REPORT_KEY); logger.debug("reportKey = " + reportKey); ReportConfig config = ReportFactory.getInstance().getReportConfigByKey(reportKey); HashMap params = new HashMap(); Enumeration en = request.getParameterNames(); while (en.hasMoreElements()) { String name = (String) en.nextElement(); String value = (String) request.getParameter(name); params.put(name, value); } params.remove(PARAM_REPORT_KEY); Class clazz = null; ReportProcessor reportProcessor = null; try { clazz = Class.forName(config.getProcessorClass()); reportProcessor = (ReportProcessor) clazz.newInstance(); logger.debug("Created instance of " + clazz.getName()); } catch (Exception ex) { logger.debug("Exception creating report processing object.", ex); throw new ServletException("Exception creating report processing object.", ex); } try { logger.debug("Beginning report processing..."); Report report = reportProcessor.processReport(params); logger.debug("Report processing complete..."); report.setHeading(config.getHeading()); if (report.getSubHeading() == null) { report.setSubHeading(config.getSubHeading()); } report.setOrientation(config.getOrientation()); report.setColumnHeadings(config.getHeadings()); report.setColumnWidths(config.getWidths()); response.setContentType("application/pdf"); response.addHeader("Content-Disposition", "inline;filename=\"report.pdf\""); logger.debug("Beginning transformation/output of PDF"); produceOutput(out, report); } catch (Exception ex) { logger.debug("Exception processing/outputting report.", ex); throw new ServletException("Exception processing/outputting report.", ex); // How to deal with these - probably go to error page. } out.flush(); out.close(); } /** * Do the initial transformation (xml -> fo here, and then do fo -> PDF..) */ public void produceOutput(OutputStream out, Report report) throws TransformerConfigurationException, TransformerException { // From the FOP documentation: // http://xml.apache.org/fop/embedding.html#render-with-xslt // // If you want to process XSL-FO generated from XML using XSLT we // recommend using standard JAXP to do the XSLT part and piping the // generated SAX events directly through to FOP. Here's how this would // look like: logger.debug("Setting up driver"); Driver driver = new Driver(); //Setup logging here: driver.setLogger(... driver.setRenderer(Driver.RENDER_PDF); //Setup the OutputStream for FOP driver.setOutputStream(out); //Make sure the XSL transformation's result is piped through to FOP Result res = new SAXResult(driver.getContentHandler()); //Setup XML input Source src = new StreamSource(report.getInputStream()); File stylesheet = null; if (report.getOrientation() == Report.ORIENTATION_PORTRAIT) { stylesheet = new File(stylesheetPath, "standard-report-portrait.xsl"); } else { stylesheet = new File(stylesheetPath, "standard-report-landscape.xsl"); } logger.debug("XSL Stylesheet is " + stylesheet.getAbsolutePath()); //Setup Transformer Source xsltSrc = new StreamSource(stylesheet); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(xsltSrc); transformer.setParameter("heading", report.getHeading()); transformer.setParameter("sub-heading", report.getSubHeading()); transformer.setParameter("date", report.getRunDate()); //Start the transformation and rendering process transformer.transform(src, res); logger.debug("Transformation complete."); } /** Handles the HTTP <code>GET</code> method. * @param request servlet request * @param response servlet response */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** Handles the HTTP <code>POST</code> method. * @param request servlet request * @param response servlet response */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** Returns a short description of the servlet. */ public String getServletInfo() { return "Short description"; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -