The Filter Mediator can be used for filtering messages based on an XPath or JSONPath. There are two modes of operation.
- If the user only specifies the XPath or JSONPath, it will be evaluated as true or false.
- If the user specifies a regular expression as well, the string returned from evaluating the XPath or JSONPath will be matched against the regular expression.
In both modes, if the evaluation of expression returns true, the child mediators will be executed.
The Filter Mediator closely resembles the "If-else" control structure.
|Table of Contents|
<filter (source="[XPath|json-eval(JSONPath)]" regex="string") | xpath="[XPath|json-eval(JSONPath)]"> mediator+ </filter>
The Filter Mediator either tests the given XPath/JSONPath expression as a Boolean expression, or matches the evaluation result of a source XPath/JSONPath 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 a failure scenario as well. In this case, the configuration will be as follows:
<filter (source="[XPath|json-eval(JSONPath)]" regex="string") | xpath="[XPath|json-eval(JSONPath)]"> <then [sequence="string"]> mediator+ </then> <else [sequence="string"]> mediator+ </else> </filter>
In this case, the Filter condition remains the same 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 the Filter Mediator:
- Specify As - Specifies whether you give the condition as an XPath or a Regular expression.
- XPath - XPath or JSONPath expression if you selected "XPath" for "Specify As". When specifying a JSONPath, use the format
json-eval(<JSON_PATH>), such as
json-eval(getQuote.request.symbol). For more information on using JSON with the ESB, see JSON Support.
Source- Expression to locate the value that matches the regular expression that you can define below.
Info title Tip
Click the "Namespaces" link to add namespaces if you are providing an expression. You will be provided another panel named "Namespace Editor," where you can provide any number of namespace prefixes and URL that you have used in the XPath or JSONPath expression.
- Regex - Regular expression to match the source value.
You can configure the Mediator using XML. Click on "switch to source view" in the "Mediator" window.
<filter source="get-property('To')" regex=".*/StockQuote.*"> <then> <send/> </then> <else> <drop/> </else> </filter>
In this example, the 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.
Description of the Filter Mediator in WSO2 ESB.