Search code examples
javascriptwso2apache-synapse

No setPayloadJSON() in Apache Synapse


I have been trying out Apache Synapse and been trying to use the JavaScript mediator to set the JSON payload. But the ScriptMessageContext class doesn't contain any method for reading a JSON payload or setting a JSON payload. But there has been numerous examples of setting and getting JSON Payload in WSO2 such as mc.getPayloadJSON(); and mc.setPayloadJSON(response). Is there anyway to workaroud this in Apache Synapse?


Solution

  • I found the answer for myself.Synapse process every data by converting it into soap.So first I needed to convert the flow flie to soap. Then processed the data using javascript and convert the script back to json using xslt. The complete xml code is shown below.Here I used synapse as proxy to send a get response from another server. then the data from that api is converted to soap and then processed in javascript and converted back from soap to json and send back to client.

    <definitions xmlns="http://ws.apache.org/ns/synapse">
    
        <localEntry key="jsonScript" src="file:repository/conf/sample/resources/script/sampleJson.js"/>
    
    
        <proxy name="SampeJsonProxy">
            <target>
                <endpoint>
                    <address uri="http://localhost:8081/kafka/publish/hello" format="json" methods="GET"/>
                </endpoint>
                <inSequence>
                    <log level="full"/>
                </inSequence>
                <outSequence>
    
                    <xslt key="in_transform"/>
                    <property name="messageType" scope="axis2" value="text/xml"/>
                    <script language="js" key="jsonScript" function="transformResponse"/>
                    <xslt key="out_transform"/>
                    <property name="messageType" scope="axis2" value="application/json"/>
                    <send/>
                </outSequence>
            </target>
        </proxy>
    
    
        <localEntry key="in_transform">
            <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                            xmlns:fn="http://www.w3.org/2005/02/xpath-functions"
                            xmlns:m0="http://services.samples" version="2.0" exclude-result-prefixes="m0 fn">
                <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
                <xsl:template match="*">
                    <xsl:element name="{local-name()}" namespace="http://services.samples">
                        <xsl:copy-of select="attribute::*"/>
                        <xsl:apply-templates/>
                    </xsl:element>
                </xsl:template>
            </xsl:stylesheet>
        </localEntry>
    
    
        <localEntry key="out_transform">
            <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
                <xsl:output method="xml" version="1.0" encoding="UTF-8"/>
                <xsl:template match="*">
                    <xsl:element name="{local-name()}">
                        <xsl:apply-templates/>
                    </xsl:element>
                </xsl:template>
            </xsl:stylesheet>
        </localEntry>
     
    </definitions>