Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

WSO2 ESB Filter Mediator Tutorial

DZone's Guide to

WSO2 ESB Filter Mediator Tutorial

· Integration Zone
Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

This post will be for WSO2 ESB Filter Mediator and it will be cover simple usecase with basic Filter Mediator functions. It can be used for XPath filtering of messages.

There are two modes of operation

  • Specifies the XPath (boolean expression), return true or false
  • XPath will be matched against the regular expression return true or false


Syntax

<filter (source="xpath" regex="string") | xpath="xpath">
  mediator+
</filter>


Usecase

I have services call 'BusService' where I can give rootId (road name) and get list bus number that going on that root. In the same services it have some train deatils also.

When client call busService it must give busService and also If client ask for train details rather bus system must give it also.

Here is busServices calls

request:
<body>
  <p:getBusNo xmlns:p="http://transport.org">
  <xs:rootId xmlns:xs="http://transport.org">root1</xs:rootId>
  </p:getBusNo>
</body>

respond:
<ns:getBusNoResponse xmlns:ns="http://transport.org">
  <ns:return>root1Colombo</ns:return>
  <ns:return>Negombo</ns:return>
  <ns:return>Galle</ns:return>
</ns:getBusNoResponse>

getTraingNo

request:
<body>
  <p:getTrainNo xmlns:p="http://transport.org">
  <xs:rootId xmlns:xs="http://transport.org">root1</xs:rootId>
  </p:getTrainNo>
</body>

respond:
<ns:getTrainNoResponse xmlns:ns="http://transport.org">
  <ns:return>12-Colombo</ns:return>
  <ns:return>13-Muthu</ns:return>
  <ns:return>01-Bange</ns:return>
</ns:getTrainNoResponse>


Now I have write simple WSO2 ESB proxy with filter mediator.

1. Download wso2 esb 4.6.0 

2. Start wso2 esb <WSO2ESB_HOME>/bin/wso2server.bat (offset 1) Other services expose in wso2 AS in offset 0

3. Go to https://localhost:9444/carbon/

4. Then Create "Pass Through Proxy"

image

5. Here I am adding WSO2 Filter Mediator

image

  • Specify As: XPath or a Regular expression.
  • XPath: XPath expression if you selected the "Specify As" option to "XPath".
  • Source: which is going match with the reguilar expression
  • Regex: Regular expression to match with the source value.

6. In Here I am filtering for the action  of the WS request and it log the client request

is it bus or train request?

image

Here is proxy Source View

<proxy xmlns="http://ws.apache.org/ns/synapse" name="transportProxy" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
   <target>
      <inSequence>
         <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>
      </inSequence>
      <outSequence>
         <send/>
      </outSequence>
      <endpoint>
         <address uri="http://localhost:9763/services/BusServices"/>
      </endpoint>
   </target>
   <publishWSDL uri="http://localhost:9763/services/BusServices?wsdl"/>
   <description></description>
</proxy> 


 

6. Go to https://localhost:9444/services/transportProxy?tryit#

Make bus request and train request and see console log

image

image

You can improve this usecase with some WSO2 ESB mediator if you wish!!

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

Topics:

Published at DZone with permission of Madhuka Udantha, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}