Versions Compared

Key

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

...


Method Name

Parameter(s)
Return Type

Description

createTicket

Map<String,Serializable> pTicketFieldsvoid

Creates a Change Request ticket using the pDescription and pComment

createIncident

Map<String,Serializable> pTicketFieldsString

Creates an Incident ticket using the pDescription and pComment

findCMSObjectByType

String pCMSObjectNumber

ChangeManagementSystem.CMSObjectType pCMSObjectType

void

Find a "ticket" or "incident" using the object identifier and type. The object type will be TICKET or INCIDENT, and implementations must translate that into the corresponding object type within the provider (e.g. Problem, Request, etc.) 

isTicketApproved

CMSObject pTicket

String pEnvironmentCode

BooleanReturns whether the ticket is approved in the CMS. 
isTicketRejected

CMSObject pTicket

String pEnvironmentCode

BooleanReturns whether the ticket is rejected in the CMS.

checkConnection

N/Avoid

This method should invoke any status or heath check URL of the change management system to ensure the system is up and running and a connection can be authenticated.  This method will be called when the Test Connection button is clicked on the CMS Instance.

isDoPolling

N/A

void

Returns whether FlexDeploy should automatically poll the CMS to identify whether the ticket is approved or rejected.  If polling is enabled FlexDeploy will lookup the ticket every 1 minute using findCMSObjectByType and then check the status using the isTicketApproved and isTicketRejected methods.

If polling is not enabled, the CMS or another external system is responsible for approving/rejecting the associated task using the FlexDeploy REST API.

getTicketURLCMSObject pCMSObjectStringreturns the REST API url of the change ticket


Tip

If your usage of the CMS will not include the automated creation of TICKET or INCIDENT objects the API can be simplified.  Although createTicket and createIncident are required to be implemented, if you are not using these auto-creation features you can choose to simply throw an exception from one or both methods as appropriate.

Code Block
languagejava
themeMidnight
titleExample
throw new UnsupportedOperationException("createIncident is not supported for Zendesk CMS integration");



...

Expand
titleSource Code


Code Block
languagejava
themeEmacs
titleZendeskServiceIntegration.java
linenumberstrue
package mycompany.extension.flexdeploy.zendesk;


import flexagon.fd.model.integration.cms.api.CMSObject;
import flexagon.fd.model.integration.cms.api.ChangeManagementSystem;
import flexagon.fd.model.integration.util.ApiException;

import java.io.Serializable;
import java.io.StringReader;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.JsonReader;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.filter.LoggingFilter;

