WSO2 ESB Enrich mediator

Posted: February 5, 2014 in Uncategorized

WSO2 ESB enrich mediator process the incoming message with the given source configuration and apply given actions based on the given target configuration.

Use cases : Alter a value of the incoming message

Adding a new part to the message

Adding a property to the message

Store message/part of the message in a property to it can be referred later

Source has different parameters.

Clone – If true message is clone, create another copy of the message. If false no copy is created, use a reference to the original message. If you want to apply the Enrich mediator and at the same time you want to send the original message to an endpoint, set clone as true. Otherwise the reference will be null when the incoming message is sent to the endpoint. Clone is set to false by default.

type –  specifies the part of the message that will be processed by the mediator.

  • Body – body of the message is processed.
  • Envelope- envelop of the message is processed
  • Property –  a property is processed
  • Custom –  We can define a custom type, a XPath.
  • Inline – You can specify a inline xml of yours.

Target configurations

Action – The action to be performed. Replace is the default action.

  • Replace – Replace with the source. This is the default action.
  • Chile – Add source as a xml child itel
  • Sibling – Add source as a xml sibling item

type – To which part of the message the action is applied. Like in source configurations we can select body,envelope, property, custom and inline.

Example 1 :Alter a value in request message

Let’s consider the very much familiar getQuote example of  SimpleStockQuote where we get the stock quotes for a given company (SUN,IBM etc..).

Sample request to get quotes of the IBM company would be as below.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.samples" xmlns:xsd="http://services.samples/xsd">
<soapenv:Header/>
<soapenv:Body>
<ser:getQuote>
<!--Optional:-->
<ser:request>
<!--Optional:-->
<xsd:symbol>IBM</xsd:symbol>
</ser:request>
</ser:getQuote>
</soapenv:Body>
</soapenv:Envelope>

However let’s say we want to change the symbol value to SUN whatever the request symbol value is. So we always get the quotes for SUN company. We can use the enrich mediator to alter the

incoming message.

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse" name="Enrich" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
<target>
<inSequence>
<log level="full"/>

<enrich xmlns="http://ws.apache.org/ns/synapse">
<source clone="true" type="inline">
<xsd:symbol xmlns:xsd="http://services.samples/xsd">SUN</xsd:symbol>
</source>
<target xmlns:xsd="http://services.samples/xsd" xmlns:ser="http://services.samples" action="replace" type="custom" xpath="//ser:getQuote/ser:request/xsd:symbol"></target>
</enrich>

<log level="full"/>
</inSequence>
<endpoint>
<address uri="http://localhost:8080/axis2/services/SimpleStockQuoteService"/>
</endpoint>
</target>
<description/>
</proxy>

Above proxy will Replace the symbol value with the SUN beofore sending the message to the endpoint. Below is the log when the proxy is called. You can see the symbol value of the incoming message IBM is replaced with SUN. The logger mediator before the enrich mediator show the symbol as IBM, but after the enrich mediator symbol has changed to SUN.

[2014-02-03 03:48:39,215] INFO – LogMediator To: /services/Enrich, WSAction: urn:getQuote, SOAPAction: urn:getQuote, MessageID: urn:uuid:607c2842-a7fc-4de3-84d8-3853c3e32134, Direction: request, Envelope: <?xml version=”1.0″ encoding=”utf-8″?><soapenv:Envelope xmlns:soapenv=”http://schemas.xmlsoap.org/soap/envelope/&#8221; xmlns:xsd=”http://services.samples/xsd&#8221; xmlns:ser=”http://services.samples”><soapenv:Body&gt;
<ser:getQuote>
<!–Optional:–>
<ser:request>
<!–Optional:–>
<xsd:symbol>IBM</xsd:symbol>
</ser:request>
</ser:getQuote>
</soapenv:Body></soapenv:Envelope>
[2014-02-03 03:48:39,273] INFO – LogMediator To: /services/Enrich, WSAction: urn:getQuote, SOAPAction: urn:getQuote, MessageID: urn:uuid:607c2842-a7fc-4de3-84d8-3853c3e32134, Direction: request, Envelope: <?xml version=”1.0″ encoding=”utf-8″?><soapenv:Envelope xmlns:soapenv=”http://schemas.xmlsoap.org/soap/envelope/&#8221; xmlns:xsd=”http://services.samples/xsd&#8221; xmlns:ser=”http://services.samples”><soapenv:Body&gt;
<ser:getQuote>
<!–Optional:–>
<ser:request>
<!–Optional:–>
<xsd:symbol>SUN</xsd:symbol>
</ser:request>
</ser:getQuote>
</soapenv:Body></soapenv:Envelope>

