Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The Filter Mediator can be used for filtering messages based on an XPath or JSONPath. There are two modes of operation.

  1. If the user only specifies the XPath or JSONPath, it will be evaluated as true or false.
  2. 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, 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.

...

Code Block
XML
XML
<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.

...

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.

Code Block
XML
XML
<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 . The messages that match the filter criteria will be mediated using the the set of mediators enclosed in the "then" element in sequence, while failed messages 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 in sequencethe 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

.
RegexThe regular expression to match the source value.

 

...

.

Info
titleTip

...

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. 

Regex

...

The regular expression to match the source value.
Info
titleNote

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

Image Added

Image Removed

ExampleExample

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.

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

Example 2: Applying separate sequences

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. 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.

Code Block
languagexml
<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>
Excerpt
hiddentrue

Description of the Filter Mediator in WSO2 ESB.