public class ZendeskServiceIntegration
  extends ChangeManagementSystem
{
  private static final String CLZ_NAM = ZendeskServiceIntegration.class.getName();
  private Logger mLogger;
  private Client mRestClient;
  private String mAccessToken;

  public ZendeskServiceIntegration()
  {
    super();
    this.mLogger = Logger.getLogger(CLZ_NAM);
  }

  private void getOAuthAccessToken(String pScope)
    throws ApiException
  {
    String methodName = "getOAuthAccessToken";
    mLogger.logp(Level.INFO, mLogger.getName(), methodName, "ENTRY");
    JsonObjectBuilder zenOAuthRequestJson = Json.createObjectBuilder();
    zenOAuthRequestJson.add("grant_type", "password");
    zenOAuthRequestJson.add("client_id", getClientId());
    zenOAuthRequestJson.add("client_secret", getClientSecret());
    zenOAuthRequestJson.add("username", getUserName());
    zenOAuthRequestJson.add("password", getPassword());
    zenOAuthRequestJson.add("scope", pScope);
    String payLoad = zenOAuthRequestJson.build().toString();
    Response clientResponse =
      getRestClient().target(getZendeskDomain()).path(getOAuthURI()).request(MediaType.APPLICATION_JSON_TYPE).accept(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(payLoad));
    checkResponse(clientResponse);
    JsonObject jsonResponseObject = readJsonObject(clientResponse.readEntity(String.class));
    mLogger.logp(Level.INFO, mLogger.getName(), methodName, "OAuth Response " + jsonResponseObject);
    mAccessToken = jsonResponseObject.getString("access_token");
    mLogger.logp(Level.INFO, mLogger.getName(), methodName, "EXIT");
  }

  private String getAccessToken(String pScope)
    throws ApiException
  {
    getOAuthAccessToken(pScope);
    return "Bearer " + mAccessToken;
  }

  private String getOAuthURI()
  {
    return getPropertyAsString("ZD_OAUTH_URI");
  }

  private String getClientId()
  {
    return getPropertyAsString("ZD_CLIENT_ID");
  }

  private String getClientSecret()
  {
    return getPropertyAsString("ZD_CLIENT_SECRET");
  }

  @Override
  public CMSObject createTicket(Map<String, Serializable> pTicketFields)
    throws ApiException
  {
    String methodName = "createRequest";
    mLogger.logp(Level.INFO, mLogger.getName(), methodName, "ENTRY");
    mLogger.logp(Level.INFO, mLogger.getName(), methodName, " Creating new Request using " + pTicketFields);
    CMSObject request = postTicket("request", "ZD_REQUEST_CREATE_PATTERN", "requests:write read", pTicketFields);
    mLogger.logp(Level.INFO, mLogger.getName(), methodName, "EXIT");
    return request;
  }

  private CMSObject postTicket(String pObjectType, String pURLKey, String pScope, Map<String, Serializable> pTicketFields)
    throws ApiException
  {
    String methodName = "postTicket";
    mLogger.logp(Level.INFO, mLogger.getName(), methodName, "ENTRY");
    JsonObjectBuilder zenInputJson = Json.createObjectBuilder();
    Serializable requestor = pTicketFields.remove("requester_id");
    JsonObjectBuilder reqParamJsonBuider = null;
    if (requestor != null)
    {
      reqParamJsonBuider = Json.createObjectBuilder();
      reqParamJsonBuider.add("name", requestor.toString());
    }
    Serializable description = pTicketFields.remove("description");
    JsonObjectBuilder desParamJsonBuider = null;
    if (description != null)
    {
      desParamJsonBuider = Json.createObjectBuilder();
      desParamJsonBuider.add("body", description.toString());
    }
    JsonObjectBuilder inputJsonObject = buildJsonRequest(pTicketFields);
    if (requestor != null)
    {
      inputJsonObject.add("requester", reqParamJsonBuider.build());
    }
    if (description != null)
    {
      inputJsonObject.add("comment", desParamJsonBuider.build());
    }
    zenInputJson.add(pObjectType, inputJsonObject);
    WebTarget postRequest = getWebResource(getPropertyAsString(pURLKey));
    String payLoad = zenInputJson.build().toString();
    mLogger.logp(Level.INFO, mLogger.getName(), methodName, String.format(" Creating new %s %s", pObjectType, payLoad));
    Response clientResponse = postRequest.request(MediaType.APPLICATION_JSON_TYPE).accept(MediaType.APPLICATION_JSON_TYPE).header("Authorization", getAccessToken(pScope)).post(Entity.json(payLoad));
    checkResponse(clientResponse);
    JsonObject jsonResponseObject = readJsonObject(clientResponse.readEntity(String.class));
    JsonObject requestResponseJson = jsonResponseObject.getJsonObject(pObjectType);
    CMSObject request = createTicketFromJson(requestResponseJson, ChangeManagementSystem.CMSObjectType.TICKET);
    mLogger.logp(Level.INFO, mLogger.getName(), methodName, String.format(" Successfully created ticket %s", request.getNumber()));
    mLogger.logp(Level.INFO, mLogger.getName(), methodName, "EXIT");
    return request;
  }

  private CMSObject createTicketFromJson(JsonObject jsonObject, CMSObjectType pType)
  {
    String methodName = "createTicketFromJson";
    mLogger.logp(Level.INFO, mLogger.getName(), methodName, "ENTRY");

    CMSObject ticket = CMSObjectImpl.getInstance(String.valueOf(jsonObject.getInt("id")), pType, jsonObject, jsonObject.getString("description"));

    mLogger.logp(Level.INFO, mLogger.getName(), methodName, "EXIT");
    return ticket;
  }

  private JsonObject readJsonObject(String source)
  {
    String methodName = "readJsonObject";
    mLogger.logp(Level.INFO, mLogger.getName(), methodName, "ENTRY");
    JsonReader jsonReader = Json.createReader(new StringReader(source));
    JsonObject object = null;
    try
    {
      object = jsonReader.readObject();
      mLogger.logp(Level.INFO, mLogger.getName(), methodName, String.format(" Json object created for response = %s", source));
    }
    catch (Exception e)
    {
      mLogger.logp(Level.INFO, mLogger.getName(), methodName, String.format(" Exception while creating response json object, error %s, response = %s", e.getMessage(), source));
    }
    finally
    {
      if (jsonReader != null)
      {
        jsonReader.close();
      }
    }
    mLogger.logp(Level.INFO, mLogger.getName(), methodName, "EXIT");
    return object;
  }

  @Override
  public CMSObject createIncident(Map<String, Serializable> pIncidentFields)
  {
    String methodName = "createIncident";
    mLogger.logp(Level.INFO, mLogger.getName(), methodName, "ENTRY");
    CMSObject request = null;
    try
    {
      request = postTicket("ticket", "ZD_TICKET_CREATE_PATTERN", "tickets:write read", pIncidentFields);
    }
    catch (ApiException e)
    {
      mLogger.logp(Level.INFO, mLogger.getName(), methodName, String.format(" Exception while creating incident, error %s", e.getMessage()));
    }
    mLogger.logp(Level.INFO, mLogger.getName(), methodName, "EXIT");
    return request;
  }

  @Override
  public CMSObject findCMSObjectByType(String pRequestNumber, ChangeManagementSystem.CMSObjectType pRequestType)
    throws ApiException
  {
    String methodName = "findCMSObjectByType";
    mLogger.logp(Level.INFO, mLogger.getName(), methodName, "ENTRY");
    CMSObject changeRequest = getChangeRequest(pRequestNumber);
    if (changeRequest != null && changeRequest.getTypeName().equalsIgnoreCase("QUESTION") && pRequestType.equals(ChangeManagementSystem.CMSObjectType.TICKET))
    {
      mLogger.logp(Level.INFO, mLogger.getName(), methodName, pRequestNumber + " is of type QUESTION, setting it as REQUEST for FD");
    }
    mLogger.logp(Level.INFO, mLogger.getName(), methodName, "EXIT");
    return getChangeRequest(pRequestNumber);
  }

  private CMSObject getChangeRequest(String pRequestNumber)
    throws ApiException
  {
    return getTicket("request", "ZD_REQUEST_GET_PATTERN", pRequestNumber, "{ZENDESK_REQUEST}");
  }


  private CMSObject getTicket(String pObjectType, String pURLKey, String pRequestNumber, String pSearchPattern)
    throws ApiException
  {
    String methodName = "getTicket";
    mLogger.logp(Level.INFO, mLogger.getName(), methodName, "ENTRY");
    mLogger.logp(Level.INFO, mLogger.getName(), methodName, String.format(" Find Ticket [%s] ", pRequestNumber));

    //Zendesk Get Request URL Pattern (/api/v2/requests/{ZENDESK_REQUEST}.json)
    String urlString = getPropertyAsString(pURLKey);

    urlString = urlString.replace(pSearchPattern, pRequestNumber);
    Response clientResponse = getWebResource(urlString).request(MediaType.APPLICATION_JSON_TYPE).header("Authorization", getAccessToken("tickets:write read")).get(Response.class);
    checkResponse(clientResponse);
    JsonObject jsonResponseObject = readJsonObject(clientResponse.readEntity(String.class));
    JsonObject ticketResponseJson = jsonResponseObject.getJsonObject(pObjectType);
    CMSObject request = createTicketFromJson(ticketResponseJson, ChangeManagementSystem.CMSObjectType.TICKET);
    mLogger.logp(Level.INFO, mLogger.getName(), methodName, String.format(" Successfully found ticket %s", request.getNumber()));
    mLogger.logp(Level.INFO, mLogger.getName(), methodName, "EXIT");
    return request;
  }

  @Override
  public Boolean isTicketApproved(CMSObject requestNumber, String environmentCode)
  {
    String methodName = "isTicketApproved";
    mLogger.logp(Level.INFO, mLogger.getName(), methodName, "ENTRY");
    CMSObject findRequest = requestNumber;
    Boolean approved = false;
    try
    {
      if (findRequest != null)
      {
        String status = findRequest.getJson().getString("status");
        mLogger.logp(Level.INFO, mLogger.getName(), methodName, String.format("#[%s] status [%s]", requestNumber, status));
        if ("solved".equalsIgnoreCase(status))
        {
          approved = true;
        }
      }
    }
    catch (Exception e)
    {
      // TODO: Add catch code
    }
    mLogger.logp(Level.INFO, mLogger.getName(), methodName, "EXIT");
    return approved;
  }


  @Override
  public Boolean isTicketRejected(CMSObject requestNumber, String environmentCode)
  {
    String methodName = "isTicketRejected";
    mLogger.logp(Level.INFO, mLogger.getName(), methodName, "ENTRY");
    CMSObject findRequest = requestNumber;
    Boolean rejected = false;
    try
    {
      if (findRequest != null)
      {
        String status = findRequest.getJson().getString("status");
        mLogger.logp(Level.INFO, mLogger.getName(), methodName, String.format("#[%s] status [%s]", requestNumber, status));
        if ((!"New".equalsIgnoreCase(status)) && ("on-hold".equalsIgnoreCase(status) || "Closed".equalsIgnoreCase(status)))
        {
          rejected = true;
        }
      }
    }
    catch (Exception e)
    {
      // TODO: Add catch code
    }
    mLogger.logp(Level.INFO, mLogger.getName(), methodName, "EXIT");
    return rejected;
  }

  @Override
  public Boolean isDoPolling()
  {
    return false;
  }

  @Override
  public void checkConnection()
    throws ApiException
  {
    String methodName = "checkConnection";
    mLogger.logp(Level.INFO, mLogger.getName(), methodName, "ENTRY");
    mLogger.logp(Level.INFO, mLogger.getName(), methodName, " getting user details to check connection");
    //    ClientResponse clientResponse = getWebResource("/api/v2/users/me.json").header("Authorization", "Basic " + getAuthString()).get(ClientResponse.class);
    Response clientResponse = getWebResource("/api/v2/users/me.json").request(MediaType.APPLICATION_JSON_TYPE).header("Authorization", getAccessToken("organizations:write read")).get(Response.class);
    checkResponse(clientResponse);
    String responseString = clientResponse.readEntity(String.class);
    mLogger.logp(Level.INFO, mLogger.getName(), methodName, "responseString=" + responseString + ", Validated that JSON data was received from test connction URL invocation.");
    mLogger.logp(Level.INFO, mLogger.getName(), methodName, "EXIT");
  }

  private StringBuilder getZendeskURLBuilder()
  {
    StringBuilder urlBuilder = new StringBuilder(getPropertyAsString("ZD_DOMAIN_NAME"));
    return urlBuilder;
  }

  private void checkResponse(Response clientResponse)
    throws ApiException
  {
    String methodName = "checkResponse";
    mLogger.logp(Level.INFO, mLogger.getName(), methodName, "EXIT");
    mLogger.logp(Level.INFO, mLogger.getName(), methodName, String.format(" Response %s", clientResponse));
    int statusCode = clientResponse.getStatusInfo().getStatusCode();
    if (statusCode == 401)
    {
      throw new ApiException("Invalid credentials.", "");
    }
    if (statusCode == 500)
    {
      //record not found or other scenarious don't want to fail.
      return;
    }
    if (!(clientResponse.getStatusInfo().getFamily() == Response.Status.Family.SUCCESSFUL))
    {
      throw new ApiException(clientResponse.toString(), clientResponse.getStatusInfo().getReasonPhrase());
    }
    mLogger.logp(Level.INFO, mLogger.getName(), methodName, "EXIT");
  }

  private String getZendeskDomain()
  {
    return getZendeskURLBuilder().toString();
  }

  private String getUserName()
  {
    String userName = getPropertyAsString("ZD_USER_NAME");
    return userName;
  }

  private String getPassword()
  {
    String pwd = getPropertyAsString("ZD_PASSWORD");
    return pwd;
  }

  private String getPropertyAsString(String pKey)
  {
    return (String) getProperties().get(pKey);
  }

  private Client getRestClient()
    throws ApiException
  {
    if (mRestClient == null)
    {
      mRestClient = ClientBuilder.newClient(new ClientConfig().register(LoggingFilter.class));
      mRestClient.property(ClientProperties.CONNECT_TIMEOUT, 10000);
      mRestClient.property(ClientProperties.READ_TIMEOUT, 20000);
    }
    return mRestClient;
  }

  public WebTarget getWebResource(String resource)
    throws ApiException
  {
    if (resource != null && !resource.isEmpty() && !resource.startsWith("/"))
    {
      resource += "/" + resource;
    }
    WebTarget webResource = null;
    try
    {
      webResource = getRestClient().target(getZendeskDomain()).path(resource);
    }
    catch (Exception e)
    {
      throw new ApiException(e.getMessage(), e.getMessage());
    }

    return webResource;
  }

  protected JsonObjectBuilder buildJsonRequest(Map<String, Serializable> pFields)
  {
    JsonObjectBuilder jsonBuider = Json.createObjectBuilder();

    for (String fieldKey: pFields.keySet())
    {
      Serializable fieldValue = pFields.get(fieldKey);

      if (fieldValue != null)
      {
        if (fieldValue instanceof String)
        {
          jsonBuider.add(fieldKey, (String) fieldValue);
        }
        else if (fieldValue instanceof Boolean)
        {
          jsonBuider.add(fieldKey, (Boolean) fieldValue);
        }
        else if (fieldValue instanceof Integer)
        {
          jsonBuider.add(fieldKey, (Integer) fieldValue);
        }
        else if (fieldValue instanceof Float)
        {
          jsonBuider.add(fieldKey, (Float) fieldValue);
        }
        else if (fieldValue instanceof Double)
        {
          jsonBuider.add(fieldKey, (Double) fieldValue);
        }
        else if (fieldValue instanceof Long)
        {
          jsonBuider.add(fieldKey, (Long) fieldValue);
        }
        else
        {
          // convert to String if type unknown
          jsonBuider.add(fieldKey, fieldValue.toString());
        }
      }
    }
    return jsonBuider;
  }
  
  }public 

