| <?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"><definitions xmlns="http://ws.apache.org/ns/synapse"> |
| <proxy name="StockQuoteProxy" transports="https http" startOnLoad="true" trace="disable"> |
| <target> |
| <inSequence> |
| <conditionalRouter continueAfter="false"> |
| <conditionalRoute breakRoute="false"> |
| <condition> |
| <match xmlns="" type="header" source="foo" regex="bar.*"/> |
| </condition> |
| <target sequence="cnd1_seq"/> |
| </conditionalRoute> |
| |
| <conditionalRoute breakRoute="false"> |
| <condition> |
| <and xmlns=""> |
| <match type="header" source="my_custom_header1" regex="foo.*"/> |
| <match type="url" regex="/services/StockQuoteProxy.*"/> |
| </and> |
| </condition> |
| <target sequence="cnd2_seq"/> |
| </conditionalRoute> |
| |
| <conditionalRoute breakRoute="false"> |
| <condition> |
| <and xmlns=""> |
| <match type="header" source="my_custom_header2" regex="bar.*"/> |
| <equal type="param" source="qparam1" value="qpv_foo"/> |
| <or> |
| <match type="url" regex="/services/StockQuoteProxy.*"/> |
| <match type="header" source="my_custom_header3" regex="foo.*"/> |
| </or> |
| <not> |
| <equal type="param" source="qparam2" value="qpv_bar"/> |
| </not> |
| </and> |
| </condition> |
| <target sequence="cnd3_seq"/> |
| </conditionalRoute> |
| </conditionalRouter> |
| </inSequence> |
| <outSequence> |
| <send/> |
| </outSequence> |
| </target> |
| </proxy> |
| |
| <sequence name="cnd1_seq"> |
| <log level="custom"> |
| <property name="MSG_FLOW" value="Condition (I) Satisfied"/> |
| </log> |
| <sequence key="send_seq"/> |
| </sequence> |
| <sequence name="cnd2_seq"> |
| <log level="custom"> |
| <property name="MSG_FLOW" value="Condition (II) Satisfied"/> |
| </log> |
| <sequence key="send_seq"/> |
| </sequence> |
| <sequence name="cnd3_seq"> |
| <log level="custom"> |
| <property name="MSG_FLOW" value="Condition (III) Satisfied"/> |
| </log> |
| <sequence key="send_seq"/> |
| </sequence> |
| |
| <sequence name="send_seq"> |
| <log level="custom"> |
| <property name="DEBUG" value="Condition Satisfied"/> |
| </log> |
| <send> |
| <endpoint name="simple"> |
| <address uri="http://localhost:9000/services/SimpleStockQuoteService"/> |
| </endpoint> |
| </send> |
| </sequence> |
| </definitions></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"><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:ser="http://services.samples" xmlns:xsd="http://services.samples/xsd"> |
| <soap:Header/> |
| <soap:Body> |
| <ser:getQuote> |
| <ser:request> |
| <xsd:symbol>IBM</xsd:symbol> |
| </ser:request> |
| </ser:getQuote> |
| </soap:Body> |
| </soap:Envelope></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&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> |