I am writing a proxy service in WSO2 ESB that accepts a JSON payload and performs some transformations (to a SOAP message) that are quite complex, so we are writing the transformation logic in a Custom Mediator.
As you can see, my custom mediator class sets a property in the message context, and the proxy flow extracts this property and sets the payload (using a Javascript API that I could find).
This results in my SOAP message being "double wrapped" in two envelope tags, and I need to use an enricher with a XPath expression to remove the outer envelope/body tags.
Is it possible to set the XML payload within the custom mediator, thus avoiding to read a property and writing the XML payload in the proxy flow?
The proxy code is listed below:
<?xml version="1.0" encoding="UTF-8"?>
<proxy name="stackOverflowProxy" startOnLoad="true" trace="disable"
transports="jms" xmlns="http://ws.apache.org/ns/synapse">
<target>
<inSequence>
<!-- process incoming request in custom class. the payload is a JSON object -->
<class name="stackOverflow.CustomMediator"/>
<!-- set registration xml string as xml payload -->
<script language="js"><![CDATA[mc.setPayloadXML(new XML(mc.getProperty('mediatorPayload')));]]></script>
<enrich>
<source clone="true"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xpath="$body//soapenv:Envelope/soapenv:Body/*"/>
<target type="body"/>
</enrich>
</inSequence>
<outSequence/>
<faultSequence/>
</target>
</proxy>
And this is the Custom Mediator class:
package stackOverflow;
// imports ...
public class CustomMediator extends AbstractMediator {
public boolean mediate(MessageContext messageContext) {
//messageContext has a json object property
//message is processed and transformed to a SOAPEnvelope (namespaces omitted for simplicity)
//soapEnvelopeString = <Envelope><Body><tag>value</tag></Body></Envelope>
messageContext.setProperty("mediatorPayload", soapEnvelope.toString());
return true;
}
}