...

String getTicketURL(CMSObject pCMSObject)
  {
	String methodName = "getTicketURL()";
	log.logFinestEntering(methodName, pCMSObject);
	StringBuilder tickerUrlBuilder = new StringBuilder();
	if(pCMSObject != null)
	{
		tickerUrlBuilder.append(getZendeskDomain());
		String urlString = getPropertyAsString("ZD_REQUEST_GET_PATTERN");
		String changeRequestURI = urlString.replace("{ZENDESK_REQUEST}"), pCMSObject.getNumber());
		tickerUrlBuilder.append(changeRequestURI);
	}
    String url = tickerUrlBuilder.toString();
	log.logFinestExiting(methodName, url);
    return url;
  } 
}


  • In order to compile your java class, you will need FlexDeployAPI.jar on classpath.
  • Implement all the methods described in the table in the API Implementation section.
  • For any failure connecting to the system or if any issues with the data, then you can throw exception. For example throw new ApiException("Invalid credentials.", "");
  • Once you are ready with unit testing, you can prepare Jar file for your credential store java class and other utility classes. This jar file can be placed on server classpath now.
    • For Tomcat, put this jar file in apache-tomcat-flexdeploy/lib folder.
    • For WebLogic, put this jar file in Domain lib folder.
    • If you are using any third party libraries from your Java implementation, then those jar files will also need to be added to same lib folder. Keep in mind that this can cause issues with server functioning, so be prepared to remove your additional library files.
  • To pickup changes to your API the FlexDeploy server must be restarted.

