Enrich component
The Enrich component lets you take a 'sample' of a message in your flow and enrich it with another set of data or files.
Configuration
The Enrich component has the following configuration options:
File Type
Type of merging you want to do. See the Using Enrich section below for an example per File Type.
Options
XML
(default)JSON
Override
Add as attachment
Add to zip
Use error route?
If disabled, failed exchanges won't go into the error route. This option is only available when File Type is set to Override.
Using Enrich
An Enrich component has one input and two outputs. When the Enrich input receives a message this takes place:
- The bottom output starts a new 'thread' with a sample of the message (body and headers) entering the input.
- The bottom flow is followed and the result is returned to the Enrich component. During this process the right side output waits.
- The component enriches the input with this result (following the File Type setting) and sends this enriched message out the right side output.
The component will wait for a response from the bottom part of the flow before continuing the flow on the right side output.
The sampled message runs in its own separate 'thread' and doesn't affect the main flow. Therefore, headers that are set and/or received on the bottom output flow do not persist on the right side output (except when File Type is set to Override).
Simple JSON example
In the example below both Velocity components contain JSON data and File Type is set to JSON.
Example JSON data
{
"original": {
"first_element": "something",
"second_element": "else"
}
}
{
"new": {
"element_one": "this",
"element_two": "that"
}
}
[
{
"original": {
"first_element": "something",
"second_element": "else"
}
},
{
"new": {
"element_one": "this",
"element_two": "that"
}
}
]
The Enrich component combines the two JSON files into one array [...].
For this enrichment to function properly both the input and bottom result JSON have to be valid. The bottom result JSON can't start with an array in its root.
Advanced XML Example
Below is a more advanced example of an Enrich use case where File Type is set to XML. In this case XML order data is enriched with XML data from a SOAP call, this enrichment is transformed and sent to an HTTP endpoint.
In this example:
- A body with XML is received on an Inbound HTTP component.
- This XML is enriched with the response of a SOAP call:
- First the received XML is transformed with an XSLT component to build the SOAP request.
- This 'transformed' body is sent to a SOAP endpoint with the SOAP component.
- The SOAP response XML is returned to the Enrich component that has been waiting for this response.
- The enriched XML is transformed with an XSLT component in the top part of the flow.
- This transformed body is posted to an HTTP endpoint with the HTTP component.
Example XML data
<?xml version="1.0"?>
<inbound_http_order>
<order_id>1234</order_id>
<order_date>25-11-2023</order_date>
<order_contact>John Doe</order_contact>
<order_contact_id>9874</order_contact_id>
</inbound_http_order>
<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/...">
<SOAP-ENV:Body xmlns:m="http://www.xyz.org/orders">
<m:GetOrder>
<m:OrderId>1234</m:OrderId>
</m:GetOrder>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/...">
<SOAP-ENV:Body xmlns:m="http://www.xyz.org/orders">
<m:GetOrderResponse>
<m:OrderId>1234</m:OrderId>
<m:OrderDeliveryDate>26-11-2023</m:OrderDeliveryDate>
<m:OrderCompany>A Company</m:OrderCompany>
<m:OrderCompanyId>6543</m:OrderCompanyId>
</m:GetOrderResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
<?xml version="1.0" encoding="UTF-8"?>
<Enriched>
<inbound_http_order>
<order_id>1234</order_id>
<order_date>25-11-2023</order_date>
<order_contact>John Doe</order_contact>
<order_contact_id>9874</order_contact_id>
</inbound_http_order>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/...">
<SOAP-ENV:Body xmlns:m="http://www.xyz.org/orders">
<m:GetOrderResponse>
<m:OrderId>1234</m:OrderId>
<m:OrderDeliveryDate>26-11-2023</m:OrderDeliveryDate>
<m:OrderCompany>A Company</m:OrderCompany>
<m:OrderCompanyId>6543</m:OrderCompanyId>
</m:GetOrderResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
</Enriched>
The Enrich component combines the two XML by wrapping them into one element called Enriched
.
For this enrichment to function properly both the input and bottom result XML have to be valid.
<?xml version="1.0" encoding="UTF-8"?>
<NewOrder>
<id>1234</id>
<date>25-11-2023</date>
<delivery_date>26-11-2023</delivery_date>
<contact>John Doe</contact>
<company>A Company</company>
<contact_id>9874</contact_id>
<company_id>6543</company_id>
</NewOrder>
The XSLT component transforms the enriched XML into a new format that's sent to the HTTP endpoint.
To learn how to make an XSLT with MapForce read this tutorial
Override type
When File Type is set to Override you force the Enrich component to override the input with the result of the bottom output flow. The result body that overrides the original input body can be any filetype because it is read as a binary.
A use case for this is:
- A request is received on an Inbound HTTP component (with Exchange pattern set to Request reply).
- An external PDF file is fetched in the bottom output flow of the Enrich component.
- This PDF overrides the original body, returns it to the Inbound HTTP component that responds to the original request.
Disable the Use error route?
property to avoid the flow entering the error route when the result of the Enrich is an empty body.
Add as attachment
This option is primarily used to send (multiple) attachments with the Email component. Imagine there's already a text or html body for your email, but you need to get and/or add some attachments.
Below is an example of adding attachments to an email with the File Type set to Add as attachment.
The Enrich component uses Multipart to add attachments to an existing body to ensure they are seperate attachments in an email. It is important to know that after you have used the Enrich with "Add as attachment" the body is transformed into Multipart.
With a SetHeaders component you can specify the CamelFileName header to set the filename of the email attachment. In general the type of attachment is set accordingly, set the Content-Type header to override it.
Add to zip
This option allows you to enrich an existing zip archive with a new body. Keep in mind while using this option:
- There has to be a zip archive in your body before the enrichment, otherwise a new body cannot be added.
- Specify the target location in the zip archive with the CamelFileName header.
- A binary body can be added.
Every file that is added to the zip archive needs to have a CamelFileName header. Set it with a SetHeaders component when necessary. Make sure to add the correct extension for the filetype of the body.
Below is an example of an Enrich use case where File Type is set to Add to zip.
In this example:
- A SetHeaders component sets the CamelFileName header of the current body, i.e. order.xml or contacts.json.
- An archive is created with the Archive component.
- The first Enrich component enriches the archive with a Velocity component body. In it's bottom output flow:
- A SetHeaders component sets the CamelFileName for the body in the Velocity that follows.
- The result is returned to the Enrich component that adds it to the archive.
- The second Enrich component enriches the archive with a file that is fetched from an FTP server:
- Files fetched with the FTP component already have a CamelFileName header, it doesn't need to be specified.
- The final FTP component writes the archive to an FTP server. Use the FTP File Name option to define the filename of the archive.
Use this option in combination with an Enrich component File Type that's set to Override if you want to get a zip archive from an external location and add bodies to that archive.