Wednesday, February 17, 2016

[WSO2 ESB]Invoke Contentful API

Lets say we want to invoke Contentful API which contains the response with Content-Type:application/vnd.contentful.delivery.v2+json

1. First we tried setting the Accept header before send the message. But that didn't change the Content-Type of the  server response.
<header name="Accept" scope="transport" value="application/json"/>
2. The second attempt was to set the BinaryRelayBuilder and ExpandingMessageFormatter in the axis2.xml file under <messageBuilders> and <messageFormatters> sections.
<messageBuilder class="org.wso2.carbon.relay.BinaryRelayBuilder" contentType="application/vnd.contentful.delivery.v2+json"/>
<messageFormatter class="org.wso2.carbon.relay.ExpandingMessageFormatter" contentType="application/vnd.contentful.delivery.v2+json"/>
Now the message building error solved. But the response contains in binary format.
Eg:
<ns:binary xmlns:ns="http://ws.apache.org/commons/ns/payload">-------CAgIH0KICAgICAgICBdCiAgICAg--------</ns:binary>
3. Finally we used the default json builder and formatter with our contentful media type(application/vnd.contentful.delivery.v2+json) to resolve the issue. (make sure to remove the configuration added in above 1 and 2 steps)
Eg:
<messageBuilder class="org.apache.synapse.commons.json.JsonStreamBuilder" contentType="application/vnd.contentful.delivery.v2+json"/>
<messageFormatter class="org.apache.synapse.commons.json.JsonStreamFormatter" contentType="application/vnd.contentful.delivery.v2+json"/>

Thursday, February 11, 2016

[WSO2 ESB] How to remove default namespace at message enrich.

1.Lets say we need to add new element (eg: foo) to the message body using enrich mediator.

<enrich>
 <source type="inline" clone="true">
 <foo>test</foo>
</source>
<target type="body" action="child"/>
</enrich>
2. After enriched, the  message will be as bellow.
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
    <soapenv:Body>
        <foo xmlns="http://ws.apache.org/ns/synapse">test</foo>
    </soapenv:Body>
</soapenv:Envelope>
3. You can see foo element contains with default synapse namespace.
4. You can avoid this adding empty namespace (xmlns="") to foo element in enrich mediator configuration.
<enrich>
 <source type="inline" clone="true">
 <foo xmlns="">test</foo>
</source>
<target type="body" action="child"/>
</enrich>

Friday, February 5, 2016

[WSO2 ESB] How to remove jsonObject root element from message body

Eg: Sample SOAP message

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soap:Header/>
    <soap:Body>
        <jsonObject>
            <foo>
                <aaa>Ajith</aaa>
                <bbb>Vitharana</bbb>
            </foo>
        </jsonObject>
    </soap:Body>
</soap:Envelope>

Eg: Sample ESB configuration.

<enrich>
        <source clone="true" xpath="$body/jsonObject/foo"/>
        <target type="body"/>
</enrich>

source : https://docs.wso2.com/display/ESB480/JSON+Support#JSONSupport-XMLrepresentationofJSONpayloads