...

Expand
titleSource Code


Code Block
languagegroovy
themeEmacs
titleExample groovy ZendeskGroovyIntegration .groovy
linenumberstrue
import flexagon.fd.model.integration.cms.api.CMSObject;
import flexagon.fd.model.integration.cms.api.ChangeManagementSystem;
import flexagon.fd.model.integration.util.ApiException;

import java.io.Serializable;
import java.io.StringReader;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.JsonReader;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.HttpUrlConnectorProvider;
import org.glassfish.jersey.filter.LoggingFilter;
import flexagon.fd.model.integration.cms.impl.CMSObjectImpl


class ZendeskGroovyIntegration {
  def className = ZendeskGroovyIntegration.class.getName();
  String mAccessToken;
  Client mRestClient;
  def now = new Date();  
  
    private void getOAuthAccessToken(String pScope)
    throws ApiException
  {
    JsonObjectBuilder zenOAuthRequestJson = Json.createObjectBuilder();
    zenOAuthRequestJson.add("grant_type", "password");
    zenOAuthRequestJson.add("client_id", getClientId());
    zenOAuthRequestJson.add("client_secret", getClientSecret());
    zenOAuthRequestJson.add("username", getUserName());
    zenOAuthRequestJson.add("password", getPassword());
    zenOAuthRequestJson.add("scope", pScope);
    String payLoad = zenOAuthRequestJson.build().toString();
    Response clientResponse =
      getRestClient().target(getZendeskDomain()).path(getOAuthURI()).request(MediaType.APPLICATION_JSON_TYPE).accept(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(payLoad));
    checkResponse(clientResponse);
    JsonObject jsonResponseObject = readJsonObject(clientResponse.readEntity(String.class));
    System.out.println("OAuth Response " + jsonResponseObject);
    mAccessToken = jsonResponseObject.getString("access_token");
  }
  
