Dynamic Filter Values (Header and Property)

Overview

Filter criterion values do not have to be hardcoded. Integration Manager supports dynamic values that are resolved at runtime from Camel message headers, exchange properties, or Groovy expressions. This enables parameterized filters where the actual filter values are determined by the route logic.

Complete XML Examples

Simple Expression in Filter Value

Use the simple: prefix to reference a Camel Simple expression:

XML
<filters>
    <filter id="filterByDynamicSku">
        <and>
            <criterion fieldName="sku" operator="equals"
                       value="simple:${exchangeProperty.currentTime}"/>
        </and>
    </filter>
</filters>

Header-Based Dynamic Value

Reference a message header set earlier in the route:

XML
<filters>
    <filter id="filterByRegion">
        <and>
            <criterion fieldName="attribute1" operator="equals"
                       value="simple:${header.regionCode}"/>
        </and>
    </filter>
</filters>

Groovy Expression in Filter Value

Use the groovy: prefix for more complex runtime logic:

XML
<filters>
    <filter id="incrementalFilter">
        <and>
            <criterion fieldName="lastUpdateDate" operator="greaterThan"
                       value="groovy:headers.lastExportedDate"/>
        </and>
    </filter>
</filters>

Multiple Dynamic Criteria

Combine dynamic and static values in the same filter:

XML
<filters>
    <filter id="parameterizedFilter">
        <and>
            <!-- Dynamic: from header -->
            <criterion fieldName="attribute1" operator="equals"
                       value="simple:${header.customerSegment}"/>
            <!-- Dynamic: from exchange property -->
            <criterion fieldName="attribute2" operator="greaterThan"
                       value="simple:${exchangeProperty.minThreshold}"/>
            <!-- Static: hardcoded -->
            <criterion fieldName="attribute3" operator="notEqual" value="DELETED"/>
        </and>
    </filter>
</filters>

Complete Route with Dynamic Filter

The filter is defined in its own file. The route sets the headers before the fetch:

filters/dynamic-customer-filter.xml:

XML
<filters>
    <filter id="dynamicCustomerFilter">
        <and>
            <criterion fieldName="attribute1" operator="equals"
                       value="simple:${header.targetRegion}"/>
            <criterion fieldName="attribute2" operator="greaterThan"
                       value="simple:${header.minRevenue}"/>
        </and>
    </filter>
</filters>

routes/export-customers.xml:

XML
<routes xmlns="http://camel.apache.org/schema/spring">
    <route id="exportCustomersByRegion">
        <from uri="direct:exportCustomers"/>

        <!-- Set dynamic filter parameters before the fetch -->
        <setHeader name="targetRegion">
            <constant>EMEA</constant>
        </setHeader>
        <setHeader name="minRevenue">
            <constant>50000</constant>
        </setHeader>

        <log message="Fetching customers in ${header.targetRegion} with revenue > ${header.minRevenue}"/>

        <to uri="pfx-api:fetch?objectType=C&amp;filter=dynamicCustomerFilter"/>
        <to uri="pfx-csv:marshal?delimiter=,"/>
        <to uri="file:{{outbound.path}}?fileName=customers-${header.targetRegion}.csv"/>
    </route>
</routes>

Groovy with Complex Runtime Logic

XML
<filters>
    <filter id="smartDateFilter">
        <and>
            <!-- Compute a date 30 days ago at runtime -->
            <criterion fieldName="lastUpdateDate" operator="greaterThan"
                       value="groovy:new java.text.SimpleDateFormat('yyyy-MM-dd').format(new Date() - 30)"/>
        </and>
    </filter>
</filters>

How It Works

  1. simple: prefix -- the value after simple: is evaluated as a Camel Simple Language expression. Supports ${header.name}, ${exchangeProperty.name}, ${body[field]}, etc.

  2. groovy: prefix -- the value after groovy: is evaluated as a Groovy expression. Has access to headers, properties, body, and exchange.

  3. No prefix -- the value is treated as a literal string.

  4. Dynamic values are resolved each time the filter is used in a fetch operation, so they reflect the current state of headers and properties at that point in the route.

Common Pitfalls

  • Header not set: If the referenced header or property is null at runtime, the criterion value will be null, which may cause the filter to match nothing or throw an error. Always set headers before the fetch step.

  • Prefix spelling: The prefixes are simple: and groovy: (lowercase, with colon). Using Simple: or GROOVY: will not work -- the value will be treated as a literal string.

  • XML escaping: In simple: expressions within XML attributes, use &amp; for &, &lt; for <.

  • Groovy sandbox: Dynamic Groovy expressions in filter values run in the same sandbox as mapper Groovy expressions. Complex operations may be restricted.

  • Debugging: Dynamic filter values are resolved at runtime and not visible in the XML. Use <log> statements before the fetch to verify that headers and properties contain expected values.