Monday, September 4, 2017

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>

No comments:

Post a Comment