You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Cyril Lakech <cy...@adeoservices.com> on 2012/10/26 18:21:04 UTC

HTTP4 component body CachedOutputStream to String

Hi,

I am trying to call a remote http service to retrieve a message using http4
component.

I would like to understand the sentence from the http4 component web page
http://camel.apache.org/http4.html

"Camel will store the HTTP response from the external server on the OUT
body. All headers from the IN message will be copied to the OUT message, so
headers are preserved during routing. Additionally Camel will add the HTTP
response headers as well to the OUT message headers."

But exchange.getOut() is null after a http4 call and the result seems to be
present in the exchange.getInt() body...

to("http4://myURI").//
                process(new Processor() {
                    @Override
                    public void process(Exchange exchange) throws Exception
{
                        log.info(exchange.getOut().getBody().toString());
==> NPE
                    }
                })

And last, how do I can read the body and convert it into a String ? The
body seems to be an instance of CachedOutputStream. How should I read this ?

I looked to convertBodyTo camel method but can't find any elegant solution
to transform my CachedOutputStream into a String.

Regards,

Cyril Lakech

-- 
Ce message et toutes les pièces jointes sont établis à l'attention 
exclusive de leurs destinataires et sont confidentiels. Si vous recevez ce 
message par erreur, merci de le détruire et d'en avertir immédiatement 
l'expéditeur. L'internet ne permettant pas d'assurer l'intégrité de ce 
message, le contenu de ce message ne représente en aucun cas un engagement 
de la part de GROUPE ADEO.

Re: HTTP4 component body CachedOutputStream to String

Posted by clakech <cy...@adeoservices.com>.
Thanks for those explanation.

Everything is clear and works now.

Regards,

Cyril Lakech


2012/10/28 Christian Mueller [via Camel] <
ml-node+s465427n5721655h80@n5.nabble.com>

> Or convert the body into a String before you log and further process it.
> And the response of the http4 call is in the in message in your processor,
> because the Camel pipeline already copied the out message into the in
> message for the next processor which is your processor which logs the
> message.
>
> Best,
> Christian
>
> On Fri, Oct 26, 2012 at 8:26 PM, Taariq Levack <[hidden email]<http://user/SendEmail.jtp?type=node&node=5721655&i=0>>
> wrote:
>
> > Hi
> > You should enable stream caching[1] if you want to read a stream more
> than
> > once.
> > [1] http://camel.apache.org/stream-caching.html
> >
> > Taariq
> >
> > On 26 Oct 2012, at 18:34, clakech <[hidden email]<http://user/SendEmail.jtp?type=node&node=5721655&i=1>>
> wrote:
> >
> > > I find an answer to my question:
> > >
> > > to("http4://sample").convertBodyTo(String.class) WORKS FINE
> > >
> > > to("http4://sample").log("${body}").convertBodyTo(String.class) DO NOT
> > WORK
> > >
> > > Why?
> > >
> > > Because the logging EIP seems to read the inputstream which moves the
> > > position to the end of the buffer.
> > >
> > > But I am still wondering why the http4 documentation says :
> > > "Camel will store the HTTP response from the external server on the
> OUT
> > > body. All headers from the IN message will be copied to the OUT
> message,
> > so
> > > headers are preserved during routing. Additionally Camel will add the
> > HTTP
> > > response headers as well to the OUT message headers."
> > >
> > > But the response is in the in body.
> > >
> > >
> > >
> > > --
> > > View this message in context:
> >
> http://camel.465427.n5.nabble.com/HTTP4-component-body-CachedOutputStream-to-String-tp5721616p5721617.html
> > > Sent from the Camel - Users mailing list archive at Nabble.com.
> >
>
>
>
> --
>
>
> ------------------------------
>  If you reply to this email, your message will be added to the discussion
> below:
>
> http://camel.465427.n5.nabble.com/HTTP4-component-body-CachedOutputStream-to-String-tp5721616p5721655.html
>  To unsubscribe from HTTP4 component body CachedOutputStream to String, click
> here<http://camel.465427.n5.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=5721616&code=Y3lyaWwubGFrZWNoQGFkZW9zZXJ2aWNlcy5jb218NTcyMTYxNnwxMjE3MDgxODg5>
> .
> NAML<http://camel.465427.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble:email.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble:email.naml-instant_emails%21nabble:email.naml-send_instant_email%21nabble:email.naml>
>

-- 
Ce message et toutes les pièces jointes sont établis à l'attention 
exclusive de leurs destinataires et sont confidentiels. Si vous recevez ce 
message par erreur, merci de le détruire et d'en avertir immédiatement 
l'expéditeur. L'internet ne permettant pas d'assurer l'intégrité de ce 
message, le contenu de ce message ne représente en aucun cas un engagement 
de la part de GROUPE ADEO.




--
View this message in context: http://camel.465427.n5.nabble.com/HTTP4-component-body-CachedOutputStream-to-String-tp5721616p5721676.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: HTTP4 component body CachedOutputStream to String

Posted by Christian Müller <ch...@gmail.com>.
Or convert the body into a String before you log and further process it.
And the response of the http4 call is in the in message in your processor,
because the Camel pipeline already copied the out message into the in
message for the next processor which is your processor which logs the
message.

Best,
Christian

