Skip to content

Instantly share code, notes, and snippets.

@vanjikumaran
Last active October 16, 2017 22:53
Show Gist options
  • Save vanjikumaran/a678dd4921ce66912fa30d0e0cde3785 to your computer and use it in GitHub Desktop.
Save vanjikumaran/a678dd4921ce66912fa30d0e0cde3785 to your computer and use it in GitHub Desktop.
Synapse configuration to connect with tcp socket via scripting
<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 -&gt; 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(&lt;response/&gt;); var resp = String(mc.getProperty("ValueInGroovyScript")).split(','); payloadXML.appendChild(new XML(&lt;resp&gt;{resp[0]}&lt;/resp&gt;)); payloadXML.appendChild(new XML(&lt;company&gt;{resp[1]}&lt;/company&gt;)); payloadXML.appendChild(new XML(&lt;itemnbr&gt;{resp[2]}&lt;/itemnbr&gt;)); payloadXML.appendChild(new XML(&lt;description1&gt;{resp[3]}&lt;/description1&gt;)); payloadXML.appendChild(new XML(&lt;description2&gt;{resp[4]}&lt;/description2&gt;)); payloadXML.appendChild(new XML(&lt;brand&gt;{resp[5]}&lt;/brand&gt;)); 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