fb
info@yenlo.com
WSO2 Enterprise Integrator 3 min

String manipulation in WSO2 ESB using the script mediator

Jan_Timmerman.jpg
Jan Timmerman
WSO2 Integration Consultant
digital transformation

WSO2 ESBAlthough not often, occasionally there are integrations that are hard to build using standard mediators. But do not worry, WSO2’s ESB got you covered.

There is always the possibility to write your own custom mediator. This is a java program that gives you almost complete control but there are perhaps easier ways of getting the job done.  As we often say: ‘there is often another way to do it with WSO2’.

Banner-WSO2-Community-1-6

Enter the Script Mediator

The script mediator is a useful last resort that greatly increases the possibilities of the WSO2 ESB but can be hard to work with as it is not always easy to debug.

Here I demonstrate a small example of string manipulation using WSO2 ESB and the script mediator followed by a step-by-step explanation.

String manipulation

Since we have access to the message context it is easy to work with variables that will inspect and perhaps change the message.

In this example some string manipulation is used to insert a header which might or might not contain a ‘version’ that needs to be replaced based on an earlier retrieved ‘version’. When a version is given the {version} part of the URL is replaced with the actual version (this is a very specific example.

Normally there are more sensible ways to do this) When no version is given the /{version} part of the URL is removed.

  <script language="js">
       var toheader = mc.getProperty('retrieved_url');
       var log = mc.getServiceLog();
       log.info(toheader);
       var back = mc.getProperty('version');
       var res = "";
      if( toheader.indexOf("/{version}") != -1 )
      {
            if(version == "")
                var res = toheader.replace('/{version}', "");
            else
                var res = toheader.replace('{version}', back)
            log.info(res);
       }
            mc.setTo(res);
  </script>

Let’s look at the individual parts of what we just did.

Logging in the script mediator

There are several examples on how to print values and logging to the wso2carbon.log. I have found that many of these examples do not work (anymore). What does work is using the servicelog.

Create a log object using ‘mc.getServiceLog();’ and use log.[loglevel]([value/var])  to print to the console. So to print a variable to the log on loglevel ‘info’ use ‘log.info(variable)

Replacing strings in the script mediator

String replace is difficult to do in the ESB. One could enable xpath 2.0 functionality, but this might impact current integrations. Using the script mediator it is rather trivial as it would be in javascript.

In this example the script checks if the given url (dynamically generated) contains a {version} an example would be: https://customer.yenlo.com/soapservice/{version}/testcall using
if( toheader.indexOf("/{version}") != -1 )

Secondly I check if the requestor cares about the version (already placed in a property earlier in the flow). If this is not the case the version part including leading slash needs to be removed so the result becomes: https://customer.yenlo.com/soapservice/testcall
if(version == "")
      var res = toheader.replace('/{version}', "");

If the customer cares about a version. the {version} is replaced in the url:
else
   var res = toheader.replace('{version}', back)

Don’t use the script mediator

Script mediators are a lender of almost last resort (barring a custom mediator) and should only be used if no easier alternative.

There are not many cases where WSO2 ESB cannot provide a solution.  But in cases where there seems to be a solution only in a script mediator or even custom mediator:

  • Is there no solution using one of the other WSO2 ESB mediators?
  • Is the ESB really the place to put this functionality?

The second question pertains to the role of the ESB. It is good for mediation and transformation but for instance not meant for large transactional processing done within a proxy, api or sequence.

If there is indeed no simple alternative and it belongs in the ESB, then script mediator is a very powerful solution.

If you have any questions about this blogpost contact us via the comments section of this blog. View also our WSO2 Tutorialswebinars or white papers for more technical information. Need support? We do deliver WSO2 Product Support, WSO2 Development SupportWSO2 Operational Support and WSO2 Training Programs

Thanks to Rob Blaauboer for his contribution to this blogpost.

Yenlo is the leading, global, multi-technology integration specialist in the field of API-management, Integration technology and Identity Management. Known for our strong focus on best-of-breed hybrid and cloud-based iPaaS technologies. Yenlo is the product leader and multi-award winner in WSO2, Boomi, MuleSoft and Microsoft Azure technologies and offers best-of-breed solutions from multiple leading integration vendors.

Whitepaper:
ESB Selection Guide

esb frontback
Get it now
eng
Close
We appreciate it
Care to share

Please select one of the social media platforms below to share this pages content with the world