private ArrayList<QueuedItem> parseExecutionListResult(final WebserviceResponse response) {
    final Document resultDoc = response.getResultDoc();

    final Node node = resultDoc.selectSingleNode("/result/executions");
    final List items = node.selectNodes("execution");
    final ArrayList<QueuedItem> list = new ArrayList<QueuedItem>();
    if (null != items && items.size() > 0) {
      for (final Object o : items) {
        final Node node1 = (Node) o;
        final String id = node1.selectSingleNode("@id").getStringValue();
        final Node jobname = node1.selectSingleNode("job/name");
        final Node desc = node1.selectSingleNode("description");
        final String name;
        if (null != jobname) {
          name = jobname.getStringValue();
        } else {
          name = desc.getStringValue();
        }
        String url = node1.selectSingleNode("@href").getStringValue();
        url = makeAbsoluteURL(url);
        logger.info("\t" + ": " + name + " [" + id + "] <" + url + ">");
        list.add(QueuedItemResultImpl.createQueuedItem(id, url, name));
      }
    }
    return list;
  }
  public Collection<QueuedItem> listDispatcherQueue() throws CentralDispatcherException {
    final HashMap<String, String> params = new HashMap<String, String>();
    params.put("xmlreq", "true");

    final WebserviceResponse response;
    try {
      response = serverService.makeRundeckRequest(RUNDECK_LIST_EXECUTIONS_PATH, params, null, null);
    } catch (MalformedURLException e) {
      throw new CentralDispatcherServerRequestException("Failed to make request", e);
    }

    validateResponse(response);

    ////////////////////
    // parse result list of queued items, return the collection of QueuedItems
    ///////////////////

    final Document resultDoc = response.getResultDoc();

    final Node node = resultDoc.selectSingleNode("/result/items");
    final List items = node.selectNodes("item");
    final ArrayList<QueuedItem> list = new ArrayList<QueuedItem>();
    if (null != items && items.size() > 0) {
      for (final Object o : items) {
        final Node node1 = (Node) o;
        final String id = node1.selectSingleNode("id").getStringValue();
        final String name = node1.selectSingleNode("name").getStringValue();
        String url = node1.selectSingleNode("url").getStringValue();
        url = makeAbsoluteURL(url);
        logger.info("\t" + ": " + name + " [" + id + "] <" + url + ">");
        list.add(QueuedItemResultImpl.createQueuedItem(id, url, name));
      }
    }

    return list;
  }
  public Collection<IStoredJob> listStoredJobs(
      final IStoredJobsQuery iStoredJobsQuery, final OutputStream output)
      throws CentralDispatcherException {
    final HashMap<String, String> params = new HashMap<String, String>();
    params.put("xmlreq", "true");
    final String nameMatch = iStoredJobsQuery.getNameMatch();
    String groupMatch = iStoredJobsQuery.getGroupMatch();
    final String projectFilter = iStoredJobsQuery.getProjectFilter();
    final String commandFilter = iStoredJobsQuery.getCommand();
    final String idlistFilter = iStoredJobsQuery.getIdlist();
    final String typeFilter = iStoredJobsQuery.getType();
    final String resourceFilter = iStoredJobsQuery.getResource();

    if (null != nameMatch) {
      params.put("jobFilter", nameMatch);
    }
    if (null != groupMatch) {
      final Matcher matcher = Pattern.compile("^/*(.+?)/*$").matcher(groupMatch);
      if (matcher.matches()) {
        // strip leading and trailing slashes
        groupMatch = matcher.group(1);
      }
      params.put("groupPath", groupMatch);
    }
    if (null != projectFilter) {
      params.put("projFilter", projectFilter);
    }
    if (null != resourceFilter) {
      params.put("objFilter", resourceFilter);
    }
    if (null != typeFilter) {
      params.put("typeFilter", typeFilter);
    }
    if (null != commandFilter) {
      params.put("cmdFilter", commandFilter);
    }
    if (null != idlistFilter) {
      params.put("idlist", idlistFilter);
    }

    params.put("xmlreq", "true");

    // 2. send request via ServerService
    final WebserviceResponse response;
    try {
      response =
          serverService.makeRundeckRequest(RUNDECK_LIST_STORED_JOBS_PATH, params, null, null);
    } catch (MalformedURLException e) {
      throw new CentralDispatcherServerRequestException("Failed to make request", e);
    }

    validateJobsResponse(response);

    ////////////////////
    // parse result list of queued items, return the collection of QueuedItems
    ///////////////////

    final Document resultDoc = response.getResultDoc();

    final Node node = resultDoc.selectSingleNode("/joblist");
    final ArrayList<IStoredJob> list = new ArrayList<IStoredJob>();
    if (null == node) {
      return list;
    }
    final List items = node.selectNodes("job");
    if (null != items && items.size() > 0) {
      for (final Object o : items) {
        final Node node1 = (Node) o;
        final String id = node1.selectSingleNode("id").getStringValue();
        final String name = node1.selectSingleNode("name").getStringValue();
        final String url = createJobURL(id);

        final Node gnode = node1.selectSingleNode("group");
        final String group = null != gnode ? gnode.getStringValue() : null;
        final String description = node1.selectSingleNode("description").getStringValue();
        list.add(StoredJobImpl.create(id, name, url, group, description));
      }
    }

    if (null != output) {
      // write output doc to the outputstream
      final OutputFormat format = OutputFormat.createPrettyPrint();
      try {
        final XMLWriter writer = new XMLWriter(output, format);
        writer.write(resultDoc);
        writer.flush();
      } catch (IOException e) {
        throw new CentralDispatcherServerRequestException(e);
      }
    }

    return list;
  }
  /**
   * Submit a request to the server which expects a list of queued item results in the response, and
   * return a single QueuedItemResult parsed from the response.
   *
   * @param tempxml xml temp file (or null)
   * @param otherparams parameters for the request
   * @param requestPath
   * @return a single QueuedItemResult
   * @throws CentralDispatcherException if an error occurs
   */
  private QueuedItemResult submitQueueRequest(
      final File tempxml, final HashMap<String, String> otherparams, final String requestPath)
      throws CentralDispatcherException {

    final HashMap<String, String> params = new HashMap<String, String>();
    params.put("xmlreq", "true");
    if (null != otherparams) {
      params.putAll(otherparams);
    }

    final WebserviceResponse response;
    try {
      response = serverService.makeRundeckRequest(requestPath, params, tempxml, null);
    } catch (MalformedURLException e) {
      throw new CentralDispatcherServerRequestException("Failed to make request", e);
    }
    validateResponse(response);

    ////////////////////
    // parse result list of execution responses.  this implementation handles multiple responses,
    // but only
    // returns a single QueuedItem (the first one found).
    // TODO: update to return multiple QueuedItems when multiple job queue requests are supported
    ///////////////////

    final Document resultDoc = response.getResultDoc();

    final int succeeded;
    final int failed;

    Node node = resultDoc.selectSingleNode("/result/succeeded/@count");
    if (null != node) {
      succeeded = Integer.parseInt(node.getStringValue());
    } else {
      succeeded = -1;
    }
    node = resultDoc.selectSingleNode("/result/failed/@count");
    if (null != node) {
      failed = Integer.parseInt(node.getStringValue());
    } else {
      failed = -1;
    }
    final String succeededId;
    if (succeeded > 0) {
      logger.info("Succeeded queueing " + succeeded + " Job" + (succeeded > 1 ? "s" : "") + ":");
      final List nodes = resultDoc.selectNodes("/result/succeeded/execution");
      final Node node1 = (Node) nodes.iterator().next();
      final String index = node1.selectSingleNode("@index").getStringValue();
      final String id = node1.selectSingleNode("id").getStringValue();
      succeededId = id;
      final String name = node1.selectSingleNode("name").getStringValue();
      String url = node1.selectSingleNode("url").getStringValue();
      url = makeAbsoluteURL(url);
      logger.info("\t" + index + ": " + name + " [" + id + "] <" + url + ">");
      return QueuedItemResultImpl.successful("Succeeded queueing " + name, succeededId, url, name);
    }
    if (failed > 0) {
      final String s1 = "Failed to queue " + failed + " Job" + (failed > 1 ? "s" : "") + ":";
      logger.error(s1);
      final List nodes = resultDoc.selectNodes("/result/failed/execution");
      final Node node1 = (Node) nodes.iterator().next();
      final String index = node1.selectSingleNode("@index").getStringValue();
      final String id =
          null != node1.selectSingleNode("id")
              ? node1.selectSingleNode("id").getStringValue()
              : null;
      String url =
          null != node1.selectSingleNode("url")
              ? node1.selectSingleNode("url").getStringValue()
              : null;
      url = makeAbsoluteURL(url);
      final String error =
          null != node1.selectSingleNode("error")
              ? node1.selectSingleNode("error").getStringValue()
              : null;
      final String message =
          null != node1.selectSingleNode("message")
              ? node1.selectSingleNode("message").getStringValue()
              : null;
      final String errmsg =
          error
              + (null != id ? " [" + id + "] <" + url + ">" : "")
              + (null != message ? " : " + message : "");
      final String s2 = index + ": " + errmsg;
      logger.error(s2);
      return QueuedItemResultImpl.failed(errmsg);
    }
    return QueuedItemResultImpl.failed("Server response contained no success information.");
  }
  public Collection<IStoredJob> listStoredJobs(
      final IStoredJobsQuery iStoredJobsQuery,
      final OutputStream output,
      final JobDefinitionFileFormat fformat)
      throws CentralDispatcherException {
    final HashMap<String, String> params = new HashMap<String, String>();
    final String nameMatch = iStoredJobsQuery.getNameMatch();
    String groupMatch = iStoredJobsQuery.getGroupMatch();
    final String projectFilter = iStoredJobsQuery.getProjectFilter();
    final String idlistFilter = iStoredJobsQuery.getIdlist();

    final String expectedContentType;
    if (null != fformat) {
      params.put("format", fformat.getName());
      expectedContentType = fformat == JobDefinitionFileFormat.xml ? "text/xml" : "text/yaml";
    } else {
      params.put("format", JobDefinitionFileFormat.xml.getName());
      expectedContentType = "text/xml";
    }
    if (null != nameMatch) {
      params.put("jobFilter", nameMatch);
    }
    if (null != groupMatch) {
      final Matcher matcher = Pattern.compile("^/*(.+?)/*$").matcher(groupMatch);
      if (matcher.matches()) {
        // strip leading and trailing slashes
        groupMatch = matcher.group(1);
      }
      params.put("groupPath", groupMatch);
    }
    if (null != projectFilter) {
      params.put("project", projectFilter);
    }
    if (null != idlistFilter) {
      params.put("idlist", idlistFilter);
    }

    // 2. send request via ServerService
    final WebserviceResponse response;
    try {
      response =
          serverService.makeRundeckRequest(
              RUNDECK_API_JOBS_EXPORT_PATH, params, null, null, expectedContentType);
    } catch (MalformedURLException e) {
      throw new CentralDispatcherServerRequestException("Failed to make request", e);
    }
    checkErrorResponse(response);
    // if xml, do local validation and listing
    if (null == fformat || fformat == JobDefinitionFileFormat.xml) {
      validateJobsResponse(response);

      ////////////////////
      // parse result list of queued items, return the collection of QueuedItems
      ///////////////////

      final Document resultDoc = response.getResultDoc();

      final Node node = resultDoc.selectSingleNode("/joblist");
      final ArrayList<IStoredJob> list = new ArrayList<IStoredJob>();
      if (null == node) {
        return list;
      }
      final List items = node.selectNodes("job");
      if (null != items && items.size() > 0) {
        for (final Object o : items) {
          final Node node1 = (Node) o;
          final Node uuid = node1.selectSingleNode("uuid");
          final Node id1 = node1.selectSingleNode("id");
          final String id = null != uuid ? uuid.getStringValue() : id1.getStringValue();
          final String name = node1.selectSingleNode("name").getStringValue();
          final String url = createJobURL(id);

          final Node gnode = node1.selectSingleNode("group");
          final String group = null != gnode ? gnode.getStringValue() : null;
          final String description = node1.selectSingleNode("description").getStringValue();
          list.add(StoredJobImpl.create(id, name, url, group, description, projectFilter));
        }
      }

      if (null != output) {
        // write output doc to the outputstream
        final OutputFormat format = OutputFormat.createPrettyPrint();
        try {
          final XMLWriter writer = new XMLWriter(output, format);
          writer.write(resultDoc);
          writer.flush();
        } catch (IOException e) {
          throw new CentralDispatcherServerRequestException(e);
        }
      }
      return list;
    } else if (fformat == JobDefinitionFileFormat.yaml) {
      // do rought yaml parse
      final Collection<Map> mapCollection = validateJobsResponseYAML(response);
      final ArrayList<IStoredJob> list = new ArrayList<IStoredJob>();

      if (null == mapCollection || mapCollection.size() < 1) {
        return list;
      }
      for (final Map map : mapCollection) {
        final Object uuidobj = map.get("uuid");
        final Object idobj = map.get("id");
        final String id = null != uuidobj ? uuidobj.toString() : idobj.toString();
        final String name = (String) map.get("name");
        final String group = map.containsKey("group") ? (String) map.get("group") : null;
        final String desc = map.containsKey("description") ? (String) map.get("description") : "";
        final String url = createJobURL(id);
        list.add(StoredJobImpl.create(id, name, url, group, desc, projectFilter));
      }

      if (null != output) {
        // write output doc to the outputstream
        try {
          final Writer writer = new OutputStreamWriter(output);
          writer.write(response.getResults());
          writer.flush();
        } catch (IOException e) {
          throw new CentralDispatcherServerRequestException(e);
        }
      }
      return list;
    }
    return null;
  }