Return-Path: X-Original-To: apmail-xmlgraphics-commits-archive@www.apache.org Delivered-To: apmail-xmlgraphics-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 5183AD049 for ; Fri, 30 Nov 2012 14:32:48 +0000 (UTC) Received: (qmail 25806 invoked by uid 500); 30 Nov 2012 14:32:48 -0000 Mailing-List: contact commits-help@xmlgraphics.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: general@xmlgraphics.apache.org Delivered-To: mailing list commits@xmlgraphics.apache.org Received: (qmail 25799 invoked by uid 99); 30 Nov 2012 14:32:48 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 30 Nov 2012 14:32:48 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 30 Nov 2012 14:32:46 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id EC0EB23889FA for ; Fri, 30 Nov 2012 14:32:25 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r840167 - in /websites/staging/xmlgraphics/trunk/content: ./ fop/trunk/servlets.html Date: Fri, 30 Nov 2012 14:32:25 -0000 To: commits@xmlgraphics.apache.org From: buildbot@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20121130143225.EC0EB23889FA@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: buildbot Date: Fri Nov 30 14:32:24 2012 New Revision: 840167 Log: Staging update by buildbot for xmlgraphics Modified: websites/staging/xmlgraphics/trunk/content/ (props changed) websites/staging/xmlgraphics/trunk/content/fop/trunk/servlets.html Propchange: websites/staging/xmlgraphics/trunk/content/ ------------------------------------------------------------------------------ --- cms:source-revision (original) +++ cms:source-revision Fri Nov 30 14:32:24 2012 @@ -1 +1 @@ -1415655 +1415657 Modified: websites/staging/xmlgraphics/trunk/content/fop/trunk/servlets.html ============================================================================== --- websites/staging/xmlgraphics/trunk/content/fop/trunk/servlets.html (original) +++ websites/staging/xmlgraphics/trunk/content/fop/trunk/servlets.html Fri Nov 30 14:32:24 2012 @@ -340,11 +340,11 @@ $(document).ready(function () {
-

Apache™ FOP: Servlets

+

Apache™ FOP: Servlets

How to use Apache™ FOP in a Servlet$Revision: 1298724 $

-

Overview

+

Overview

This page discusses topic all around using Apache™ FOP in a servlet environment.

-

Example Servlets in the FOP distribution

+

Example Servlets in the FOP distribution

In the directory {fop-dir}/src/java/org/apache/fop/servlet, you'll find a working example of a FOP-enabled servlet.

The servlet is automatically built when you build Apache FOP using the supplied Ant script. After building the servlet, drop fop.war into the webapps directory of Apache Tomcat (or any other web container). Then, you can use URLs like the following to generate PDF files:

    @@ -357,32 +357,41 @@ $(document).ready(function () {

The source code for the servlet can be found under {fop-dir}/src/java/org/apache/fop/servlet/FopServlet.java. This example servlet should not be used on a public web server connected to the Internet as it does not contain any measures to prevent Denial-of-Service-Attacks. It is provided as an example and as a starting point for your own servlet.

-

Create your own Servlet

+

Create your own Servlet

This section assumes you are familiar with embedding FOP .

-

A minimal Servlet

-

Here is a minimal code snippet to demonstrate the basics: -private FopFactory fopFactory = FopFactory.newInstance(); -private TransformerFactory tFactory = TransformerFactory.newInstance();

-

public void doGet(HttpServletRequest request, - HttpServletResponse response) throws ServletException { - try { - response.setContentType("application/pdf"); - Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, response.getOutputStream()); - Transformer transformer = tFactory.newTransformer(); - Source src = new StreamSource("foo.fo"); - Result res = new SAXResult(fop.getDefaultHandler()); - transformer.transform(src, res); - } catch (Exception ex) { - throw new ServletException(ex); - } -}There are numerous problems with the code snippet above. Its purpose is only to demonstrate the basic concepts. See below for details.

-

Adding XSL tranformation (XSLT)

-

A common requirement is to transform an XML source to XSL-FO using an XSL transformation. It is recommended to use JAXP for this task. The following snippet shows the basic code: -private FopFactory fopFactory = FopFactory.newInstance(); -private TransformerFactory tFactory = TransformerFactory.newInstance();

-

public void init() throws ServletException { - //Optionally customize the FopFactory and TransformerFactory here -}

+

A minimal Servlet

+

Here is a minimal code snippet to demonstrate the basics:

+
private FopFactory fopFactory = FopFactory.newInstance();
+private TransformerFactory tFactory = TransformerFactory.newInstance();
+
+public void doGet(HttpServletRequest request,
+                   HttpServletResponse response) throws ServletException {
+    try {
+        response.setContentType("application/pdf");
+        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, response.getOutputStream());
+        Transformer transformer = tFactory.newTransformer();
+        Source src = new StreamSource("foo.fo");
+        Result res = new SAXResult(fop.getDefaultHandler());
+        transformer.transform(src, res);
+    } catch (Exception ex) {
+        throw new ServletException(ex);
+    }
+}
+
+ + +

There are numerous problems with the code snippet above. Its purpose is only to demonstrate the basic concepts. See below for details.

+

Adding XSL tranformation (XSLT)

+

A common requirement is to transform an XML source to XSL-FO using an XSL transformation. It is recommended to use JAXP for this task. The following snippet shows the basic code:

+
private FopFactory fopFactory = FopFactory.newInstance();
+private TransformerFactory tFactory = TransformerFactory.newInstance();
+
+public void init() throws ServletException {
+    //Optionally customize the FopFactory and TransformerFactory here
+}
+
+ +

//Setup a buffer to obtain the content length
 ByteArrayOutputStream out = new ByteArrayOutputStream();
@@ -416,9 +425,9 @@ private TransformerFactory tFactory = Tr
 

The Source instance used above is simply an example. If you have to read the XML from a string, supply a new StreamSource(new StringReader(xmlstring)) . Constructing and reparsing an XML string is generally less desirable than using a SAXSource if you generate your XML. You can alternatively supply a DOMSource as well. You may also use dynamically generated XSL if you like.

Because you have an explicit Transformer object, you can also use it to explicitely set parameters for the transformation run.

-

Custom configuration

+

Custom configuration

You can easily set up your own FOUserAgent as demonstrated on the Embedding page .

-

Improving performance

+

Improving performance

There are several options to consider:

  • @@ -429,7 +438,7 @@ private TransformerFactory tFactory = Tr

Of course, the performance hints from the Embedding page apply here, too.

-

Accessing resources in your web application

+

Accessing resources in your web application

Often, you will want to use resources (stylesheets, images etc.) which are bundled with your web application. FOP provides a URIResolver implementation that lets you access files via the Servlet's ServletContext. The class is called org.apache.fop.servlet.ServletContextURIResolver .

Here's how to set it up in your servlet. Instantiate a new instance in the servlet's init() method:

/** URIResolver for use by this servlet */
@@ -456,20 +465,27 @@ private TransformerFactory tFactory = Tr
 
 
 

Here are some example snippets:

-

//Setting up the JAXP TransformerFactory -this.transFactory = TransformerFactory.newInstance(); -this.transFactory.setURIResolver(this.uriResolver);

-

-

//Setting up the FOP factory -this.fopFactory = FopFactory.newInstance(); -this.fopFactory.setURIResolver(this.uriResolver);

-

-

//The stylesheet for the JAXP Transfomer -Source xsltSrc = this.uriResolver.resolve( - "servlet-context:/xslt/mystylesheet.xsl", null); -Transformer transformer = this.transFactory.newTransformer(xsltSrc); -transformer.setURIResolver(this.uriResolver);

-

Notes on Microsoft Internet Explorer

+
//Setting up the JAXP TransformerFactory
+this.transFactory = TransformerFactory.newInstance();
+this.transFactory.setURIResolver(this.uriResolver);
+
+[..]
+
+//Setting up the FOP factory
+this.fopFactory = FopFactory.newInstance();
+this.fopFactory.setURIResolver(this.uriResolver);
+
+[..]
+
+//The stylesheet for the JAXP Transfomer
+Source xsltSrc = this.uriResolver.resolve(
+    "servlet-context:/xslt/mystylesheet.xsl", null);
+Transformer transformer = this.transFactory.newTransformer(xsltSrc);
+transformer.setURIResolver(this.uriResolver);
+
+ + +

Notes on Microsoft Internet Explorer

Some versions of Internet Explorer will not automatically show the PDF or call the servlet multiple times. These are well-known limitations of Internet Explorer and are not a problem of the servlet. However, Internet Explorer can still be used to download the PDF so that it can be viewed later. Here are some suggestions in this context:

  • @@ -484,13 +500,13 @@ transformer.setURIResolver(this.uriResol

    Cache in the server. It may help to include a parameter in the URL which has a timestamp as the value min order to decide whether a request is repeated. IEx is reported to retrieve a document up to three times, but never more often.

-

Servlet Engines

+

Servlet Engines

When using a servlet engine, there are potential CLASSPATH issues, and potential conflicts with existing XML/XSLT libraries. Servlet containers also often use their own classloaders for loading webapps, which can cause bugs and security problems.

-

Tomcat

+

Tomcat

Check Tomcat's documentation for detailed instructions about installing FOP and Cocoon. There are known bugs that must be addressed, particularly for Tomcat 4.0.3.

-

WebSphere 3.5

+

WebSphere 3.5

Put a copy of a working parser in some directory where WebSphere can access it. For example, if /usr/webapps/yourapp/servlets is the CLASSPATH for your servlets, copy the Xerces jar into it (any other directory would also be fine). Do not add the jar to the servlet CLASSPATH, but add it to the CLASSPATH of the application server which contains your web application. In the WebSphere administration console, click on the "environment" button in the "general" tab. In the "variable name" box, enter "CLASSPATH". In the "value" box, enter the correct path to the parser jar file (/usr/webapps/yourapp/servlets/Xerces.jar in our example here). Press "OK", then apply the change and restart the application server.

-

Handling complex use cases

+

Handling complex use cases

Sometimes the requirements for a servlet get quite sophisticated: SQL data sources, multiple XSL transformations, merging of several datasources etc. In such a case consider using Apache Cocoon instead of a custom servlet to accomplish your goal.

--------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscribe@xmlgraphics.apache.org For additional commands, e-mail: commits-help@xmlgraphics.apache.org