Hello!

SUMMARY: How to use a servlet as a generator in Cocoon, but allowing forms 
data to be passed via POST method (And NOT GET!!)
----------------------------------------------

We use Cocoon 2.0.1 and Tomcat 3.2.1, JDK 1.3.1.

I've been trying to solve this proble since a week, but I've been quite 
unlucky, everyone refers to FileGenerator and HTMLGenrator, which doesn't 
work with a Servlet that needs POSTed parameters.

I have a big servlet-based application that must be used from Cocoon. If 
possible, without rewrinting the whole application...
This servlet generates XML responses, that are translated by cocoon to HTML.
UNtil now we have used the request Action to invoke the servlet URL in the 
generator appending the query string, thus
GET ting the parameters to the servlet.

So for example the servlet is invoked in the generator by wrintig something 
like this

<map:generator src="it.test.BigServlet?{QUERY_STRING}"/>

This worked fine for a test environment, but now we must go online.. and we 
must handle submit from forms that can have long input texts. So we need to 
POST data.

I've written a generator that calls directly the (modified) Servlet code 
(that runs in the same Tomcat installation) by passing HttpRequest and 
HttpResponse to a ad-hoc service() method .. thus avoiding the overhead of 
another URL request.

THe Generator emulates instantiation of servlet, intialization(only 
once..), and service calling.. the acutal sending of SAX events in the 
pipeline is done via a callback method in the generator.
Since the servlet generates internally a Document (DOM), this Document must 
be "output" using a SAX parser, in the aforementioned  callback method of 
the generator. (see later for details)

If i put in the sitemap.xconf


<!-- define new generator -->
    <map:generator 
name="testgen"        logger="sitemap.generator.testgen" 
label="content,data"
                   src="TestGenerator"/>
  <!-- ...... -->

         <map:match pattern="test/test.html">

                 <map:generate  type="testgen" >
                         <map:parameter name="servlet-class" 
value="TestServlet"/>
                 </map:generate>

                 < <map:serialize type="xml"/>
         </map:match>


I can obtain correct XML output, provided I am happy  with the raw XML and 
i don't translate it .. (and a side question could be.. how can I make the 
transformation from DOM to SAX efficiently, using some xml.apache.org 
library..  without serializing the DOM on a Stream and parsing it back?? 
THis brute force approach is the one I use now... but this is another 
question.. i suppose.. )

THe Problem arises when I add a Trasformer:

         <map:match pattern="prova/test.html">
                 <map:generate  type="testgen" >
                         <map:parameter name="servlet-class" 
value="TestServlet"/>
                 </map:generate>
                 <map:transform src="stylesheets/prova/TestServlet.xsl"/>
                 <map:serialize type="html"/>
         </map:match>

I've made the xsl such that it works fine with our testServlet and the 
requestGenerator:
...
       <map:generate  type="request" />
      <map:transform src="stylesheets/prova/TestServlet.xsl"/>

With the request generator it works fine and I get neatly formatted HTML, 
with our servlet, that apparently generates the same kind of XMLm it breaks 
down..

Or, to be correct, it does not match any tags. The XSL is read because in 
the browser the basic HTML tags and title of the page (as specified inthe 
top level XSL "/" template-mathcing rule) are displayed, but any further 
XML tag is ignored, and moreover cocoon complains:

FATAL_E (2002-03-13) 09:54.57:446   [core.xslt-processor] 
(/cocoon/prova/test.html) Thread-20/TraxErrorHandler: Error in 
TraxTransformer: javax.xml.transform.TransformerException: 
java.lang.NullPointerException
javax.xml.transform.TransformerException: java.lang.NullPointerException
         at 
org.apache.xalan.transformer.TransformerImpl.transformNode(TransformerImpl.java:1230)
         at 
org.apache.xalan.transformer.TransformerImpl.run(TransformerImpl.java:3139)
         at java.lang.Thread.run(Thread.java:484)
---------
java.lang.NullPointerException
         at 
org.apache.xalan.transformer.TransformerImpl.applyTemplateToNode(TransformerImpl.java:1887)
         at 
org.apache.xalan.transformer.TransformerImpl.transformNode(TransformerImpl.java:1175)
         at 
org.apache.xalan.transformer.TransformerImpl.run(TransformerImpl.java:3139)
         at java.lang.Thread.run(Thread.java:484)


THat's it. I enclose the bit of code of the Generator that translated a DOM 
to SAX events.. perhaps there is something here that works with raw XML but 
breaks down when SAX events are sent down the pipeline..

Any help would be appreciated!
Thanx

Walter Gamba

-----------------------
public synchronized void outputXML(String NSURI, Document doc)
{
         try{
                 super.contentHandler.startDocument();
                 //super.contentHandler.startPrefixMapping("",NSURI);
                 transformDOM2SAX(super.contentHandler,doc);
                 //super.contentHandler.endPrefixMapping("");
                 super.contentHandler.endDocument();
         }catch(SAXException s){
                 log.error("Errore SAX!:"+s);
         }
}

private static /* synchronized */ void transformDOM2SAX(ContentHandler 
cHandler, Document doc) {
  //brute force way
try {
         //1) open stream
         CharArrayWriter writer = new CharArrayWriter();

         printDocument(writer, doc);

         //2) create SAX parserthat can read from stream
         SAXParserFactory spf = SAXParserFactory.newInstance();
         SAXParser parser = spf.newSAXParser();

         //3) build a reader and InputSource
         Reader reader = new CharArrayReader(writer.toCharArray());
         InputSource source = new InputSource(reader);

         log.debug("TestGenerator::transformDOM2SAX -> parse");
         ServletDefaultHandler sdh=new ServletDefaultHandler(cHandler);
         try{
           parser.parse(source, sdh);
         }
         catch(Exception npe){
                 log.error("Errore in PARSE:"+npe,npe);
         }
         //writer.close();
         log.debug("TestGenerator::transformDOM2SAX -> end parse");

}
catch (IOException ioe) {
         log.error("Errore in creazione reader/writer oppure in source:" + 
ioe, ioe);
}
catch (ParserConfigurationException pfe) {
         log.error("Errore parseconfig!:" + pfe, pfe);
}
catch (SAXException sax) {
         log.error("Errore sax!:" + sax, sax);
}


Walter Gamba
[EMAIL PROTECTED]
E-Gramma srl
via Palazzo di Citta' 8 - 10122 TORINO
tel 011-5136583 fax 011-5136582
www.e-gramma.it


---------------------------------------------------------------------
Please check that your question has not already been answered in the
FAQ before posting. <http://xml.apache.org/cocoon/faqs.html>

To unsubscribe, e-mail: <[EMAIL PROTECTED]>
For additional commands, e-mail: <[EMAIL PROTECTED]>

Reply via email to