Constructing Dynamic Endpoint URLs in WSO2 ESB
There are situations where you need to modify or generate endpoint URL dynamically in runtime. It can be entire endpoint URL, context of the URL, resource path of the URL, or query parameters.
This post will explain three different ways to construct a dynamic URL in ESB.
1. HTTP Endpoint
HTTP endpoint can be dynamically constructed using uri template. Create a properties name starting with uri.var prefix and define the uri template to refer those created properties e.g if the property name is uri.var.context, then uri template cane be defined as: http:://localhost/{uri.var.context}. In the runtime, these place holders in the uri templates will be populated by the values in the property related to that.
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="SampleProxy"
startOnLoad="true"
statistics="disable"
trace="disable"
transports="http,https">
<target>
<inSequence>
<property name="uri.var.host" value="http://localhost:8280"/>
<property name="uri.var.context" value="services"/>
<property name="uri.var.resourcepath" value="get-records"/>
<call>
<endpoint>
<http method="GET"
uri-template="{uri.var.host}/{uri.var.context}/{uri.var.resourcepath}"/>
</endpoint>
</call>
</inSequence>
<outSequence/>
<faultSequence/>
</target>
<description/>
</proxy>
In the runtime, the URL would be http://localhost:8280/services/get-records
2. Using Default Endpoint
The default endpoint will look for the endpoint url from “To” transport header. Therefore, the endpoint can be constructed dynamically and set to “To” header.
<?xml version="1.0" encoding="UTF-8"?>
<proxy name="SampleProxy" startOnLoad="true" transports="http https" xmlns="http://ws.apache.org/ns/synapse">
<target>
<inSequence>
<property name="host" scope="default" type="STRING" value="http://localhost:8280"/>
<property name="context" scope="default" type="STRING" value="services"/>
<property name="resourcepath" scope="default" type="STRING" value="get-records"/>
<header expression="fn:concat($ctx:host,'/',$ctx:context,'/',$ctx:resourcepath)" name="To" scope="default"/>
<property name="HTTP_METHOD" value="GET" scope="axis2" type="STRING"/>
<call>
<endpoint>
<default/>
</endpoint>
</call>
</inSequence>
<outSequence/>
<faultSequence/>
</target>
</proxy>
In the runtime, the URL would be http://localhost:8280/services/get-records
3. Using Address Endpoint
When working with address endpoint url context and resource path, query params can be dynamically appended to the endpoint using REST_URL_POSTFIX property.
<?xml version="1.0" encoding="UTF-8"?>
<proxy name="SampleProxy" startOnLoad="true" transports="http https" xmlns="http://ws.apache.org/ns/synapse">
<target>
<inSequence>
<property name="context" scope="default" type="STRING" value="services"/>
<property name="resourcepath" scope="default" type="STRING" value="get-records"/>
<property expression="fn:concat($ctx:context,'/',$ctx:resourcepath)" name="REST_URL_POSTFIX" scope="axis2" type="STRING"/>
<property name="HTTP_METHOD" value="GET" scope="axis2" type="STRING"/>
<call>
<endpoint>
<address uri="http://localhost:8280/"/>
</endpoint>
</call>
</inSequence>
<outSequence/>
<faultSequence/>
</target>
</proxy>
In the runtime, the URL would be http://localhost:8280/services/get-records