You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Tom Eugelink <T....@KnowledgePlaza.nl> on 2008/11/04 10:23:18 UTC

file vs servlet through a filter

I have created a XSL-FO filter, it reacts on a parameter and then pulls 
the contents of the response through Apache's FOP. Simple exercise and 
it almost works.

In order to capture the response output I have created a 
BufferedHttpResponseWrapper and BufferedServletOutputStream similar to 
the O'Reilly example 
(http://www.unix.com.ua/orelly/xml/jxslt/ch08_04.htm). The filter reacts 
to any url and the behavior is determined by a parameter being present 
or not.

    <filter>
        <filter-name>XsltfoFilter</filter-name>
        <filter-class>nl.knowledgeplaza.filters.XsltfoFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>XsltfoFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


Now, if the filter captures the result of a servlet, then it works 
perfectly:

public class XslfoTestServlet extends HttpServlet
{
    public void doGet(final HttpServletRequest request, final 
HttpServletResponse response) throws IOException, ServletException
    {
        PrintWriter lWriter = response.getWriter();
        lWriter.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
        lWriter.append("<fo:root 
xmlns:fo=\"http://www.w3.org/1999/XSL/Format\">\n");
        lWriter.append("  <fo:layout-master-set>\n");
        lWriter.append("    <fo:simple-page-master 
master-name=\"A4-portrait\"\n");
        lWriter.append("          page-height=\"29.7cm\" 
page-width=\"21.0cm\" margin=\"2cm\">\n");
        lWriter.append("      <fo:region-body/>\n");
        lWriter.append("    </fo:simple-page-master>\n");
        lWriter.append("  </fo:layout-master-set>\n");
        lWriter.append("  <fo:page-sequence 
master-reference=\"A4-portrait\">\n");
        lWriter.append("    <fo:flow flow-name=\"xsl-region-body\">\n");
        lWriter.append("      <fo:block>\n");
        lWriter.append("        Hello world!\n");
        lWriter.append("      </fo:block>\n");
        lWriter.append("    </fo:flow>\n");
        lWriter.append("  </fo:page-sequence>\n");
        lWriter.append("</fo:root>\n");
        IOUtil.close(lWriter);
    }
}

The filter's log says:

 >> XSLFO
 >> Content: 514 bytes

The content denotes the captured number of bytes by the 
BufferedServletOutputStream. I get a nice PDF and all is well...
But if I use exactly the same XSL-FO source in a file I get this:

 >> XSLFO
 >> Content: 0 bytes
[Fatal Error] :-1:-1: Premature end of file.
[http-8080-1] |/CheyenneServlet, 
session=F7CCB78F6878465CC3BE3DBBC7F874DF| 2008-11-04 10:14:12,125 ERROR 
nl.knowledgeplaza.filters.XsltfoFilter.doFilter(XsltfoFilter.java:137) - 
=========== 2008-11-04 10:14:12

javax.xml.transform.TransformerException: org.xml.sax.SAXParseException: 
Premature end of file.
    at 
org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:501)
    at 
nl.knowledgeplaza.filters.XsltfoFilter.doFilter(XsltfoFilter.java:132)
    at 
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at 
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at 
org.tbee.servlets.util.ConfigurationPropertiesFilter.doFilter(ConfigurationPropertiesFilter.java:194)
    at 
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at 
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at 
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at 
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at 
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at 
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at 
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at 
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
    at 
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
    at 
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
    at 
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.xml.sax.SAXParseException: Premature end of file.
    at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
    at 
org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:484)
    ... 16 more

I can however access the file directly (the filter will not try to 
capture the output when the parameter is not present) and the content is 
returned correctly.

It seems that when accessing the file, there are no calls made to the 
"public void write(int data)" in the BufferedServletOutputStream. I 
added some debugging there and the method simply is not called. I tried 
flushing the original response, closing its stream, but to no avail.

Increasing the file size suddenly does make it write to the outputstream:

 >> XSLFO
 >> Content: 1766 bytes

But then there is an error in stating the generate PDF is corrupt. It 
seems this has something to do with buffers... From servlet works 
perfectly, from a file does not.

Any suggestions?


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: file vs servlet through a filter

Posted by Tom Eugelink <T....@KnowledgePlaza.nl>.
Right. Sorry. Tomcat 6.0.14, Java 1.6.0_03, WindowsXP SP3.

It gets even better: if the servlet copies the file's contents to its 
response, both have 1766 bytes returned. One gives an invalid PDF, the 
other not.

        IOUtil.copy(new FileReader(new File("C:\\...\\test.xslfo.xml")), 
response.getWriter());


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org