This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.

Filter Mediator

The Filter Mediator can be used for filtering messages based on an XPath, JSONPath or a regular expression. If the test succeeds, the Filter mediator executes the other mediators enclosed in the sequence.

The Filter Mediator closely resembles the "If-else" control structure.

The Filter mediator is a conditionally content aware mediator.



Syntax

<filter (source="[XPath|json-eval(JSONPath)]" regex="string") | xpath="[XPath|json-eval(JSONPath)]">
   mediator+
</filter>

This mediator could also be used to handle a scenario where two different sequences are applied to messages that meet the filter criteria and messages that do not meet the filter criteria.

<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. The messages that match the filter criteria will be mediated using the set of mediators enclosed in the then element. The messages that do not match the filter criteria will be mediated using the set of mediators enclosed in the else element.


UI Configuration

The parameters available for configuring the Filter mediator are as follows:

Parameter NameDescription
Specify As

This is used to specify whether you want to specify the filter criteria via an XPath expression or a regular expression.

  • XPath: If this option is selected, the Filter mediator tests the given XPath/JSONPath expression as a Boolean expression. 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 and Regular Expression: If this option is selected, the Filter mediator matches the evaluation result of a source XPath/JSONPath expression as a string against the given regular expression.
Source

The expression to locate the value that matches the regular expression that you can define in the Regex parameter.

Tip

You can click NameSpaces to add namespaces if you are providing an expression. Then the Namespace Editor panel would appear where you can provide any number of namespace prefixes and URLs used in the XPath expression. 

RegexThe regular expression to match the source value.

Note

You can configure the mediator using XML. Click switch to source view in the Mediator window.

Example

Example 1: Sending only messages matching the filter criteria

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.

<filter source="get-property('To')" regex=".*/StockQuote.*">
      <then>
          <send/>
      </then>
      <else>
          <drop/>
      </else>
</filter>

Example 2: Applying separate sequences

In this example, the Log mediator is used to log information from a service named Bus Services via a property when the request matches the filter criteria. When the request does not match the filter criteria, another log mediator configuration is used log information from a service named Train Service in a similar way.

<filter source="get-property('Action')" regex=".*getBusNo"> 
   <then> 
      <log level="custom"> 
         <property name="service" value="Bus Services is called"/> 
      </log> 
   </then> 
   <else> 
      <log level="custom"> 
         <property name="service" value="Train Service is called"/> 
      </log> 
   </else> 
</filter>