Filter mediator can be used for XPath filtering of messages. There are two modes of operation.
In both modes if evaluation of expression returns true child mediators will be executed.
Filter mediator closely resembles the If/else control structure.
<filter (source="xpath" regex="string") | xpath="xpath"> mediator+ </filter>
The <filter> mediator either test the given xpath expression as a Boolean expression, or match the evaluation result of a source xpath expression as a String against the given regular expression. If the test succeeds, the filter mediator will execute the enclosed mediators in sequence.
One could also use this mediator to handle the failure case as well, in which case the configuration would be as follows
<filter (source="xpath" regex="string") | xpath="xpath"> <then [sequence="string"]> mediator+ </then> <else [sequence="string"]> mediator+ </else> </filter>
In this case the filter condition remains as earlier and the succeeded messages will be mediated using the the set of mediators enclosed in the 'then' element in sequence, while failed messages will be mediated using the set of mediators enclosed in the else element in sequence
Here are the configuration options related to Filter Mediator.
<filter source="get-property('To')" regex=".*/StockQuote.*"> <then> <send/> </then> <else> <drop/> </else> </filter>
In this example, filter will get the To header value and match it against the given regular expression. If this evaluation returns true, it will send the message. If the evaluation returns false it will drop the message.