You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by alapaka <al...@rocketmail.com> on 2013/09/30 07:11:57 UTC

Jetty consumer - how to send reply?

Hi all;

I am playing around with the jetty component. Simple use case - I want to
receive a POST request, log the request body, read in a file and return the
file contents as the POST reply.
Here is the camel route:

				from("jetty:http://localhost:8123/services?matchOnUriPrefix=true")
				.log("Received Request\n----------------\n ${body}
\n---------------------\n")
				.from("file:C:\\dev?fileName=TestResponse.xml&noop=true")
				.log("Sending Response\n----------------\n ${body}
\n---------------------\n")
				.end();

All seems to be working - I get the request, see it in the log, read in the
file, see the file contents in the log; however the HTTP client gets an HTTP
200 response code with no body (content-length=0);

Obviously the response body is not being set...
Do I need to use a processor to get the Exchange through which I set the
response body?
 i.e.
public void process(Exchange exch) throws Exception
{exch.getOut().setBody("some response");}

Is there any other way to set the HTTP response body for a jetty consumer?

aloha
ala



--
View this message in context: http://camel.465427.n5.nabble.com/Jetty-consumer-how-to-send-reply-tp5740450.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Jetty consumer - how to send reply?

Posted by Dale King <da...@jadabeauty.com>.
On Oct 1, 2013, at 2:14 AM, Claus Ibsen <cl...@gmail.com> wrote:

> You can do
> 
> setBody(constant(new File("xxxx")))

That works if the file name is fixed at the time of route construction.

The method I showed using simple and the built in conversion from string to File will allow you to determine the file name dynamically when the route is running, which is what I needed in my case.


Re: Jetty consumer - how to send reply?

Posted by alapaka <al...@rocketmail.com>.
thanks again guys!

sorry for the newbie questions - still getting my head around the
architecture and the major moving parts.

aloha
ala



--
View this message in context: http://camel.465427.n5.nabble.com/Jetty-consumer-how-to-send-reply-tp5740450p5740633.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Jetty consumer - how to send reply?

Posted by Claus Ibsen <cl...@gmail.com>.
You can do

setBody(constant(new File("xxxx")))


On Tue, Oct 1, 2013 at 1:24 AM, alapaka <al...@rocketmail.com> wrote:
> Seems like the only way (without using a processor/filter/endpoint ) is to
> use enrich.
> I tried to use a file directly in setBody:
>
> from("jetty:http://localhost:8123/services?matchOnUriPrefix=true")
> .log("Received Request\n----------------\n ${body}
> \n---------------------\n")
> .setBody(body().append(new File("C:\\devTestResponse.xml")))
> .log("Sending Response\n----------------\n ${body}
> \n---------------------\n")
> .end();
>
> this does not work. I infer from the documentation that setBody() is meant
> to work with expressions i.e. scripts/xpath etc.. that work on the current
> content of the Message.
>
> This exercise is more academic than a real requirement - just seeing what I
> can and cannot do within these routes. I can certainly live with pollEnrich
> as is.
> Thanks for all the help guys!
>
> aloha
> ala
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Jetty-consumer-how-to-send-reply-tp5740450p5740616.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



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

Re: Jetty consumer - how to send reply?

Posted by Dale King <da...@jadabeauty.com>.
Appending a file like that probably won't work. Here is what I am doing in a route where I need to read a file:

   .setBody().simple("path/To/File", File.class)

which also lets you use ${...} expressions to form the file path

If absolutely necessary you could then do a convertBodyTo but Camel should handle conversion to an InputStream automatically.

Note that reading the body in a log message like that will mess up the process for later steps because you will exhaust the InputStream and then there will be nothing to read later so in real code if you want to log it you might have to convert it to a string or byte array first. 

On Sep 30, 2013, at 7:24 PM, alapaka <al...@rocketmail.com> wrote:

> Seems like the only way (without using a processor/filter/endpoint ) is to
> use enrich. 
> I tried to use a file directly in setBody: 
> 
> from("jetty:http://localhost:8123/services?matchOnUriPrefix=true")
> .log("Received Request\n----------------\n ${body}
> \n---------------------\n")
> .setBody(body().append(new File("C:\\devTestResponse.xml")))
> .log("Sending Response\n----------------\n ${body}
> \n---------------------\n")
> .end();
> 
> this does not work. I infer from the documentation that setBody() is meant
> to work with expressions i.e. scripts/xpath etc.. that work on the current
> content of the Message.
> 
> This exercise is more academic than a real requirement - just seeing what I
> can and cannot do within these routes. I can certainly live with pollEnrich
> as is.
> Thanks for all the help guys!
> 
> aloha
> ala
> 
> 
> 
> --
> View this message in context: http://camel.465427.n5.nabble.com/Jetty-consumer-how-to-send-reply-tp5740450p5740616.html
> Sent from the Camel - Users mailing list archive at Nabble.com.


Re: Jetty consumer - how to send reply?

Posted by alapaka <al...@rocketmail.com>.
Seems like the only way (without using a processor/filter/endpoint ) is to
use enrich. 
I tried to use a file directly in setBody: 

from("jetty:http://localhost:8123/services?matchOnUriPrefix=true")
.log("Received Request\n----------------\n ${body}
\n---------------------\n")
.setBody(body().append(new File("C:\\devTestResponse.xml")))
.log("Sending Response\n----------------\n ${body}
\n---------------------\n")
.end();

this does not work. I infer from the documentation that setBody() is meant
to work with expressions i.e. scripts/xpath etc.. that work on the current
content of the Message.

This exercise is more academic than a real requirement - just seeing what I
can and cannot do within these routes. I can certainly live with pollEnrich
as is.
Thanks for all the help guys!

aloha
ala



--
View this message in context: http://camel.465427.n5.nabble.com/Jetty-consumer-how-to-send-reply-tp5740450p5740616.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Jetty consumer - how to send reply?

Posted by Dale King <da...@jadabeauty.com>.
Actually if you are trying to return one specific file there is probably an easier way. If you set the body of a message to a java.io.File object pointing to the file you want and the built-in type converters can convert that to an InputStream.

Setting up a file endpoint is way overkill. A file endpoint polls a directory looking for a file. If you know exactly what file you want to send then what I described is easier and faster.


On Sep 30, 2013, at 3:18 AM, alapaka <al...@rocketmail.com> wrote:

> Yep - pollEnrich works fine.
> 
> I tried using setBody() directly in the route like so:
> 
> from("jetty:http://localhost:8123/services?matchOnUriPrefix=true")
> .streamCaching()
> . setBody(body().append("TEST RESPONSE"))
> .end();
> 
> this does not seem to work... 
> any reason why?
> 
> thanks for the fast reply Claus
> 
> tusen takk og mvh
> ala
> 
> 
> 
> --
> View this message in context: http://camel.465427.n5.nabble.com/Jetty-consumer-how-to-send-reply-tp5740450p5740459.html
> Sent from the Camel - Users mailing list archive at Nabble.com.


Re: Jetty consumer - how to send reply?

Posted by alapaka <al...@rocketmail.com>.
Yep - pollEnrich works fine.

I tried using setBody() directly in the route like so:

 from("jetty:http://localhost:8123/services?matchOnUriPrefix=true")
.streamCaching()
. setBody(body().append("TEST RESPONSE"))
.end();

this does not seem to work... 
any reason why?

thanks for the fast reply Claus

tusen takk og mvh
ala



--
View this message in context: http://camel.465427.n5.nabble.com/Jetty-consumer-how-to-send-reply-tp5740450p5740459.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Jetty consumer - how to send reply?

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

Take a look at the content enricher eip, and use <pollEnrich> to grab
the file you want to enrich the message with.

http://camel.apache.org/content-enricher.html

On Mon, Sep 30, 2013 at 7:11 AM, alapaka <al...@rocketmail.com> wrote:
> Hi all;
>
> I am playing around with the jetty component. Simple use case - I want to
> receive a POST request, log the request body, read in a file and return the
> file contents as the POST reply.
> Here is the camel route:
>
>                                 from("jetty:http://localhost:8123/services?matchOnUriPrefix=true")
>                                 .log("Received Request\n----------------\n ${body}
> \n---------------------\n")
>                                 .from("file:C:\\dev?fileName=TestResponse.xml&noop=true")
>                                 .log("Sending Response\n----------------\n ${body}
> \n---------------------\n")
>                                 .end();
>
> All seems to be working - I get the request, see it in the log, read in the
> file, see the file contents in the log; however the HTTP client gets an HTTP
> 200 response code with no body (content-length=0);
>
> Obviously the response body is not being set...
> Do I need to use a processor to get the Exchange through which I set the
> response body?
>  i.e.
> public void process(Exchange exch) throws Exception
> {exch.getOut().setBody("some response");}
>
> Is there any other way to set the HTTP response body for a jetty consumer?
>
> aloha
> ala
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Jetty-consumer-how-to-send-reply-tp5740450.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



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