Skip Navigation

Defining custom Java handler classes accessible from JavaScript in WebKit browser

You can call Java methods from JavaScript in WebKit browser. To achieve this the following steps needs to be done:

  1. Using Eclipse, add the following required dependencies to your plug-in project:

    • com.sabre.edge.platform.optional.webkit

  2. Create any class in your Red App plug-in sources defining public methods you want to expose to JavaScript in WebKit browser. Only Double, String and Boolean are supported as parameters of Java methods called from JavaScript in WebKit browser. The following rules are used to convert JavaScript into Java types:

    • JavaScript numbers to java.lang.Double

    • JavaScript string to java.lang.String

    • JavaScript boolean to java.lang.Boolean

    • JavaScript null and undefined to null (Java null is converted to JavaScript null)

Example handler class is presented below:

package redapp.example;

public class WelcomeService
{
    public String welcome(String name)
    {
        return "Hello " + name;
    }
}
  1. In plugin.xml add a contribution to the extension point called com.sabre.edge.platform.optional.webkit.js.handler to pass the name of your handler class and JavaScript object reference name, at which your service is accessible in JavaScript in WebKit browser.  

<extension point="com.sabre.edge.platform.optional.webkit.js.handler">
    <handler class="redapp.example.WelcomeService" jsVarReferenceName="welcomeJava"/>
</extension>

It is possible and more convenient to add extension using eclipse IDE graphical representation of plugin.xml file:

image30

 

image29

 

image31

 

  1. Now you can use welcomeJava reference in your JavaScript code to call all methods defined in WelcomeService class as presented below:

<html>
<body>
    <a href="javascript: doWelcome()">Do welcome</a>
    <script>
    function doWelcome() {
        console.log(welcomeJava.welcome('Harry'));
    }
    </script>
</body>
</html>

Important: welcomeJava reference is always accessible after successful loading your Red App page.
Multiple handlers can be defined in single extension point as presented below:

<extension point="com.sabre.edge.platform.optional.webkit.js.handler">
    <handler class="redapp.example.WelcomeService" jsVarReferenceName="welcomeJava"/>
    <handler class="redapp.example.HelloService" jsVarReferenceName="helloJava"/>
    <handler class="redapp.example.HelloService" jsVarReferenceName="helloJava2"/>
</extension>
Note
Your jsVarReferenceName cannot override any existing variables available from page context for e.g. SrwApi, SrwOSGiApi and CloseApi. There can be more than one variable for accessing methods from single class, for each variable a new instance of class is created.
  1. Using argument type which cannot be converted to matching java type.

        Javascript strings are not automatically converted to suitable Java types. Ex. '123.4' is passed to java as String, not a Double. In JxBrowser all numbers are passed to Java as Double.

        This means that developer should provide in own java handler methods which arguments have appropriate type (Double, String or Boolean) and then parse them to expected type.

        In following example testArgs.addD method has Double argument. When argument with wrong type will be passed, in this example it is string '123.4', then similar exception will be thrown:

Uncaught NoSuchMethodException: com.sabre.redapp.example.webkit.extensions.js.TestArgs.addD(java.lang.String)

        To avoid such issues it is advised to check what type of arguments are expected on java side and on javascript side use appropriate value type or cast or convert argument values to proper types.

         

Note
See com.sabre.redapp.example.webkit sample for details.