Thursday, February 9, 2017

How to use an existing java class method inside a script mediator in WSO2

If you need to access a java class method inside WSO2 ESB script mediator, you can simply call it.

Below is an example done to call matches() method inside java.util.regex.Pattern class.

You can simply do it as below.

  <script language="js" description="extract username">  
          var isMatch = java.util.regex.Pattern.matches(".*test.*", "This is a test description!");       
  </script>  

You can access this value using property mediator if you set this in to message context.


  mc.setProperty("isMatch",isMatch);   

So a Sample synapse will be,



    <script language="js" description="extract username">
   var isMatch = java.util.regex.Pattern.matches(".*test.*", "This is a test description!"); 
          mc.setProperty("isMatch",isMatch);                 
    </script>

    <log level="custom">
       <property name="isMatch" expression="get-property('isMatch')"/>
    </log> 
                 

You can use this in a custom sequence in WSO2 API Manager as well to perform your task.

As an example, by using java.util.regex.Pattern.Matched method, you can use regular expression support in Java inside the script mediator.