/**
   * Validate the response is in expected yaml format
   *
   * @param response response
   * @return Collection of job data maps if format is correct and there is no error
   * @throws com.dtolabs.client.services.CentralDispatcherServerRequestException if the format is
   *     incorrect, or the response indicates an error response.
   */
  private Collection<Map> validateJobsResponseYAML(final WebserviceResponse response)
      throws CentralDispatcherServerRequestException {
    if (null == response) {
      throw new CentralDispatcherServerRequestException("Response was null");
    }

    if (null != response.getResponseMessage()) {
      logger.info("Response: " + response.getResponseMessage());
    }
    String resultContentType = response.getResultContentType();
    if (resultContentType.contains(";")) {
      resultContentType = resultContentType.substring(0, resultContentType.indexOf(";"));
    }
    if (!resultContentType.endsWith("/yaml")) {
      throw new CentralDispatcherServerRequestException(
          "Expected YAML response, unexpected content type: " + response.getResultContentType());
    }
    final ArrayList<Map> dataset = new ArrayList<Map>();
    final Object resobj;
    try {
      final Yaml yaml = new Yaml(new SafeConstructor());
      resobj = yaml.load(response.getResults());
    } catch (YAMLException e) {
      throw new CentralDispatcherServerRequestException(
          "Failed to parse YAML: " + e.getMessage(), e);
    }
    if (resobj instanceof Collection) {
      dataset.addAll((Collection) resobj);
    } else {
      throw new CentralDispatcherServerRequestException(
          "Response had unexpected content type: " + resobj);
    }

    for (final Map map : dataset) {
      if (!map.containsKey("name") || !map.containsKey("id") && !map.containsKey("uuid")) {
        throw new CentralDispatcherServerRequestException(
            "Response had unexpected dataset: " + resobj);
      }
    }
    return dataset;
  }
  /**
   * Validate the response is in expected envlope form with &lt;result&gt; content.
   *
   * @param response response
   * @return Envelope if format is correct and there is no error
   * @throws CentralDispatcherException if the format is incorrect, or the Envelope indicates an
   *     error response.
   */
  private Envelope validateResponse(final WebserviceResponse response)
      throws CentralDispatcherException {
    if (null == response) {
      throw new CentralDispatcherServerRequestException("Response was null");
    }

    if (null != response.getResponseMessage()) {
      logger.info("Response: " + response.getResponseMessage());
    }
    final Document resultDoc = response.getResultDoc();
    if (null == resultDoc) {
      throw new CentralDispatcherServerRequestException(
          "Response content unexpectedly empty. "
              + (response.getResponseMessage() != null ? response.getResponseMessage() : ""));
    }
    try {
      logger.debug(serialize(resultDoc));
    } catch (IOException e) {
      logger.debug("ioexception serializing result doc", e);
    }

    if (!"result".equals(resultDoc.getRootElement().getName())) {
      throw new CentralDispatcherServerRequestException(
          "Response had unexpected content: " + resultDoc);
    }
    final Envelope envelope = new Envelope(response.getResultDoc());
    if (envelope.isErrorResult()) {
      final StringBuffer sb = new StringBuffer();
      final StringBuffer buffer = envelope.errorMessages();
      if (sb.length() > 0) {
        sb.append(buffer);
      } else {
        sb.append("Server reported an error");
        if (null != response.getResponseMessage()) {
          sb.append(": ").append(response.getResponseMessage());
        }
      }
      if (null != response.getResponseMessage()) {
        logger.error("Server reported an error: " + response.getResponseMessage());
      } else {
        logger.error("Server reported an error.");
      }
      throw new CentralDispatcherFailureResponseException(sb.toString());
    }
    return envelope;
  }
  /**
   * Validate the response is in expected envlope form with &lt;result&gt; content.
   *
   * @param response response
   * @return Envelope if format is correct and there is no error
   * @throws CentralDispatcherServerRequestException if the format is incorrect, or the Envelope
   *     indicates an error response.
   */
  private void validateJobsResponse(final WebserviceResponse response)
      throws CentralDispatcherServerRequestException {
    if (null == response) {
      throw new CentralDispatcherServerRequestException("Response was null");
    }

    if (null != response.getResponseMessage()) {
      logger.info("Response: " + response.getResponseMessage());
    }
    final Document resultDoc = response.getResultDoc();
    if (null == resultDoc) {
      throw new CentralDispatcherServerRequestException("Response content unexpectedly empty");
    }
    try {
      logger.debug(serialize(resultDoc));
    } catch (IOException e) {
      logger.debug("ioexception serializing result doc", e);
    }

    if (!"joblist".equals(resultDoc.getRootElement().getName())) {
      throw new CentralDispatcherServerRequestException(
          "Response had unexpected content: " + resultDoc);
    }
  }