Thursday, April 3, 2014

Using WSO2 ESB to modify messages with text content

WSO2 ESB provides 1st class support to SOAP and REST message formats. The WSO2 ESB is also capable of mediating messages with plain text contents. This can be done using a script mediator. You can read more about the script mediator from this link[1]. For this scenario I would be sending a text payload over HTTP. The ESB would extract the message payload and perform text manipulation using the script mediator and send the modified payload to the backend service. The script used in the script mediator is Javascripts. The following diagram illustrates the message flow.


Given below is a sample synapse configuration for the scenario. In the example below HTTP POST with a payload "Text send by Nadeesha" is sent to the ESB, which would be replaced as "Text from Nadeesha" and sent to the backend service.

<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="testProxy"
       transports="https,http"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
         <log level="full"/>
         <log>
            <property xmlns:m0="http://ws.apache.org/commons/ns/payload"
                      name="payload"
                      expression="$body/m0:text"/>
         </log>
         <property xmlns:m0="http://ws.apache.org/commons/ns/payload"
                   name="payload"
                   expression="$body/m0:text"/>
         <script language="js">var payload= mc.getProperty('payload').trim();var newPayload= payload.replace("send by","from");mc.setProperty("newPayload",newPayload);</script>
         <enrich>
            <source type="property" clone="true" property="newPayload"/>
            <target type="body"/>
         </enrich>
         <log level="full"/>
      </inSequence>
      <outSequence>
         <send/>
      </outSequence>
      <endpoint>
         <address uri="http://localhost/testEP"/>
      </endpoint>
   </target>
   <description/>
</proxy>


The same functionality can also be done using a class mediator where by a Java class can be written to perform the same task.

[1] https://docs.wso2.org/display/ESB481/Script+Mediator

No comments:

Post a Comment