Friday, May 30, 2014

API Gateway How to condition a result based on some JSON data


problem:

We need to develop the policy routing flow based on the incoming message What is the way to do the content based routing and condition based routing in OAG. 

For example I have two (may be more) types of JSON message response from a service . I want to set the message based on the JSON response from service . What is the way to develop in this in OAG. . 

Here is the two JOSN response from service that OAG is calling . 

JSOA Type-1 
========================================== 
{"soapenv:Envelope":{"@xmlns:soapenv":"http://schemas.xmlsoap.org/soap/envelope/","env:Header":{"@xmlns:env":"http://schemas.xmlsoap.org/soap/envelope/","wsse:Security":{"@xmlns:wsse":"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"}},"env:Body":{"@xmlns:env":"http://schemas.xmlsoap.org/soap/envelope/","mes:createUserAccountResponse":{"@xmlns:mes":"http://celgene.com/security/messages_v2","mes:createUserAccountOutput":{"mes:status":"SUCCESS"}}}}} 

JSON Type-2 
========================================== 
{"soapenv:Envelope": {"@xmlns:soapenv": "http://schemas.xmlsoap.org/soap/envelope/", "env:Header": {"@xmlns:env": "http://schemas.xmlsoap.org/soap/envelope/"}, "env:Body": {"@xmlns:env": "http://schemas.xmlsoap.org/soap/envelope/", "env:Fault": {"@xmlns:xs": "http://www.w3.org/2001/XMLSchema", "@xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance", "faultcode": "soapenv:Server", "faultstring": null, "detail": {"java:UserAlreadyExistsException": {"@xmlns:java": "http://celgene.com/security/exceptions_v2", "java:message": "UserAlreadyExists" }}}}} 


For JSON Msg 1 I want to set the message 
========================================== 
{"Message": "User Created successfully"} 

For JSON Msg 2 I want to set the message 
========================================== 
{"Message": "UserAlreadyExists"} 
SOLUTION:


We can implement this using the Scripting Language filter as follows: 

function invoke(msg) { 

// get the body as JsonNode 
var node = com.vordel.mime.JSONBody.getJSON(msg); 

// get the root 
var root = node.path("soapenv:Envelope"); 

// get the value of reference from body 
var envBody = root.path("env:Body"); 

// get the value of reference from Fault 
var envFault = envBody.path("env:Fault"); 

if (envFault .size() != 0) 
// Follow the line for the message with error 
// get the value of reference from detail 
var detail = envFault.path("detail"); 

// get the value of reference from UserAlreadyExistsException 
var javaUser = detail.path("java:UserAlreadyExistsException"); 

// get the value of reference from message 
var javaMessage = javaUser.path("java:message"); 

// get the text value 
var message = javaMessage.getTextValue(); 

// put the data in the result 
msg.put("message", "UserAlreadyExists"); 
return true; 

// get the value of reference from createUserAccountResponse 
var mesCreateUserAccountResponse = envBody.path("mes:createUserAccountResponse"); 
if (mesCreateUserAccountResponse .size() !=0) 
// Follow the line for the Success 


// get the value of reference from mesCreateUserAccountResponse 
var mesCreateUserAccountOutput = mesCreateUserAccountResponse.path("mes:createUserAccountOutput"); 

// get the value of reference from mesCreateUserAccountOutput 
var mesStatus = mesCreateUserAccountOutput.path("mes:status"); 

// get the text value 
var message = mesStatus.getTextValue(); 

// put the data in the result 
msg.put("message", "User Created successfully"); 

return true; 




1 comment:

  1. To conditionally modify the result from an API Gateway, you can typically use policies or rules within the API Gateway tool you are working with. How File Parse Here's a general outline of how you might approach this.

    ReplyDelete