Archive for February, 2014

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
This turotial explains how to run multiple ActiveMQ brokers within the same machine.
Prerequisites
Download ActiveMQ packs from http://activemq.apache.org/download.html
Below are the minimum commands we required for this set up.
create                   – Creates a runnable broker instance in the specified path.

setup                    –  Create the specified configuration file for this init script

You can view all the available commands by executing  bin/activemq

Step 1: Create broker 1 and 2

cd $active_mq_installation_folder // this is where you extract the apache-activemq-x.x.x.tar.gz
 
#create a runnable broker called “broker1”
bin/activemq create broker1
# Create the configuration file for the init script of broker1.
bin/activemq setup ~/.activemqrc-instance-broker1
#create a symbolic link
ln -s activemq bin/activemq-instance-broker1
 
#create the broker2
bin/activemq create broker2
bin/activemq setup ~/.activemqrc-instance-broker2
ln -s activemq bin/activemq-instance-broker2
Step 2: Change ports

grep 16 broker[12]/conf/*.xml

broker1/conf/activemq.xml: <transportConnector name=”openwire” uri=”tcp://0.0.0.0:61616?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600″/>
broker1/conf/activemq.xml: <transportConnector name=”stomp” uri=”stomp://0.0.0.0:61613?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600″/>
broker1/conf/activemq.xml: <transportConnector name=”ws” uri=”ws://0.0.0.0:61614?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600″/>
broker1/conf/jetty.xml: <property name=”port” value=”8161″/>
broker1/conf/jetty.xml: <property name=”port” value=”8162″ />
broker2/conf/activemq.xml: <transportConnector name=”openwire” uri=”tcp://0.0.0.0:61616?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600″/>
broker2/conf/activemq.xml: <transportConnector name=”stomp” uri=”stomp://0.0.0.0:61613?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600″/>
broker2/conf/activemq.xml: <transportConnector name=”ws” uri=”ws://0.0.0.0:61614?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600″/>
broker2/conf/jetty.xml: <property name=”port” value=”8161“/>
broker2/conf/jetty.xml: <property name=”port” value=”8161” />

You can see that both broker1 and broker2 has same configurations. For instance both have the tcp runs on port 61616. This will be a problem when we start the broker2 since broker1 is already running on port 61616.
Also we need to edit the webConsole port(which is by default 8162) in one of the brokers. This is the port the web console runs. You can point to http://localhost:8161 to view the ActiveMQ web console.
So we need to edit either broker1 or broker2 configurations in a way that same port number does not appear in both the brokers.
Start the multiple instances
Start broker 1:
sh broker1/bin/broker1 console
Start broker 2 (in another console):
sh broker2/bin/broker2 console
You may see the logs printing in the console.
Locate your browser to https://localhost:8161 to view the web console. This port (8161) is configured in conf/jetty.xml
broker1