You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cocoon.apache.org by Peter Neu <pe...@gmx.net> on 2006/02/14 10:40:00 UTC

How to use cocoon servlet only for rendering documents?

Hello,

I already posted this question in the dev list but nobody responded so I put
it here.

I have the use case that I want to combine Cocoon with another framework say
Struts (there is lots of legacy code). I want to do all the web front end
work with Struts and do the rendering of pdf, xml, excel etc with Cocoon.
Thus I want pass parameters to the Cocoon servlet and then let cocoon query
data from the database and then render the stuff. I would only need the
basic functionality to render the documents and the SQLTransformer. Is this
possible?

I heared some talk about the Paranoid cocoon servlet but I don't really
understand how this fits in. Can somebody please explain or tell me where to

find information on this?

Cheers,
Pete



Re: AW: How to use cocoon servlet only for rendering documents?

Posted by Peter Hunsberger <pe...@gmail.com>.
On 2/15/06, Peter Neu <pe...@gmx.net> wrote:
> Hello Bertrand,
>
> I managed to send my xml in a post request to cocoon and retrieve the ouput
> back in pdf. But one thing I couldn't figure out so far is how to actually
> read the xml from the post request into my sitemap. Can you please provide a
> short code snippet?
>
> The relevant part looks like this:
>
> <map:match pattern="pdf">
>  <map:generate src="data/user.xml"/>  //still a hard coded resource
>  <map:transform src="stylesheets/user.xsl"/>
>  <map:serialize type="fo2pdf"/>
>  </map:match>
>

Don't you just want something like:

<map:match pattern="pdf/*">
    <map:generate src="data/{1}"/>

Or is the source name not something the user can specify? If not,
where is it supposed to come from?

--
Peter Hunsberger

[Solved] How to use cocoon servlet only for rendering documents?

Posted by Peter Neu <pe...@gmx.net>.
Ok, got it. Again thanks for the help. Maybed I could refactor the code for
a tutorial in the near future. 

-Pete



AW: AW: AW: How to use cocoon servlet only for rendering documents?

Posted by Peter Neu <pe...@gmx.net>.
Hello,

thanks for the advice this works perfectly! As suggested only adding
streaming generator suffices to do it.

Just one last question to end the subject: When I generate the xml in my
non-cocoon apps. It looks somehow like this:

PrintWriter out = response.getWriter();
StreamResult streamResult = new StreamResult(out);
SAXTransformerFactory tf = (SAXTransformerFactory)
SAXTransformerFactory.newInstance();
TransformerHandler hd = tf.newTransformerHandler();
Transformer serializer = hd.getTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
hd.setResult(streamResult);
AttributesImpl atts = new AttributesImpl();
hd.startDocument();
hd.startElement("", "", "Document", atts);
hd.characters("Hello Document".toCharArray(), 0, "Hello Document".length());
atts.clear();
hd.endElement("", "", "Document");
hd.endDocument();

Now in order to pass it to cocoon via the commons httpclient I need it to
be a File Input Stream e.g. a file object. 

post.setRequestEntity(new InputStreamRequestEntity(
                new FileInputStream(input), input.length()));

But this is not a good idea because it would be then saved locally and would
create some overhead. What I need to do is keep it in memory to pass the sax
pipeline to the post call. How could I do this?

Cheers,
Pete




-----Ursprüngliche Nachricht-----
Von: Bertrand Delacretaz [mailto:bdelacretaz@apache.org] 
Gesendet: Mittwoch, 15. Februar 2006 17:58
An: dev@cocoon.apache.org
Betreff: Re: AW: AW: How to use cocoon servlet only for rendering documents?

Le 15 févr. 06, à 17:49, Peter Hunsberger a écrit :

> ...Won't the request generator also
> do the trick at this point?  Or will it muck the embedded XML?..

The RequestGenerator should work if input values are passed as  
parameters (although I've never tried it with XML fragments as  
parameters).

The StreamGenerator also gives access to the POST body (depending on  
the mimetype of the request, see  
http://cocoon.apache.org/2.1/apidocs/org/apache/cocoon/generation/ 
StreamGenerator.html)

-Bertrand



Re: AW: AW: How to use cocoon servlet only for rendering documents?

