You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Dew Francisco <an...@gmail.com> on 2015/01/30 15:26:33 UTC

Camel Binding - Header manipulation

Hi!

I were using the camel exchange header and I weren't being able to
manipulate the header once it's set. I am setting an XML (as String) in the
header and would like to set some values to those XML nodes.
 
So far I'm receiving the exchange to be manipulated by JMS and am trying to
manipulate the header to add values to the nodes.
My main route is something like this:
 
    <route streamCache="true" id="Main">
        <from uri="switchyard://endpoint" />
        /** Here we would like to manipulate the header ${headers.header}
**/
       <wireTap uri="direct:routeMessage" copy="true">
            <body>
<simple>${headers.header}</simple>
            </body>
       </wireTap>
        <to uri="switchyard://newEndPoint"/>
        /** Here we would like to manipulate the header ${headers.header}
again **/
        <wireTap uri="direct: routeMessage " copy="true">
            <body>
<simple>${headers.header}</simple>
            </body>
       </wireTap>
    </route>
  
And my header is something like this, where I'm trying to populate the
nodes:
<Header>
<Info>
<InternalID></ InternalID >
</Info>
<StartTime></StartTime>
<EndTime></EndTime>
</Header>



As I was trying to avoid the use of JAVA, I end up with the following
concept:
 
I use multicast so I can work the header as being my body and in the end I
use a CustomAggregationStrategy to build it back together.
 
I ended with something like this:
 
<route streamCache="true" id="Main">
        <from uri="jms:endpoint" />
   <to uri="direct:manipulateHeader" />
       <wireTap uri="direct:routeMessage" copy="true">
            <body>
       <simple>${headers.header}</simple>
            </body>
       </wireTap>
        <to uri="switchyard://newEndPoint"/>
   <to uri="direct:manipulateHeader" />
        <wireTap uri="direct: routeMessage " copy="true">
            <body>
       <simple>${headers.header}</simple>
            </body>
       </wireTap>
    </route>

<route>
        <from uri="direct:manipulateHeader" />
      <multicast parallelProcessing="true" strategyRef="headerProcessor" >
            <pipeline id="Header">
                <setBody id="[HeaderAsBody]">
                    <simple>${headers.header}</simple>
                </setBody>
                <recipientList strategyRef="headerProcessor">
                  
<simple>xslt:${property.myProperty}_Transformer.xsl</simple>
                </recipientList>
            </pipeline>
            <pipeline id="Body">
                <setBody id="[BodyAsBody]">
                    <simple>${body}</simple>
                </setBody>
            </pipeline>
        </multicast>
</route>


And my AggregationStrategy is

public class HeaderProcessor implements AggregationStrategy{
  @Override
  public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
       if (oldExchange == null)
       {
           // the first time we have the header in our body
           return newExchange;
        }
       else {
            // the second time we will have the header in the old exchange
            String oldEx = oldExchange.getIn().getBody(String.class);
  
            // so we set the old body in the header of the new exchange
            newExchange.getIn().setHeader("header", oldEx);
  
            // return the new exchange with our JMS exchange edited
            return newExchange;
       }
   }
}


I originally posted this in the JBoss Fuse Service Works Forum (
https://developer.jboss.org/thread/251854
<https://developer.jboss.org/thread/251854>  ), since this is a switchyard
project, and some one advice me to put it here.

So I did, so it could help some one, or could some one give me a better way
of doing it.


Thank you




--
View this message in context: http://camel.465427.n5.nabble.com/Camel-Binding-Header-manipulation-tp5762261.html
Sent from the Camel - Users mailing list archive at Nabble.com.