<?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 157</title>
    </properties>
    <body>
        <section name="Sample 157: Conditional Router Mediator for Implementing Complex Routing Scenarios">
            <div class="xmlConf">&lt;definitions xmlns=&quot;http://ws.apache.org/ns/synapse&quot;&gt;
    &lt;proxy name=&quot;StockQuoteProxy&quot; transports=&quot;https http&quot; startOnLoad=&quot;true&quot; trace=&quot;disable&quot;&gt;
        &lt;target&gt;
            &lt;inSequence&gt;
                &lt;conditionalRouter continueAfter=&quot;false&quot;&gt;
                    &lt;conditionalRoute breakRoute=&quot;false&quot;&gt;
                        &lt;condition&gt;
                            &lt;match xmlns=&quot;&quot; type=&quot;header&quot; source=&quot;foo&quot; regex=&quot;bar.*&quot;/&gt;
                        &lt;/condition&gt;
                        &lt;target sequence=&quot;cnd1_seq&quot;/&gt;
                    &lt;/conditionalRoute&gt;

                    &lt;conditionalRoute breakRoute=&quot;false&quot;&gt;
                        &lt;condition&gt;
                            &lt;and xmlns=&quot;&quot;&gt;
                                &lt;match type=&quot;header&quot; source=&quot;my_custom_header1&quot; regex=&quot;foo.*&quot;/&gt;
                                &lt;match type=&quot;url&quot; regex=&quot;/services/StockQuoteProxy.*&quot;/&gt;
                            &lt;/and&gt;
                        &lt;/condition&gt;
                        &lt;target sequence=&quot;cnd2_seq&quot;/&gt;
                    &lt;/conditionalRoute&gt;

                    &lt;conditionalRoute breakRoute=&quot;false&quot;&gt;
                        &lt;condition&gt;
                            &lt;and xmlns=&quot;&quot;&gt;
                                &lt;match type=&quot;header&quot; source=&quot;my_custom_header2&quot; regex=&quot;bar.*&quot;/&gt;
                                &lt;equal type=&quot;param&quot; source=&quot;qparam1&quot; value=&quot;qpv_foo&quot;/&gt;
                                &lt;or&gt;
                                    &lt;match type=&quot;url&quot; regex=&quot;/services/StockQuoteProxy.*&quot;/&gt;
                                    &lt;match type=&quot;header&quot; source=&quot;my_custom_header3&quot; regex=&quot;foo.*&quot;/&gt;
                                &lt;/or&gt;
                                &lt;not&gt;
                                    &lt;equal type=&quot;param&quot; source=&quot;qparam2&quot; value=&quot;qpv_bar&quot;/&gt;
                                &lt;/not&gt;
                            &lt;/and&gt;
                        &lt;/condition&gt;
                        &lt;target sequence=&quot;cnd3_seq&quot;/&gt;
                    &lt;/conditionalRoute&gt;
                &lt;/conditionalRouter&gt;
            &lt;/inSequence&gt;
            &lt;outSequence&gt;
                &lt;send/&gt;
            &lt;/outSequence&gt;
        &lt;/target&gt;
    &lt;/proxy&gt;

    &lt;sequence name=&quot;cnd1_seq&quot;&gt;
        &lt;log level=&quot;custom&quot;&gt;
            &lt;property name=&quot;MSG_FLOW&quot; value=&quot;Condition (I) Satisfied&quot;/&gt;
        &lt;/log&gt;
        &lt;sequence key=&quot;send_seq&quot;/&gt;
    &lt;/sequence&gt;
    &lt;sequence name=&quot;cnd2_seq&quot;&gt;
        &lt;log level=&quot;custom&quot;&gt;
            &lt;property name=&quot;MSG_FLOW&quot; value=&quot;Condition (II) Satisfied&quot;/&gt;
        &lt;/log&gt;
        &lt;sequence key=&quot;send_seq&quot;/&gt;
    &lt;/sequence&gt;
    &lt;sequence name=&quot;cnd3_seq&quot;&gt;
        &lt;log level=&quot;custom&quot;&gt;
            &lt;property name=&quot;MSG_FLOW&quot; value=&quot;Condition (III) Satisfied&quot;/&gt;
        &lt;/log&gt;
        &lt;sequence key=&quot;send_seq&quot;/&gt;
    &lt;/sequence&gt;

    &lt;sequence name=&quot;send_seq&quot;&gt;
        &lt;log level=&quot;custom&quot;&gt;
            &lt;property name=&quot;DEBUG&quot; value=&quot;Condition Satisfied&quot;/&gt;
        &lt;/log&gt;
        &lt;send&gt;
            &lt;endpoint name=&quot;simple&quot;&gt;
                &lt;address uri=&quot;http://localhost:9000/services/SimpleStockQuoteService&quot;/&gt;
            &lt;/endpoint&gt;
        &lt;/send&gt;
    &lt;/sequence&gt;
