KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > FopServlet


1 /*
2  * $Id: FopServlet.java,v 1.1.2.2 2003/02/25 16:06:45 jeremias Exp $
3  * ============================================================================
4  * The Apache Software License, Version 1.1
5  * ============================================================================
6  *
7  * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modifica-
10  * tion, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if any, must
20  * include the following acknowledgment: "This product includes software
21  * developed by the Apache Software Foundation (http://www.apache.org/)."
22  * Alternately, this acknowledgment may appear in the software itself, if
23  * and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "FOP" and "Apache Software Foundation" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * apache@apache.org.
29  *
30  * 5. Products derived from this software may not be called "Apache", nor may
31  * "Apache" appear in their name, without prior written permission of the
32  * Apache Software Foundation.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  * ============================================================================
45  *
46  * This software consists of voluntary contributions made by many individuals
47  * on behalf of the Apache Software Foundation and was originally created by
48  * James Tauber <jtauber@jtauber.com>. For more information on the Apache
49  * Software Foundation, please see <http://www.apache.org/>.
50  */

51
52 import java.io.*;
53
54 import javax.servlet.*;
55 import javax.servlet.http.*;
56
57 import org.xml.sax.InputSource JavaDoc;
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 /**
67  * Example servlet to generate a PDF from a servlet.
68  * Servlet param is:
69  * <ul>
70  * <li>fo: the path to a formatting object file to render
71  * </ul>
72  *
73  * Example URL: http://servername/fop/servlet/FopServlet?fo=readme.fo
74  * Example URL: http://servername/fop/servlet/FopServlet?xml=data.xml&xsl=format.xsl
75  * Compiling: you will need
76  * - servlet_2_2.jar
77  * - fop.jar
78  * - sax api
79  * - avalon-framework-x.jar (where x is the version found the FOP lib dir)
80  *
81  * Running: you will need in the WEB-INF/lib/ directory:
82  * - fop.jar
83  * - batik.jar
84  * - xalan-2.0.0.jar
85  * - avalon-framework-x.jar (where x is the version found the FOP lib dir)
86  */

87 public class FopServlet extends HttpServlet {
88     public static final String JavaDoc FO_REQUEST_PARAM = "fo";
89     public static final String JavaDoc XML_REQUEST_PARAM = "xml";
90     public static final String JavaDoc 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 JavaDoc foParam = request.getParameter(FO_REQUEST_PARAM);
101             String JavaDoc xmlParam = request.getParameter(XML_REQUEST_PARAM);
102             String JavaDoc xslParam = request.getParameter(XSL_REQUEST_PARAM);
103
104             if (foParam != null) {
105                 File fofile = new File(foParam);
106                 //log.warn("FO: "+fofile.getCanonicalPath());
107
FileInputStream file = new FileInputStream(fofile);
108                 renderFO(new InputSource JavaDoc(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 JavaDoc ex) {
124             throw new ServletException(ex);
125         }
126     }
127
128     /**
129      * Renders an FO inputsource into a PDF file which is written
130      * directly to the response object's OutputStream
131      */

132     public void renderFO(InputSource JavaDoc 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 JavaDoc ex) {
149             throw new ServletException(ex);
150         }
151     }
152
153     /**
154      * Renders an XML file into a PDF file by applying a stylesheet
155      * that converts the XML to XSL:FO. The PDF is written
156      * directly to the response object's OutputStream
157      */

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 JavaDoc ex) {
176             throw new ServletException(ex);
177         }
178     }
179
180 }
181
Popular Tags