1 17 18 19 20 package org.apache.fop.servlet; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.PrintWriter ; 25 26 import javax.servlet.ServletException ; 27 import javax.servlet.http.HttpServlet ; 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.http.HttpServletResponse ; 30 import javax.xml.transform.Result ; 31 import javax.xml.transform.Source ; 32 import javax.xml.transform.Transformer ; 33 import javax.xml.transform.TransformerException ; 34 import javax.xml.transform.TransformerFactory ; 35 import javax.xml.transform.URIResolver ; 36 import javax.xml.transform.sax.SAXResult ; 37 import javax.xml.transform.stream.StreamSource ; 38 39 import org.apache.commons.io.output.ByteArrayOutputStream; 40 import org.apache.commons.logging.impl.SimpleLog; 41 42 import org.apache.fop.apps.FOUserAgent; 43 import org.apache.fop.apps.Fop; 44 import org.apache.fop.apps.FOPException; 45 import org.apache.fop.apps.FopFactory; 46 import org.apache.fop.apps.MimeConstants; 47 48 72 public class FopServlet extends HttpServlet { 73 74 75 protected static final String FO_REQUEST_PARAM = "fo"; 76 77 protected static final String XML_REQUEST_PARAM = "xml"; 78 79 protected static final String XSLT_REQUEST_PARAM = "xslt"; 80 81 82 protected SimpleLog log = null; 83 84 protected TransformerFactory transFactory = null; 85 86 protected FopFactory fopFactory = null; 87 88 protected URIResolver uriResolver; 89 90 93 public void init() throws ServletException { 94 this.log = new SimpleLog("FOP/Servlet"); 95 log.setLevel(SimpleLog.LOG_LEVEL_WARN); 96 this.uriResolver = new ServletContextURIResolver(getServletContext()); 97 this.transFactory = TransformerFactory.newInstance(); 98 this.transFactory.setURIResolver(this.uriResolver); 99 this.fopFactory = FopFactory.newInstance(); 101 this.fopFactory.setURIResolver(this.uriResolver); 102 configureFopFactory(); 103 } 104 105 109 protected void configureFopFactory() { 110 } 112 113 116 public void doGet(HttpServletRequest request, 117 HttpServletResponse response) throws ServletException { 118 try { 119 String foParam = request.getParameter(FO_REQUEST_PARAM); 121 String xmlParam = request.getParameter(XML_REQUEST_PARAM); 122 String xsltParam = request.getParameter(XSLT_REQUEST_PARAM); 123 124 if (foParam != null) { 126 renderFO(foParam, response); 127 } else if ((xmlParam != null) && (xsltParam != null)) { 128 renderXML(xmlParam, xsltParam, response); 129 } else { 130 response.setContentType("text/html"); 131 PrintWriter out = response.getWriter(); 132 out.println("<html><head><title>Error</title></head>\n" 133 + "<body><h1>FopServlet Error</h1><h3>No 'fo' " 134 + "request param given.</body></html>"); 135 } 136 } catch (Exception ex) { 137 throw new ServletException (ex); 138 } 139 } 140 141 146 protected Source convertString2Source(String param) { 147 return new StreamSource (new File (param)); 148 } 149 150 private void sendPDF(byte[] content, HttpServletResponse response) throws IOException { 151 response.setContentType("application/pdf"); 153 response.setContentLength(content.length); 154 response.getOutputStream().write(content); 155 response.getOutputStream().flush(); 156 } 157 158 169 protected void renderFO(String fo, HttpServletResponse response) 170 throws FOPException, TransformerException , IOException { 171 172 Source foSrc = convertString2Source(fo); 174 175 Transformer transformer = this.transFactory.newTransformer(); 177 transformer.setURIResolver(this.uriResolver); 178 179 render(foSrc, transformer, response); 181 } 182 183 196 protected void renderXML(String xml, String xslt, HttpServletResponse response) 197 throws FOPException, TransformerException , IOException { 198 199 Source xmlSrc = convertString2Source(xml); 201 Source xsltSrc = convertString2Source(xslt); 202 203 Transformer transformer = this.transFactory.newTransformer(xsltSrc); 205 transformer.setURIResolver(this.uriResolver); 206 207 render(xmlSrc, transformer, response); 209 } 210 211 226 protected void render(Source src, Transformer transformer, HttpServletResponse response) 227 throws FOPException, TransformerException , IOException { 228 229 FOUserAgent foUserAgent = getFOUserAgent(); 230 231 ByteArrayOutputStream out = new ByteArrayOutputStream(); 233 234 Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out); 236 237 Result res = new SAXResult (fop.getDefaultHandler()); 239 240 transformer.transform(src, res); 242 243 sendPDF(out.toByteArray(), response); 245 } 246 247 248 protected FOUserAgent getFOUserAgent() { 249 FOUserAgent userAgent = fopFactory.newFOUserAgent(); 250 return userAgent; 252 } 253 254 } 255 | Popular Tags |