    private void checkResponse(Response clientResponse)
    throws ApiException
  {
    String methodName = "checkResponse";
    log.logFinestEntering(methodName, clientResponse);  
    print(methodName + String.format(" Invoked connection URL %s", clientResponse));
    int statusCode = clientResponse.getStatusInfo().getStatusCode();
    if (statusCode == 401)
    {
      throw new ApiException("Invalid credentials.", "");
    }
    if (!(clientResponse.getStatusInfo().getFamily() == Response.Status.Family.SUCCESSFUL))
    {
      throw new ApiException(clientResponse.toString(), clientResponse.getStatusInfo().getReasonPhrase());
    }
    log.logFinestExiting(methodName);  
  }
  
  def createTicket(Map<String,Serializable> pTicketFields)
  {
    String methodName = "createTicket";  
    log.logFinestEntering(methodName, pTicketFields);  
    CMSObject ticket = postTicket("request", ZD_REQUEST_CREATE_PATTERN, "question", "requests:write read", pTicketFields);
    log.logFinestExiting(methodName, ticket);  
    return ticket;
  }
  
    private CMSObject postTicket(String pObjectType, String pURLKey, String pTicketType, String pScope, Map<String, Serializable> pTicketFields)
    throws ApiException
  {
    String methodName = "postTicket";
    log.logFinestEntering(methodName, pTicketFields);  
    JsonObjectBuilder zenInputJson = Json.createObjectBuilder();
	Serializable requestor = pTicketFields.remove("requester_id");
	JsonObjectBuilder reqParamJsonBuider = null
	if(requestor != null)
	{
	  reqParamJsonBuider = Json.createObjectBuilder();
	  reqParamJsonBuider.add("name", requestor.toString())
	}
	Serializable description = pTicketFields.remove("description");
	JsonObjectBuilder desParamJsonBuider = null
	if(description != null)
	{
	  desParamJsonBuider = Json.createObjectBuilder();
	  desParamJsonBuider.add("body", description.toString())
	}
	JsonObjectBuilder inputJsonObject = buildJsonRequest(pTicketFields)
	if(requestor != null)
	{
		inputJsonObject.add("requester", reqParamJsonBuider.build())
	}
	if(description != null)
	{
		inputJsonObject.add("comment", desParamJsonBuider.build())
	}
    zenInputJson.add(pObjectType,  inputJsonObject);

    WebTarget postRequest = getWebResource(pURLKey);//getPropertyAsString(pURLKey)
    String payLoad = zenInputJson.build().toString();
    print(methodName + String.format(" Creating new %s %s", pObjectType, payLoad));
    Response clientResponse = postRequest.request(MediaType.APPLICATION_JSON_TYPE).accept(MediaType.APPLICATION_JSON_TYPE).header("Authorization", getAccessToken(pScope)).post(Entity.json(payLoad));
    checkResponse(clientResponse);
    JsonObject jsonResponseObject = readJsonObject(clientResponse.readEntity(String.class));
    JsonObject requestResponseJson = jsonResponseObject.getJsonObject(pObjectType);
    CMSObject request = createTicketFromJson(requestResponseJson, ChangeManagementSystem.CMSObjectType.TICKET);
    print(methodName + String.format(" Successfully created ticket %s", request.getNumber()));
    log.logFinestExiting(methodName, request);  
    return request;
  }
  
