1 51 52 import java.io.*; 53 54 import javax.servlet.*; 55 import javax.servlet.http.*; 56 57 import org.xml.sax.InputSource ; 58 59 import org.apache.fop.apps.Driver; 60 import org.apache.fop.apps.XSLTInputHandler; 61 import org.apache.fop.messaging.MessageHandler; 62 63 import org.apache.avalon.framework.logger.ConsoleLogger; 64 import org.apache.avalon.framework.logger.Logger; 65 66 87 public class FopServlet extends HttpServlet { 88 public static final String FO_REQUEST_PARAM = "fo"; 89 public static final String XML_REQUEST_PARAM = "xml"; 90 public static final String XSL_REQUEST_PARAM = "xsl"; 91 Logger log = null; 92 93 public void doGet(HttpServletRequest request, 94 HttpServletResponse response) throws ServletException { 95 if (log == null) { 96 log = new ConsoleLogger(ConsoleLogger.LEVEL_WARN); 97 MessageHandler.setScreenLogger(log); 98 } 99 try { 100 String foParam = request.getParameter(FO_REQUEST_PARAM); 101 String xmlParam = request.getParameter(XML_REQUEST_PARAM); 102 String xslParam = request.getParameter(XSL_REQUEST_PARAM); 103 104 if (foParam != null) { 105 File fofile = new File(foParam); 106 FileInputStream file = new FileInputStream(fofile); 108 renderFO(new InputSource (file), response); 109 } else if ((xmlParam != null) && (xslParam != null)) { 110 XSLTInputHandler input = 111 new XSLTInputHandler(new File(xmlParam), 112 new File(xslParam)); 113 renderXML(input, response); 114 } else { 115 PrintWriter out = response.getWriter(); 116 out.println("<html><head><title>Error</title></head>\n"+ 117 "<body><h1>FopServlet Error</h1><h3>No 'fo' "+ 118 "request param given.</body></html>"); 119 } 120 } catch (ServletException ex) { 121 throw ex; 122 } 123 catch (Exception ex) { 124 throw new ServletException(ex); 125 } 126 } 127 128 132 public void renderFO(InputSource foFile, 133 HttpServletResponse response) throws ServletException { 134 try { 135 ByteArrayOutputStream out = new ByteArrayOutputStream(); 136 137 response.setContentType("application/pdf"); 138 139 Driver driver = new Driver(foFile, out); 140 driver.setLogger(log); 141 driver.setRenderer(Driver.RENDER_PDF); 142 driver.run(); 143 144 byte[] content = out.toByteArray(); 145 response.setContentLength(content.length); 146 response.getOutputStream().write(content); 147 response.getOutputStream().flush(); 148 } catch (Exception ex) { 149 throw new ServletException(ex); 150 } 151 } 152 153 158 public void renderXML(XSLTInputHandler input, 159 HttpServletResponse response) throws ServletException { 160 try { 161 ByteArrayOutputStream out = new ByteArrayOutputStream(); 162 163 response.setContentType("application/pdf"); 164 165 Driver driver = new Driver(); 166 driver.setLogger(log); 167 driver.setRenderer(Driver.RENDER_PDF); 168 driver.setOutputStream(out); 169 driver.render(input.getParser(), input.getInputSource()); 170 171 byte[] content = out.toByteArray(); 172 response.setContentLength(content.length); 173 response.getOutputStream().write(content); 174 response.getOutputStream().flush(); 175 } catch (Exception ex) { 176 throw new ServletException(ex); 177 } 178 } 179 180 } 181 | Popular Tags |