Friday, October 11, 2013

How to Perform REST Call from BPM Server Script

IBM BPM has provided a java connector in the system toollkit which will help performing this rest operation. But that is not very robust and doesn't allow us to perform all the kinds of REST operations. In this post I would like to illustrate on how to invoke a REST service from a server script.
Below is the script which can be used to perform a Rest Call from BPM Server sccript.

var client = new Packages.org.apache.commons.httpclient.HttpClient();
var op = new Packages.org.apache.commons.httpclient.methods.PostMethod(tw.local.restSerUrl);

op.setParameter("action","start");
op.setParameter("param1",tw.local.var1);
op.setParameter("param 2",tw.localvar2);
op.setParameter("params",tw.local.JSONparameters);


op.setRequestHeader(new Packages.org.apache.commons.httpclient.Header("Accept","application/xml"));

var auth = new Packages.org.apache.commons.httpclient.UsernamePasswordCredentials("restapiuser","restapipass");
op.setDoAuthentication(true);
client.getState().setCredentials(null, null, auth);

try {
  var statusCode = client.executeMethod(op);
  tw.local.output = op.getResponseBodyAsString();
  tw.local.response =Packages.com.lombardisoftware.core.XMLUtilities.stringToElement(tw.local.output);

} finally {
  op.releaseConnection();
}

parse the status

if(tw.local.response.getElementByTagName("status").item(0).getText()=="200"){
    tw.local.instanceId= tw.local.response.getElementByTagName("data").item(0).getElementByTagName("element").item(0).getText();
}else{
    tw.local.errorMessage=tw.local.response.toString(true);
  //  tw.local.instanceId=tw.local.response.toString(true);
}

Also check the post to perform rest call in IBM with input as Entity.
Hope this post is help full !!!

Thanks for stopping by. If you like this post leave a comment, share it and do a g +1.

Wednesday, October 2, 2013

How to Convert TWObject to JSON


JSON has been very famous now a days, especially while using REST.
Here is the script to create a JSON out of a teamworks object.

Say I have tw object of type EMPLOYEE with the parameters (name, place,phone). After executing the script below it will give the JSON as below.

statement:
tw.local.json="{\"Employee\":"+createJson(tw.local.employee)+"\""};

result :

tw.local.json  will have the value {"Employee":{"name":"abcd","place":"ongole","phone":"xxxxxx"}}