Forceful JSON Convertor with WSO2 ESB

Hi all, I am writing this blog after a long time. I am trying to address a one use case I could find when  I was working on a customer issue.Use case is as follows.Forceful JSON Convertor

As show in the above figure , There is a client that sends a any type of message to the ESB and then to the Back end. and Then Back end sends a message with type other than JSON. And there is a use case user wants to access the incoming payload from the Back end in the Script Mediator. using

var payload=mc.getPayloadJSON();

This works perfectly when Backend sends messages of type application/json. If Backend sends a message of any type other than application/json ,  above line of code cannot be used to access the JSON payload inside the script mediator.

i wrote the following custom mediator [1]  to convert the payload into JSON and set it as a property in the message context. So it can be accessed in the script mediator and do what ever you want to do with the payload :).

Instructions are as follows.

How to Use

  1. Download Jar [2]  and copy it to CARBON_HOME/repository/components/lib.
  2. Change the Builder and Formatter for application/json in CARBON_HOME/repository/conf/axis2/axis2.xml to

WSO2 ESB 4.7.0

org.apache.axis2.json.JSONStreamFormatter

org.apache.axis2.json.JSONStreamBuilder

WSO2 ESB 4.8.1

org.apache.synapse.commons.json.JsonStreamFormatter

org.apache.synapse.commons.json.JsonStreamBuilder

Then in the out sequence use the following synapse config

 
<outSequence>

<class name="org.wso2.carbon.esb.forceful.json.ForcefulJsonConvertor"/>

<script language="js">
 
var json=mc.getProperty("FORCE_BUILT_JSON");

mc.setPayloadJSON(json);
 
</script>

<property name="messageType" value="application/json" scope="axis2" type="STRING"/>

<send/>

</outSequence>
 

This can be used in the in sequence as well only if the incoming message type is other than application/json. Because if the incoming message type is application/json , getPayloadJSON() can be used to get the json payload in the script mediator. Hope this may be a help .

[1] https://github.com/asanka88/ForcefulJsonConvertor.git

[2]https://github.com/asanka88/ForcefulJsonConvertor/blob/master/Artifact_24-11-2014/ForceJsonConvertor-1.0.0.jar   

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