You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Ah...@Bertelsmann.de on 2007/06/27 13:09:50 UTC

Write binary data (file content) to the output stream

Hi all, 
 
i wanna open a generate a binary file on the fly and write send it directly to the user.
 
I didn't found an easy way in cocoon to achieve this.
 
Therefore i have tried using an action, which gets the output stream and writes the byte array into the stream.
...
response.setContentType("application/x-octet-stream");

response.setHeader("Content-Disposition", "attachment; filename=" + filename);

OutputStream out = response.getOutputStream();

out.write(data);

out.flush();

...

 
 
 
the sitemap.xmap looks like this:
 
<map:match pattern="**download.do">

<map:act type="binary-download"/>

</map:match> 

 
 
It does function, but when i open in an editor, then i see the file content followed by an cocoon error message saying that a ResourceNotFoundException has been occured.
 
1. Any idea why i get the message?
2. Is there a better way sending binary data without writing it to the filesystem?
 
Thanks in advance.
 

Re: Write binary data (file content) to the output stream

Posted by Tobia <to...@linux.it>.
Ahmet.Tokalak@Bertelsmann.de wrote:
> generate a binary file on the fly and send it directly to the user

What you need is a reader, not an action.

If the data is already in a file, you can use the file reader:

	<!-- /downloads/myfile.zip -->
	<match pattern="downloads/*">
	  <read src="downloads/{1}"/>
	</match>

If the data is in a database, you can use the database reader:

	<reader name="db" src="org.apache.cocoon.reading.DatabaseReader">
	  <use-connection>my_database</use-connection>
	</reader>

	<!-- /binary?id=1234 -->
	<match pattern="binary">
	  <read type="db" src="{request-param:id}">
	    <parameter name="table" value="binary_objects"/>
	    <parameter name="key" value="binobj_id"/>
	    <parameter name="image" value="binobj_blob"/>
	  </read>
	</match>

If your use case is more complex, then you need to use Javascript (by
writing a piece of Flowscript) or Java (by writing a custom reader or
some Javaflow, not an action.)


Tobia

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


AW: Write binary data (file content) to the output stream

Posted by Ah...@Bertelsmann.de.
Hi Jeroen,
 
in some cases i read the file from the database depending on the parameters passed by the user. Sometimes i create the file on the fly. 
I thought that this is a very common requirement that it should be solved easily without writing a new reader or anything else.
 
Greetings,
Ahmet 

________________________________

Von: Jeroen Reijn [mailto:j.reijn@hippo.nl]
Gesendet: Mi 27.06.2007 13:42
An: users@cocoon.apache.org; users@cocoon.apache.org
Betreff: RE: Write binary data (file content) to the output stream



Hi,

do you already have your binary file or are you creating the file? If you have the file already, you could use a cocoon reader and set some extra http headers.

Otherwise I would go for creating your own reader.

There is already a set header action available in case you wish to set the Content-Disposition. See the example below

<map:act type="set-header">
  <map:parameter name="Content-Disposition" value="attachment;"/>
</map:act>

Regards,

Jeroen Reijn


-----Original Message-----
From:   Christian Schlichtherle [mailto:christian@schlichtherle.de]
Sent:   Wed 27-6-2007 13:26
To:     users@cocoon.apache.org
Cc:    
Subject:        RE: Write binary data (file content) to the output stream

Hi,

I wonder if you really need a pipeline for your example, a simple Servlet
may actually be the best option.

If your binary file would be the result of a transformation however, I would
recommend to write a Serializer component.

Kind regards,
Christian


  _____ 

From: Ahmet.Tokalak@Bertelsmann.de [mailto:Ahmet.Tokalak@Bertelsmann.de]
Sent: Wednesday, June 27, 2007 1:10 PM
To: users@cocoon.apache.org
Subject: Write binary data (file content) to the output stream


Hi all,

i wanna open a generate a binary file on the fly and write send it directly
to the user.

I didn't found an easy way in cocoon to achieve this.

Therefore i have tried using an action, which gets the output stream and
writes the byte array into the stream.
...
response.setContentType("application/x-octet-stream");

response.setHeader("Content-Disposition", "attachment; filename=" +
filename);

