Content Filter in Mule: invoicing receives only what it needs from the paid order
- 9 min read
On this page
Code
After payment, the order carries much more than invoicing uses: the card it was paid with, the customer’s email and phone, the cost of each item, the supplier and the internal notes. Invoicing needs the customer, the billing country, the payment reference and the lines. Someone has to strip the rest before the order leaves, and in Mule 4 that someone is a Transform that writes only the allowed fields.
The problem
In the series’ order scenario (order → payment → inventory → invoice), the paid order arrives like this:
{ orderId, customer: { id, name, email, phone, taxId, address: { street, city, country } },
payment: { method, authorizationCode, card: { number, holder, expiry } },
items: [ { sku, quantity, unitPrice, unitCost, supplier } ], internalNotes }
Sending everything to invoicing spreads the card number and the product costs across a system that does not need them, and forces invoicing to navigate a three-level structure to find the few fields it needs.
The pattern in the book
The book presents the pattern as the opposite of the Content Enricher: instead of adding data the target needs, there are many situations where the goal is to remove data from the message. The question is how to simplify the handling of a large message when only some of the data is of interest. The answer is a Content Filter, which removes unimportant data and leaves only the important data. The book adds that the filter does not necessarily only remove: it also serves to simplify the structure. Messages that come from external systems or packaged software often have many levels of nested groups, modeled on generic database structures, and the Content Filter can flatten that hierarchy into a simple list of elements, easier to understand and process (Content Filter).
A large message in, a small one out
The diagram, redrawn from the official sketch (CC BY 4.0), answers: what changes in the message that passes through the filter?
The message is still a single message. What comes out is smaller and flatter.
In Mule
In Mule 4, the Content Filter is written with the Transform, the component that converts input data into a new output structure or format (Transform Message Component). The documentation lists filtering among the advanced transformations written in the component’s DataWeave script (Transform Message Component). The filter can be written two ways: removing the known fields with the - operator, as the documentation’s example does when it excludes ssn and password to remove sensitive data (Exclude Fields from the Output), or writing to the output only the fields that stay. This page’s example uses the second.
The name is misleading: the Content Filter is not the Message Filter. The Message Filter decides whether the whole message continues or is discarded, and in Mule it is a Choice; the Content Filter lets the message continue and removes fields from inside it.
The invoice request flow
The diagram answers: what happens to the paid order until it reaches invoicing?
The same component also does two other patterns
The Transform also implements the Message Translator, which translates one format into another, and the Canonical Data Model, in which each system translates to and from a common model. In the book these are three patterns; it was Mule, not the book, that put the three in one component. A Transform that changes the format is a Message Translator; one that only selects and flattens fields is a Content Filter.
In the MuleSoft documentation
The documentation page Enterprise Integration Patterns Using Mule does not list the Content Filter. It maps the Message Translator to the Message Transformer, the Transform, and that is where this page writes the filter: the Transform documentation cites filtering among the transformations the DataWeave script does (Transform Message Component).
The example
The POST /eip/billing/invoice-requests endpoint receives the paid order, rejects with 400 an order without orderId, customer.id, payment.authorizationCode or items, and sends invoicing the result of the filter:
<ee:transform doc:name="Filter Order To Invoice Request">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{
orderId: payload.orderId,
customerId: payload.customer.id,
customerName: payload.customer.name,
taxId: payload.customer.taxId,
billingCountry: payload.customer.address.country,
// The authorization code identifies the payment; the card itself stays here.
paymentReference: payload.payment.authorizationCode,
lines: payload.items map ((item) -> { sku: item.sku, quantity: item.quantity, unitPrice: item.unitPrice })
}]]></ee:set-payload>
</ee:message>
</ee:transform>
In the repository: src/main/mule/eip/content-filter/content-filter.xml
The filter is a list of what stays. The output names each field invoicing receives; a new field on the order only gets there when someone adds it to the Transform. The Caveats show what the alternative, removing by name, lets through.
The filter flattens. customer.id, customer.address.country and payment.authorizationCode become customerId, billingCountry and paymentReference, at the first level. The card stays behind; the authorization reference identifies the payment.
The filter does not calculate. The invoice total is not sent. Calculating it from the lines is invoicing’s job, not the filter’s.
Invoicing is simulated in the same application (content-filter-invoicing.xml) and stores the invoice request it received, so the suite reads what arrived there, not what the Transform should have produced:
<munit-tools:assert-that doc:name="Exactly the allowed fields" expression="#[keysOf(payload) map ($ as String)]" is="#[MunitTools::equalTo(['orderId', 'customerId', 'customerName', 'taxId', 'billingCountry', 'paymentReference', 'lines'])]" />
<munit-tools:assert-that doc:name="Flattened customer and payment" expression="#[[payload.customerId, payload.customerName, payload.billingCountry, payload.paymentReference]]" is="#[MunitTools::equalTo(['CUS-100', 'Ana Lima', 'BR', 'AUTH-777'])]" />
<munit-tools:assert-that doc:name="Lines without cost or supplier" expression="#[payload.lines]" is="#[MunitTools::equalTo([{sku: 'SKU-BOOK', quantity: 2, unitPrice: 30}, {sku: 'SKU-MUG', quantity: 1, unitPrice: 12.5}])]" />
<munit-tools:assert-that doc:name="Card number nowhere in what arrived" expression="#[write(payload, 'application/json') contains '4111111111111111']" is="#[MunitTools::equalTo(false)]" />
<munit-tools:assert-that doc:name="Email nowhere in what arrived" expression="#[write(payload, 'application/json') contains 'ana@example.com']" is="#[MunitTools::equalTo(false)]" />
In the repository: src/test/munit/eip/content-filter-test-suite.xml
Caveats
Removing by name lets through what nobody listed. In a 2018 Stack Overflow question, still on DataWeave 1 in Mule 3, someone wanted to remove the fields c and d from each object of an array; the answer uses map with $ - "c" - "d" (Stack Overflow: how to remove specific fields from List maps using dataweave mule). In DataWeave 2, in Mule 4, the - operator still removes key-value pairs, and the documentation uses it to remove sensitive data (Exclude Fields from the Output). It works for the fields known today; a field the order gains later, such as a loyalty card number, goes through without anyone deciding that invoicing should have it. The cf-field-added-to-the-order-later-does-not-leak test sends the order with a new field, loyaltyCardNumber, and checks that it does not reach invoicing:
<munit-tools:assert-that doc:name="New field not received" expression="#[write(payload, 'application/json') contains 'LOY-5555-0001']" is="#[MunitTools::equalTo(false)]" />
<munit-tools:assert-that doc:name="Order still received" expression="#[payload.orderId]" is="#[MunitTools::equalTo('ORD-CF-2')]" />
The - acts on the object it is applied to. In the documentation’s example, ssn is inside personal_information and password inside login_information, and the script applies - to each of those objects, walking the users with mapObject (Exclude Fields from the Output). To remove a nested field, the removal has to go down to the object that contains it; in an order with the card inside payment, each level that holds sensitive data needs its own removal. In the list of what stays, whatever is not written does not go out, at any level.
Validate before filtering. The filter chooses fields; it does not check that the order is complete. The example rejects with 400 an order without payment.authorizationCode before the Transform, and the cf-order-without-authorization-returns-400-and-nothing-is-sent test checks that the HTTP Request to invoicing is not called.
When not to use it
When the target needs the whole message, the filter only adds a Transform to maintain. When what must disappear is the whole message, not a few fields, the pattern is the Message Filter. When the removed fields will be needed later in the flow, the pattern is the Claim Check, which stores them and sends a key. And when the target expects another format, not just fewer fields, the job is a Message Translator; often the same Transform does both, and the pattern’s name says which of the two is the reason for the transformation.
Running the example and the tests
git clone https://github.com/brunosouzas/mulesoft-patterns-examples.git
cd mulesoft-patterns-examples
mvn clean test
The project requires Java 17, Maven 3.9+ and the Mule 4.9 runtime. MUnit runs on the Enterprise runtime, so Maven needs access to MuleSoft’s Enterprise repository (credentials in ~/.m2/settings.xml). The example runs with nothing external.
What each test in the suite proves, observed on Mule 4.9.17 with MUnit 3.7.1:
| Test | What it proves |
|---|---|
cf-invoicing-receives-only-the-fields-it-needs |
Invoicing receives exactly the allowed fields, flattened and with no calculated fields; card, contact, costs, suppliers and notes stay behind |
cf-field-added-to-the-order-later-does-not-leak |
A field the order gains later (loyaltyCardNumber) does not reach invoicing |
cf-order-without-authorization-returns-400-and-nothing-is-sent |
An order without an authorization code gets 400, and invoicing is not called |
cf-invoicing-has-no-request-for-an-unknown-order |
The simulated invoicing answers 404 for an order it did not receive |
To call the endpoint, run the application in Anypoint Studio (port 8081) and open requests.http in VS Code (REST Client) or IntelliJ. The first entry sends the paid order; the second reads what invoicing received:
### EIP content filter — invoicing gets only what it needs (no card, contact or cost data)
POST {{base}}/eip/billing/invoice-requests
Content-Type: application/json
{ "orderId": "ORD-3601", "customer": { "id": "CUS-100", "name": "Ana Lima", "email": "ana@example.com", "phone": "+55 11 99999-0000", "taxId": "123.456.789-09", "address": { "street": "Rua A, 1", "city": "Sao Paulo", "country": "BR" } }, "payment": { "method": "CARD", "authorizationCode": "AUTH-777", "card": { "number": "4111111111111111", "holder": "ANA LIMA", "expiry": "12/29" } }, "items": [ { "sku": "SKU-BOOK", "quantity": 2, "unitPrice": 30, "unitCost": 18, "supplier": "SUP-1" } ], "internalNotes": "VIP" }
### What invoicing received
GET {{base}}/eip/invoicing/invoices/ORD-3601
In the repository: requests.http
The other patterns, with the Mule component that implements each one, are in the catalog of integration patterns in Mule; the reverse path, from component to patterns, is in the index by component.