You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by fliot33 <fr...@poplidays.com> on 2013/12/18 21:13:03 UTC

Aggregator, beyond the simple string example

Hi,

I really enjoy the pattern <enrich uri="..." strategyRef="..."/>
It's straight forward and really very reusable, really great.

Btw, beyond the StringInAggregatingStrategy documented example,
does anybody have a real life XML Aggregating Strategy example ?

Indeed, since, depending your endpoints, you do not control obviously the
encoding variables, either your body type, try to aggregate :
"""<?xml version="1.0" encoding="UTF-8"?><test1>content</test1>"""
with :
"""<?xml version="1.0" encoding="ISO-8859-1"?><test2>éèàù$ê£ë</test2>"""

to obtain :
"""<?xml version="1.0"
encoding="UTF-8"?><xml><test1>content</test1><test2>éèàù$ê£ë</test2></xml>"""
(encoded into utf-8)

This is slightly more complex than just :
    String oldBody = oldExchange.getIn().getBody(String.class);
    String newBody = newExchange.getIn().getBody(String.class);
    result = "<xml>" + oldBody + newBody + "</xml>";
    ...

I didn't get success trying to read newExchange as something else that a
"String.class", to get better Bytes level reading.

Thanks in advance for any advice.

Best regards,

Francois




--
View this message in context: http://camel.465427.n5.nabble.com/Aggregator-beyond-the-simple-string-example-tp5745012.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Aggregator, beyond the simple string example

Posted by "kraythe ." <kr...@gmail.com>.
I think I can share this one. .. its a pretty mundane one I've used in more
than one company.

public class ListAggregationStrategy<T> implements AggregationStrategy {
  /** The comparator used to keep the list sorted if any. */
  final Comparator<T> comparator;

  /**
   * @param comparator The comparator used to keep the list sorted if any;
if this is null then the list will not be kept sorted.
   */

  public ListAggregationStrategy(final Comparator<T> comparator) {
    this.comparator = comparator;
  }

  @Override
  public Exchange aggregate(final Exchange aggregatedExchange,
finalExchange newExchange) {
    final T newBody = (T) newExchange.getIn().getBody();
    List<T> exchangeBodies = null;
    if (aggregatedExchange == null) {
      exchangeBodies = new LinkedList<>();
      exchangeBodies.add(newBody);
      newExchange.getIn().setBody(exchangeBodies);
      return newExchange;
    }
    exchangeBodies = aggregatedExchange.getIn().getBody(List.class);
    exchangeBodies.add(newBody);
    if (null != this.comparator) Collections.sort(exchangeBodies, this.
comparator);
    return aggregatedExchange;
  }
}

*Robert Simmons Jr. MSc. - Lead Java Architect @ EA*
*Author of: Hardcore Java (2003) and Maintainable Java (2012)*
*LinkedIn: **http://www.linkedin.com/pub/robert-simmons/40/852/a39
<http://www.linkedin.com/pub/robert-simmons/40/852/a39>*


On Thu, Dec 19, 2013 at 7:39 AM, Cecilio Alvarez <
cecilio.alvarez@hotmail.com> wrote:

> Hi,
>
> I did long time ago a test to add a child node to an xml root, you could
> modify it.
>
> public class XmlAggregator implements AggregationStrategy {
>
>         public Exchange aggregate(Exchange oldExchange, Exchange
> newExchange) {
>
>             Document oldBody =
> oldExchange.getIn().getBody(org.w3c.dom.Document.class);
>             Document newBody =
> newExchange.getIn().getBody(org.w3c.dom.Document.class);
>
>             NodeList list = newBody.getElementsByTagName("tag");
>
>             Element element = (Element) list.item(0);
>
>             Node copiedNode = oldBody.importNode(element, true);
>
>             oldBody.getDocumentElement().appendChild(copiedNode);
>
>             oldExchange.getIn().setBody(oldBody);
>
>         return oldExchange;
>     }
> }
>
> Hope it help.
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/Aggregator-beyond-the-simple-string-example-tp5745012p5745055.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>

Re: Aggregator, beyond the simple string example

Posted by Cecilio Alvarez <ce...@hotmail.com>.
Hi,

I did long time ago a test to add a child node to an xml root, you could
modify it.

public class XmlAggregator implements AggregationStrategy {
		
	public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
        
	    Document oldBody =
oldExchange.getIn().getBody(org.w3c.dom.Document.class);
	    Document newBody =
newExchange.getIn().getBody(org.w3c.dom.Document.class);
		
	    NodeList list = newBody.getElementsByTagName("tag");
	    
	    Element element = (Element) list.item(0);

	    Node copiedNode = oldBody.importNode(element, true);

	    oldBody.getDocumentElement().appendChild(copiedNode);

            oldExchange.getIn().setBody(oldBody);

        return oldExchange;
    }
}

Hope it help.














--
View this message in context: http://camel.465427.n5.nabble.com/Aggregator-beyond-the-simple-string-example-tp5745012p5745055.html
Sent from the Camel - Users mailing list archive at Nabble.com.