/** {@inheritDoc} */
  public void _doBuild(StaplerRequest req, StaplerResponse rsp)
      throws IOException, ServletException {
    if (!req.getMethod().equals("POST")) {
      // show the parameter entry form.
      req.getView(this, "index.jelly").forward(req, rsp);
      return;
    }

    List<ParameterValue> values = new ArrayList<ParameterValue>();

    JSONObject formData = req.getSubmittedForm();
    JSONArray a = JSONArray.fromObject(formData.get("parameter"));

    for (Object o : a) {
      if (o instanceof JSONObject) {
        JSONObject jo = (JSONObject) o;
        String name = jo.getString("name");

        ParameterDefinition d = getParameterDefinition(name);
        if (d == null) throw new IllegalArgumentException("No such parameter definition: " + name);
        ParameterValue parameterValue = d.createValue(req, jo);
        values.add(parameterValue);
      }
    }

    TimeDuration delay =
        (req.hasParameter("delay"))
            ? TimeDuration.fromString(req.getParameter("delay"))
            : new TimeDuration(0);

    Jenkins.getInstance()
        .getQueue()
        .schedule(
            owner,
            (int) delay.as(TimeUnit.SECONDS),
            new ParametersAction(values),
            new CauseAction(new Cause.UserIdCause()),
            new VersioningAction(this.getVersioningMap()));

    // Send the user back to the job page, except if "rebuildNoRedirect" is set
    if (req.getAttribute("rebuildNoRedirect") == null) {
      rsp.sendRedirect(".");
    }
  }
  public void buildWithParameters(StaplerRequest req, StaplerResponse rsp)
      throws IOException, ServletException {
    List<ParameterValue> values = new ArrayList<ParameterValue>();
    for (ParameterDefinition d : this.getParameterDefinitions()) {
      ParameterValue value = d.createValue(req);
      if (value != null) {
        values.add(value);
      }
    }

    CauseAction buildCause = null;
    if (owner instanceof InheritanceProject) {
      buildCause = ((InheritanceProject) owner).getBuildCauseOverride(req);
    } else {
      buildCause = new CauseAction(new Cause.UserIdCause());
    }

    TimeDuration delay =
        (req.hasParameter("delay"))
            ? TimeDuration.fromString(req.getParameter("delay"))
            : new TimeDuration(0);

    Jenkins.getInstance()
        .getQueue()
        .schedule(
            owner,
            (int) delay.as(TimeUnit.SECONDS),
            new ParametersAction(values),
            buildCause,
            new VersioningAction(this.getVersioningMap()));

    if (requestWantsJson(req)) {
      rsp.setContentType("application/json");
      rsp.serveExposedBean(req, owner, Flavor.JSON);
    } else {
      // send the user back to the job top page.
      rsp.sendRedirect(".");
    }
  }