Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Camel Spliter using custom split method

I maybe doing something wrong here but i don't seem to be getting the expected result when using the split().method combination. The incoming xml varies slightly between incoming messages and the elementKey node within it describes what the XML node within in it we are looking for.

from("direct:fromWhereEver")
  ...
  .setHeader("dynamicToken", xpath("//*[local-name()='elementKey']/text()").stringResult())
  ...
  .split().method(DynamicSplitToken.class, "extractTokens")
    .to("direct:outgoing")
.routeId("BhahBlah")

I have the following defined in a method for use by the route

public class DynamicSplitToken {
    public static Expression extractTokens(Exchange exchange){

        String splitToken = exchange.getIn().getHeader("dynamicToken").toString();

        TokenizeLanguage tok = new TokenizeLanguage();
        tok.setXml(true);
        tok.setIncludeTokens(true);
        tok.setToken(splitToken);

        return tok.createExpression();
    }
}

Now the xml tags are pulled from the message no problem, BUT the resultant "body" that is extracted and pushed through contains <tagname>...</tagname> that matches the elementKey above with no child nodes/elements in between. I would expect to receive everything between the tags as well, similar to if i used the following

.split().tokenizeXML("tagname")

When using this method i get the full content of tags and all nodes between the tags, unforunately i can't pass a {header.dynaimcToken} to the tokenizeXML ProcessorDefinition otherwise i wouldn't need to ask for help here.

Where have i gone wrong here, i know im missing an important step after the method processor, but unsure what

For now we are stuck with camel 2.9 (fuse-esb) if that makes a difference

Cheers Mark

like image 631
Mark Avatar asked Feb 26 '14 03:02

Mark


1 Answers

method() must not return an Expresssion but something that is iterable such as java.util.List, see here. Your original solution was quite close, but you have to evaluate the expression and return its result such as:

protected static class DynamicSplitToken {
    @SuppressWarnings("unchecked")
    public static List<String> extractTokens(final Exchange exchange) {
        final String splitToken = exchange.getIn().getHeader("dynamicToken").toString();

        final TokenizeLanguage tok = new TokenizeLanguage();
        tok.setXml(true);
        tok.setIncludeTokens(true);
        tok.setToken(splitToken);

        final Expression expression = tok.createExpression();
        return expression.evaluate(exchange, List.class);
    }
}
like image 76
Peter Keller Avatar answered Sep 29 '22 08:09

Peter Keller