esb

Hello all, if the title hasn’t deterred you already, then you’re in for a treat, and possibly long sought after solutions. This post is going to be all about the WSO2 Enterprise Service Bus (ESB), and handling errors that appear in it. If this is too techy for your taste, you might want to bail out now.

 However, if you’ve been working on the WSO2 ESB, then this may be what you’re looking for to shed some light on those pesky Error Requests. I’m going to explain some of the various methods I’ve tried to resolve these.

 I was once working on a project where the use case was for a catch error response, using a selected element in request. It doesn’t matter whether the element is empty or not, just needed the element to exist in request. If the element exists, it is a correct request; if not, it is an error request. So I found a few solutions:

Example 1:

USE FILTER MEDIATOR

kapila-blog-img-1

I added a filter mediator to sequence in my proxy service. Filter mediator is like an if/else condition in programming languages. In my case, I defined it as:

<?xml version=”1.0″ encoding=”UTF-8″?>
<filter xmlns=”http://ws.apache.org/ns/synapse&#8221; xpath=”//a”><then>
<log>
<property name=”success” value=”element exist”/>
</log>
</then>
<else>
<log>
<property name=”error” value=”element doesn’t exist”/>
</log>
</else>
</filter>

Using filter mediator, we can check the existence of elements. Say we want to filter element <a>, I set xpath to it, then the mediator will return true if element <a> exists. If not, it returns false. However, if you use xpath in filter mediator, it will only consider the existing elements.

Example 2:

USING SWITCH MEDIATOR & FILTER MEDIATOR

kapila-blog-img-2

Naturally it will think you want to consider the content of an element, and if that element doesn’t exist, it’ll think you want to handle that first. To get around this use a combination of Switch Mediator & Filter Mediator:

<?xml version=”1.0″ encoding=”UTF-8″?>

<switch source=”//a” xmlns=”http://ws.apache.org/ns/synapse”&gt;

<case regex=”case one”>

<log>
<property name=”CASE1_________-” value=”_________”/>
</log>
</case>
<case regex=”case 2″>
<log>
<property name=”CASE2_________-” value=”_________”/>
</log>
</case>
<case regex=”error”>
<log>
<property name=”CASE3_________-” value=”_________________”/>
</log>
</case>
<default>
<filter xpath=”//a”>
<then>
<log>
<property name=”success” value=”element exist”/>
</log>
</then>
<else>
<log>

<property name=”error” value=”element doesn’t exist”/>

</log>
</else>
</filter>
</default>
</switch>

In the above example you can see I use xpath //a for switch mediator, then switch mediator selects element <a> and considers content in that case. When the case matches with the content of the element, it will execute. However, if only using switch mediator, it cannot catch null elements. So I use filter mediator on the default case, as the default case filter can identify the existence of elements. Using this approach, you can check both content and existence.

Example 3:

USE SCRIPT MEDIATOR

kapila-blog-img-3

If you don’t feel comfortable with the above two solutions, you can write a script mediator. In this approach, I have used java script for scripting. WSO2 ESB used Rhino Engine to execute java scripts. In WSO2 script mediator, I used the method “getPayloadXML()” to get the payload in an XML representation. You can then find the existence of elements using java script coding: we now have the freedom to do many things with one mediator using java script.

I also happen to come across yet another approach that may work, depending on your situation. What I ended up doing was, analysing the payload and using an ‘if’ condition, to set a property. Then I could use filter mediator to check for that property.

The above listed methods are by far not the only approaches you can try. As you navigate the WSO2 ESB, you’ll be sure to come up with ideas of your own that may better solve the errors you find yourself with.

We’d love to hear them! WSO2 is an Open Source community, and one of progressive knowledge sharing. Until the community comes up with some better solutions, I hope ours can help.

kapila nishantha

Kapila Nishantha | Associate Software Engineer
Mitra Innovation

Leave a Reply