In this scenario, we will look at how WSO2 ESB handles message transformation. This scenario contains the following sections:
Transformation is necessary when the message format sent by the client is different from the message format expected by the back-end service. The Message Translator architectural pattern in WSO2 ESB describes how to translate from one data format to another.
For example, let’s assume this is the format of the request sent by the client:
However, the format of the message must be as follows to be compatible with SimpleStockQuoteService:
Furthermore, following is the format of the response expected by the client:
Transformation is required for both the request and response messages. The client message format must be transformed to the back-end service message format within the In sequence, and the opposite must be done within the Out sequence.
Updating the deployable artifacts
We will use the PayloadFactory mediator to transform the message within the In and Out sequences. The PayloadFactory mediator transforms or replaces the contents of the request message so that it is in the format expected by the back-end service. Let’s take a look at how this is done in WSO2 Developer Studio.
First we need to update the Log mediators that are within the Switch mediator so that they are able to extract the log values from the new request format. In the Log mediators, update the XPath expression on the Properties tab as follows:
For the French Message Log mediator in Case, update the XPath expression to:
- For the Welcome Message Log mediator in Default, update the XPath expression to:
Notice that we are not using the
m1namespace in the new expressions, so you can remove the
m1namespace definitions in both of these Log mediators.
- Add a PayloadFactory mediator just after the Switch mediator in the In sequence of the proxy service.
Go to the properties of this PayloadFactory mediator and fill in the details as follows:
Payload Format: Inline
- Payload (the XML format for the message payload):
We are using the PayloadFactory mediator to specify the transformed format for the incoming message. In the Payload property, we have indicated that we need to add a variable to this message format by using $1 in the above XML.
Click the ... button and enter the expression:
Here we are defining the value of the variable in the format definition. This value is obtained from the request message using the above expression. Because we're using a namespace in the expression, add the following namespace:
The properties for the PayloadFactory mediator should now look like this:
Add another PayloadFactory mediator to the Out sequence of the proxy service, just before the Send mediator, to transform the response message to the format expected by the client.
Go to the properties of this second PayloadFactory mediator and fill in the details as follows:
Payload Format: Inline
- Args (note that you are creating two arguments here):
Type (for both arguments): Expression
- Value for the first argument:
- Value for the second argument:
- Evaluator (for both arguments):
- Namespaces (for both arguments):
Keep the rest of the fields as they are and save the proxy service.
We are now ready to redeploy the C-App and send the request.
Redeploying and sending the request in SoapUI
Redeploy SampleCApp in your server as you did in the previous lessons.
In SoapUI, go to Request 1, delete the default content of the request, and then copy and paste the following request there.
Send the request. You will get a response similar to the following:
We have now explored how WSO2 ESB can take a message in one format, transform it into the format expected by the back-end service, and then transform the response back to the client’s required format. In the next scenario, you will learn how to monitor the ESB and debug using the management console.