This page last changed on Aug 05, 2005 by [email protected].

Filters Quick Reference

Configuring Filters

Filters can be used to express the rules that control how routers behave. Mule has the following default filters -


PayloadTypeFilter

will check the class type of the payload object inside an event.

<filter expectedType="java.lang.String" 
      className="org.mule.routing.filters.PayloadTypeFilter"/>


RegExFilter

will apply a regular pattern to the event payload. The filter will do a toString() on the event payload, so users may also want to apply a PayloadTypeFilter as well to the event using an AndFilter to make sure the event payload is a String.

<filter pattern="the quick brown (.*)" 
    className="org.mule.routing.filters.RegExFilter"/>


WildcardFilter

will apply a wildcard pattern to the event payload. The filter will do a toString() on the event payload, so users may also want to apply a PayloadTypeFilter as well to the event using an AndFilter to make sure the event payload is a String.

For the string "the quick brown fox jumped over the lazy dog" the following patterns would match -

  • *x jumped over the lazy dog
  • the quick*
  • *fox*
<filter pattern="the quick brown *" 
    className="org.mule.routing.filters.WildcardFilter"/>


JXPathFilter

JXPath is an XPath interpreter that can apply XPath expressions to xml dom objects or any other object graph. For more information about JXpath see the user guide. Also there is a good XPath tutorial at zvon.

<filter expression="(msg/header/resultcode)='success'"   
  className="org.mule.routing.filters.xml.JXPathFilter"/>

It is possible to set other JXPath context properties on the filter. Two of the important ones are namespaces and value which is used to compare to the result of the XPath expression (value is 'true' by default).

<filter expression="msg/header/resultcode"   
  className="org.mule.routing.filters.xml.JXPathFilter">
    <properties>
        <property name="value" value="success"/>
        <map name="namespaces">
            <propety name="foo" value="http://www.foo.org/ns"/>
        </map>
    </properties>
</filter>


MessagePropertyFilter

This filter allows you add logic to your routers based on the value of one or more properties of a Message. This can be very powerful because Message properties of the underlying message are exposed meaning you can reference any transport-specific or user defined property. So for an Http event you can match one or more Http Headers; the same applies for all other message types such as Jms or email.

<filter expression="Content-Type=text/xml"   
  className="org.mule.routing.filters.MessagePropertyFilter"/>

The expression is always a key value pair. If you want to have more complex extressions you can use the logic filters. For example -

<filter className="org.mule.routing.filters.logic.AndFilter">
    <left-filter expression="JMSCorrelationID=1234567890"   
      className="org.mule.routing.filters.MessagePropertyFilter"/>
    <right-filter expression="JMSReplyTo=null"   
      className="org.mule.routing.filters.MessagePropertyFilter"/>
</filter>

Logic Filters


AndFilter

An And filter combines two filters and only accepts if both filters match. Logic filters can be nested so that more complex logic can be expressed.

<filter className="org.mule.routing.filters.logic.AndFilter">
    <left-filter expectedType="java.lang.String"   
       className="org.mule.routing.filters.PayloadTypeFilter"/>
    <right-filter pattern="the quick brown (.*)" 
        className="org.mule.routing.filters.RegExFilter"/>     
</filter>


OrFilter

The Or filter combines two filters and accepts if either of the filters match.

<filter className="org.mule.routing.filters.logic.OrFilter">
    <left-filter expectedType="java.lang.String" 
        className="org.mule.routing.filters.PayloadTypeFilter"/>
    <right-filter expectedType="java.lang.StringBuffer" 
       className="org.mule.routing.filters.PayloadTypeFilter"/>
</filter>


NotFilter

A not filter will accept if the filter assigned to it does not accept.

<filter className="org.mule.routing.filters.logic.NotFilter">
    <filter expectedType="java.lang.String" 
        className="org.mule.routing.filters.PayloadTypeFilter"/>
</filter>

Writing your own filters

The filters provided should be able to handle most filtering requirements however if you need to write your own it is really easy. You just need to implement the UMOFilter interface which has a single method.

public boolean accept(UMOMessage message);

This method should return true if the UMOMessage matches whatever critia the filter imposes otherwise it should return false.

Like all other Mule object bean properties can be set on the filter.
The Mule DTD defines a number of default attibutes that can be set on a <filter> element in Mule Xml such as pattern, expression, configFile, expectedType and path. You don't have to use these if you dont want to as you can set any property using the <properties> element.

Document generated by Confluence on Nov 27, 2006 10:27