    private CMSObject createTicketFromJson(JsonObject jsonObject, ChangeManagementSystem.CMSObjectType pType)
  {
    String methodName = "createTicketFromJson";
     log.logFinestEntering(methodName, jsonObject, pType);  

    CMSObject ticket = CMSObjectImpl.getInstance(String.valueOf(jsonObject.getInt("id")), pType, jsonObject, jsonObject.getString("description"));

    log.logFinestExiting(methodName, ticket);  
    return ticket;
  }

  private JsonObject readJsonObject(String source)
  {
    String methodName = "readJsonObject";
    log.logFinestEntering(methodName, source);  
    JsonReader jsonReader = Json.createReader(new StringReader(source));
    JsonObject object = null;
    try
    {
      object = jsonReader.readObject();
      print(methodName + String.format(" Json object created for response = %s", source));
    }
    catch (Exception e)
    {
      print(methodName + String.format(" Exception while creating response json object, error %s, response = %s", e.getMessage(), source));
      print(methodName + " - " + e.getStackTrace());
    }
    finally
    {
      if (jsonReader != null)
      {
        jsonReader.close();
      }
    }
    log.logFinestExiting(methodName, object);  
    return object;
  }
  
  def createIncident(Map<String,Serializable> pIncidentFields)
  {
    String methodName = "createIncident";  
    log.logFinestEntering(methodName, pIncidentFields);
    CMSObject incident = postTicket("ticket", ZD_TICKET_CREATE_PATTERN, "incident", "tickets:write read", pIncidentFields);
    log.logFinestExiting(methodName, incident);
    return incident;
  }
  