Posted by Bertrand Delacretaz <bd...@apache.org>.
Le 15 févr. 06, à 17:49, Peter Hunsberger a écrit :

> ...Won't the request generator also
> do the trick at this point?  Or will it muck the embedded XML?..

The RequestGenerator should work if input values are passed as  
parameters (although I've never tried it with XML fragments as  
parameters).

The StreamGenerator also gives access to the POST body (depending on  
the mimetype of the request, see  
http://cocoon.apache.org/2.1/apidocs/org/apache/cocoon/generation/ 
StreamGenerator.html)

-Bertrand

Re: AW: AW: How to use cocoon servlet only for rendering documents?

Posted by Upayavira <uv...@odoko.co.uk>.
Peter Hunsberger wrote:
> On 2/15/06, Upayavira <uv...@odoko.co.uk> wrote:
>> Peter Neu wrote:
>>> Hello Bertrand,
>>>
>>> I managed to send my xml in a post request to cocoon and retrieve the ouput
>>> back in pdf. But one thing I couldn't figure out so far is how to actually
>>> read the xml from the post request into my sitemap. Can you please provide a
>>> short code snippet?
>>>
>>> The relevant part looks like this:
>>>
>>> <map:match pattern="pdf">
>>>   <map:generate src="data/user.xml"/>  //still a hard coded resource
>>>   <map:transform src="stylesheets/user.xsl"/>
>>>   <map:serialize type="fo2pdf"/>
>>>  </map:match>
>> You need the stream generator:
>>
>>  <map:match pattern="pdf">
>>   <map:generate type="stream"/>
>>   <map:transform src="stylesheets/user.xsl"/>
>>   <map:serialize type="fo2pdf"/>
>>  </map:match>
>>
>> I think that is enough. Try it.
> 
> Ahh, I get what he's trying to do.  Won't the request generator also
> do the trick at this point?  Or will it muck the embedded XML?