Example 2 : Add a text to the message


In the earlier example 1, we set the action as Replace so the original value will be replaced with the given text. If we set action as Sibling, the given text will be added to the original message.


<enrich xmlns="http://ws.apache.org/ns/synapse">
<source clone="true" type="inline">
<xsd:symbol xmlns:xsd="http://services.samples/xsd">SUN</xsd:symbol>
</source>
<target xmlns:xsd="http://services.samples/xsd" xmlns:ser="http://services.samples" action="sibling" type="custom" xpath="//ser:getQuote/ser:request/xsd:symbol"></target>
</enrich>

Output before and after the enrish mediator.

[2014-02-03 06:10:22,589] INFO – LogMediator To: /services/Enrich, WSAction: urn:getQuote, SOAPAction: urn:getQuote, MessageID: urn:uuid:915ae481-31e6-4485-b9ef-9ae641a444c4, Direction: request, Envelope: <?xml version=”1.0″ encoding=”utf-8″?><soapenv:Envelope xmlns:soapenv=”http://schemas.xmlsoap.org/soap/envelope/&#8221; xmlns:xsd=”http://services.samples/xsd&#8221; xmlns:ser=”http://services.samples”><soapenv:Body&gt;
<ser:getQuote>
<!–Optional:–>
<ser:request>
<!–Optional:–>
<xsd:symbol>IBM</xsd:symbol>
</ser:request>
</ser:getQuote>
</soapenv:Body></soapenv:Envelope>
[2014-02-03 06:10:22,590] INFO – LogMediator To: /services/Enrich, WSAction: urn:getQuote, SOAPAction: urn:getQuote, MessageID: urn:uuid:915ae481-31e6-4485-b9ef-9ae641a444c4, Direction: request, Envelope: <?xml version=”1.0″ encoding=”utf-8″?><soapenv:Envelope xmlns:soapenv=”http://schemas.xmlsoap.org/soap/envelope/&#8221; xmlns:xsd=”http://services.samples/xsd&#8221; xmlns:ser=”http://services.samples”><soapenv:Body&gt;
<ser:getQuote>
<!–Optional:–>
<ser:request>
<!–Optional:–>
<xsd:symbol>IBM</xsd:symbol><xsd:symbol>SUN</xsd:symbol>
</ser:request>
</ser:getQuote>
</soapenv:Body></soapenv:Envelope>

 

Example 3 : Store message body into a property

<enrich>
 <source type="body" clone="true"/>
 <target type="property" action="child" property="REQUEST_PAYLOAD"/>
 </enrich>

Above enrich mediator will store the body of the message into a property named REQUEST_PAYLOAD. Note that there clone

Example 4 : Attach the request message to the response message

This is a extends of the Example 3. Here inside In Sequence the content of the body is stored in the property called REQUEST_PAYLOAD so we can refer to that property to get the content of the request body. There is another enrich mediator which add the content(since action is sibling) of the property REQUEST_PAYLOAD to the body of the messageSo now the response to the client should be the response from the back end plus the request message

Note that inside the in sequence body refers to the body of the incoming request message, but inside out sequence the body refers to the response from the client. 

 

<proxy xmlns="http://ws.apache.org/ns/synapse"
name="Enrich"
transports="https,http"
statistics="disable"
trace="disable"
startOnLoad="true">
<target>
<inSequence>
<log level="full"/>
<enrich>
<source type="body" clone="true"/>
<target type="property" property="REQUEST_PAYLOAD"/>
</enrich>
<log level="full"/>
</inSequence>
<outSequence>
<log level="full"/>
<enrich>
<source type="property" property="REQUEST_PAYLOAD"/>
<target type="body" action="sibling"/>
</enrich>
<send/>
</outSequence>
<endpoint>
<address uri="http://localhost:8080/axis2/services/SimpleStockQuoteService"/>
</endpoint>
</target>
<description/>
</proxy>
Advertisements
Comments
  1. roshan says:

    well nicely explained !!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s