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

Types of Anypoint Filters With Mulesoft

DZone's Guide to

Types of Anypoint Filters With Mulesoft

Whether you want to apply regular expression patterns on a message payload or identify duplicate messages, there's a filter for that.

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

Filters in Mule flows play a vital role in deciding whether messages can be passed to other message processors or not. When a message reaches a filter in the flow and meets the required conditions, then it passed to other processors in the flow. Otherwise, flow execution will be terminated and no more processing will be done.

Idempotent Filter

The idempotent filter is one of the most important filters when it comes to processing unique messages within a flow. You can set Id Expression using MEL to identify the duplicate message on basis of the expression defined. The default expression used is #[message:id].

ThrowOnUnaccepted being checked implies that when duplicate messages are received within the flow, the further processing stops and control is transferred to the error handler.

Image title

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:file="http://www.mulesoft.org/schema/mule/file" 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.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://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8087" doc:name="HTTP Listener Configuration" />
    <flow name="logicmuleFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/unique" allowedMethods="POST" doc:name="HTTP" />
        <idempotent-message-filter idExpression="#[message.inboundProperties.'http.query.params'.code]" doc:name="Idempotent Message" throwOnUnaccepted="true" />
        <file:outbound-endpoint path="src/main/resources/out" responseTimeout="10000" doc:name="File" />
        <catch-exception-strategy doc:name="Catch Exception Strategy">
            <set-payload value="&quot;Message already received.&quot;" doc:name="Set Payload" />
            <set-property propertyName="http.status" value="400" doc:name="Property" />
        </catch-exception-strategy>
    </flow>
</mule>

Regex Filter

The regex filter applies regular expression patterns on the message payload. If the message doesn’t match the pattern, then further processing stops. The filter applies the toString() method to the payload to convert the payload to a String.

Image title

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:file="http://www.mulesoft.org/schema/mule/file" 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.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://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8087" doc:name="HTTP Listener Configuration" />
    <flow name="logicmuleFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/unique" allowedMethods="POST" doc:name="HTTP" />
        <regex-filter pattern="^[a-zA-Z]{3,15}$" doc:name="Regex" />
        <file:outbound-endpoint path="src/main/resources/out" responseTimeout="10000" doc:name="File" />
        <catch-exception-strategy doc:name="Catch Exception Strategy">
            <set-payload value="&quot;Message doesn't match pattern.&quot;" doc:name="Set Payload" />
            <set-property propertyName="http.status" value="400" doc:name="Property" />
        </catch-exception-strategy>
    </flow>
</mule>

Wildcard Filter

The wildcard filter applies a pattern on the message payload. If the message doesn’t match the pattern, then further processing stops. The filter applies the toString() method to the payload to convert the payload to a String. 

Image title

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:file="http://www.mulesoft.org/schema/mule/file" 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.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://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8087" doc:name="HTTP Listener Configuration" />
    <flow name="logicmuleFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/unique" allowedMethods="POST" doc:name="HTTP" />
        <wildcard-filter pattern="the fox is cunning*" caseSensitive="false" doc:name="Wildcard" />
        <file:outbound-endpoint path="src/main/resources/out" responseTimeout="10000" doc:name="File" />
        <catch-exception-strategy doc:name="Catch Exception Strategy">
            <set-payload value="&quot;Message doesn't match pattern.&quot;" doc:name="Set Payload" />
            <set-property propertyName="http.status" value="400" doc:name="Property" />
        </catch-exception-strategy>
    </flow>
</mule>

Schema Validation Filter

The schema validation filter takes XML as an input and validates against the referenced XSD. You can place this filter before another message processor (such as a connector) to determine whether the incoming message or event should be handled or not, allowing the message to only continue on along the flow when the filter validations are met and the included XML is considered valid.

Image title

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:file="http://www.mulesoft.org/schema/mule/file" 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.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://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8087" doc:name="HTTP Listener Configuration" />
    <flow name="logicmuleFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/unique" allowedMethods="POST" doc:name="HTTP" />
        <mulexml:schema-validation-filter schemaLocations="src/test/resources/cust.xsd" returnResult="true" doc:name="Schema Validation" />
        <file:outbound-endpoint path="src/main/resources/out" responseTimeout="10000" doc:name="File" />
        <catch-exception-strategy doc:name="Catch Exception Strategy">
            <set-payload value="&quot;Input xml message doesn't match schema.&quot;" doc:name="Set Payload" />
            <set-property propertyName="http.status" value="400" doc:name="Property" />
        </catch-exception-strategy>
    </flow>
</mule>

Logic Filter

The logic filter allows filtering message on and/or/not expressions. Logic filters apply the and/or/not logic to one or more nested filters that they enclose. When you use these logic filters, you add nested filters to them from within the nested pane for the and/or/not filter.

Image title

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:file="http://www.mulesoft.org/schema/mule/file" 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.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://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8087" doc:name="HTTP Listener Configuration" />
    <flow name="logicmuleFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/unique" allowedMethods="POST" doc:name="HTTP" />
        <and-filter doc:name="And">
            <regex-filter pattern="^[0-9]" />
            <wildcard-filter pattern="this is article*" caseSensitive="false" />
        </and-filter>
        <file:outbound-endpoint path="src/main/resources/out" responseTimeout="10000" doc:name="File" />
        <catch-exception-strategy doc:name="Catch Exception Strategy">
            <set-payload value="&quot;Input xml message doesn't match schema.&quot;" doc:name="Set Payload" />
            <set-property propertyName="http.status" value="400" doc:name="Property" />
        </catch-exception-strategy>
    </flow>
</mule>

Filter Reference

Filter reference is used to reference the global filter. You must have already created a global configuration element and entered the filter name in the global reference property.Image title

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:file="http://www.mulesoft.org/schema/mule/file" 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.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://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8087" doc:name="HTTP Listener Configuration" />
    <flow name="logicmuleFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/unique" allowedMethods="POST" doc:name="HTTP" />
        <filter ref="Regex_Global_Filter" doc:name="Filter Reference" />
        <file:outbound-endpoint path="src/main/resources/out" responseTimeout="10000" doc:name="File" />
        <catch-exception-strategy doc:name="Catch Exception Strategy">
            <set-payload value="&quot;Input xml message doesn't match schema.&quot;" doc:name="Set Payload" />
            <set-property propertyName="http.status" value="400" doc:name="Property" />
        </catch-exception-strategy>
    </flow>
</mule>

The Idempotent Filter has property ThrowOnUnaccepted by default. If it is checked, this means that whenever duplicate messages are received, control is transferred to Error Handler. However, this is not the case with other filters like regex, wildcard, schema validation, etc. To achieve this, wrap the filter with Message Filter and mark ThrowOnUnaccepted as true on the Message Filter. This way, in case of failure, the control will transfer to Error Handler.

<message-filter throwOnUnaccepted="true">
<mulexml:schema-validation-filter schemaLocations="src/test/resources/customer.xsd" returnResult="true" doc:name="Schema Validation"/>
</message-filter>

I hope that this article clarifies any confusion that you may have had with Anypoint Mule filters!

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:
mulesoft ,anypoint ,filters ,integration

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}