On Fri, Oct 26, 2012 at 8:26 PM, Taariq Levack <ta...@gmail.com> wrote:

> Hi
> You should enable stream caching[1] if you want to read a stream more than
> once.
> [1] http://camel.apache.org/stream-caching.html
>
> Taariq
>
> On 26 Oct 2012, at 18:34, clakech <cy...@adeoservices.com> wrote:
>
> > I find an answer to my question:
> >
> > to("http4://sample").convertBodyTo(String.class) WORKS FINE
> >
> > to("http4://sample").log("${body}").convertBodyTo(String.class) DO NOT
> WORK
> >
> > Why?
> >
> > Because the logging EIP seems to read the inputstream which moves the
> > position to the end of the buffer.
> >
> > But I am still wondering why the http4 documentation says :
> > "Camel will store the HTTP response from the external server on the OUT
> > body. All headers from the IN message will be copied to the OUT message,
> so
> > headers are preserved during routing. Additionally Camel will add the
> HTTP
> > response headers as well to the OUT message headers."
> >
> > But the response is in the in body.
> >
> >
> >
> > --
> > View this message in context:
> http://camel.465427.n5.nabble.com/HTTP4-component-body-CachedOutputStream-to-String-tp5721616p5721617.html
> > Sent from the Camel - Users mailing list archive at Nabble.com.
>



--

Re: HTTP4 component body CachedOutputStream to String

Posted by Taariq Levack <ta...@gmail.com>.
Hi
You should enable stream caching[1] if you want to read a stream more than once.
[1] http://camel.apache.org/stream-caching.html

Taariq

On 26 Oct 2012, at 18:34, clakech <cy...@adeoservices.com> wrote:

> I find an answer to my question:
> 
> to("http4://sample").convertBodyTo(String.class) WORKS FINE
> 
> to("http4://sample").log("${body}").convertBodyTo(String.class) DO NOT WORK
> 
> Why?
> 
> Because the logging EIP seems to read the inputstream which moves the
> position to the end of the buffer.
> 
> But I am still wondering why the http4 documentation says :
> "Camel will store the HTTP response from the external server on the OUT
> body. All headers from the IN message will be copied to the OUT message, so
> headers are preserved during routing. Additionally Camel will add the HTTP
> response headers as well to the OUT message headers." 
> 
> But the response is in the in body.
> 
> 
> 
> --
> View this message in context: http://camel.465427.n5.nabble.com/HTTP4-component-body-CachedOutputStream-to-String-tp5721616p5721617.html
> Sent from the Camel - Users mailing list archive at Nabble.com.

Re: HTTP4 component body CachedOutputStream to String

Posted by clakech <cy...@adeoservices.com>.
I find an answer to my question:

to("http4://sample").convertBodyTo(String.class) WORKS FINE

to("http4://sample").log("${body}").convertBodyTo(String.class) DO NOT WORK

Why?

Because the logging EIP seems to read the inputstream which moves the
position to the end of the buffer.

But I am still wondering why the http4 documentation says :
"Camel will store the HTTP response from the external server on the OUT
body. All headers from the IN message will be copied to the OUT message, so
headers are preserved during routing. Additionally Camel will add the HTTP
response headers as well to the OUT message headers." 

But the response is in the in body.



--
View this message in context: http://camel.465427.n5.nabble.com/HTTP4-component-body-CachedOutputStream-to-String-tp5721616p5721617.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: HTTP4 component body CachedOutputStream to String

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

See these links also
http://camel.apache.org/using-getin-or-getout-methods-on-exchange.html
http://camel.apache.org/pipes-and-filters.html
http://camel.apache.org/how-do-i-enable-streams-when-debug-logging-messages-in-camel.html
http://camel.apache.org/stream-caching.html


On Fri, Oct 26, 2012 at 6:21 PM, Cyril Lakech
<cy...@adeoservices.com> wrote:
> Hi,
>
> I am trying to call a remote http service to retrieve a message using http4
> component.
>
> I would like to understand the sentence from the http4 component web page
> http://camel.apache.org/http4.html
>
> "Camel will store the HTTP response from the external server on the OUT
> body. All headers from the IN message will be copied to the OUT message, so
> headers are preserved during routing. Additionally Camel will add the HTTP
> response headers as well to the OUT message headers."
>
> But exchange.getOut() is null after a http4 call and the result seems to be
> present in the exchange.getInt() body...
>
> to("http4://myURI").//
>                 process(new Processor() {
>                     @Override
>                     public void process(Exchange exchange) throws Exception
> {
>                         log.info(exchange.getOut().getBody().toString());
> ==> NPE
>                     }
>                 })
>
> And last, how do I can read the body and convert it into a String ? The
> body seems to be an instance of CachedOutputStream. How should I read this ?
>
> I looked to convertBodyTo camel method but can't find any elegant solution
> to transform my CachedOutputStream into a String.
>
> Regards,
>
> Cyril Lakech
>
> --
> Ce message et toutes les pièces jointes sont établis à l'attention
> exclusive de leurs destinataires et sont confidentiels. Si vous recevez ce
> message par erreur, merci de le détruire et d'en avertir immédiatement
> l'expéditeur. L'internet ne permettant pas d'assurer l'intégrité de ce
> message, le contenu de ce message ne représente en aucun cas un engagement
> de la part de GROUPE ADEO.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: cibsen@redhat.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen