Author: ravi
Date: Tue Dec 27 13:15:34 2016
New Revision: 1776121

URL: http://svn.apache.org/viewvc?rev=1776121&view=rev
Log:
Adding new script mediator sample 355 on using python for mediation

Added:
    
synapse/trunk/java/modules/documentation/src/site/xdoc/userguide/samples/sample355.xml
    
synapse/trunk/java/repository/conf/sample/resources/script/stockquoteTransformRequest.py
    
synapse/trunk/java/repository/conf/sample/resources/script/stockquoteTransformResponse.py
    synapse/trunk/java/repository/conf/sample/synapse_sample_355.xml
Modified:
    synapse/trunk/java/modules/documentation/src/site/xdoc/userguide/samples.xml
    
synapse/trunk/java/modules/documentation/src/site/xdoc/userguide/samples/setup/script.xml

Modified: 
synapse/trunk/java/modules/documentation/src/site/xdoc/userguide/samples.xml
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/documentation/src/site/xdoc/userguide/samples.xml?rev=1776121&r1=1776120&r2=1776121&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/documentation/src/site/xdoc/userguide/samples.xml 
(original)
+++ 
synapse/trunk/java/modules/documentation/src/site/xdoc/userguide/samples.xml 
Tue Dec 27 13:15:34 2016
@@ -140,6 +140,7 @@
                         <li><a href="samples/sample352.html">Sample 352: 
Accessing Synapse MessageContext API through scripts</a></li>
                         <li><a href="samples/sample353.html">Sample 353: Using 
Ruby scripts for mediation</a></li>
                         <li><a href="samples/sample354.html">Sample 354: Using 
In-lined Ruby scripts for mediation</a></li>
+                        <li><a href="samples/sample355.html">Sample 355: Using 
Python scripts for mediation</a></li>
                     </ul>
                 </p>
                 <h4>Database Mediators (Interacting with Databases)</h4>

Added: 
synapse/trunk/java/modules/documentation/src/site/xdoc/userguide/samples/sample355.xml
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/documentation/src/site/xdoc/userguide/samples/sample355.xml?rev=1776121&view=auto
==============================================================================
--- 
synapse/trunk/java/modules/documentation/src/site/xdoc/userguide/samples/sample355.xml
 (added)
+++ 
synapse/trunk/java/modules/documentation/src/site/xdoc/userguide/samples/sample355.xml
 Tue Dec 27 13:15:34 2016
