Skip to main content

IMPORTANT - Product End of Life Statement - IMPORTANT

Request RE, Survey, and Calendar 1.5 Support Ending December 31, 2020
Contact Kinetic Data Support with Questions
Kinetic Community

Sample Client - Java

Sample Java client used to make Kinetic Task API requests.


The sample code below utilizes the apache commons HttpClient library, which can be found on the Apache Commons HttpComponents site.  It requires the commons-codec, commons-logging, and httpcore .jar files (along with the httpclient .jar file) to compile and run.



import java.util.ArrayList;
import java.util.List;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

public class KineticTaskRestClient {
    public static void main(String[] args) throws IOException  {
        // Initialize the Http Client
        HttpClient client = new DefaultHttpClient();
        // Initialize the request
        HttpPost request = new HttpPost("http://SERVER:PORT/kineticTask/rest/v1/Trigger/createStart");
        // Initialize the headers necessary for the request (in this case, use the
        // X-Simulate-Success header to simulate a successful API request).
        request.setHeader("X-Simulate-Success", "KS0000000_SIMULATED_TRIGGER_ID");
        // Build the parameters necessary for the request
        List<NameValuePair> parameterList = new ArrayList<NameValuePair>();
        parameterList.add(new BasicNameValuePair("source_name", "Kinetic Request"));
        parameterList.add(new BasicNameValuePair("source_group", 
            "Kinetic Service Center > iPad Request"));
        parameterList.add(new BasicNameValuePair("source_id", "AG005056b50003ralGTwMDGfEgBLzN"));
        parameterList.add(new BasicNameValuePair("tree_name", "Procure iPad"));
        // Set the request content based on the parameter list
        request.setEntity(new UrlEncodedFormEntity(parameterList, HTTP.UTF_8));
        // Make the request
        HttpResponse response = client.execute(request);
        // Parse the response body content
        String responseContent = EntityUtils.toString(response.getEntity());
        // If the response code returned is 200, indicating the API responded with a success
        if (response.getStatusLine().getStatusCode() == 200) {
            // Reference to the API Message
            String message = response.getFirstHeader("X-Message").getValue();
            // Reference to the generated ID value
            String triggerId = response.getFirstHeader("X-ID").getValue();
            // Here is where additional logic would go if the calling application needs to do
            // anything with the generated trigger ID or the API response message
            System.out.println(triggerId+": "+message);
        // If the response code returned is 400, indicating the API responded with a failure
        else if (response.getStatusLine().getStatusCode() == 400) {
            // Reference to the API Message
            String message = response.getFirstHeader("X-Error-Message").getValue();
            // Here is where additional logic should go to handle invalid API requests (such as
            // an invalid token, or an invalid source_name/source_group/tree_name combination).
        // If the response code returned is 401 (unauthorized), indicating the applied KSL policies
        // denied the request
        else if (response.getStatusLine().getStatusCode() == 401) {
            // Reference to the API Message
            String message = response.getFirstHeader("X-Error-Message").getValue();
            // Here is where additional logic should go to handle unauthorized API requests (IE a
            // request that is denied by the applied KSL policies).  The 'message' will be a comma
            // separated list of failed policy messages.
            System.err.println("You are not authorized to access this resource: "+message);
        // If the status code returned is anything else, such as when the server is not accessible
        else {
            // Here is where logic dealing with an unexpected server exception would go
            StringBuilder buffer = new StringBuilder();
            buffer.append("    ").append(response.getStatusLine().toString()).append("\n");
            for (Header header : response.getAllHeaders()) {
                buffer.append("    ").append(header.toString()).append("\n");
            buffer.append("    ").append(responseContent.replaceAll("\\n", "\n    "));