Monday, September 4, 2017

Convert XML payload to JSON payload in a WSO2 ESB Class Mediator

Bellow class mediator can access the XML payload and convert it to JSON message and set It as a payload.



package org.wso2.demo;

import org.apache.axiom.om.OMElement;
import org.apache.synapse.MessageContext;
import org.apache.synapse.commons.json.JsonUtil;
import org.apache.synapse.core.axis2.Axis2MessageContext;
import org.apache.synapse.mediators.AbstractMediator;
import org.json.JSONObject;

public class ConvertXmlToJSON  extends AbstractMediator {

    public boolean mediate(MessageContext context) {
        try {
            org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) context)
                    .getAxis2MessageContext();
            OMElement xmlResponse = context.getEnvelope().getBody().getFirstElement();
            xmlResponse.build();
            JSONObject jsonObject = new JSONObject(JsonUtil.toJsonString(xmlResponse).toString());

            //setting the payload as the message payload
            JsonUtil.getNewJsonPayload(axis2MessageContext, jsonObject.toString(), true, true);
        } catch (Exception e) {
            handleException("Error while converting the message", e, context);
        }
        return true;
    }
}

Working with JSON Message in WSO2 ESB Class Mediator

                 How to access the JSON payload from the class mediator

WSO2 ESB provides inbuilt mediators for mediating the messages. However when It comes to custom requirement, Class mediators are very useful. Using a java class mediator, you can do any custom modification to the message. Here this sample will demonstrate how to access the JSON message payload within class mediator and set a new JSON message as the payload.


package org.wso2.demo;

import org.apache.synapse.MessageContext;
import org.apache.synapse.commons.json.JsonUtil;
import org.apache.synapse.core.axis2.Axis2MessageContext;
import org.apache.synapse.mediators.AbstractMediator;
import org.json.JSONObject;

/**
 * This mediator will replace the element fName to name
 * {"fName" : "Nuwan"} --> {"name" : "Nuwan"}
 */

public class HandleJSONPayload extends AbstractMediator {

    public boolean mediate(MessageContext context) {
        try {
            org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) context)
                    .getAxis2MessageContext();
            JSONObject jsonPayload = new JSONObject(JsonUtil.jsonPayloadToString(axis2MessageContext));

            String name = jsonPayload.getString("fName");
            jsonPayload.remove("fName");
            jsonPayload.put("name", name);

            //setting the payload as the message payload
            JsonUtil.getNewJsonPayload(axis2MessageContext, jsonPayload.toString(), true, true);
        } catch (Exception e) {
            handleException("Error while mediating the message", e, context);
        }
        return true;
    }
}

     


You need to add a additional dependency to access the JSON classes.

 <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20090211</version>
 </dependency>