IBM Datapower GatewayScript

Introduction

IBM API Assembly Language

Introduction

GatewayScript is a new transformation technology for API, Web, and Mobile and is available in DataPower. GatewayScript is based on the JavaScript programming language. More precisely, it supports the ECMAScript 5.1 version of the specification. On the DataPower appliance, JavaScript runs in ‘strict’ mode only. The strict mode provides a more consistent and more disciplined behavior.

For example, if a script attempts to write a property of a DataPower API object and that object is frozen, an immediate exception is thrown. The ‘strict’ mode behavior supports stronger error checking and handling and increases security.

DataPower Playground (GatewayScript Fiddle)

IBM DataPower Playground is an interactive website that lets you write GatewayScript code and execute it on a cloud-hosted DataPower Gateway for learning purposes. Available at : https://datapower-playground.mybluemix.net/

GatewayScript provides a series of libraries that give the developer access to specific APIs. All access to DataPower facilities is provided through these APIs.

IBM DataPower has below important modules and access to these modules can be done with nodeJS module import syntax.

i.e., var module=require('moduleName');

Following are some important modules and their usage

  1. Metadata Headers

  2. Console Logging

  3. Utilities

  4. Testing/Assertions

  5. URL Opener

  6. Service Metadata

Other Implicit Objects

  • Session - useful to read input data as JSON,Buffer etc.

  • APIM - This works only in IBM DataPower Gateway but not in Datapower Playground. This object useful to send response back to calling service

Examples of the above modules

Metadata Headers

This module used to read header information. The headers module is broken down into three sub-modules:

  • Original: Represents the original collection of headers received off the wire. For a request rule, these are the headers received in the request from the client. For a response rule, these are the headers received in the response from the remote, target host. Original headers are read-only.

  • Current: Represents the current state of the headers as they are being passed from action to action along with a processing rule. The processing rule may be either a request rule or a response rule. Current headers are read/write.

  • Response: Represents the header collection that is used for a response that is being accessed in the request rule. This is used for a loopback firewall or when skip-backside is used in a multi-protocol gateway. Response headers are read/write.

var hm=require('header-metadata');
hm.current.set('Authorization','SECRETKEY');
hm.response.set('content-type','application/xml');

Console

The console module is provided without having to use the require('console') statement.

This module is automatically attached to the Global object.

ar hm=require('header-metadata');
hm.current.set('Authorization','SECRETKEY');
hm.response.set('content-type','application/xml');
console.log(hm.current.headers);
console.log(hm.original.headers);
console.log(hm.response.headers);

APIM

APIM is an implicit object available in DataPower Gateway

Below is the example to read query parameters from the input source and converting into string format using 'querystring' module.

var qs = require ('querystring');
 var reqp = apim.getvariable('request.parameters');
 var accountId=reqp['accountId'];
  //preparing queryString
  var params = {}
  for (var name in reqp) {
          params[name] = reqp[name];
  }
  var queryString=qs.stringify(params);
  console.log(queryString);

URLOpen

This URL Open module used to connect to services and reading the response in a callback method.

var urlopen = require('urlopen');
var hm = require('header-metadata') ;
var ibmUser = hm.current.get("Authorization") ;
console.log(ibmUser);
console.log('session.parameters :'+session.parameters);
var my_content = hm.current.get("Content-type") ;
var options = {
            target: 'http://server.com',
            method: 'GET',
           headers: { 'Authorization' : ibmUser  },
       contentType: 'application/x-www-form-urlencoded',
           timeout: 60};

urlopen.open(options, function(error, response) {
  if (error) {
    // an error occurred during the request sending or response header parsing
    session.output.write("urlopen error: "+JSON.stringify(error));
  } else {
      response.contentType="application/json";
    // get the response status code
    var responseStatusCode = response.statusCode;
    var responseReasonPhrase = response.contentType;
    console.log("Response statusCode: " + responseStatusCode);
    console.log("Response reason phrase: " + responseReasonPhrase);
    // reading response data
    response.readAsJSON(function(err, json) {
        if (err) throw err;
        // Update the transactional payload to that just fetched
            apim.setvariable('message.body', json.Capability);
            apim.output('application/json');
        console.info("Response Sent Successfully");
    });
  }
});

Below example describes reading input stream from preceding component

hm.response.set('Content-Type','application/json');
apim.readInputAsJSON(function(err, json) {
        if (err) throw err;
        // Update the transactional payload to that just fetched
            apim.setvariable('message.body', json.Customer);
            apim.output('application/json');
        console.info("Response Sent Successfully");
});

session object represents session of GatewayScript action. This is an implicit object and no import required.

session.input.readAsJSON(function (error, jsonObject) {
           session.output.write(jsonObject);
           console.info("readAsJSON success: %s", jsonObject);
           console.info("Read/Write Payload Demo Complete");
});

Service Metadata module used to read service variables

var sm = require('service-metadata');
var list = sm.list();
console.log(list);
console.log(sm.getVar('var://service/protocol-method'));
session.output.write(sm.getVar('var://service/protocol-method'));
session.output.write(sm.getVar('var://service/URL-out'));
session.output.write(sm.getVar('var://service/URL-in'));

Comments

Popular posts from this blog

Spring boot Kafka Integration

Spring boot SOAP Web Service Performance