1.12 Java Web Services

Java is the default language for Service Desk. There are many ways to program web services using Java, as there is a rich infrastructure already present in the language. The high transactional rate and scalability of the language makes it perfect for more complex Web Services applications, fault tolerance and communication with other applications.

We have provided a link to the complete source code of a more complex application. Our Web Services are based on Apache Axis 1.x in these examples for the greatest compatibility with existing applications.

In order to communicate with Service Desk’s web services you should have a thorough knowledge of SOAP and also Service Desk’s Web Services API’s. Service Desk has a User guide which details each of the API calls and how to use them. What follows are scripts you can use to handle some of the more difficult parts of communication. Notably session management.

package com.livetime.sample;import com.livetime.ws.cust.Customer_PortType;import com.livetime.ws.cust.Customer_Service;import com.livetime.ws.cust.Customer_ServiceLocator;public class CreateCustomer extends BaseClient { /* Customer Service URL */ public static final String customerServiceURL = "http://10.0.1.11/LiveTime/WebObjects/LiveTime.woa/ws/Customer"; /* Constructor - doesn't need to do anything coz this is just a sample */ public CreateCustomer() { } private boolean createCustomer() { java.net.URL custURL = null; try { // Service endpoint custURL = new java.net.URL(customerServiceURL); // Get handle to the service Customer_Service service = new Customer_ServiceLocator(); Customer_PortType port = service.getCustomer(custURL); // Call BaseClient method to populate persistent headers populateHeaders((javax.xml.rpc.Stub)port); // create properties you want to set for this person - properties you may want to set here: // custom1 (String 255), custom2(String 255), custom3(String 255), custom4(String 255), custom5(String 255), // aliases (comma separated list of email addresses), orgunit (String - case insensitive match to a LiveTime org unit), // phone(String 32), phone2(String 32), phone3(String 32), address(String 128), addressTwo(String 128), postalCode(String 32), city(String 64), // country (Integer as String - must be id from getCountries() call), state(Integer as String - must be id from getStatesForCountry(String countryId) call) java.util.HashMap<String, String> properties = new java.util.HashMap<String, String>(); properties.put("phone", "+1 949 777 5800"); properties.put("phone2", "+61 3 9620 7588"); // So this is generally reusable without scrubbing database each time, we add a random number after the username & email // wouldn't do this in the real world obviously - but I'll mention it in case someone tries :p String suffix = Integer.toString((new Double(Math.random() * 1000000)).intValue()); // username, email, first name, last name, properties (above) java.util.HashMap response = port.createCustomer("bob_" + suffix, "bob_" + suffix + "@mycompany.com", "Bob", "Nelson", properties); // Just output stuff from here on String successString = response.get("success").toString(); boolean successful = Boolean.parseBoolean(successString); if(!successful) { System.out.println("Customer Creation failed: " + response); } else { System.out.println("Customer Creation succeeded: " + response); } return successful; } catch(Exception ex) { ex.printStackTrace(); return false; } } // Entry point for execution public static void main(String[] args) { // create instance of this class CreateCustomer createCustomer = new CreateCustomer(); // try and connect, if successful, create customer and logoug if(createCustomer.connect()) { // create the customer createCustomer.createCustomer(); // logout createCustomer.disconnect(); } }}