RESTful Webservices in todo4teams scripts
Within the chapter RESTful Webservices we have shown how jobs in todo4teams may be generated, changed or requested by means of a REST-API.
Todo4teams acts as a server while you implement the client.
Here we go the reverse way and make todo4teams to the client of an external RESTful webservice. They can be used in the decribed way for the interaction with interfaces, ie Microsoft SharePoint, Salesforce, SAP, or many other systems. You can find out how to access the appropriate target system via RESTful web services via the documentation of the corresponding software solution.
To find a basic introduction to REST and JSON please use a search engine of your choice.
This example shows you how to change a purchase order record in an order management system.
The record to be changed has the ID 7654. To change it an HTTP-PUT request must be sent to the URL http://ordermanagemement.mycompany.net:8080/OrderManagement/rest/orders/7654, which contains the user data as JSON construct.
The Java libraries of the jersey project (jersey.java.net) as well as net.sf.json for the JSON handling are located in the (server-side) todo4teams scripts (start action, takeover action, end action).
Since many Java classes are used by third-party libraries our script prepares the use in Javascript with the following lines and makes the Java types known to the script:
var StringArray = Java.type("java.lang.String[]");
var JsonType = Java.type("net.sf.json.JSONObject");
var ClientConfig = Java.type("org.glassfish.jersey.client.ClientConfig");
var ClientBuilder = Java.type("javax.ws.rs.client.ClientBuilder");
var Entity = Java.type("javax.ws.rs.client.Entity");
var UriBuilder = Java.type("javax.ws.rs.core.UriBuilder");
The following lines create a "WebTarget" - the target system of our service. The target system accepts json as a data exchange format.
var accept = new StringArray(1);
accept[0]=new java.lang.String("application/json");
var config = new ClientConfig();
var client = ClientBuilder.newClient(config);
var target = client.target(UriBuilder.fromUri("http://ordermanagemement.mycompany.net:8080/OrderManagement").build());
The user data are prepared as a JSON Object in order to be able to transfer them comfortably to the JSON format:
var orderid = 7654;
json.put("id", orderid);
json.put("state", 1);
json.put("approvalComment", "Ok, approved!");
The actual REST call is thus as follows. The user data request is converted to a JSON string and then sent to the destination:
accept[0]=new java.lang.String("application/json");
var response = target.path("rest").path("orders").path(orderid).request(accept).put(Entity.json(payload));
The request to the path method generate the path to the above URL http://ordermanagemement.mycompany.net:8080/OrderManagement/rest/orders/7654. The accept method is used to tell the WebTarget that the data transfer takes place in JSON format. Alternatively, XML could be used when payload is generated as an XML code.
As a result we get in response an object of the type javax.ws.rs.core.Response, which displays the HTTP status 204 via the status attribute "No Content The request was successfully executed, but the response deliberately does not contain any data. ".
For a simple copy & paste again here is again the complete script:
var JsonType = Java.type("net.sf.json.JSONObject");
var ClientConfig = Java.type("org.glassfish.jersey.client.ClientConfig");
var ClientBuilder = Java.type("javax.ws.rs.client.ClientBuilder");
var Entity = Java.type("javax.ws.rs.client.Entity");
var UriBuilder = Java.type("javax.ws.rs.core.UriBuilder");
var config = new ClientConfig();
var client = ClientBuilder.newClient(config);
var target = client.target(UriBuilder.fromUri("http://ordermanagemement.mycompany.net:8080/OrderManagement").build());
var json = new JsonType;
//json.putAll(order);
var orderid = 7654;
json.put("id", orderid);
json.put("state", 1);
json.put("approvalComment", "Ok, approved!");
var payload = json.toString();
var accept = new StringArray(1);
accept[0]=new java.lang.String("application/json");
var response = target.path("rest").path("orders").path(orderid).request(accept).put(Entity.json(payload));