Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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

 

Adding Header items

...

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

...

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.executeRestServiceCommandheaders.put("getheader","http://localhost:4445/jobscheduler/agent/api/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
    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:

        Code Block
        languagejs
        titleExample for REST web service client processing a JSON response
        linenumberstrue
        collapsetrue
        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

        Code Block
        languagejs
        titleExample for REST web service client processing an XML response
        linenumberstrue
        collapsetrue
        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;
        }

...

  • 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
languagexmljs
titleJob rest_client
collapsetrue
<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>

...

Code Block
languagejs
titleJob rest_client processing XML responses
linenumberstrue
collapsetrue
<job  stop_on_error="no" >
    <params/ >

    <script  language="java:javascript">
        <![CDATA[
function spooler_process() { 
//GettingSet theproxy name of a person
  var s = if necessary
//  com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService( "http://www.thomas-bayer.com/sqlrest/CUSTOMER/18/proxyHost", proxyPort );
//or with credentials
//  com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService( "proxyHost", proxyPort, "proxyUser", "proxyPassword" );

//Getting  spooler_log.info( s );the name of a person
  var xmlDOMs = 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>

...

Code Block
languagejs
titleJob rest_client processing JSON responses
linenumberstrue
collapsetrue
<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 stop_on_error="no" order="yes">
    <params />
    <script  language="java:javascript">= " + response + ";" );
		        spooler_log.info( "REST service GET response [" + rest_service + "]: " + jsonObject.title );
		    } else {
        <![CDATA[
function 		spooler_process() {
    // for sample REST servivce see https://github.com/typicode/jsonplaceholder#how-to
 log.error( "no response from REST service at: " + rest_service );
		    }
			break;
		case "post":
    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":
    		 // 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( rest_operationpost, rest_service );
				    if (response) {
        		eval ( "var jsonObject = " + response + ";" );
		        spooler_log.info( "REST service GETPOST response [" + rest_service + "]: " + jsonObject.titleresponse );
				    } 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() ) { 		spooler_log.error( "no response from REST service at: " + rest_service );
				    }
					break;
            	case "xmljson":
				        		 	com.sos.jitl.restclient.JobSchedulerRestClient.headers.put( "Content-Type", "application/xmljson" );
				    var post = "post( <resource><ID>4711</ID><LASTNAME>world</LASTNAME><FIRSTNAME>Hello</FIRSTNAME></resource> { title: 'foo', body: 'bar', userId: 1 } )";
				    var response = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand( post, rest_service );
				    if (response) {
				if (response) {
        				eval ( "var jsonObject = " + response + ";" );
		        		spooler_log.info( "REST service POST response [" + rest_service + "]: " + responsejsonObject.title );
				    } else {
		        				spooler_log.error( "no response from REST service atREST service at: " + rest_service );
				    }
					break;
				default:
					spooler_log.error( "invalid content type [rest_content_type]: " + rest_servicecontent_type );
					var response =  }"";
			}
	 		break;
		case "put":
            	case "json":// use put( ... ) to forward content to REST service
				    com.sos.jitl.restclient.JobSchedulerRestClient.headers.put( "Content-Type", "application/json" );
				    var postput = "postput( { title: 'foo', body: 'bar', userId: 1 } )";
				    var response = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand( postput, rest_service );
				    if (response) {
        				eval ( "var jsonObject = " + response + ";" );
		        		spooler_log.info( "REST service POSTPUT 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] service at: " + rest_content_typeservice );
					var  response = "";
			}
	 		break;
		case "putdelete":
		    var response = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand( "delete", rest_service ); 
		    if (response) {
     // use put( ... ) to forward content to REST service
		    com.sos.jitl.restclient.JobSchedulerRestClient.headers.put( "Content-Type", "application/json"		spooler_log.info( "REST service DELETE response [" + rest_service + "]: " );
		    var} putelse = "put({
 title: 'foo', body: 'bar', userId: 1 })";
		    var response = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand( put,       		spooler_log.error( "no response from REST web service at: " + rest_service );
		    if (response) {
}
			break;
        default:  
			eval spooler_log.error( "varinvalid REST jsonObject =operation (get|put|post|delete): " + response + ";"rest_operation );
			var response = "";
     spooler_log.info( "REST service PUT response [" + rest_service + "]: " + jsonObject.title );
		    } else {
		}   
 
    return (response != "");
}
        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

Code Block
languagejs
titleJob Chain rest_client
linenumberstrue
collapsetrue
<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
languagejs
titleOrder rest_client_post_json
linenumberstrue
collapsetrue
<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"/>
    </script>params>
    <run_time />
</job>

Job Chain: rest_client

...

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
    languagexml

...

  • collapsetrue
    <job

...

  •   

...

Orders: rest_client

Find additional orders for REST operations with the download of this example.

...

languagejs
titleOrder rest_client_post_json
linenumberstrue
collapsetrue

...

  • 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

...