Made some progress on this, but am left with some questions and would love it
if someone could fill me/us in.

First, here is the code that does essentially what we were trying to do:
from("jetty:http://localhost:8888/test";).to("seda:foo",
"file://response.txt");
from("seda:foo").process(new Processor(){
    public void process(Exchange exchange) throws Exception {
        DefaultExchange ex = (DefaultExchange) exchange;
        ex.setPattern(ExchangePattern.InOnly); // ouch
        exchange.getIn().setBody("Headers: " +
exchange.getIn().getHeaders()); // just some test content       
    }
}).to("file://requests/");


What I realized:
1. The message exchange created by the Jetty component has a pattern of
"InOut".  This means that "out"  part of the message being handled by the
pipeline will be returned to the client by Jetty.  In my code above, the
content for the "out" message is read from response.txt via
"file://response.txt".

2. As the message is sent into the file component, a new exchange is
created, it also is set to InOut. When the file component sees this "InOut",
it automatically expects to READ from a file, not write.   So how the file
component behaves (read vs. write) is determined by the type of exchange
coming in.    

I was able to hack around this by sending the message to another queue,
converting it to a InOnly (via casting, ouch), then sending it to a
different file endpoint.

3. The "in" message coming into a File endpoint, must have content in its
body in order for a file to be written (makes sense).  That is why I write
the headers into the "in" body, just for some test content.

The end result is that each incoming http request is logged to a file, and
the contents of response.txt are sent back to the user.  This is essentially
what I was trying to do in the first place.  Hope this helps Patrick.

Questions:
- Is there a better/simpler way to accomplish this?  It seems the File
Component's read/write behavior is determined by exchange pattern being
used.  Is there a standard way to convert these exchange patterns (without
casting), or should the File Component be a little more configurable in
terms of its read / write behavior?

Thanks!  Rich



RichTaylor wrote:
> 
> I'm trying to do this exact same thing as an exercise to learn Camel.  
> Hopefully someone can chime in to help us out.  Here is what I've
> found....
> 
> I'm noticing that the Jetty endpoint expects an output message so the
> message exchange used is "InOut".  This "InOut" exchange seems to make the
> file component try to READ the specified file instead of WRITING to it.  I
> found that if I created a text file (where I was expecting things to be
> written to), it would be returned to the browser via the Jetty endpoint.  
> 
> So now I'm left wondering how one would pull messages from a Jetty
> endpoint, write certain aspects of the message to a file (or another
> endpoint) AND specify the output message for the Jetty endpoint to send
> back to the client?  I'm wondering if this can't be done with the DSL and
> instead needs a custom processor?      Cheers, Rich
> 
> 
> 
> Patrick Shea wrote:
>> 
>> I'm trying to get a message from http (jetty) and save it to a file but
>> it never works, the file is never created.
>> 
>> Here's my routing code:
>> 
>> public void configure()
>> {
>>     Processor proc = new Processor() {
>>         public void process(Exchange e) throws Exception
>>      {
>>       getLog().info("Headers: " + e.getIn().getHeaders());
>>          System.out.println("Received exchange: " +
>> e.getIn().getBody(String.class));
>>      }
>>     };
>> 
>>    
>> from("jetty:http://localhost:8080/hello";).process(proc).setHeader(FileComponent.HEADER_FILE_NAME,
>>         constant("message.txt")).to("file:target/hello?noop=true");
>> 
>> }
>> 
>> I'm expecting target/hello/message.txt but only target/hello is created.
>> Also the file endpoint seems to be polling that folder like it is a
>> consumer!
>> 
>> Patrick
>> 
>> 
>> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/from-jetty-to-file-tp14453030s22882p14544887.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Reply via email to