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.
How can we access RESTFul Web Services with Java in a IDE, like Eclipse?
CA Service desk Manager 12.9
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
<Please see attached file for image>
<Please see attached file for image>
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; } }
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()); } }
<?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>
ARCHIVED