title: Apache Camel: access both request and reply message at end of route
body: <p>I would like to process both request and response messages at the end of my route. However, I do not see a way how to access the original request message.</p>
  
  <p>I have the terrible feeling I am struggling with some basic concept.</p>
  
  <p>Here is a simple example route in DSL to outline my problem (streamCaching is enabled for the whole context):</p>
  
  <pre><code>from("activemq:queue:myQueue")
  .to("log:" + getClass().getName() + "?showOut=true")
  .to("http://localhost:8080/someBackend")
  .log("Now in.Body returns this: ${in.body} and out.Body this: ${out.body}")
  .to("log:" + getClass().getName() + "?showOut=true");
  </code></pre>
  
  <p>Here is an according excerpt from my logs (line-breaks edited for better reading). As one can see, the original SOAP message is lost once the http server replied, and the SOAP response object is stored in the inBody of the message.</p>
  
  <pre><code>2012-09-25 17:28:08,312 local.bar.foo.MyRouteBuilder INFO - 
      Exchange[ExchangePattern:InOut, BodyType:byte[],
      Body:&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"&gt;&lt;env:Header /&gt;&lt;env:Body&gt;&lt;urn:someRequest  xmlns:urn="http://foo.bar.local/ns"&gt;&lt;/urn:someRequest&gt;&lt;/env:Body&gt;&lt;/env:Envelope&gt;, 
      Out: null]
  2012-09-25 17:28:08,398 org.apache.camel.component.http.HttpProducer DEBUG - 
      Executing http POST method: http://localhost:8080/someBackend
  2012-09-25 17:28:09,389 org.apache.camel.component.http.HttpProducer DEBUG - 
      Http responseCode: 200
  2012-09-25 17:28:09,392 route2 INFO - 
      Now in.Body returns this: &lt;soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"&gt;&lt;soap:Body&gt;&lt;ns2:someResponse xmlns:ns2="http://foo.bar.local/ns"&lt;/ns2:someResponse&gt;&lt;/soap:Body&gt;&lt;/soap:Envelope&gt;
      and out.Body this: 
  2012-09-25 17:28:09,392 local.bar.foo.MyRouteBuilder INFO - 
      Exchange[ExchangePattern:InOut,  
      BodyType:org.apache.camel.converter.stream.InputStreamCache,
      Body:[Body is instance of org.apache.camel.StreamCache],
      Out: null]
  </code></pre>
  
  <p>I would have expected to have in.body and out.body be preserved across the whole route?</p>
  
  <p><strong>Alternative solutions I am considering:</strong></p>
  
  <ul>
  <li>Make use of the <a href="http://camel.apache.org/correlation-identifier.html">Correlation Identifier</a> pattern to correlate both request and reply. But would this preserve the message bodies as well? Also, my request/reply messages do not have unique identifiers for correlation.</li>
  <li>Write a custom bean, which performs the call to the http backend, processing both request and reply objects (but this is basically a no-Camel solution, reinventing the wheel and hence not preferred)</li>
  </ul>
  
  <p><strong>Already failed approaches:</strong></p>
  
  <p>I tried to access the original request message using a Processor like this at the end of my route, with no success:</p>
  
  <pre><code> process(new Processor() {
     @Override
     public void process(Exchange exchange) throws Exception {
        Message originalInMessage = exchange.getUnitOfWork().getOriginalInMessage();
           logger.debug(originalInMessage.getBody(String.class));
           logger.debug(exchange.getIn().getBody(String.class));
     }
   });
  </code></pre>
  
  <p>Thanks for any help</p>