1 11 package com.tonbeller.jpivot.print; 12 13 import java.io.ByteArrayInputStream ; 14 import java.io.ByteArrayOutputStream ; 15 import java.io.IOException ; 16 import java.io.OutputStream ; 17 import java.io.PrintWriter ; 18 import java.io.StringWriter ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 import java.io.File ; 23 24 import org.apache.log4j.Logger; 25 26 import javax.servlet.ServletConfig ; 27 import javax.servlet.ServletException ; 28 import javax.servlet.http.HttpServlet ; 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletResponse ; 31 import javax.servlet.http.HttpSession ; 32 import javax.xml.transform.Transformer ; 33 import javax.xml.transform.dom.DOMSource ; 34 import javax.xml.transform.stream.StreamResult ; 35 36 import org.apache.fop.apps.Driver; 37 import org.apache.fop.apps.FOPException; 38 import org.apache.fop.configuration.Configuration; 39 import org.apache.fop.apps.Options; 40 import org.w3c.dom.Document ; 41 import org.xml.sax.InputSource ; 42 43 import com.tonbeller.jpivot.chart.ChartComponent; 44 import com.tonbeller.jpivot.table.TableComponent; 45 import com.tonbeller.wcf.component.RendererParameters; 46 import com.tonbeller.wcf.controller.RequestContext; 47 import com.tonbeller.wcf.controller.RequestContextFactoryFinder; 48 import com.tonbeller.wcf.utils.XmlUtils; 49 54 55 public class PrintServlet extends HttpServlet { 56 private static Logger logger = Logger.getLogger(PrintServlet.class); 57 private static final int XML = 0; 58 private static final int PDF = 1; 59 String basePath; 60 String filename; 61 62 64 public void init(ServletConfig config) throws ServletException { 65 super.init(config); 66 try { 67 Configuration.put("fontBaseDir", config.getServletContext().getRealPath("/WEB-INF/jpivot/print/")); 69 String fopConfigPath = config.getServletContext().getRealPath("/WEB-INF/jpivot/print/userconfig.xml"); 71 new Options(new File (fopConfigPath)); 73 74 } catch (FOPException e) { 75 e.printStackTrace(); 76 logger.info("FOP user config file not loaded"); 77 } catch (Exception e) { 78 e.printStackTrace(); 79 logger.info("FOP user config file not loaded"); 80 } 81 } 82 83 85 public void destroy() { 86 87 } 88 89 93 94 protected void processRequest(RequestContext context) throws ServletException , IOException { 95 HttpServletRequest request = context.getRequest(); 96 HttpServletResponse response = context.getResponse(); 97 if (request.getParameter("cube") != null && request.getParameter("type") != null) { 98 try { 99 String xslUri = null; 100 int type = Integer.parseInt(request.getParameter("type")); 101 switch (type) { 102 case XML: 103 xslUri = "/WEB-INF/jpivot/table/xls_mdxtable.xsl"; 104 RendererParameters.setParameter(context.getRequest(), "mode", "excel", "request"); 105 response.setContentType("application/vnd.ms-excel"); 106 filename = "xls_export.xls"; 107 break; 108 case PDF: 109 xslUri = "/WEB-INF/jpivot/table/fo_mdxtable.xsl"; 110 RendererParameters.setParameter(context.getRequest(), "mode", "print", "request"); 111 response.setContentType("application/pdf"); 112 filename = "xls_export.pdf"; 113 break; 114 } 115 if (xslUri != null) { 116 boolean xslCache = true; 117 String tableRef = "table" + request.getParameter("cube"); 119 String chartRef = "chart" + request.getParameter("cube"); 120 String printRef = "print" + request.getParameter("cube"); 121 122 Map parameters = new HashMap (); 123 124 OutputStream outStream = response.getOutputStream(); 125 PrintWriter out = new PrintWriter (outStream); 126 HttpSession session = request.getSession(); 127 response.setHeader("Content-Disposition", "attachment; filename=" + filename); 129 130 TableComponent table = (TableComponent) context.getModelReference(tableRef); 132 if (table != null) { 134 PrintComponent printConfig = (PrintComponent) context.getModelReference(printRef); 136 if (printConfig != null) { 137 if (printConfig.isSetTableWidth()) { 138 parameters.put(printConfig.PRINT_TABLE_WIDTH, new Double (printConfig.getTableWidth())); 139 } 140 if (printConfig.getReportTitle().trim().length() != 0) { 141 parameters.put(printConfig.PRINT_TITLE, printConfig.getReportTitle().trim()); 142 } 143 parameters.put(printConfig.PRINT_PAGE_ORIENTATION, printConfig.getPageOrientation()); 144 parameters.put(printConfig.PRINT_PAPER_TYPE, printConfig.getPaperType()); 145 if (printConfig.getPaperType().equals("custom")) { 146 parameters.put(printConfig.PRINT_PAGE_WIDTH, new Double (printConfig.getPageWidth())); 147 parameters.put(printConfig.PRINT_PAGE_HEIGHT, new Double (printConfig.getPageHeight())); 148 } 149 parameters.put(printConfig.PRINT_CHART_PAGEBREAK, new Boolean (printConfig.isChartPageBreak())); 150 151 } 152 153 ChartComponent chart = (ChartComponent) request.getSession().getAttribute(chartRef); 155 if (chart != null && chart.isVisible()) { 156 157 String host = request.getServerName(); 158 int port = request.getServerPort(); 159 String location = request.getContextPath(); 160 String scheme = request.getScheme(); 161 162 String chartServlet = scheme + "://" + host + ":" + port + location + "/GetChart"; 163 parameters.put("chartimage", chartServlet + "?filename=" + chart.getFilename()); 164 parameters.put("chartheight", new Integer (chart.getChartHeight())); 165 parameters.put("chartwidth", new Integer (chart.getChartWidth())); 166 } 167 168 171 parameters.put("context", context.getRequest().getContextPath()); 173 174 177 table.setDirty(true); 178 Document document = table.render(context); 179 table.setDirty(true); 180 181 DOMSource source = new DOMSource (document); 182 Transformer transformer = XmlUtils.getTransformer(session, xslUri, xslCache); 184 for (Iterator it = parameters.keySet().iterator(); it.hasNext();) { 185 String name = (String ) it.next(); 186 Object value = parameters.get(name); 187 transformer.setParameter(name, value); 188 } 189 StringWriter sw = new StringWriter (); 190 StreamResult result = new StreamResult (sw); 191 transformer.transform(source, result); 193 sw.flush(); 194 195 if (type == XML) { 197 System.out.println("Writing XLS"); 198 response.setContentLength(sw.toString().length()); 199 out.write(sw.toString()); 200 RendererParameters.removeParameter(context.getRequest(), "mode", "excel", "request"); 201 } else { 203 System.out.println("Creating PDF!"); 205 try { 206 ByteArrayInputStream bain = new ByteArrayInputStream (sw.toString().getBytes("UTF-8")); 207 ByteArrayOutputStream baout = new ByteArrayOutputStream (16384); 208 convertFO2PDF(bain, baout); 209 final byte[] content = baout.toByteArray(); 210 response.setContentLength(content.length); 211 outStream.write(content); 212 RendererParameters.removeParameter(context.getRequest(), "mode", "print", "request"); 213 } catch (Exception e) { 215 e.printStackTrace(); 216 } 217 } 218 out.flush(); 220 out.close(); 221 outStream.flush(); 222 } 223 } 224 } catch (Exception e) { 225 e.printStackTrace(); 226 } 227 } 228 } 229 230 233 public void convertFO2PDF(ByteArrayInputStream bain, ByteArrayOutputStream baout) throws IOException , FOPException { 234 235 System.out.println("Construct driver"); 236 Driver driver = new Driver(); 237 238 System.out.println("Setup Renderer (output format)"); 239 driver.setRenderer(Driver.RENDER_PDF); 240 241 try { 242 driver.setOutputStream(baout); 243 System.out.println("Setup input"); 244 try { 245 driver.setInputSource(new InputSource (bain)); 246 247 System.out.println("Process FO"); 248 driver.run(); 249 System.out.println("PDF file generation completed"); 250 } finally { 251 } 252 } finally { 253 } 254 } 255 256 260 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 261 doPost(request, response); 262 } 263 264 268 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 269 RequestContext context = RequestContextFactoryFinder.createContext(request, response, true); 270 try { 271 processRequest(context); 272 } finally { 273 context.invalidate(); 274 } 275 } 276 277 279 public String getServletInfo() { 280 return "Export OLAP table"; 281 } 282 283 } 284 | Popular Tags |