...
Method | Arguments | Description |
---|---|---|
com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService | execute a GET operation and return the result object | |
url | a URL that includes host and port of the web service, e.g. http://localhost:44445/jobscheduler/agent/api/ | |
com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand | execute a configurable operation and return the result object | |
operation | one of the HTTP verbs:
POST|PUT can be parameterized with the body
| |
url | a URL that includes host and port of the web service, e.g.http://localhost:44445/jobscheduler/agent/api/ | |
com.sos.jitl.restclient.JobSchedulerRestClient.getRestService | execute the web service operation using GET | |
host | hostname or ip address of the web service, e.g.
| |
port | port for which the web service is accessible, e.g. 4445 | |
path | path and query string of the web service URL, e.g.
| |
protocol | protocol in use including | |
com.sos.jitl.restclient.JobSchedulerRestClient.postRestService | execute the web service operation using POST | |
host | hostname or ip address of the web service, e.g.
| |
port | port for which the web service is accessible, e.g. 4445 | |
path | path and query string of the web service URL, e.g.
| |
body | The body for the POST | |
com.sos.jitl.restclient.JobSchedulerRestClient.putRestService | execute the web service operation using PUT | |
host | hostname or ip address of the web service, e.g.
| |
port | port for which the web service is accessible, e.g. 4445 | |
path | path and query string of the web service URL, e.g.
| |
body | The body for the PUT |
Adding Header items
...
com.sos.jitl.restclient.JobSchedulerRestClient.setProxy | set Http proxy without credentials | |
proxyHost | hostname or ip address of the http proxy | |
proxyPort | port of the http proxy | |
com |
...
.sos.jitl.restclient.JobSchedulerRestClient. |
...
setProxy | set Http proxy with credentials if necessary | |
proxyHost / proxyPort | see setProxy without credentials | |
proxyUser | user of the http proxy if necessary | |
proxyPassword | user's password of the http proxy if necessary |
Adding Header items
- The header "Accept=application/json" will automatically be added.
- You can add header items with
com
...
Integration with JavaScript
- Basically a web service call is implemented by executing one of the above methods from the JITL Java class, e.g.:
var response = com.sos.jitl.restclient.JobSchedulerRestClient.headers.executeRestServiceput( "http://localhost:44445/jobscheduler/agent/api/" );"header","value");
Integration with JavaScript
- Basically a web service call is implemented by executing one of the above methods from the JITL Java class, e.g.:
var response = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService( "http://localhost:4445/jobscheduler/agent/api/" );
var response
var response = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand( "get", "http://localhost:444454445/jobscheduler/agent/api/" );
- The web service returns a serialized JSON Object:
- The result object returned by the web service can be converted to a JavaScript JSON object by use of the
eval()
function:eval ( "var jsonObject = " + response + ";" );
- Properties of the JSON object can be accessed within the object hierarchy provided by the web service result object like this:
see example for
jsonObject.system.hostname
- The result can be parsed using the SOSXMLXPath Java class.
- see example below
from the JobScheduler Agent REST web service
below:
Code Block language js title Example for REST web service client processing a JSON response linenumbers true collapse true function spooler_process() { var
- The result object returned by the web service can be converted to a JavaScript JSON object by use of the
...
response = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService( "http://
...
localhost:4445/jobscheduler/agent/api/" );
...
if (response) { eval ( "var jsonObject = " + response + ";" ); spooler_log.info(
...
jsonObject.totalTaskCount + "
...
tasks executed on Agent [" + rest_service + "]: " + jsonObject.system.hostname ); } else { spooler_log.error( "no response from REST web service at: " + rest_service ); } return false; }
- The web service returns an XML document:
- The result can be parsed using the SOSXMLXPath Java class.
see example below
Code Block language js title Example for REST web service client processing an XML response linenumbers true collapse true function spooler_process() { var response = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService( "http://www.thomas-bayer.com/sqlrest/CUSTOMER/18/" ); spooler_log.info( response ); var xmlDOM = new Packages.sos.xml.SOSXMLXPath( new java.lang.StringBuffer( response ) ); spooler_log.info( "Firstname is " + xmlDOM.selectSingleNodeValue( "//CUSTOMER/FIRSTNAME" )); return false; }
- The result can be parsed using the SOSXMLXPath Java class.
Examples
Simple standalone Job with REST Client
- JavaScript example for use with a job: this job requests the status information from the JobScheduler Universal Agent and returns the total number of tasks that have been executed during the Agents' lifetime. This job reads the web service URL from a job parameter.
- Download: rest_client.job.xml
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<job stop_on_error="no" >
<params >
<param name="agent_service" value="localhost:4445/jobscheduler/agent/api/"/>
</params>
<script language="java:javascript">
<![CDATA[
function spooler_process() {
var parameters = spooler.create_variable_set();
parameters.merge( spooler_task.params );
var agent_service = parameters.value( "agent_service" );
var response = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService( agent_service );
// alternative REST methods
// var response = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService( "http://localhost:4445/jobscheduler/agent/api/" );
// var response = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand("get", "http://localhost:4445/jobscheduler/agent/api/" );
// var response = com.sos.jitl.restclient.JobSchedulerRestClient.getRestService( "localhost", 4445, "/jobscheduler/agent/api/", "http" );
if (response) {
eval ( "var jsonObject = " + response + ";" );
spooler_log.info( jsonObject.totalTaskCount + " tasks on Agent [" + agent_service + "]: " + jsonObject.system.hostname );
} else {
spooler_log.error( "no response from Agent web service at: " + agent_service );
}
return false;
}
]]>
</script>
<run_time />
</job> |
Full standalone Job with GET|POST|PUT|DELETE and XML Responses
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
<job stop_on_error="no" >
<params/ >
<script language="java:javascript">
<![CDATA[
function spooler_process() {
//Set proxy if necessary
// com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService( "proxyHost", proxyPort );
//or with credentials
// com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService( "proxyHost", proxyPort, "proxyUser", "proxyPassword" );
//Getting the name of a person
var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService( "http://www.thomas-bayer.com/sqlrest/CUSTOMER/18/" );
spooler_log.info( s );
var xmlDOM = new Packages.sos.xml.SOSXMLXPath( new java.lang.StringBuffer( s ) );
spooler_log.info( "Firstname is " + xmlDOM.selectSingleNodeValue( "//CUSTOMER/FIRSTNAME" ));
//Creating a person with post
com.sos.jitl.restclient.JobSchedulerRestClient.headers.put("Content-Type", "application/xml");
var post = "post(<resource><ID>4711</ID><LASTNAME>world</LASTNAME><FIRSTNAME>Hello</FIRSTNAME></resource>)";
var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand(post, "http://www.thomas-bayer.com/sqlrest/CUSTOMER/" );
//Reading the new entry
var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService( "http://www.thomas-bayer.com/sqlrest/CUSTOMER/4711/" );
var xmlDOM = new Packages.sos.xml.SOSXMLXPath( new java.lang.StringBuffer( s ) );
spooler_log.info( "Name is " + xmlDOM.selectSingleNodeValue( "//CUSTOMER/FIRSTNAME" ) + " " + xmlDOM.selectSingleNodeValue( "//CUSTOMER/LASTNAME" ));
//Changing the name
com.sos.jitl.restclient.JobSchedulerRestClient.headers.put("Content-Type", "application/xml");
var post = "post(<resource><FIRSTNAME>Uwe</FIRSTNAME></resource>)";
var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand(post, "http://www.thomas-bayer.com/sqlrest/CUSTOMER/4711/" );
//Reading the name
var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService( "http://www.thomas-bayer.com/sqlrest/CUSTOMER/18/" );
var xmlDOM = new Packages.sos.xml.SOSXMLXPath( new java.lang.StringBuffer( s ) );
spooler_log.info( "Firstname is " + xmlDOM.selectSingleNodeValue( "//CUSTOMER/FIRSTNAME" ));
//Deleting the new entry
com.sos.jitl.restclient.JobSchedulerRestClient.headers.put("Content-Type", "application/xml");
var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand("delete", "http://www.thomas-bayer.com/sqlrest/CUSTOMER/4711/" );
//Reading the deleted entry
var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService( "http://www.thomas-bayer.com/sqlrest/CUSTOMER/4711/" );
spooler_log.info( s);
//Creating a person with put
com.sos.jitl.restclient.JobSchedulerRestClient.headers.put("Content-Type", "application/xml");
var put = "put(<resource><LASTNAME>world</LASTNAME><FIRSTNAME>Hello</FIRSTNAME></resource>)";
var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand(put, "http://www.thomas-bayer.com/sqlrest/CUSTOMER/4711" );
//Reading the new entry
var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService( "http://www.thomas-bayer.com/sqlrest/CUSTOMER/4711/" );
var xmlDOM = new Packages.sos.xml.SOSXMLXPath( new java.lang.StringBuffer( s ) );
spooler_log.info( "Name is " + xmlDOM.selectSingleNodeValue( "//CUSTOMER/FIRSTNAME" ) + " " + xmlDOM.selectSingleNodeValue( "//CUSTOMER/LASTNAME" ));
//Deleting the new entry
com.sos.jitl.restclient.JobSchedulerRestClient.headers.put("Content-Type", "application/xml");
var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand("delete", "http://www.thomas-bayer.com/sqlrest/CUSTOMER/4711/" );
//Reading the deleted entry
var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService( "http://www.thomas-bayer.com/sqlrest/CUSTOMER/4711/" );
spooler_log.info( s);
return false;
}
]]>
</script>
<run_time />
</job> |
Full Job Chain with GET|POST|PUT|DELETE and JSON Responses
Download the example: rest_client.zip
Job: rest_client
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
<job stop_on_error="no" order="yes">
<params />
<script language="java:javascript">
<![CDATA[
function spooler_process() {
// for sample REST servivce see https://github.com/typicode/jsonplaceholder#how-to
var parameters = spooler.create_variable_set();
parameters.merge( spooler_task.params );
parameters.merge( spooler_task.order.params );
var rest_service = parameters.value( "rest_service" );
var rest_operation = parameters.value( "rest_operation" );
var rest_content_type = parameters.value( "rest_content_type" );
switch( rest_operation.toLowerCase() ) {
case "get":
var response = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand( rest_operation, rest_service );
if (response) {
eval ( "var jsonObject = " + response + ";" );
spooler_log.info( "REST service GET response [" + rest_service + "]: " + jsonObject.title );
} else {
spooler_log.error( "no response from REST service at: " + rest_service );
}
break;
case "post":
// use post( ... ) to forward content to REST service
switch ( rest_content_type.toLowerCase() ) {
case "xml":
com.sos.jitl.restclient.JobSchedulerRestClient.headers.put("Content-Type", "application/xml");
var post = "post( <resource><ID>4711</ID><LASTNAME>world</LASTNAME><FIRSTNAME>Hello</FIRSTNAME></resource> )";
var response = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand( post, rest_service )
if (response) {
spooler_log.info( "REST service POST response [" + rest_service + "]: " + response );
} else {
spooler_log.error( "no response from REST service at: " + rest_service );
}
break;
case "json":
com.sos.jitl.restclient.JobSchedulerRestClient.headers.put( "Content-Type", "application/json" );
var post = "post( { title: 'foo', body: 'bar', userId: 1 } )";
var response = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand( post, rest_service );
if (response) {
eval ( "var jsonObject = " + response + ";" );
spooler_log.info( "REST service POST response [" + rest_service + "]: " + jsonObject.title );
} else {
spooler_log.error( "no response from REST service at: " + rest_service );
}
break;
default:
spooler_log.error( "invalid content type [rest_content_type]: " + rest_content_type );
var response = "";
}
break;
case "put":
// use put( ... ) to forward content to REST service
|
Examples
Simple REST Client Example
- JavaScript example for use with a job: this job requests the status information from the JobScheduler Universal Agent and returns the total number of tasks that have been executed during the Agents' lifetime. This job reads the web service URL from a job parameter.
- Download: rest_client.job.xml
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<job stop_on_error="no" > <params > <param name="agent_service" value="localhost:4445/jobscheduler/agent/api/"/> </params> <script language="java:javascript"> <![CDATA[ function spooler_process() { var parameters = spooler.create_variable_set(); parameters.merge( spooler_task.params ); var agent_service = parameters.value( "agent_service" ); var response = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService( agent_service ); // alternative REST methods // var response = com.sos.jitl.restclient.JobSchedulerRestClient.headers.executeRestServiceput( "http://localhost:44445/jobscheduler/agent/api/Content-Type", "application/json" ); // var responseput = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand("get", "http://localhost:4445/jobscheduler/agent/api/" ); //"put({ title: 'foo', body: 'bar', userId: 1 })"; var response = com.sos.jitl.restclient.JobSchedulerRestClient.getRestServiceexecuteRestServiceCommand( "localhost", 4445, "/jobscheduler/agent/api/", "http" put, rest_service ); if (response) { eval ( "var jsonObject = " + response + ";" ); spooler_log.info( jsonObject.totalTaskCount + "REST tasksservice onPUT Agentresponse [" + agentrest_service + "]: " + jsonObject.system.hostnametitle ); } else { spooler_log.error( "no response from Agent webREST service at: " + agentrest_service ); } return (response != ""); } ]]> ); </script> <run_time /> </job> |
Full Example with GET|POST|PUT|DELETE and XML Responses
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
function spooler_process() { } break; case "delete": var sresponse = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceexecuteRestServiceCommand( "http://www.thomas-bayer.com/sqlrest/CUSTOMER/18/" ); spooler_log.info( s); var xmlDOM = new Packages.sos.xml.SOSXMLXPath( new java.lang.StringBuffer( s ) ); delete", rest_service ); if (response) { spooler_log.info( "FirstnameREST isservice "DELETE + xmlDOM.selectSingleNodeValue( "//CUSTOMER/FIRSTNAME" )); //Creating a person with post com.sos.jitl.restclient.JobSchedulerRestClient.headers.put("Content-Type", "application/xml"); var post = "post(<resource><ID>4711</ID><LASTNAME>world</LASTNAME><FIRSTNAME>Hello</FIRSTNAME></resource>)"; var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand(post, "http://www.thomas-bayer.com/sqlrest/CUSTOMER/" ); //Reading the new entry var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService( "http://www.thomas-bayer.com/sqlrest/CUSTOMER/4711/" ); var xmlDOM = new Packages.sos.xml.SOSXMLXPath( new java.lang.StringBuffer( s ) ); spooler_log.info( "Name is " + xmlDOM.selectSingleNodeValue( "//CUSTOMER/FIRSTNAME" ) + " " + xmlDOM.selectSingleNodeValue( "//CUSTOMER/LASTNAME" )); //Changing the name com.sos.jitl.restclient.JobSchedulerRestClient.headers.put("Content-Type", "application/xml"); var post = "post(<resource><FIRSTNAME>Uwe</FIRSTNAME></resource>)"; var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand(post, "http://www.thomas-bayer.com/sqlrest/CUSTOMER/4711/" ); //Reading the name var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService( "http://www.thomas-bayer.com/sqlrest/CUSTOMER/18/" ); var xmlDOM = new Packages.sos.xml.SOSXMLXPath( new java.lang.StringBuffer( s ) ); spooler_log.info( "Firstname is " + xmlDOM.selectSingleNodeValue( "//CUSTOMER/FIRSTNAME" )); //Deleting the new entry com.sos.jitl.restclient.JobSchedulerRestClient.headers.put("Content-Type", "application/xml"); var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand("delete", "http://www.thomas-bayer.com/sqlrest/CUSTOMER/4711/" ); //Reading the deleted entry var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService( "http://www.thomas-bayer.com/sqlrest/CUSTOMER/4711/" ); spooler_log.info( s); //Creating a person with put com.sos.jitl.restclient.JobSchedulerRestClient.headers.put("Content-Type", "application/xml"); var put = "put(<resource><LASTNAME>world</LASTNAME><FIRSTNAME>Hello</FIRSTNAME></resource>)"; var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand(put, "http://www.thomas-bayer.com/sqlrest/CUSTOMER/4711" ); //Reading the new entry var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService( "http://www.thomas-bayer.com/sqlrest/CUSTOMER/4711/" ); var xmlDOM = new Packages.sos.xml.SOSXMLXPath( new java.lang.StringBuffer( s ) ); spooler_log.info( "Name is " + xmlDOM.selectSingleNodeValue( "//CUSTOMER/FIRSTNAME" ) + " " + xmlDOM.selectSingleNodeValue( "//CUSTOMER/LASTNAME" )); //Deleting the new entry com.sos.jitl.restclient.JobSchedulerRestClient.headers.put("Content-Type", "application/xml"); var s =response [" + rest_service + "]: " ); } else { spooler_log.error( "no response from REST web service at: " + rest_service ); } break; default: spooler_log.error( "invalid REST operation (get|put|post|delete): " + rest_operation ); var response = ""; } return (response != ""); } ]]> </script> <run_time /> </job> |
Job Chain: rest_client
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
<job_chain title="Call REST service with HTTP GET|POST|PUT|DELETE operation" name="rest_client">
<job_chain_node state="rest_client" job="rest_client" next_state="success" error_state="error"/>
<job_chain_node state="success"/>
<job_chain_node state="error"/>
</job_chain> |
Orders: rest_client
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
<order job_chain="rest_client" id="rest_client_post_json">
<params >
<param name="rest_service" value="http://jsonplaceholder.typicode.com/posts"/>
<param name="rest_operation" value="post"/>
<param name="rest_content_type" value="json"/>
</params>
<run_time />
</order> |
Find additional orders for REST operations with the download of this example.
Example for HTTPS
- We assume that a SSL certificate is imported in a Java truststore
/path/to/java/myTruststore.jks
- Please note that the REST client varify the hostname of the URL with the certificate
Code Block language xml collapse true <job stop_on_error="no" > <params/> <script language="java:javascript"> <![CDATA[ function spooler_process() { //set location of the java truststore with ssl certificates java.lang.System.setProperty("javax.net.ssl.trustStore", "/path/to/java/truststore.jks"); //add an authorization if necessary com.sos.jitl.restclient.JobSchedulerRestClient.
...
headers.put("
...
Authorization",
...
"Basic cm9vdDpyOm9vdA=="); //
...
request
...
with
...
https
...
url var
...
response = com.sos.jitl.restclient.JobSchedulerRestClient.
...
executeRestServiceCommand("get", "
...
https://
...
..
...
.
...
" ); spooler_log.info(
...
response); return false; }
...
]]> </script> <run_time/> </job>
References
See also
...