Use JavaScript to call existing Java code

In this approach, you write your custom requirement in Java and invoke it using JavaScript in a Scripting Language filter.

Follow these guidelines:

  1. Create a Java class that meets your custom requirement.
  2. Build a JAR file from the Java class and add it, and any third-party dependencies, to the API Gateway CLASSPATH and to the runtime dependencies in Policy Studio.
  3. Create a policy (for example, called InvokeJava) in Policy Studio that contains only a Scripting Language filter. Configure the filter to invoke the Java code using JavaScript.
Note   We recommend that you select JavaScript in the Language field of the Scripting Language filter, and ensure that the JavaScript syntax in the script conforms with Nashorn engine syntax. For more information about migrating from Rhino to Nashorn, see the Rhino Migration Guide.
  1. Configure API Gateway to invoke the policy. For more information, see Invoke the policy.
  2. Test the policy using API Tester. For more information, see Test the policy.

Invoke the policy

To configure the API Gateway to invoke the new policy, follow these steps:

  1. Under the Environment Configuration > Listeners node in Policy Studio, select the path (for example, API Gateway > Default Services > Paths).
  2. On the resolvers window on the right, click Add > Relative Path.
  3. Enter the following values on the dialog and click OK:
    • When a request arrives that matches the path: /invokejava
    • Path Specific Policy: Click the browse button and select the InvokeJava policy. This sends all requests received on the path configured above to your newly configured policy.
  4. To deploy the new configuration to API Gateway, click the Deploy button on the toolbar or press F6 and follow the instructions.

Test the policy

To test the configuration, follow these steps:

  1. Start API Tester.
  2. Click the arrow next to the Play icon and select Request Settings.
  3. In the Url field, enter http://localhost:8080/invokejava to send the message to the relative path you configured above.
  4. Click Run to send the message to API Gateway.
Tip   Alternatively, you can test the policy by entering the URL http://localhost:8080/invokejava into any web browser.

Related Links