Scripting - Example

Example

For our example we use a RestService for a faculty calculation. We use 2 GET methods in this example to have to different outputs for our calculation.

  1. Answer in plain text format
  2. HTML formatted answer, you can check in your browser 

Requirements

You can download the interface here.

Unzip the file and install the interface by creating a new Business Connector with the name DemoConnectors (for example) and import the interface via the Create new interface window.

Select the demoScripting.cadapter file and press Save to upload the file into the system. 

If the import was successful, you will see something like the next screen.

Business connector / Interface and Flows

As you can see we now have a business connector with the name demoConnectors. Within this business connector we find an interface with the name demoScripting. This interface has two flows, directChannel and HTMLChannel

 

How it works 

As you can see the interface starts at the directChannels HTTP/Rest Listener. The following settings are set for the HTTP/Rest Listener: 

For this example we use a get method, we can use two calls on this listener

  • http:/url/factorial/<number>
  • http:/url/factorial/html/<number>

The next step is the Header Enricher. In this step we create 2 header properties

  • fac_input, for getting the input paramter
  • outputtype, for setting the output type parameter

Both will be done with a script. The fac_input will be determined by the script GetInputParameter.js and is selected in the Reference to script selection box. 

See that the Header Function is set to Script. The script,GetInputParamter.js, looks like this:

// Please enter javascript here
// Available objects :
//	log : Object of class org.slf4j.Logger. Can be used to write logmessages to the interface logfile.
//	connectmsg : Object of class com.opdion.myesb.eaf.pojo.interfaces.ConnectMessage. Holds the incomming ConnectMessage
//	properties : Object of class java.util.Properties. Holds the properties of the incomming ConnectMessage

// main
(function(){
    log.info("Enter GetInputParameter.js");
    var request = properties.get("http_requestUrl");
    log.info("http_requestUrl : {}",request);
    if (typeof request != "string") {
        return "";
    }
    var idx = request.lastIndexOf("/");
    if (idx < 0) {
        return "";
    }
    var input = request.substring(idx+1);
    log.info("Input : {}" ,input);
    return input;
})();

// sub functions here
// function foo(param) { 
// }

The script will take the request URL and checks the last part of the input paramter and returns it's value. This value is the factorial you want to know. 

The next header value, outputtype, is the format you want your answer in.  

// Please enter javascript here
// Available objects :
//	log : Object of class org.slf4j.Logger. Can be used to write logmessages to the interface logfile.
//	connectmsg : Object of class com.opdion.myesb.eaf.pojo.interfaces.ConnectMessage. Holds the incomming ConnectMessage
//	properties : Object of class java.util.Properties. Holds the properties of the incomming ConnectMessage

// main
/**
 * outputtype is (if!) defined between the contextpath factorial/ and the last /
 * like http://localhost:8088/factorial/html/4
 */
(function(){
    log.info("Enter GetOutputTypeParameter.js");
    var request = properties.get("http_requestUrl");
    log.info("http_requestUrl : {}",request);
    if (typeof request != "string") {
        return "";
    }
    var idxInputParam = request.lastIndexOf("/");
    if (idxInputParam < 0) {
        return "";
    }
    var idxOutputParam = request.indexOf("/factorial/") + 11;
    if (idxOutputParam < 0) {
        return "";
    }
    var outputtype = "";
    if (idxOutputParam < idxInputParam) {
        outputtype = request.substring(idxOutputParam,idxInputParam);
    }
    log.info("OutputType : {}" ,outputtype);
    return outputtype;
})();

// sub functions here
// function foo(param) { 
// }

The value can be an empty string, or html.

The next part of our service is a User-defined Service, but this time we use a javascript, Factorial.js, which looks like this:

// Please enter javascript here
// Available objects :
//	log : Object of class org.slf4j.Logger. Can be used to write logmessages to the interface logfile.
//	connectmsg : Object of class com.opdion.myesb.eaf.pojo.interfaces.ConnectMessage. Holds the incomming ConnectMessage
//	properties : Object of class java.util.Properties. Holds the properties of the incomming ConnectMessage