@@ -0,0 +1,101 @@
+<?xml version="1.0" encoding="ISO-8859-1" ?>
+<!--
+  ~  Licensed to the Apache Software Foundation (ASF) under one
+  ~  or more contributor license agreements.  See the NOTICE file
+  ~  distributed with this work for additional information
+  ~  regarding copyright ownership.  The ASF licenses this file
+  ~  to you under the Apache License, Version 2.0 (the
+  ~  "License"); you may not use this file except in compliance
+  ~  with the License.  You may obtain a copy of the License at
+  ~
+  ~   http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~  Unless required by applicable law or agreed to in writing,
+  ~  software distributed under the License is distributed on an
+  ~   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  ~  KIND, either express or implied.  See the License for the
+  ~  specific language governing permissions and limitations
+  ~  under the License.
+  -->
+
+<document>
+    <properties>
+        <title>Apache Synapse - Sample 355</title>
+    </properties>
+    <body>
+        <section name="Sample 355: Using Python Scripts for Mediation">
+            <div class="xmlConf">
+                &lt;definitions 
xmlns=&quot;http://ws.apache.org/ns/synapse&quot;&gt;
+
+                    &lt;registry 
provider=&quot;org.apache.synapse.registry.url.SimpleURLRegistry&quot;&gt;
+                        &lt;!-- the root property of the simple URL registry 
helps resolve a resource URL as root + key --&gt;
+                        &lt;parameter 
name=&quot;root&quot;&gt;file:repository/conf/sample/resources/&lt;/parameter&gt;
+                        &lt;!-- all resources loaded from the URL registry 
would be cached for this number of milli seconds --&gt;
+                        &lt;parameter 
name=&quot;cachableDuration&quot;&gt;15000&lt;/parameter&gt;
+                    &lt;/registry&gt;
+
+                    &lt;localEntry key=&quot;stockquoteScript&quot;
+                                
src=&quot;file:repository/conf/sample/resources/script/stockquoteTransformRequest.py&quot;/&gt;
+
+                    &lt;sequence name=&quot;main&quot;&gt;
+                        &lt;in&gt;
+                            &lt;!-- transform the custom quote request into a 
standard quote request expected by the service --&gt;
+                            &lt;script language=&quot;py&quot; 
key=&quot;stockquoteScript&quot; function=&quot;transformRequest&quot;/&gt;
+                            &lt;send&gt;
+                                &lt;endpoint&gt;
+                                    &lt;address 
uri=&quot;http://localhost:9000/services/SimpleStockQuoteService&quot;/&gt;
+                                &lt;/endpoint&gt;
+                            &lt;/send&gt;
+                        &lt;/in&gt;
+                        &lt;out&gt;
+                            &lt;!-- transform the standard response back into 
the custom format the client expects --&gt;
+                            &lt;script language=&quot;py&quot; 
key=&quot;script/stockquoteTransformResponse.py&quot;
+                                    function=&quot;transformResponse&quot;/&gt;
+                            &lt;send/&gt;
+                        &lt;/out&gt;
+                    &lt;/sequence&gt;
+
+                &lt;/definitions&gt;</div>
+            <subsection name="Objective">
+                <p>
+                    Shows how to embed Python scripts in the Synapse 
configuration itself.
+                </p>
+            </subsection>
+            <subsection name="Pre-requisites">
+                <p>
+                    <ul>
+                       <li>
+                            This sample uses Jython, so first setup support 
for this in Synapse as described at
+                                                       <a 
href="setup/script.html#python">Configuring Jython</a>
+                        </li>
+                        <li>
+                            Deploy the SimpleStockQuoteService in the sample 
Axis2 server and start Axis2
+                        </li>
+                        <li>
+                            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.
+                        </li>
+                        <li>
+                            Start Synapse using the configuration numbered 355 
(repository/conf/sample/synapse_sample_355.xml)
+                            <div class="command">
+                                Unix/Linux: sh synapse.sh -sample 355<br/>
+                                Windows: synapse.bat -sample 355
+                            </div>
+                        </li>
+                    </ul>
+                </p>
+            </subsection>
+            <subsection name="Executing the Client">
+                <p>
+                    Run the sample client as follows.
+                </p>
+                <div class="command">ant stockquote 
-Daddurl=http://localhost:9000/services/SimpleStockQuoteService 
-Dtrpurl=http://localhost:8280/ -Dmode=customquote</div>
+                <p>
+                    The Python scripts will transform the requests and 
responses.
+                </p>                
+            </subsection>
+        </section>
+        <p><a href="../samples.html">Back to Catalog</a></p>        
+    </body>
+</document>
\ No newline at end of file

Modified: 
synapse/trunk/java/modules/documentation/src/site/xdoc/userguide/samples/setup/script.xml
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/documentation/src/site/xdoc/userguide/samples/setup/script.xml?rev=1776121&r1=1776120&r2=1776121&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/documentation/src/site/xdoc/userguide/samples/setup/script.xml
 (original)
+++ 
synapse/trunk/java/modules/documentation/src/site/xdoc/userguide/samples/setup/script.xml
 Tue Dec 27 13:15:34 2016
@@ -35,6 +35,9 @@
                                <li>
                                        <a href="#ruby">Ruby Support</a>
                                </li>
+                <li>
+                                       <a href="#python">Python Support</a>
+                               </li>
                                <li>
                                        <a href="#json">JSON Support</a>
                                </li>
@@ -69,6 +72,18 @@
                                        </a>
                                </p>
                        </subsection>
+                       <subsection name="Python Support" id="python">
+                               <p>
+                                       For Python support you need to download 
the 'jython.jar'
+                                       from the Maven repository for Jython, 
and copy it into the 'lib'
+                                       folder of Synapse . The Jython JAR can 
be downloaded from
+                                       <a class="externalLink"
+                                               
href="http://central.maven.org/maven2/org/python/jython/2.2.1/jython-2.2.1.jar";>
+                                               here
+                                       </a>
+                               </p>
+                       </subsection>
+
                        <subsection name="JSON Support" id="json">
                                <p>
                                        <a class="externalLink" 
href="http://json.org";>JSON</a>
@@ -107,4 +122,4 @@
 
                </section>
        </body>
-</document>
\ No newline at end of file
+</document>