  def findCMSObjectByType(String pCMSObjectNumber, ChangeManagementSystem.CMSObjectType pCMSObjectType)
  {
    String methodName = "findCMSObjectByType";
    log.logFinestEntering(methodName, pCMSObjectNumber, pCMSObjectType);
	CMSObject ticket = getChangeRequest(pCMSObjectNumber);
    log.logFinestExiting(methodName, ticket);
    return ticket;
  }
  
    private CMSObject getChangeRequest(String pRequestNumber)
    throws ApiException
  {
    return getTicket("request", ZD_REQUEST_GET_PATTERN, pRequestNumber, "{ZENDESK_REQUEST}");
  }
  
    private CMSObject getTicket(String pObjectType, String pURLKey, String pRequestNumber, String pSearchPattern)
    throws ApiException
  {
    String methodName = "getTicket";
    log.logFinestEntering(methodName);

    String urlString = pURLKey;//getPropertyAsString(pURLKey);

    urlString = urlString.replace(pSearchPattern, pRequestNumber);
    Response clientResponse = getWebResource(urlString).request(MediaType.APPLICATION_JSON_TYPE).header("Authorization", getAccessToken("tickets:write read")).get(Response.class);
    checkResponse(clientResponse);
    JsonObject jsonResponseObject = readJsonObject(clientResponse.readEntity(String.class));
    JsonObject ticketResponseJson = jsonResponseObject.getJsonObject(pObjectType);
    CMSObject ticket = createTicketFromJson(ticketResponseJson, ChangeManagementSystem.CMSObjectType.TICKET);
    print(methodName + String.format(" Successfully found ticket %s", ticket.getNumber()));
    log.logFinestExiting(methodName, ticket);
    return ticket;
  }
  
  def findTicketByType(String pTicketNumber)
  {
    String methodName = "findTicketByType";
    log.logFinestEntering(methodName, pTicketNumber);
    CMSObject ticket = findCMSObjectByType(pTicketNumber, ChangeManagementSystem.CMSObjectType.TICKET);
    log.logFinestExiting(methodName, ticket);
    return ticket;
  }

  def isTicketApproved(CMSObject pTicket, String pEnvironmentCode)
  {
    String methodName = "isTicketApproved";
    log.logFinestEntering(methodName, pTicket, pEnvironmentCode);
    CMSObject findRequest = pTicket;
    Boolean approved = false;
    try
    {
      if (findRequest != null)
      {
        String status = findRequest.getJson().getString("status");
        print(String.format("#[%s] status [%s]", pTicket.getNumber(), status));
        if ("solved".equalsIgnoreCase(status))
        {
          approved = true;
        }
      }
    }
    catch (Exception e)
    {
      // TODO: Add catch code
    }
    log.logFinestExiting(methodName, approved);
    return approved;
  }
  
  def isTicketRejected(CMSObject pTicket, String pEnvironmentCode)
  {
    String methodName = "isTicketRejected()";
    log.logFinestEntering(methodName, pTicket, pEnvironmentCode);
    CMSObject findRequest = pTicket;
    Boolean rejected = false;
    try
    {
      if (findRequest != null)
      {
        String status = findRequest.getJson().getString("status");
        print(String.format("#[%s] status [%s] script[%s]", pTicket.getNumber(), status, ZD_REJECTED_SCRIPT));
        if ((!"New".equalsIgnoreCase(status)) && ("on-hold".equalsIgnoreCase(status) || "pending".equalsIgnoreCase(status)))
        {
          rejected = true;
        }
      }
    }
    catch (Exception e)
    {
      // TODO: Add catch code
    }
    log.logFinestExiting(methodName, rejected);
    return rejected;
  }
  
  def isDoPolling()
  {
    String methodName = "isDoPolling()";
    log.logFinestEntering(methodName);
	boolean flag = true;
    log.logFinestExiting(methodName, flag);
	return flag;
  }
  
    private void print(String pText)
  {
    System.out.println(String.format("%s %s", now.toString(), pText));
  }

  private String getUserName()
  {
    return ZD_USER_NAME;
  }


  private String getPassword()
  {
    return ZD_PASSWORD;
  }
  
  def checkConnection()
  {
    String methodName = "checkConnection()";
    log.logFinestEntering(methodName);
    try  
    {
		print(methodName + " getting user details to check connection");
		Response clientResponse = getWebResource("/api/v2/users/me.json").request(MediaType.APPLICATION_JSON_TYPE).header("Authorization", getAccessToken("organizations:write read")).get(Response.class);
		checkResponse(clientResponse);
		print(methodName + " Test connection response code looks valid, check content of response");
		String responseString = clientResponse.readEntity(String.class);
		print(methodName + "responseString=" + responseString + ", Validated that JSON data was received from test connction URL invocation.");
    }
    catch (Exception e)
    {
      log.logInfo(methodName, " Test connection failed " + e.getMessage() + " " + e)
      throw new ApiException("Connection failed. " + e.getMessage());  
    }
    log.logFinestExiting(methodName);
  }
  