// main
(function(){
	// var Long = Java.type("java.lang.Long");
	var Integer = Java.type("java.lang.Integer");
	log.debug("This function will calculate the factorial of the number in property fac_input");
	var input = Integer.valueOf(properties.getProperty("fac_input"));
	var result = findFactorial(input);
	log.info("{}! = {}",input,result);
	return "" + result;
})();

// sub functions here

function findFactorial(n) { 
	return (n <= 1) ? 1 : n * findFactorial(n - 1); 
}

This script will calculate the factorial and returns the result as a string.

Now it is time to return the result in the way you want it, so in HTML or as plain text. For this we use a Filter. The filter itself uses a javascript with the name IsOutputTypeHTML.js.

// Please enter javascript here
// Available objects :
//	log : Object of class org.slf4j.Logger. Can be used to write logmessages to the interface logfile.
//	connectmsg : Object of class com.opdion.myesb.eaf.pojo.interfaces.ConnectMessage. Holds the incomming ConnectMessage
//	properties : Object of class java.util.Properties. Holds the properties of the incomming ConnectMessage

// main
(function(){
    log.info("Enter IsOutputTypeHTML.js");
    var result = false;
    var outputtype = properties.get("outputtype");
    if (typeof outputtype == "string") {
        log.info("outputtype property : {}",outputtype);
        if (outputtype.toUpperCase() == "HTML") {
            result = true;
        }
    }
    log.info("Output as HTML : {}",result);
    return result;
})();

// sub functions here
// function foo(param) { 
// }

This filter will return the result TRUE if you output type has the value HTML, otherwise it will return false. The filter is inversed, so this means that, if the value is TRUE, the value will be send to the discard flow, which in our case is the flow demoScripting.HTMLChannel

Sending the result to the browser in text format

If you sent your answer in plain text format, we first have to set the content-type for the anser to text/plain, we do that in our next Header Enricher.

The content type is now written to our message header as text/plain

In the last part of our flow, you will see the Message Reply producer, which returns the message to the browser in plain text format.

Sending it to your browser in HTML format

As we have arrived in our HTML part of the interface via the Discard flow property of the Filter, we see a User-defined service which is a Javascript with the name CreateHTMLParts.js

// Please enter javascript here
// Available objects :
//	log : Object of class org.slf4j.Logger. Can be used to write logmessages to the interface logfile.
//	connectmsg : Object of class com.opdion.myesb.eaf.pojo.interfaces.ConnectMessage. Holds the incomming ConnectMessage
//	properties : Object of class java.util.Properties. Holds the properties of the incomming ConnectMessage

// main
/**
 * This will add a msgprt with name html with the answer in HTML
 */
(function(){
    log.info("Entering CreateHTMLPart.js")
    // input is in property fac_input
    var input = properties.get("fac_input");
    // answer is as string in msgprt0
    var answer = connectmsg.getPartById("msgprt0").getStringContent();
    
    // create a new string part
    var ConnectMessageFactory =  Java.type("com.opdion.myesb.eaf.pojo.impl.ConnectMessageFactoryImpl");
    var part = ConnectMessageFactory.getFactory().createStringMessagePart();
    
    // create and set part content
    var htmlString = "<html><H1>The factorial of " + input + "! = " + answer + "</H1></html>";
    part.setStringContent(htmlString);
    log.info("Set content to {}",htmlString );
    
    // add the new part
    connectmsg.addPart("HTML",part);

    log.info("ConnectMsg : {}",connectmsg.toString() );    
    return connectmsg;
})();

// sub functions here
// function foo(param) { 
// }

In this script we create a complete new message with the name for our messagePart as HTML. Because we return a complete new Message, the MessagePart-Out will be discarded.

After this, the Header Enricher will set the content-type in de header to text/html

The anwser can now be displayed as a HTML page via the Message-Reply producer