How can we consume the RESTful Web Services for Service Desk Manager with Java in Eclipse?

Document ID : KB000021139
Last Modified Date : 14/02/2018
Show Technical Document Details

Introduction:

There are out of box code examples to consume the Service Desk Manager  RESTful Web Services in Java under the "NX_ROOT\samples\sdk\rest\java" directory on the  CA SDM server.

The README.txt file in that folder documents how to use them.

 

Background:

How can we access RESTFul Web Services with Java in a IDE, like Eclipse?


Environment:

CA Service desk Manager 12.9

 

Instructions:

This is provided as an EXAMPLE by CA Support to help get you started.

** We offer no guarantees and cannot assist you with Java Development. **

We can assist you with questions or problems with the Service Desk RESTful Web Services API and the samples provided only.

Requirements:

 CA SDM with the RESTful Web Services API - you will need to know the hostname and port (8050 by default).

Eclipse IDE for Java Developers.  In this example we used Juno (see Figure 1).

Figure 1

Figure 1

  1. Create a new Java Project in Eclipse. You will need three (3) jar files copied from NX_ROOT\java\lib\ in your Build Path (see Figure 2).

    Figure 2

    Figure 2
  2. Create a java class called: "sdRestGetAccessID" with the following code:
    import java.io.IOException;   import java.io.InputStream;    import java.io.InputStreamReader;    import java.io.Reader;   import java.util.Enumeration;   import java.util.Properties;
      /** You must reference libraries: commons-codec-1.3.jar, commons-httpclient-3.0.jar and   commons-logging-api.jar from NX_ROOT/java/lib/ for the imports below **/   import org.apache.commons.codec.binary.Base64;   import org.apache.commons.httpclient.HttpClient;   import org.apache.commons.httpclient.HttpException;   import org.apache.commons.httpclient.methods.PostMethod;
      public class sdRestGetAccessID {
                String proto;            String host;            String portNo;            String user;            String pass;
      public sdRestGetAccessID(String protocol, String hostname, String port, String username, String password){
                proto = protocol;            host = hostname;            portNo = port;            user = username;            pass = password;
      }
      public String getAccessId(){
                String accessID = "";            String endpoint = proto + "://" + host + ":" + portNo + "/caisd-rest/rest_access";            HttpClient client = new HttpClient();            String encodedCredentials = new String(Base64.encodeBase64((user + ":" + pass).getBytes()));            PostMethod post = new PostMethod(endpoint);            post.addRequestHeader("Accept" , "application/xml");            post.addRequestHeader("Content-Type", "application/xml; charset=UTF-8");            post.addRequestHeader("Authorization" , "Basic " + encodedCredentials);            post.      setRequestBody      ("<rest_access/>")   ;
      try {
                System.out.println("Execute Basic Authentication request on " + endpoint);            // Execute request            int result = client.executeMethod(post);
                System.out.println("Response status code: " + result);            System.out.println("Response body: ");            System.out.println(post.getResponseBodyAsString());            String sessionResponse = new String(getAccessIDFromResponse(post.getResponseBodyAsStream()));            System.out.println("Access id: " + sessionResponse);
                accessID = sessionResponse;
      } catch (HttpException e) {            e.printStackTrace();   } catch (IOException e) {            e.printStackTrace();   } finally {   post.releaseConnection();   }   return accessID;   }
      // parse access id from xml response   private static String getAccessIDFromResponse(   InputStream in) throws IOException, NumberFormatException,   StringIndexOutOfBoundsException {
      StringBuffer sb = new StringBuffer();  Reader reader = new InputStreamReader(in, "UTF-8");  int c;  while ((c = in.read()) != -1) sb.append((char) c);
      String document = sb.toString();  String startTag = "<access_key>";  String endTag = "</access_key>";  int start = document.indexOf(startTag) + startTag.length();  int end = document.indexOf(endTag);  String access_id = document.substring(start, end);  return access_id;  }   }
  3. Create a test harness for your java class. For example:
     import java.util.Scanner;        public class TestDrivesdRestGetAccessID {  public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.println("Enter the server name for the Service Desk RESTful Tomcat:\t"); String hostname = input.next(); System.out.println("Enter the server port for the Service Desk RESTful Tomcat:\t"); String port = input.next(); System.out.println("Enter the username for the Service Desk RESTful Tomcat:\t"); String username = input.next(); System.out.println("Enter the password for the Service Desk RESTful Tomcat:\t"); String password = input.next(); System.out.println("Is Tomcat running on http or https:\t"); String protocol = input.next(); //new SampleBasicAuthTest(protocol, hostname, port, username, password); sdRestGetAccessID testAccess = new sdRestGetAccessID(protocol, hostname, port, username, password); System.out.println(testAccess.getAccessId()); } }
  4. Compile and run the test harness and you should see output like the following:

    Enter the server name for the Service Desk RESTful Tomcat:

    servername
    Enter the server port for the Service Desk RESTful Tomcat:
    8050
    Enter the username for the Service Desk RESTful Tomcat:
    ServiceDesk
    Enter the password for the Service Desk RESTful Tomcat:
    Passwordr11
    Is Tomcat running on http or https:
    http
    Execute Basic Authentication request on http://SDMHostname:8050/caisd-rest/rest_access
    Response status code: 201
    Response body:
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?><rest_access id="400050" REL_ATTR="400050" COMMON_NAME="2145630653"><link 	href="http://SDMHostname:8050/caisd-rest/rest_access/400050" 	rel="self"/><access_key>2145630653</access_key><expiration_date>1354055103</expiration_date></rest_access> 

    Access id: 2145630653

    2145630653