<?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 156</title>
    </properties>
    <body>
        <section name="Sample 156: Service Integration with Specifying the Receiving Sequence">
            <div class="xmlConf"><![CDATA[<definitions xmlns="http://ws.apache.org/ns/synapse">

    <localEntry key="sec_policy" src="file:repository/conf/sample/resources/policy/policy_3.xml"/>

    <proxy name="StockQuoteProxy">
        <target>
            <inSequence>
                <enrich>
                    <source type="body"/>
                    <target type="property" property="REQUEST"/>
                </enrich>

                <send receive="SimpleServiceSeq">
                    <endpoint name="secure">
                        <address uri="http://localhost:9000/services/SecureStockQuoteService">
                            <enableSec policy="sec_policy"/>
                        </address>
                    </endpoint>
                </send>
            </inSequence>
            <outSequence>
                <drop/>
            </outSequence>
        </target>
    </proxy>


    <sequence name="SimpleServiceSeq">
        <property name="SECURE_SER_AMT" expression="//ns:getQuoteResponse/ns:return/ns:last"
                xmlns:ns="http://services.samples"/>
        <log level="custom">
            <property name="SecureStockQuoteService-Amount" expression="get-property('SECURE_SER_AMT')"/>
        </log>
        <enrich>
            <source type="body"/>
            <target type="property" property="SecureService_Res"/>
        </enrich>
        <enrich>
            <source type="property" property="REQUEST"/>
            <target type="body"/>
        </enrich>
        <send receive="ClientOutSeq">
            <endpoint name="SimpleStockQuoteService">
                <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
            </endpoint>
        </send>
    </sequence>

    <sequence name="ClientOutSeq">
        <property name="SIMPLE_SER_AMT" expression="//ns:getQuoteResponse/ns:return/ns:last"
                       xmlns:ns="http://services.samples"/>
        <log level="custom">
            <property name="SimpleStockQuoteService-Amount" expression="get-property('SIMPLE_SER_AMT')"/>
        </log>
        <enrich>
            <source type="body"/>
            <target type="property" property="SimpleService_Res"/>
        </enrich>

        <filter xpath="fn:number(get-property('SIMPLE_SER_AMT')) > fn:number(get-property('SECURE_SER_AMT'))">
            <then>
                <log>
                    <property name="StockQuote" value="SecureStockQuoteService"/>
                </log>
                <enrich>
                    <source type="property" property="SecureService_Res"/>
                    <target type="body"/>
                </enrich>
            </then>
            <else>
                <log>
                    <property name="StockQuote" value="SimpleStockQuoteService"/>
                </log>
            </else>
        </filter>

        <send/>
    </sequence>

</definitions>]]></div>
            <subsection name="Objective">
                <p>
                    Synapse is capable of mediating requests among multiple services and managing
                    complex message flows thereby acting as a lightweight orchestration engine. This
                    sample demonstrates how to easily integrate multiple services with Synapse using
                    the 'receiving sequence' feature of Synapse.
                </p>
            </subsection>
            <subsection name="Pre-requisites">
                <p>
                    <ul>
                        <li>
                            Deploy the SimpleStockQuoteService in the sample Axis2 server
                        </li>
                        <li>
                            Deploy the SecureStockQuoteService in the sample Axis2 server and start Axis2
                        </li>
                        <li>
                            Start Synapse using the configuration numbered 156 (repository/conf/sample/synapse_sample_156.xml)
                            <div class="command">
                                Unix/Linux: sh synapse.sh -sample 156<br/>
                                Windows: synapse.bat -sample 156
                            </div>
                        </li>
                    </ul>
                </p>
            </subsection>
            <subsection name="Executing the Client">
                <p>
                    This sample includes a proxy service which first forwards the client request to
                    the SecureStockQuoteService. Once a response has been received from this service,
                    Synapse will turn around and invoke the SimpleStockQuoteService. To do this proxy
                    service must hold on to the original request in memory. This is done using an
                    enrich mediator. Once Synapse has received a response from the SimpleStockQuoteService
                    it will compare the two responses received from the two services and select the
                    one with the lower stock quote value. This response will be then sent to the
                    client.
                </p>
                <p>
                    The important feature to note here is the 'receive' attribute set on the 'send'
                    mediators. This tells Synapse that responses of those send operations should be
                    directed to the sequences referred by the 'receive' attribute. Therefore the
                    response from the SecureStockQuoteService is directed to the sequence named
                    'SimpleServiceSeq'. Similarly the response from the SimpleStockQuoteService will
                    be handled by the sequence named 'ClientOutSeq'.
                </p>
                <p>
                    To try this out, execute the stock quote client as follows:
                </p>
                <div class="command">ant stockquote -Daddurl=http://localhost:8280/services/StockQuoteProxy</div>
                <p>
                    You can confirm that Synapse invokes both services by going through the console
                    output of the Axis2 server. However Axis2 client will receive only one response
                    back. As far as  the client is concerned, only one HTTP transaction is taking place.
                    But Synapse does multiple service invocations with the back-end to make the whole
                    integration scenario tick.
                </p>
            </subsection>
        </section>
        <p><a href="../samples.html">Back to Catalog</a></p>
    </body>
</document>