public QueuedItemResult queueDispatcherJob(final IDispatchedJob dispatchedJob)
      throws CentralDispatcherException {
    final HashMap<String, String> params = new HashMap<String, String>();
    if (null == dispatchedJob.getJobRef()) {
      throw new IllegalArgumentException("JobRef was null");
    }
    final String jobid;
    if (null != dispatchedJob.getJobRef().getJobId()) {
      jobid = dispatchedJob.getJobRef().getJobId();
    } else {
      if (null == dispatchedJob.getJobRef().getName()
          || "".equals(dispatchedJob.getJobRef().getName())) {
        throw new CentralDispatcherException("job name input is required");
      }
      final String project = dispatchedJob.getJobRef().getProject();
      final String name = dispatchedJob.getJobRef().getName();
      final String group;
      if (null != dispatchedJob.getJobRef().getGroup()
          && !"".equals(dispatchedJob.getJobRef().getGroup())) {
        group = dispatchedJob.getJobRef().getGroup();
      } else {
        // indicates a top level job
        group = "-";
      }

      // Query to find matching job
      final Collection<IStoredJob> iStoredJobs =
          reallistStoredJobs(new IStoredJobsQueryImpl(name, group, null, project));
      if (null == iStoredJobs) {
        throw new CentralDispatcherException("Unable to query jobs");
      }
      if (iStoredJobs.size() < 1) {
        throw new CentralDispatcherException(
            "Job not found matching query: " + (null != group ? group : "") + "/" + name);
      }
      if (iStoredJobs.size() > 1) {
        ArrayList<String> ids = new ArrayList<String>();
        for (final IStoredJob iStoredJob : iStoredJobs) {
          ids.add(iStoredJob.getJobId());
        }
        throw new CentralDispatcherException(
            "Job was not unique: "
                + (null != group ? group : "")
                + "/"
                + name
                + ": "
                + iStoredJobs.size()
                + " jobs found: "
                + ids);
      }
      // use found id
      final IStoredJob next = iStoredJobs.iterator().next();
      jobid = next.getJobId();
    }

    addAPINodeSetParams(params, dispatchedJob.getNodeSet(), dispatchedJob.isKeepgoing());
    addLoglevelParams(params, dispatchedJob.getLoglevel());
    if (null != dispatchedJob.getArgs() && dispatchedJob.getArgs().length > 0) {
      params.put("argString", CLIUtils.generateArgline(null, dispatchedJob.getArgs()));
    }

    final String apipath = substitutePathVariable(RUNDECK_API_JOBS_RUN, "id", jobid);
    return submitExecutionRequest(null, params, apipath);
  }
  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;
  }