Walking through Wso2 ESB synapse mediation engine

Tags

,

Wso2 ESB is using to handle real world integration scenarios with the messages transforming and routing.The mediation process of the Wso2 ESB is handled by Synapse mediation engine.

synapse-arch

Let’s take following proxy service configuration.

<proxy name="Sampleproxy" startOnLoad="true" transports="http https" xmlns="http://ws.apache.org/ns/synapse">
<target>
<inSequence>
<log>
<property name="in" value="==== IN ===="/>
</log>
<send>
<endpoint>
<address uri="http://172.18.0.1:9764/services/Version/"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<log>
<property name="out" value="=== OUT ===="/>
</log>
<send/>
</outSequence>
<faultSequence/>
</target>
</proxy>

When we invoking above proxy service deployed in Wso2 ESB, Synapse mediation engine execute following classes and methods
to handle the request payload.

In Flow (Request flow)

org.apache.synapse.core.axis2.ProxyServiceMessageReceiver#receive
org.apache.synapse.core.axis2.ProxyService#registerFaultHandler
org.apache.synapse.mediators.base.SequenceMediator#mediate(org.apache.synapse.MessageContext)
org.apache.synapse.mediators.AbstractListMediator#mediate(org.apache.synapse.MessageContext)
org.apache.synapse.mediators.AbstractListMediator#mediate(org.apache.synapse.MessageContext, int)
org.apache.synapse.mediators.AbstractListMediator#buildMessage
org.apache.synapse.mediators.builtin.LogMediator#mediate
org.apache.synapse.mediators.builtin.SendMediator#mediate
org.apache.synapse.endpoints.AddressEndpoint#send
org.apache.synapse.endpoints.AbstractEndpoint#send
org.apache.synapse.core.axis2.Axis2SynapseEnvironment#send
org.apache.synapse.core.axis2.Axis2Sender#sendOn
org.apache.synapse.core.axis2.Axis2FlexibleMEPClient#send
org.apache.axis2.client.OperationClient#execute
org.apache.synapse.core.axis2.DynamicAxisOperation.DynamicOperationClient#executeImpl
org.apache.synapse.core.axis2.DynamicAxisOperation.DynamicOperationClient#send
org.apache.axis2.engine.AxisEngine#send

Out Flow (Response flow)

org.apache.axis2.engine.AxisEngine#receive
org.apache.synapse.core.axis2.SynapseCallbackReceiver#receive
org.apache.synapse.core.axis2.SynapseCallbackReceiver#handleMessage
org.apache.synapse.core.axis2.Axis2SynapseEnvironment#injectMessage(org.apache.synapse.MessageContext)
org.apache.synapse.mediators.base.SequenceMediator#mediate(org.apache.synapse.MessageContext)
org.apache.synapse.mediators.AbstractListMediator#mediate(org.apache.synapse.MessageContext)
org.apache.synapse.mediators.AbstractListMediator#mediate(org.apache.synapse.MessageContext, int)
org.apache.synapse.mediators.AbstractListMediator#buildMessage
org.apache.synapse.mediators.builtin.LogMediator#mediate
org.apache.synapse.mediators.builtin.SendMediator#mediate
org.apache.synapse.core.axis2.Axis2SynapseEnvironment#send
org.apache.synapse.core.axis2.Axis2Sender#sendBack
org.apache.axis2.engine.AxisEngine#send

Hope above information will help you to debug the synapse mediation engine.

How to appending a context and query string parameters to the target REST endpoint using Wso2 ESB

Tags

, ,

When we use REST endpoints within the Wso2 ESB REST API (or Proxy service), sometimes we need to dynamically change the endpoint context and values of the query string parameters. To achieve that kind of requirement, we can use HTTP endpoint within Wso2 ESB with URI Template. You can populate the query string parameters during mediation runtime, using property values whose names have the “uri.var.” prefix.

example:

<property name="uri.var.service.user" scope="default" type="STRING" value="testuser"/>

And then you can access that query string parameter value within the URI like below.

{uri.var.service.user}

See the following example and you can see I have set the values to query string parameters, using property mediators.
Then I have access those values within the HTTP endpoint like below.

<http method="GET" uri-template="http://localhost:9000/{uri.var.service.host}/{uri.var.service.path}?USERNAME={uri.var.service.user}&amp;PASSWORD={uri.var.service.pass}&amp;ioName={uri.var.service.wfName}"/>

Also within the API configuration, we can use “POST_TO_URI” property to make the outgoing URL of the ESB a complete URL.

API Configuration:

<api xmlns="http://ws.apache.org/ns/synapse" name="TririgaApi" context="/tririga">
<resource methods="GET" url-mapping="/employee">
<inSequence>
<log>
<property name="====== API IN =====" value="==== INSEQ ===="/>
</log>
<property name="uri.var.service.user" scope="default" type="STRING" value="testuser"/>
<property name="uri.var.service.pass" scope="default" type="STRING" value="testpasswd"/>
<property name="uri.var.service.host" scope="default" type="STRING" value="testhost"/>
<property name="uri.var.service.path" scope="default" type="STRING" value="testpath"/>
<property name="uri.var.service.wfName" scope="default" type="STRING" value="testwfName"/>
<property name="POST_TO_URI" value="true" scope="axis2"/>
<send>
<endpoint>
<http method="GET" uri-template="http://localhost:9000/{uri.var.service.host}/{uri.var.service.path}?USERNAME={uri.var.service.user}&amp;PASSWORD={uri.var.service.pass}&amp;ioName={uri.var.service.wfName}"/>
</endpoint>
</send>
</inSequence>
</resource>
</api>

When we are invoking above API, we can see the endpoint URI like below.

http://localhost:9000/testhost/testpath?USERNAME=testuser&PASSWORD=testpasswd&ioName=testwfName

Thanks.

Accessing content from JSON payloads using wso2 ESB

Tags

,

If you have come up with the requirement of accessing JSON content from JSON payload within the mediation flow of the Wso2 ESB, You should send the “Content-Type” HTTP header with the request like below.

“Content-Type : application/json”

Then with in the mediation flow of the ESB (within proxy service,sequence,REST API), you can access the JSON payload using JSONPath syntax. See the following example. JSON payload like below

{
  "coordinates":{
     "location":[
        {
           "name":"Bermuda Triangle",
           "n":25.0000,
           "w":71.0000
        },
        {
           "name":"Eiffel Tower",
           "n":48.8582,
           "e":2.2945
        }
     ]
  }
}

If you want to access the “name” field of the first “location” member of the above JSON payload,

You can achieve that using following JSONPath expression.

<log>
<property name="location" expression="json-eval($.coordinates.location[0].name)"></property>
</log>

If you send a request like following

con_type

You can see below log line in the Wso2Carbon logs.

  INFO - LogMediator To: /def, MessageID: urn:uuid:89c8074e-e03f-4e0f-9dc7-c01bc2c3c30c, Direction: request, location = Bermuda Triangle

You can refer document [1],[2] for get more idea about the JSON payload handling in the Wso2 ESB and get an idea about the JSONPath syntax.

[1] – https://docs.wso2.com/display/ESB490/JSON+Support

[2] – http://goessner.net/articles/JsonPath/