Normalizer Message Transformation Pattern With WSO2 ESB

Introduction

The Normalizer is used in scenario where ESB has to mediate different types of message formats and get the service of a certain endpoint service which support only a single format of messages. In a business-to-business (B2B) integration scenario it is quite common for an enterprise to receive messages from different business partners. These message may have the same meaning, but follow different formats, depending on the partners’ internal systems and preferences. For example, we built a solution for a pay-per-view provider that has to accept and process viewership information from over 1700 (!) affiliates, most of which did not conform to a standard format.

Therefore ESB needs to identify those formats and send the responses back in the same format. In such case client application and back-end service do not have to worry about message type because Message Builders and Message Formatters in WSO2 ESB do that on behalf of them.

Implementation Using the WSO2 ESB

Prerequisites
  • Start the WSO2 ESB with the following configuration
  • Start the Axis2 server and deploy the SimpleStockQuoteService if not already deployed.
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://ws.apache.org/ns/synapse">

<!-- The proxy service to receive all kinds of messages -->
<proxy name="ServiceProxy" transports="https http" startOnLoad="true" trace="disable">
<target>
<inSequence>
<log level="full"/>

<!-- Filters incoming JSON messages -->
<filter xmlns:m0="http://services.samples" xpath="//m0:getQuote/m0:request/m0:symbol">
<then>
<sequence key="sendSeq"/>
</then>
<else>
<sequence key="jsonInTransformSeq"/>
</else>
</filter>

</inSequence>

<outSequence>

<!-- Filters outgoing JSON messages -->
<filter source="get-property('TRANSFORMATION')" regex="JSONtoSOAP">
<then>
<property name="messageType" value="application/json" scope="axis2" type="STRING"/>
</then>
</filter>

<log level="full"/>
<send/>

</outSequence>

</target>
</proxy>

<localEntry key="in_transform">
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/02/xpath-functions"
xmlns:m0="http://services.samples"
version="2.0"
exclude-result-prefixes="m0 fn">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="*">
<xsl:element name="{local-name()}" namespace="http://services.samples">
<xsl:copy-of select="attribute::*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
</localEntry>

<!-- Transform a JSON message -->
<sequence name="jsonInTransformSeq">
<xslt key="in_transform"/>
<property name="TRANSFORMATION" value="JSONtoSOAP" scope="default" type="STRING"/>
<sequence key="sendSeq"/>
</sequence>

<!-- Normal flow of messages -->
<sequence name="sendSeq">
<send>
<endpoint>
<address uri="http://localhost:9000/services/SimpleStockQuoteService" format="soap11"/>
</endpoint>
</send>
</sequence>

<sequence name="fault">
<log level="full">
<property name="MESSAGE" value="Executing default 'fault' sequence"/>
<property xmlns:ns="http://org.apache.synapse/xsd" name="ERROR_CODE" expression="get-property('ERROR_CODE')"/>
<property xmlns:ns="http://org.apache.synapse/xsd" name="ERROR_MESSAGE" expression="get-property('ERROR_MESSAGE')"/>
</log>
<drop/>
</sequence>

<sequence name="main">
<log/>
<drop/>
</sequence>

</definitions>

In the above synapse configuration first filter-out JSON messages to do necessary transformation and then add to the normal message flow since back-end service only know SOAP. In out sequence also it filters the JSON messages separately in-order to do necessary transformation. Major part of these transformation is done at code level. You can test this configuration for JSON, SOAP, POX messages by using sample axis2client shipped with WSO2ESB.

For SOAP

ant stockquote -Dtrpurl=http://localhost:8280/services/ServiceProxy

For POX – In here the XML message contains exactly the format that the service wants. Else you need to use Payload Factory Mediator to create required message from incoming message.

ant stockquote -Dtrpurl=http://localhost:8280/services/ServiceProxy

For JSON

ant jsonclient -Daddurl=http://localhost:8280/services/ServiceProxy

References

[1] WSO2 ESB – http://wso2.com/products/enterprise-service-bus/

[2] ESB Samples – http://docs.wso2.org/wiki/display/ESB450/Samples

[3] Normalizer EAI pattern – http://www.eaipatterns.com/Normalizer.html

Advertisements

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