&lt;/definitions&gt;</div>
            <subsection name="Objective">
                <p>
                    Conditional router mediator can be used to implement complex routing rules in
                    Synapse. It can route messages to various endpoints based on URLs, query parameters
                    and transport headers. This sample demonstrates how to use the conditional router
                    mediator within a proxy service to build a smart routing proxy.
                </p>
            </subsection>
            <subsection name="Pre-requisites">
                <p>
                    <ul>
                        <li>
                            Deploy the SimpleStockQuoteService in the sample Axis2 server a d start
                            Axis2 server.
                        </li>
                        <li>
                            Start Synapse using the configuration numbered 157 (repository/conf/sample/synapse_sample_157.xml)
                            <div class="command">
                                Unix/Linux: sh synapse.sh -sample 157<br/>
                                Windows: synapse.bat -sample 157
                            </div>
                        </li>
                    </ul>
                </p>
            </subsection>
            <subsection name="Executing the Client">
                <p>
                    We will be using 'curl' as the client in this scenario. <a href="http://curl.haxx.se/">Curl</a>
                    is a neat little command line tool that can be used to generate various types
                    of HTTP requests (among other things).
                </p>
                <p>
                    First create a sample input file named stockQuoteReq.xml with the following
                    content.
                </p>
                <div class="xmlConf">&lt;soap:Envelope xmlns:soap=&quot;http://www.w3.org/2003/05/soap-envelope&quot; xmlns:ser=&quot;http://services.samples&quot;&gt;
   &lt;soap:Header/&gt;
   &lt;soap:Body&gt;
      &lt;ser:getQuote&gt;
         &lt;ser:request&gt;
            &lt;ser:symbol&gt;IBM&lt;/ser:symbol&gt;
         &lt;/ser:request&gt;
      &lt;/ser:getQuote&gt;
   &lt;/soap:Body&gt;
&lt;/soap:Envelope&gt;</div>
                <p>
                    Invoke curl as follows to see header based routing feature in action.
                </p>
                <div class="command">curl -d @stockQuoteReq.xml -H "Content-Type: application/soap+xml;charset=UTF-8" -H "foo:bar" "http://localhost:8280/services/StockQuoteProxy"</div>
                <p>
                    This sends a HTTP request with a custom header named 'foo'. Proxy service will
                    detect this header and print a custom log message confirming the receipt of the
                    request.
                </p>
                <p>
                    Now invoke curl as follows to test a combination header and URL based routing.
                </p>
                <div class="command">curl -d @stockQuoteReq.xml -H "Content-Type: application/soap+xml;charset=UTF-8" -H "my_custom_header1:foo1" "http://localhost:8280/services/StockQuoteProxy"</div>
                <p>
                    Finally invoke curl as follows to test routing based on complex conditions.
                </p>
                <div class="command">curl -d @stockQuoteReq.xml -H "Content-Type: application/soap+xml;charset=UTF-8" -H "my_custom_header2:bar" -H "my_custom_header3:foo" "http://localhost:8280/services/StockQuoteProxy?qparam1=qpv_foo&amp;qparam2=qpv_foo2"</div>
                <p>
                    In each case Synapse will log a different log entry because the conditional router
                    mediator uses different sequences to process the three messages.
                </p>
            </subsection>
        </section>
        <p><a href="../samples.html">Back to Catalog</a></p>
    </body>
</document>