## Apache Synapse

## Sample 452: Conditional URL Rewriting with Multiple Rules

<definitions xmlns="http://ws.apache.org/ns/synapse"> <sequence name="main"> <in> <property name="http.port" value="9000"/> <property name="https.port" value="9002"/> <rewrite> <rule> <action fragment="host" value="localhost"/> <action fragment="path" type="prepend" value="/services"/> </rule> <rule> <condition> <equal type="url" source="protocol" value="http"/> </condition> <action fragment="port" xpath="get-property('http.port')"/> </rule> <rule> <condition> <equal type="url" source="protocol" value="https"/> </condition> <action fragment="port" xpath="get-property('https.port')"/> </rule> </rewrite> <log level="full"/> <send/> </in> <out> <send/> </out> </sequence> </definitions>

### Objective

Demonstrate the ability of the URL rewrite mediator to perform rewrites based on multiple rules

### Pre-requisites

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

### Executing the Client

One may specify multiple rewrite rules for a URL rewrite mediator instance. In that case Synapse will execute all the rules on each message, in the order they appear. This particular sample lists 3 rewrite rules. To try it out, invoke the client as follows.

ant stockquote -Dtrpurl=http://localhost:8280 -Daddurl=http://test.com/SimpleStockQuoteService

The provided address URL does not contain a port number and the context. The URL rewrite mediator will replace the hostname (test.com) with 'localhost' and add the context '/services' to the path. Then it will add the appropriate port number to the URL by looking at the protocol prefix. Ultimately the service request will be routed the sample Axis2 server and the client will receive a valid response.

Another important aspect shown by this sample is the ability of the URL rewirte mediator to obtain the necessary values by executing XPath expressions. The port numbers are calculated by executing an XPath on the messages.

Back to Catalog