Added: 
synapse/trunk/java/repository/conf/sample/resources/script/stockquoteTransformRequest.py
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/repository/conf/sample/resources/script/stockquoteTransformRequest.py?rev=1776121&view=auto
==============================================================================
--- 
synapse/trunk/java/repository/conf/sample/resources/script/stockquoteTransformRequest.py
 (added)
+++ 
synapse/trunk/java/repository/conf/sample/resources/script/stockquoteTransformRequest.py
 Tue Dec 27 13:15:34 2016
@@ -0,0 +1,32 @@
+#
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing,
+#  software distributed under the License is distributed on an
+#   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#  KIND, either express or implied.  See the License for the
+#  specific language governing permissions and limitations
+#  under the License. 
+# 
+
+from org.apache.synapse.util.xpath import SynapseXPath
+
+def transformRequest(mc):
+    symbolXPath = SynapseXPath("//*[local-name()='Code']/text()")
+    symbol = symbolXPath.stringValueOf(mc)
+    mc.setPayloadXML('''
+       <m:getQuote xmlns:m="http://services.samples";>
+               <m:request>
+                       <m:symbol>''' + symbol + '''</m:symbol>
+               </m:request>
+       </m:getQuote>''')
+
+

Added: 
synapse/trunk/java/repository/conf/sample/resources/script/stockquoteTransformResponse.py
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/repository/conf/sample/resources/script/stockquoteTransformResponse.py?rev=1776121&view=auto
==============================================================================
--- 
synapse/trunk/java/repository/conf/sample/resources/script/stockquoteTransformResponse.py
 (added)
+++ 
synapse/trunk/java/repository/conf/sample/resources/script/stockquoteTransformResponse.py
 Tue Dec 27 13:15:34 2016
@@ -0,0 +1,32 @@
+#
+#    Licensed to the Apache Software Foundation (ASF) under one
+#    or more contributor license agreements.  See the NOTICE file
+#    distributed with this work for additional information
+#    regarding copyright ownership.  The ASF licenses this file
+#    to you under the Apache License, Version 2.0 (the
+#    "License"); you may not use this file except in compliance
+#    with the License.  You may obtain a copy of the License at
+#  
+#     http://www.apache.org/licenses/LICENSE-2.0
+#  
+#    Unless required by applicable law or agreed to in writing,
+#    software distributed under the License is distributed on an
+#     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#    KIND, either express or implied.  See the License for the
+#    specific language governing permissions and limitations
+#    under the License.
+# 
+
+from org.apache.synapse.util.xpath import SynapseXPath
+
+def transformResponse(mc):
+    symbolXPath = SynapseXPath("//*[local-name()='symbol']/text()")
+    symbol = symbolXPath.stringValueOf(mc)
+    lastXPath = SynapseXPath("//*[local-name()='last']/text()")
+    price = lastXPath.stringValueOf(mc)
+    mc.setPayloadXML('''
+       <m:CheckPriceResponse xmlns:m="http://services.samples/xsd";>
+         <m:Code>''' + symbol + '''</m:Code>
+         <m:Price>''' + price + '''</m:Price>
+       </m:CheckPriceResponse>
+   ''')

Added: synapse/trunk/java/repository/conf/sample/synapse_sample_355.xml
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/repository/conf/sample/synapse_sample_355.xml?rev=1776121&view=auto
==============================================================================
--- synapse/trunk/java/repository/conf/sample/synapse_sample_355.xml (added)
+++ synapse/trunk/java/repository/conf/sample/synapse_sample_355.xml Tue Dec 27 
13:15:34 2016
@@ -0,0 +1,54 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~  Licensed to the Apache Software Foundation (ASF) under one
+  ~  or more contributor license agreements.  See the NOTICE file
+  ~  distributed with this work for additional information
+  ~  regarding copyright ownership.  The ASF licenses this file
+  ~  to you under the Apache License, Version 2.0 (the
+  ~  "License"); you may not use this file except in compliance
+  ~  with the License.  You may obtain a copy of the License at
+  ~
+  ~   http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~  Unless required by applicable law or agreed to in writing,
+  ~  software distributed under the License is distributed on an
+  ~   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  ~  KIND, either express or implied.  See the License for the
+  ~  specific language governing permissions and limitations
+  ~  under the License.
+  -->
+
+<!-- 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>
+
+


Reply via email to