Skip to end of metadata
Go to start of metadata

Scope

  • Jobs can be implemented to access RESTful web services.
  • A REST web service client is available for Scripting of Jobs and Monitors that are implemented with Java or JavaScript.
  • This feature is provided with  JITL-213 - Getting issue details... STATUS
  • POST and PUT with a body is provided with JITL-272 - Getting issue details... STATUS

Implementation

  • JITL provides a static Java class that can be used with Java and JavaScript. This class accepts a URL and returns an object in JSON syntax.
  • For use with JavaScript the JSON object can be converted to a JavaScript object.

Methods

MethodArgumentsDescription
com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService
 execute a GET operation and return the result object
 urla 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:

GET, POST, PUT, DELETE

POST|PUT can be parameterized with the body

POST(<person><id>4711</id><name>helloworld</name></person>)

 urla 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.

localhost

 port

port for which the web service is accessible, e.g.

4445
 path

path and query string of the web service URL, e.g.

/jobscheduler/agent/api/

 protocol

protocol in use including http, https

com.sos.jitl.restclient.JobSchedulerRestClient.postRestService
 execute the web service operation using POST
 host

hostname or ip address of the web service, e.g.

localhost

 port

port for which the web service is accessible, e.g.

4445
 path

path and query string of the web service URL, e.g.

/jobscheduler/agent/api/

 bodyThe 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.

localhost

 port

port for which the web service is accessible, e.g.

4445
 path

path and query string of the web service URL, e.g.

/jobscheduler/agent/api/

 bodyThe body for the PUT
com.sos.jitl.restclient.JobSchedulerRestClient.setProxy set Http proxy without credentials
 proxyHosthostname or ip address of the http proxy
 proxyPortport of the http proxy
com.sos.jitl.restclient.JobSchedulerRestClient.setProxy set Http proxy with credentials if necessary
 proxyHost / proxyPortsee setProxy without credentials
 proxyUseruser of the http proxy if necessary
 proxyPassworduser'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.sos.jitl.restclient.JobSchedulerRestClient.headers.put("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 = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand( "get", "http://localhost:4445/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 from the JobScheduler Agent REST web service below:

        Example for REST web service client processing a JSON response
        function spooler_process() { 
            var 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

        Example for REST web service client processing an XML response
        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;
        }

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 
     
Job rest_client
<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

Job rest_client processing XML responses
<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

Job rest_client processing JSON responses
<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
		    com.sos.jitl.restclient.JobSchedulerRestClient.headers.put( "Content-Type", "application/json" );
		    var put = "put({ title: 'foo', body: 'bar', userId: 1 })";
		    var response = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand( put, rest_service );
		    if (response) {
        		eval ( "var jsonObject = " + response + ";" );
		        spooler_log.info( "REST service PUT response [" + rest_service + "]: " + jsonObject.title );
		    } else {
		        spooler_log.error( "no response from REST service at: " + rest_service );
		    }
			break;
		case "delete":
		    var response = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand( "delete", rest_service ); 
		    if (response) {
        		spooler_log.info( "REST service DELETE 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

Job Chain rest_client
<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

Order rest_client_post_json
<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
  • <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

Change Management References

T Key Linked Issues Fix Version/s Status P Summary Updated
Loading...
Refresh

Documentation

  • REST (Representational State Transfer)

 

  • No labels
Write a comment…