OutputStream out = response.getOutputStream();

out.write(data);

out.flush();

...




the sitemap.xmap looks like this:

<map:match pattern="**download.do">

<map:act type="binary-download"/>

</map:match>



It does function, but when i open in an editor, then i see the file content
followed by an cocoon error message saying that a ResourceNotFoundException
has been occured.

1. Any idea why i get the message?
2. Is there a better way sending binary data without writing it to the
filesystem?

Thanks in advance.






---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org




RE: Write binary data (file content) to the output stream

Posted by Jeroen Reijn <j....@hippo.nl>.
Hi,

do you already have your binary file or are you creating the file? If you have the file already, you could use a cocoon reader and set some extra http headers.

Otherwise I would go for creating your own reader.

There is already a set header action available in case you wish to set the Content-Disposition. See the example below

<map:act type="set-header">
  <map:parameter name="Content-Disposition" value="attachment;"/>
</map:act>

Regards,

Jeroen Reijn


-----Original Message-----
From:	Christian Schlichtherle [mailto:christian@schlichtherle.de]
Sent:	Wed 27-6-2007 13:26
To:	users@cocoon.apache.org
Cc:	
Subject:	RE: Write binary data (file content) to the output stream

Hi,
 
I wonder if you really need a pipeline for your example, a simple Servlet
may actually be the best option.
 
If your binary file would be the result of a transformation however, I would
recommend to write a Serializer component.
 
Kind regards,
Christian


  _____  

From: Ahmet.Tokalak@Bertelsmann.de [mailto:Ahmet.Tokalak@Bertelsmann.de] 
Sent: Wednesday, June 27, 2007 1:10 PM
To: users@cocoon.apache.org
Subject: Write binary data (file content) to the output stream


Hi all, 
 
i wanna open a generate a binary file on the fly and write send it directly
to the user.
 
I didn't found an easy way in cocoon to achieve this.
 
Therefore i have tried using an action, which gets the output stream and
writes the byte array into the stream.
...
response.setContentType("application/x-octet-stream");

response.setHeader("Content-Disposition", "attachment; filename=" +
filename);

OutputStream out = response.getOutputStream();

out.write(data);

out.flush();

...

 
 
 
the sitemap.xmap looks like this:
 
<map:match pattern="**download.do">

<map:act type="binary-download"/>

</map:match> 

 
 
It does function, but when i open in an editor, then i see the file content
followed by an cocoon error message saying that a ResourceNotFoundException
has been occured.
 
1. Any idea why i get the message?
2. Is there a better way sending binary data without writing it to the
filesystem?
 
Thanks in advance.
 





---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


RE: Write binary data (file content) to the output stream

Posted by Christian Schlichtherle <ch...@schlichtherle.de>.
Hi,
 
I wonder if you really need a pipeline for your example, a simple Servlet
may actually be the best option.
 
If your binary file would be the result of a transformation however, I would
recommend to write a Serializer component.
 
Kind regards,
Christian


  _____  

From: Ahmet.Tokalak@Bertelsmann.de [mailto:Ahmet.Tokalak@Bertelsmann.de] 
Sent: Wednesday, June 27, 2007 1:10 PM
To: users@cocoon.apache.org
Subject: Write binary data (file content) to the output stream


Hi all, 
 
i wanna open a generate a binary file on the fly and write send it directly
to the user.
 
I didn't found an easy way in cocoon to achieve this.
 
Therefore i have tried using an action, which gets the output stream and
writes the byte array into the stream.
...
response.setContentType("application/x-octet-stream");

response.setHeader("Content-Disposition", "attachment; filename=" +
filename);

OutputStream out = response.getOutputStream();

out.write(data);

out.flush();

...

 
 
 
the sitemap.xmap looks like this:
 
<map:match pattern="**download.do">

<map:act type="binary-download"/>

</map:match> 

 
 
It does function, but when i open in an editor, then i see the file content
followed by an cocoon error message saying that a ResourceNotFoundException
has been occured.
 
1. Any idea why i get the message?
2. Is there a better way sending binary data without writing it to the
filesystem?
 
Thanks in advance.