Re: Aggregator, beyond the simple string example

2013-12-23 Thread kraythe .
I think I can share this one. .. its a pretty mundane one I've used in more
than one company.

public class ListAggregationStrategyT implements AggregationStrategy {
  /** The comparator used to keep the list sorted if any. */
  final ComparatorT 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 ComparatorT comparator) {
this.comparator = comparator;
  }

  @Override
  public Exchange aggregate(final Exchange aggregatedExchange,
finalExchange newExchange) {
final T newBody = (T) newExchange.getIn().getBody();
ListT 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.alva...@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

2013-12-19 Thread Cecilio Alvarez
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.


Aggregator, beyond the simple string example

2013-12-18 Thread fliot33
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?test1content/test1
with :
?xml version=1.0 encoding=ISO-8859-1?test2éèàù$ê£ë/test2

to obtain :
?xml version=1.0
encoding=UTF-8?xmltest1content/test1test2éèàù$ê£ë/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.