Sample 355: Using Python Scripts for Mediation

<definitions xmlns="http://ws.apache.org/ns/synapse"> <registry provider="org.apache.synapse.registry.url.SimpleURLRegistry"> <!-- the root property of the simple URL registry helps resolve a resource URL as root + key --> <parameter name="root">file:repository/conf/sample/resources/</parameter> <!-- all resources loaded from the URL registry would be cached for this number of milli seconds --> <parameter name="cachableDuration">15000</parameter> </registry> <localEntry key="stockquoteScript" src="file:repository/conf/sample/resources/script/stockquoteTransformRequest.py"/> <sequence name="main"> <in> <!-- transform the custom quote request into a standard quote request expected by the service --> <script language="py" key="stockquoteScript" function="transformRequest"/> <send> <endpoint> <address uri="http://localhost:9000/services/SimpleStockQuoteService"/> </endpoint> </send> </in> <out> <!-- transform the standard response back into the custom format the client expects --> <script language="py" key="script/stockquoteTransformResponse.py" function="transformResponse"/> <send/> </out> </sequence> </definitions>

Objective

Shows how to embed Python scripts in the Synapse configuration itself.

Pre-requisites

  • This sample uses Jython, so first setup support for this in Synapse as described at Configuring Jython
  • Deploy the SimpleStockQuoteService in the sample Axis2 server and start Axis2
  • Synapse does not ship with a Jython engine by default. Therefore you should download the Jython engine from Jython site and copy the downloaded jar file to the 'lib' directory of Synapse.
  • Start Synapse using the configuration numbered 355 (repository/conf/sample/synapse_sample_355.xml)
    Unix/Linux: sh synapse.sh -sample 355
    Windows: synapse.bat -sample 355

Executing the Client

Run the sample client as follows.

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dmode=customquote

The Python scripts will transform the requests and responses.

Back to Catalog