• fb
  • tw
  • gg
97008 48403
+91 40-27177600

Blog

Applying filtering logic for custom filters in Mule ESB?

single-blog

From the technical center of excellence of Massil Technologies, our technology leaders make it easy to understand the complex situations you come across in Mule ESB from the experience of Massil Technologies to have countered them in their experience of working on client projects in real time. This blog talks about applying filtering logic for custom filters in Mule ESB. This blog is a part of series of blogs being authored and published by Massil Technologies for the benefit of the IT community globally.

We have found to our surprise that extensive documentation on mulesoft does not cover the line of applying filtering logic for custom filters. In addition to applying standard filters, you can create a custom filter to specify a precise set of conditions a message must satisfy before it can continue through the flow. Note that the reference is to a class implementing the Filter interface.

STEP 1:

In The Anypoint top left click on the file => New => Mule Project. Click on the Mule Project enter the your project name.

STEP 2:

Drag and drop the connecters you required from the Mule ESB Palette to the canvas.

STEP 3:

Custom filter is used where you can have an out-of-box filter logic which you can say your own custom filter logic. Generally, you can implement your Custom Filter in a Java class implementing org.mule.api.routing.filter.Filter interface

A small example of your custom filter:

Create following Java Class as a custom filter:

package com.filter;

import org.mule.api.MuleMessage;

import org.mule.api.routing.filter.Filter;

 

public class PayloadFilter implements Filter {

/**

* This class implements a custom Mule filter.

*/

@Override

public boolean accept(MuleMessage input) {

boolean msgValidated = false;

try {

String payloadStr = input.getPayloadAsString();

if (payloadStr != null && (payloadStr.trim().length() > 0)) {

if (payloadStr.contains(“@”)) {

msgValidated = true;

} else

return false;

} else {

return false;

}

 

} catch (Exception e) {

e.printStackTrace();

msgValidated = false;

}

 

return msgValidated;

}

}

STEP 4:

In Mule ESB flow, you can implement this custom Filter by calling this java class as follows:

FLOW :

What this Custom Filter will do is when you pass any String payload into your flow it validates the String payload and check if it contains @ in it. If it finds @ within the String, it will pass or else it will throw exceptions. For example, String like support@mulesoft.com will pass successfully, while Sting like support will fail. You can try passing a String payload from POSTMAN client to the flow as POST method and header as Content-Type=text/plain

Xml:

<?xml version=“1.0” encoding=“UTF-8”?>

 

<mule xmlns:http=“http://www.mulesoft.org/schema/mule/http”

xmlns=“http://www.mulesoft.org/schema/mule/core” xmlns:doc=“http://www.mulesoft.org/schema/mule/documentation”

xmlns:spring=“http://www.springframework.org/schema/beans”

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xsi:schemaLocation=“http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd

http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd>

<http:listener-config name=“HTTP_Listener_Configuration” host=“0.0.0.0” port=“8081” doc:name=“HTTP Listener Configuration”/>

<flow name=“TestFlow”>

<http:listener config-ref=“HTTP_Listener_Configuration” path=“/test” doc:name=“HTTP”/>

<object-to-string-transformer doc:name=“Object to String”/>

<message-filter throwOnUnaccepted=“true” doc:name=“RequestHeaderFilter”>

<custom-filter class=“com.filter.PayloadFilter”/>

 

</message-filter>

<set-payload value=“Test payload” doc:name=“Set Payload”/>

</flow>

</mule>

POSTMAN OUTPUT:

In a simple way, you can post the support@mulesoft.com or any other wit will contains @ it will gives the success results otherwise like supportmulesoft.com it will fail. The success text is shown below.

Hence, we have gone through the step by step process to depict the way Massil Technologies team has addressed the scenario mentioned above. Hope this article from Massil Technologies was useful for you. If you have any further queries on this topic, please reach out to info@massil.tech.

Leave a Reply

Your email address will not be published. Required fields are marked *

map

Contact Us

Get in touch with us