Convert JSON body using WSO2 broker

Below is the log of my current json body. And I want to add a new property to this body. "NewPropertyName": "value" . Since this value is in the database, I use the class proxy to add this property.

 [2015-05-18 05:47:08,730] INFO - LogMediator To: /a/create-project, MessageID: urn:uuid:b7b6efa6-5fff-49be-a94a-320cee1d4406, Direction: request, _______BODY_______ = { "token": "abc123", "usertype":"ext", "request": "create" } 

Class mediation method,

 public boolean mediate(MessageContext mc) { mc.setProperty("key", "vale retrived from db"); return true; } 

but it doesn’t work as I expected. I could not find any guidance on adding a property to the json body using the class broker, please help.

+5
source share
2 answers

To enter a property into the body, you must use the following code snippet,

 JsonUtil.newJsonPayload( ((Axis2MessageContext) context).getAxis2MessageContext(), transformedJson, true, true); 

inside the class mediator. The following is an example of a proxy method.

 /** * Mediate overridden method to set the token property. */@Override public boolean mediate(MessageContext context) { try { // Getting the json payload to string String jsonPayloadToString = JsonUtil.jsonPayloadToString(((Axis2MessageContext) context) .getAxis2MessageContext()); // Make a json object JSONObject jsonBody = new JSONObject(jsonPayloadToString); // Adding the name:nameParam. jsonBody.put("name", getNameParam()); String transformedJson = jsonBody.toString(); // Setting the new json payload. JsonUtil.newJsonPayload( ((Axis2MessageContext) context).getAxis2MessageContext(), transformedJson, true, true); System.out.println("Transformed JSON body:\n" + transformedJson); } catch (Exception e) { System.err.println("Error occurred: " + e); return false; } return true; } 

For this you will need json and other libraries. This is fully explained in the next blog post.

json-support-for-wso2-esb-class-mediator

+6
source

mc.setProperty is used to create a new property, as if you were using a property broker.

If you want to add a new element inside your message, in java you can process it as if it were an XML message (for example, to get the first element:
OMElement element = (OMElement) context.getEnvelope().getBody().getFirstOMChild(); )

An example of adding a new element using javascript:

 <script language="js"><![CDATA[ var payloadXML = mc.getPayloadXML(); payloadXML.appendChild(new XML(<NewPropertyName>value</NewPropertyName>)); mc.setPayloadXML(payloadXML); ]]></script> 

Record the message in XML format with <log level="full"> and you will get:

 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Body> <jsonObject> <token>abc123</token> <usertype>ext</usertype> <request>create</request> <NewPropertyName>value</NewPropertyName> </jsonObject> </soapenv:Body> </soapenv:Envelope> 

Write the message in JSON using

 <log> <property name="JSON-Payload" expression="json-eval($.)"/> </log> 

and you will receive: JSON-Payload = {"token":"abc123","usertype":"ext","request":"create","NewPropertyName":"value"}

0
source

All Articles