-
Create the Router Multi-Protocol Gateway
This section will show case creating the MPGW for the Router service.
The Router service will interrogate the input body of the request, and based on the input being, “name”, “accounts”, or “beneficiaries”, will direct traffic to a back endpoint based on the input.
- Log into DataPower and into a sandbox application domain for this lab.
- Click on the File Management icon. Click on the Actions… for the local:/// directory and click on Create Subdirectory.
- Create the file directory Router_MPG.
- Upload the router.js file.
NOTE: This router.js script uses DataPower GatewayScript APIs, which are similar to DataPower extension function on the XSLT side. In the script, you will see that the skipBackside and setVar are used to update DataPower content variables.
The “service.mpgw.skipBackside = true” tells the gateway that there is no backside endpoint and to loop back the request or service output to the client.
The “service.setVar (‘var://service/routing-url’, ‘<endpoint-here>’)” tells the gateway what the backside endpoint will be.
This script will be the primary logic where the input contains a certain key value, the javascript IF statement will trigger the extension setVar() function which directs the traffic to the backend depending on the input key value. - Navigate back to the main page and click on the Multi-Protocol Gateway icon. Click on Add.
- When the Configure Multi-Protocol Gateway windows opens, ensure name the service Router_MPG, then right under, select dynamic-backends type, then locate the Request Type and Response Type, and change them both to Non-XML.
- Locate the Front Side Protocol and click on the Add icon. Select the HTTP Handler.
Once in the Configure HTTP Handler, title the name HTTP_Router_FSH. Enter in the port 2050, or an open port available for your to use (remember this port since it will be used later to test with). Check the GET method to enable and click Apply. - Back in the Configure Multi-Protocol Gateway, locate the Processing Policy and click the add icon.
- When the Configuration Multi-Protocol Gateway Style Policy pops up, title the Policy Name: Router_Policy and click Apply Policy.
- Click on New Rule, drop down the Rule Direction to select Client to Server. Double-click the Match Action icon and use the “default-accept-service-providers” Matching Rule. Click Done.
- Locate the GatewayScript Action icon, drag and drop after the Match Action and double click to open. Once opened, locate the GatewayScript file section, drop down the first section to select local:///Router_MPG, and drop down the section section below it and select router.js. Leave the input and output as default, it will automatically update once the configuration is saved. Select Done.
- Once back in the Configure Multi-Protocol Gateway Style Policy window, click on the Apply Policy, then click on New Rule again to create a response rule.
Update the Rule Name to “Router_Policy_rule_response” and drop down the Rule Direction and select Server to Client.
Double-click on the Match Action and use the “default-accept-service-providers“.
Click Apply Policy.
Click the link on the upper right Close Window. - Back in the Configure Multi-Protocol Gateway, click Apply and Save Configuration.
-
Testing & Conclusion
From Postman or using cURL, invoke the Router_MPG service and you should see different responses based on the content sent.
For example, if there is a request for “name”, the response will bring back profile details:
If there is a request for “accounts”, the response will bring back account details:
If there is a request for “beneficiaries”, the response will bring back beneficiary details:As you can see, the router.js is similar to the content based routing from a stylesheet IBM tech note: https://www.ibm.com/support/pages/content-based-routing-stylesheet-using-routing-url-method-websphere-datapower-soa-appliance
The <dp-set-target> and <dp:set-variable name=”‘var://service/routing-url'” value=”$targetURL”/> extension functions are similar to “setVar(‘var://service/routing-url’)” found in the router.js script as shown below.
The gatewayscript apis used in the router.js script may be used to route requests to multiple backends based on different content, such as:
- the inbound request URI:
- serviceVars.URI
- a certain HTTP Header:
- var hm = require(‘header-metadata’);
- query paramters:
- var query = new String(service.URI);
var querys = query.split(“=”);
- var query = new String(service.URI);
More GatewayScript APIs may be found in the IBM DataPower Knowledge Center under the GatewayScript APIs section.
More service variables may be found in the IBM DataPower Knowledge Center under the Service Variables section.
var hm = require('header-metadata');
var service = require('service-metadata');
//Read input
session.input.readAsBuffers(function (error, input) {
if (error) {
// handle error
session.output.write (error.errorMessage);
}
else {
if (input == '') {
session.output.write("Please enter name, accounts, or beneficiaries.");
service.mpgw.skipBackside = true;
}
//If input contains "name", route traffic to /profile endpoint.
if (input.indexOf('name') > -1) {
service.setVar('var://service/routing-url', 'http://localhost:2000/profile');
}
//If input contains "accounts", route traffic to /accounts endpoint.
if (input.indexOf('accounts') > -1) {
service.setVar('var://service/routing-url', 'http://localhost:2000/accounts');
}
//If input contains "beneficiaries", route traffic to /beneficiaries endpoint.
if (input.indexOf('beneficiaries') > -1) {
service.setVar('var://service/routing-url', 'http://localhost:2000/beneficiaries');
}
}
}); - the inbound request URI:
-
Artifacts
- Router_MPG.zip –> Solution exportrunning on port 2050.
- Mock_XMLF.zip –> The mock service running on port 2000.
The post DataPower: Content based routing from a GatewayScript appeared first on IBM Developer Recipes.