Last active
October 16, 2017 22:53
-
-
Save vanjikumaran/a678dd4921ce66912fa30d0e0cde3785 to your computer and use it in GitHub Desktop.
Synapse configuration to connect with tcp socket via scripting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<api xmlns="http://ws.apache.org/ns/synapse" name="TcpConnectionAsAPI" context="/tcp"> | |
<resource methods="POST GET" url-mapping="/connection"> | |
<inSequence> | |
<script language="groovy">def myValue='value';def socket = new Socket('localhost', 4444); myValue = socket.withStreams { input, output -> buffer = input.newReader().readLine(); return buffer;}; mc.setProperty('ValueInGroovyScript',myValue);</script> | |
<log> | |
<property name="Value from TCP Server" expression="$ctx:ValueInGroovyScript" /> | |
</log> | |
<script language="js">var resp = String(mc.getProperty("ValueInGroovyScript")).split(','); var payloadXML = new XML(<response/>); var resp = String(mc.getProperty("ValueInGroovyScript")).split(','); payloadXML.appendChild(new XML(<resp>{resp[0]}</resp>)); payloadXML.appendChild(new XML(<company>{resp[1]}</company>)); payloadXML.appendChild(new XML(<itemnbr>{resp[2]}</itemnbr>)); payloadXML.appendChild(new XML(<description1>{resp[3]}</description1>)); payloadXML.appendChild(new XML(<description2>{resp[4]}</description2>)); payloadXML.appendChild(new XML(<brand>{resp[5]}</brand>)); mc.setPayloadXML(payloadXML); mc.setProperty('resp',resp[0]); mc.setProperty('company',resp[1]); mc.setProperty('itemnbr',resp[2]); mc.setProperty('description1',resp[3]); mc.setProperty('description2',resp[4]); mc.setProperty('brand',resp[5]); | |
</script> | |
<log level="full" /> | |
<property name="messageType" value="application/json" scope="axis2" /> | |
<payloadFactory media-type="json"> | |
<format>{"resp": $1,"company":$2,"itemnbr":$3,"description1":$4,"description2":$5,"brand":$6}</format> | |
<args> | |
<arg evaluator="xml" expression="$ctx:resp" /> | |
<arg evaluator="xml" expression="$ctx:company" /> | |
<arg evaluator="xml" expression="$ctx:itemnbr" /> | |
<arg evaluator="xml" expression="$ctx:description1" /> | |
<arg evaluator="xml" expression="$ctx:description2" /> | |
<arg evaluator="xml" expression="$ctx:brand" /> | |
</args> | |
</payloadFactory> | |
<respond /> | |
</inSequence> | |
</resource> | |
</api> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment