WSO2 Micro Integrator: Issue with using enrich mediator on JSON
By: Priyashani Fernando
Tags: Enrich Mediator, Cannot Be Cast To Omnode, Wso2 Micro Integrator, Wso2esb, Json

In a mediation flow, I’m working with I needed to first make a POST request to a REST JSON endpoint then make few other POST XML calls and enrich the first JSON payload with values extracted from the XMLs but when I use the enrich mediator I got this error.

org.apache.synapse.core.axis2.Axis2MessageContext cannot be cast to org.apache.axiom.om.OMNode

First what I did was try to do the enrich thing in the first sequence itself, since I don’t wanna make too many calls to the third party APIs and it got done successfully as below;

<property name=”bundleProvisioned” scope=”default” type=”STRING” expression=”fn:concat(‘{"bundleProvisioned":"’, ‘true’, ‘"}’)”/> <enrich> <source clone=”true” property=”bundleProvisioned” type=”property”/> <target action=”child” xpath=”json-eval($)”/> </enrich>

You must be thinking I have used fn:concat for no reason and I could just put the whole thing in a value attribute, But no! That’s the first way I tried and it didn’t really work, this on the other hand worked.

But when I put this in a place that I wanted, it started giving the same error again.

So what I thought was probably there’s some header or something in the air(I mean mediation flow) after I invoke a call to an XML REST endpoint which is disrupting the peace in my coding.

So the first thing I could think of was content type! So I added messageType and Content-type after I set the JSON payload back to the body as below.

<property name=”bundleProvisioned” scope=”default” type=”STRING” expression=”fn:concat(‘{"bundleProvisioned":"’, ‘true’, ‘"}’)”/> <enrich> <source clone=”false” property=”ORIGINAL_PAYLOAD” type=”property”/> <target type=”body”/> </enrich> <property name=”messageType” value=”application/json” scope=”axis2"/> <property name=”ContentType” value=”application/json” scope=”axis2"/> <enrich> <source clone=”true” property=”bundleProvisioned” type=”property”/> <target action=”child” xpath=”json-eval($)”/> </enrich>

You guessed right that didn’t work as well. I also tried adding below properties right before the above code snipped, that too was of no use.

<property name=”NO_ENTITY_BODY” value=”true” scope=”axis2" type=”BOOLEAN”/> <property name=”TRANSPORT_HEADERS” action=”remove” scope=”axis2"/>

Anyway in the final code, I ended up just setting the extracted values from XML payload to properties. And at the end of the mediation enriched the payload with the properties as below.

<payloadFactory media-type=”json”> <format>{ “Test”:”this is used to avoid an error” }</format> <args/> </payloadFactory> <enrich> <source clone=”false” property=”ORIGINAL_PAYLOAD” type=”property”/> <target type=”body”/> </enrich> <property name=”bundleProvisioned” scope=”default” type=”STRING” expression=”fn:concat(‘{"bundleProvisioned":"’, $ctx:provisioned, ‘"}’)”/> <enrich> <source clone=”true” property=”bundleProvisioned” type=”property”/> <target action=”child” xpath=”json-eval($)”/> </enrich>

So ‘provisioned’ is the property I create with just the value in the sequence which invokes the XML endpoint. And before I set the body with the property which contains the JSON I made a simple valid JSON using a payload factory, I’m not exactly sure what it does but this solves the issue I have and gives me the JSON response out of the mediation.