    private Client getRestClient()
    throws ApiException
  {
    if (mRestClient == null)
    {
      ClientBuilder builder;
      builder = ClientBuilder.newBuilder().withConfig(new ClientConfig().register(LoggingFilter.class)).property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true);
      mRestClient = builder.build();
      mRestClient.property(ClientProperties.CONNECT_TIMEOUT, 10000);
      mRestClient.property(ClientProperties.READ_TIMEOUT, 20000);
    }
    return mRestClient;
  }
  
    private WebTarget getWebResource(String resource)
    throws ApiException
  {
    if (resource != null && !resource.isEmpty() && !resource.startsWith("/"))
    {
      resource += "/" + resource;
    }
    WebTarget webResource = null;
    try
    {
      webResource = getRestClient().target(getZendeskDomain()).path(resource);
    }
    catch (Exception e)
    {
      throw new ApiException(e.getMessage(), e.getMessage());
    }

    return webResource;
  }

  private String getAccessToken(String pScope)
    throws ApiException
  {
    getOAuthAccessToken(pScope);
    return "Bearer " + mAccessToken;
  }

  private String getOAuthURI()
  {
    return ZD_OAUTH_URI;
  }

  private String getClientId()
  {
    return ZD_CLIENT_ID;
  }

  private String getClientSecret()
  {
    return ZD_CLIENT_SECRET;
  }
  
    protected JsonObjectBuilder buildJsonRequest(Map<String, Serializable> pFields)
  {
    String methodName = "buildJsonRequest";
    JsonObjectBuilder jsonBuider = Json.createObjectBuilder();

    for (String fieldKey: pFields.keySet())
    {
      Serializable fieldValue = pFields.get(fieldKey);

      if (fieldValue != null)
      {
        if (fieldValue instanceof String)
        {
          jsonBuider.add(fieldKey, (String) fieldValue);
        }
        else if (fieldValue instanceof Boolean)
        {
          jsonBuider.add(fieldKey, (Boolean) fieldValue);
        }
        else if (fieldValue instanceof Integer)
        {
          jsonBuider.add(fieldKey, (Integer) fieldValue);
        }
        else if (fieldValue instanceof Float)
        {
          jsonBuider.add(fieldKey, (Float) fieldValue);
        }
        else if (fieldValue instanceof Double)
        {
          jsonBuider.add(fieldKey, (Double) fieldValue);
        }
        else if (fieldValue instanceof Long)
        { instanceof Long)
        {
          jsonBuider.add(fieldKey, (Long) fieldValue);
        }
        else
        {
          // convert to String if type unknown
          jsonBuider.add(fieldKey, fieldValue.toString(Long) fieldValue);
        }
      }
 else   }
    return {jsonBuider;
  }
  
    // convert to String if type unknown
     private String getZendeskDomain()
  {
    return getZendeskURLBuilder().toString();
  }

   jsonBuider.add(fieldKey, fieldValue.toString());private StringBuilder getZendeskURLBuilder()
  {
    StringBuilder }urlBuilder = new StringBuilder(ZD_DOMAIN_NAME);
   } return urlBuilder;
  }

   return jsonBuider;def getTicketURL(CMSObject pCMSObject)
  {
}	String methodName = "getTicketURL()";
	log.logFinestEntering(methodName, pCMSObject);
	StringBuilder tickerUrlBuilder private= Stringnew getZendeskDomainStringBuilder();
	if(pCMSObject != null)
	{
		tickerUrlBuilder.append(getZendeskDomain());
		String changeRequestURI  return getZendeskURLBuilder().toString();
  }

  private StringBuilder getZendeskURLBuilder()
  {
    StringBuilder urlBuilder = new StringBuilder(ZD_DOMAIN_NAME= ZD_REQUEST_GET_PATTERN.replaceAll("\\{ZENDESK_REQUEST\\}", pCMSObject.getNumber());
		tickerUrlBuilder.append(changeRequestURI);
	}
    String url = tickerUrlBuilder.toString();
	log.logFinestExiting(methodName, url);
    return urlBuilderurl;
  }
}


  • Implement all the methods described in the table in the API Implementation section
  • For any failure connecting to the system or if any issues with the data, then you can throw exception. For example throw new ApiException("Invalid credentials.", "");

...