## Apache Synapse

## Sample 2: CBR with Switch Case Mediator

<definitions xmlns="http://ws.apache.org/ns/synapse"> <sequence name="main"> <switch source="//m0:getQuote/m0:request/m0:symbol" xmlns:m0="http://services.samples"> <case regex="IBM"> <!-- the property mediator sets a local property on the *current* message --> <property name="symbol" value="Great stock - IBM"/> </case> <case regex="MSFT"> <property name="symbol" value="Are you sure? - MSFT"/> </case> <default> <!-- it is possible to assign the result of an XPath expression as well --> <property name="symbol" expression="fn:concat('Normal Stock - ', //m0:getQuote/m0:request/m0:symbol)"/> </default> </switch> <log level="custom"> <!-- the get-property() XPath extension function allows the lookup of local message properties as well as properties from the Axis2 or Transport contexts (i.e. transport headers) --> <property name="symbol" expression="get-property('symbol')"/> <!-- the get-property() function supports the implicit message headers To/From/Action/FaultTo/ReplyTo --> <property name="epr" expression="get-property('To')"/> </log> <!-- Send the messages where they are destined to (i.e. the 'To' EPR of the message) --> <send/> </sequence> </definitions>

### Objective

Introduction to the switch-case mediator and manipulating properties set on the messages.

### Pre-requisites

- Deploy the SimpleStockQuoteService in the sample Axis2 server and start Axis2
- Start Synapse using the configuration numbered 2 (repository/conf/sample/synapse_sample_2.xml)
  Unix/Linux: sh synapse.sh -sample 2
  Windows: synapse.bat -sample 2

### Executing the Client

Execute the sample Axis2 client in the smart client using different symbols such as IBM, MSFT and SUN.

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=IBM
ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=MSFT
ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=SUN

When the symbol IBM is requested, viewing the mediation logs you will see that the switch mediator's first case for 'IBM' is executed and a local property named 'symbol' is set to 'Great stock - IBM'. Subsequently this local property value is looked up by the log mediator and logged using the 'get-property()' XPath extension function.

INFO LogMediator - symbol = Great stock - IBM, epr = http://localhost:9000/axis2/services/SimpleStockQuoteService

Similarly for the symbol 'MSFT' the second case statement in the switch mediator will be executed which will result in the following log.

INFO LogMediator - symbol = Are you sure? - MSFT, epr = http://localhost:9000/axis2/services/SimpleStockQuoteService

Back to Catalog