You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Zemin Hu <ze...@hotmail.com> on 2013/10/08 00:29:41 UTC

return binary image data in camel/restlet environment

I need to return image from route in restlet environment, if in normal camel
environment I should be able to do
   byte[] imageData = getImage();
   HttpServletResponse response =
exchange.getIn().getBody(HttpServletResponse.class);
   response.setContentType("image/png");
   OutputStream os = response.getOutputStream();
   os.write(imageData);
   os.close();
Now, in restlet environment, the response is null. I am doing:
   exchange.getOut().setBody(imageData);
While it executed correctly, the data is encoded and inflated(about 30-40%
more), the header is set to:
   Content-Type: text/plain;charset=UTF-8
if I don't set "Content-Type", but even I set it to original type:
"image/png", the header will be
   Content-Type: image/pgn;charset=UTF-8
The returning binary data is still inflated and encoded. 
Is there any way to prevent this to happen? I guess my another question
would be:
How do I get HttpServletResponse or OutputStream for response in restlet
environment so I can simply set up and output my binary data as in normal
servlet environment? Where is it hidden(I tried to find it under debugging
environment but could not find it)?
Thanks.



--
View this message in context: http://camel.465427.n5.nabble.com/return-binary-image-data-in-camel-restlet-environment-tp5741062.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: return binary image data in camel/restlet environment

Posted by Zemin Hu <ze...@hotmail.com>.
Thanks for quick fix, Willem. I will check it out. Actually your suggestion
to set body to inputstream works very well, here is part of the code in case
anybody wants to do the same:
	        exchange.getOut().setHeader( Exchange.HTTP_RESPONSE_CODE, 200 );
	        exchange.getOut().setHeader( Exchange.CONTENT_TYPE, mediaType );
	        
        	ByteArrayInputStream bais = new ByteArrayInputStream(imageData);
	        exchange.getOut().setBody( bais );

Finally, I tried to use ByteArrayOutputStream at first place, it did not
work, I am still wondering why it's inputsteam not outputstream since I am
send data out from Camel point of view.



--
View this message in context: http://camel.465427.n5.nabble.com/return-binary-image-data-in-camel-restlet-environment-tp5741062p5741238.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: return binary image data in camel/restlet environment

Posted by Willem jiang <wi...@gmail.com>.
You can check the commit log from the JIRA[1] and apply the path yourself if you don't want to wait for the another round of Camel release.

[1]https://issues.apache.org/jira/browse/CAMEL-6834  

--  
Willem Jiang

Red Hat, Inc.
Web: http://www.redhat.com
Blog: http://willemjiang.blogspot.com (http://willemjiang.blogspot.com/) (English)
          http://jnn.iteye.com (http://jnn.javaeye.com/) (Chinese)
Twitter: willemjiang  
Weibo: 姜宁willem





On Tuesday, October 8, 2013 at 10:35 PM, Zemin Hu wrote:

> Thanks Willem. I will try the work around. How do I know the fix is ready? Do
> I need to upgrade to latest Camel version in order to get fix?
>  
>  
>  
> --
> View this message in context: http://camel.465427.n5.nabble.com/return-binary-image-data-in-camel-restlet-environment-tp5741062p5741140.html
> Sent from the Camel - Users mailing list archive at Nabble.com (http://Nabble.com).




Re: return binary image data in camel/restlet environment

Posted by Christian Müller <ch...@gmail.com>.
Willem committed it. You have to use Camel 2.13-SNAPSHOT.

Best,
Christian
Am 08.10.2013 16:36 schrieb "Zemin Hu" <ze...@hotmail.com>:

> Thanks Willem. I will try the work around. How do I know the fix is ready?
> Do
> I need to upgrade to latest Camel version in order to get fix?
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/return-binary-image-data-in-camel-restlet-environment-tp5741062p5741140.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>

Re: return binary image data in camel/restlet environment

Posted by Zemin Hu <ze...@hotmail.com>.
Thanks Willem. I will try the work around. How do I know the fix is ready? Do
I need to upgrade to latest Camel version in order to get fix?



--
View this message in context: http://camel.465427.n5.nabble.com/return-binary-image-data-in-camel-restlet-environment-tp5741062p5741140.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: return binary image data in camel/restlet environment

Posted by Willem jiang <wi...@gmail.com>.
Hi,

Restlet uses the Representation to define the message and mediate type[1].
Camel-Restlet doesn't support the Representation well, and it just try to turn the bytes into String as a fall back.
I just fill a JIRA[2] for it and will commit the fix shortly.

You can work around it by changing the message body into an InputStream and set Content-Type on the message header.  

[1]http://stackoverflow.com/questions/1509873/serve-dynamic-generated-images-using-restlet  
[2]https://issues.apache.org/jira/browse/CAMEL-6834
--  
Willem Jiang

Red Hat, Inc.
Web: http://www.redhat.com
Blog: http://willemjiang.blogspot.com (http://willemjiang.blogspot.com/) (English)
          http://jnn.iteye.com (http://jnn.javaeye.com/) (Chinese)
Twitter: willemjiang  
Weibo: 姜宁willem





On Tuesday, October 8, 2013 at 6:29 AM, Zemin Hu wrote:

> I need to return image from route in restlet environment, if in normal camel
> environment I should be able to do
> byte[] imageData = getImage();
> HttpServletResponse response =
> exchange.getIn().getBody(HttpServletResponse.class);
> response.setContentType("image/png");
> OutputStream os = response.getOutputStream();
> os.write(imageData);
> os.close();
> Now, in restlet environment, the response is null. I am doing:
> exchange.getOut().setBody(imageData);
> While it executed correctly, the data is encoded and inflated(about 30-40%
> more), the header is set to:
> Content-Type: text/plain;charset=UTF-8
> if I don't set "Content-Type", but even I set it to original type:
> "image/png", the header will be
> Content-Type: image/pgn;charset=UTF-8
> The returning binary data is still inflated and encoded.  
> Is there any way to prevent this to happen? I guess my another question
> would be:
> How do I get HttpServletResponse or OutputStream for response in restlet
> environment so I can simply set up and output my binary data as in normal
> servlet environment? Where is it hidden(I tried to find it under debugging
> environment but could not find it)?
> Thanks.
>  
>  
>  
> --
> View this message in context: http://camel.465427.n5.nabble.com/return-binary-image-data-in-camel-restlet-environment-tp5741062.html
> Sent from the Camel - Users mailing list archive at Nabble.com (http://Nabble.com).