RequestGenerator will give far too much info (assuming it gives the
stream content, which I'm not sure). The StreamGenerator is really easy,
although it might need a little configuring. It can either take data
sent with PUT, or take a particular form field sent by POST.

Upayavira

Re: AW: AW: How to use cocoon servlet only for rendering documents?

Posted by Peter Hunsberger <pe...@gmail.com>.
On 2/15/06, Upayavira <uv...@odoko.co.uk> wrote:
> Peter Neu wrote:
> > Hello Bertrand,
> >
> > I managed to send my xml in a post request to cocoon and retrieve the ouput
> > back in pdf. But one thing I couldn't figure out so far is how to actually
> > read the xml from the post request into my sitemap. Can you please provide a
> > short code snippet?
> >
> > The relevant part looks like this:
> >
> > <map:match pattern="pdf">
> >   <map:generate src="data/user.xml"/>  //still a hard coded resource
> >   <map:transform src="stylesheets/user.xsl"/>
> >   <map:serialize type="fo2pdf"/>
> >  </map:match>
>
> You need the stream generator:
>
>  <map:match pattern="pdf">
>   <map:generate type="stream"/>
>   <map:transform src="stylesheets/user.xsl"/>
>   <map:serialize type="fo2pdf"/>
>  </map:match>
>
> I think that is enough. Try it.

Ahh, I get what he's trying to do.  Won't the request generator also
do the trick at this point?  Or will it muck the embedded XML?

--
Peter Hunsberger

Re: AW: AW: How to use cocoon servlet only for rendering documents?

Posted by Upayavira <uv...@odoko.co.uk>.
Peter Neu wrote:
> Hello Bertrand,
> 
> I managed to send my xml in a post request to cocoon and retrieve the ouput
> back in pdf. But one thing I couldn't figure out so far is how to actually
> read the xml from the post request into my sitemap. Can you please provide a
> short code snippet?
> 
> The relevant part looks like this:
> 
> <map:match pattern="pdf">
>   <map:generate src="data/user.xml"/>  //still a hard coded resource
>   <map:transform src="stylesheets/user.xsl"/>
>   <map:serialize type="fo2pdf"/>
>  </map:match>

You need the stream generator:

 <map:match pattern="pdf">
   <map:generate type="stream"/>
   <map:transform src="stylesheets/user.xsl"/>
   <map:serialize type="fo2pdf"/>
  </map:match>

I think that is enough. Try it.

Upayavira

AW: AW: How to use cocoon servlet only for rendering documents?

Posted by Peter Neu <pe...@gmx.net>.
Hello Bertrand,

I managed to send my xml in a post request to cocoon and retrieve the ouput
back in pdf. But one thing I couldn't figure out so far is how to actually
read the xml from the post request into my sitemap. Can you please provide a
short code snippet?

The relevant part looks like this:

<map:match pattern="pdf">
  <map:generate src="data/user.xml"/>  //still a hard coded resource
  <map:transform src="stylesheets/user.xsl"/>
  <map:serialize type="fo2pdf"/>
 </map:match>


Cheers,
Pete

-----Ursprüngliche Nachricht-----
Von: Bertrand Delacretaz [mailto:bdelacretaz@apache.org] 
Gesendet: Dienstag, 14. Februar 2006 14:16
An: dev@cocoon.apache.org
Betreff: Re: AW: How to use cocoon servlet only for rendering documents?

> ...Your are refering to the jakarta commons httpclient library?...

yes.

-Bertrand


Re: AW: How to use cocoon servlet only for rendering documents?

Posted by Bertrand Delacretaz <bd...@apache.org>.
> ...Your are refering to the jakarta commons httpclient library?...

yes.

-Bertrand

AW: How to use cocoon servlet only for rendering documents?

Posted by Peter Neu <pe...@gmx.net>.
Yes, that sounds good. 
Your are refering to the jakarta commons httpclient library?

I suppose a one way communication will suffice for the first iteration. I'm
thinking of just sendig the usual http attachment header back from cocoon
instead of from my client code. So the user will only see the url in the
attachment window.

Cheers,
Pete



-----Ursprüngliche Nachricht-----
Von: Bertrand Delacretaz [mailto:bdelacretaz@apache.org] 
Gesendet: Dienstag, 14. Februar 2006 10:58
An: dev@cocoon.apache.org
Betreff: Re: How to use cocoon servlet only for rendering documents?

Le 14 févr. 06, à 10:40, Peter Neu a écrit :

> ...I have the use case that I want to combine Cocoon with another 
> framework say
> Struts (there is lots of legacy code). I want to do all the web front 
> end
> work with Struts and do the rendering of pdf, xml, excel etc with 
> Cocoon...

A nicely decoupled way of doing this is by using an HTTP client in your 
Struts (or whatever) code: run Cocoon in a separate servlet, even on a 
different host if it's useful, and have your other module send XML data 
to Cocoon via HTTP POST, receive the results and proxy them to the web 
client.

You could use the httpclient java library to do this, it's not that 
much code to write, and if you do it right you can keep it all 
streaming without having to buffer data in memory, so it would be 
efficient (not as much as a more tightly coupled solution, but if 
you're talking about generating PDF it's not the communications costs 
that will be a problem).

Such a client could be a welcome addition to Cocoon, if you want to go 
this route I'll try to find some time to help and possibly integrate 
the client in a future relelase.

-Bertrand



Re: How to use cocoon servlet only for rendering documents?

Posted by Bertrand Delacretaz <bd...@apache.org>.
Le 14 févr. 06, à 10:40, Peter Neu a écrit :

> ...I have the use case that I want to combine Cocoon with another 
> framework say
> Struts (there is lots of legacy code). I want to do all the web front 
> end
> work with Struts and do the rendering of pdf, xml, excel etc with 
> Cocoon...

A nicely decoupled way of doing this is by using an HTTP client in your 
Struts (or whatever) code: run Cocoon in a separate servlet, even on a 
different host if it's useful, and have your other module send XML data 
to Cocoon via HTTP POST, receive the results and proxy them to the web 
client.

You could use the httpclient java library to do this, it's not that 
much code to write, and if you do it right you can keep it all 
streaming without having to buffer data in memory, so it would be 
efficient (not as much as a more tightly coupled solution, but if 
you're talking about generating PDF it's not the communications costs 
that will be a problem).

Such a client could be a welcome addition to Cocoon, if you want to go 
this route I'll try to find some time to help and possibly integrate 
the client in a future relelase.

-Bertrand