public final void doBuild(StaplerRequest req, StaplerResponse rsp)
      throws ServletException, IOException {
    if (LOGGER.isLoggable(Level.FINE)) {
      LOGGER.log(Level.FINE, "doBuild action called");
    }

    String buildAction = req.getParameter("action");
    if (buildAction == null) {
      rsp.forwardToPreviousPage(req);
      return;
    }

    String buildType = req.getParameter("build");
    if (buildType == null) {
      rsp.forwardToPreviousPage(req);
      return;
    }

    BuildAction action;
    BuildType type;

    try {
      action = BuildAction.valueOf(buildAction.toUpperCase());
      type = BuildType.valueOf(buildType.toUpperCase());
    } catch (IllegalArgumentException e) {
      rsp.forwardToPreviousPage(req);
      return;
    }

    Builder builder = new Builder(action);

    String params = req.getParameter("params");
    BulkParamProcessor processor = new BulkParamProcessor(params);
    Map<String, String> projectParams = processor.getProjectParams();

    String paramBuild = req.getParameter("paramBuild");
    if (paramBuild != null && !paramBuild.isEmpty() && !projectParams.isEmpty()) {
      builder.setUserParams(projectParams);
    }

    String pattern = req.getParameter("pattern");
    if (pattern != null && !pattern.isEmpty()) {
      builder.setPattern(pattern);
      BuildHistory history = Jenkins.getInstance().getPlugin(BuildHistory.class);
      history.add(new BuildHistoryItem(pattern));
    }

    String view = req.getParameter("view");
    if (view != null && !view.isEmpty()) {
      builder.setView(view);
    }

    switch (type) {
      case ABORTED:
        builder.buildAborted();
        break;
      case ALL:
        builder.buildAll();
        break;
      case FAILED:
        builder.buildFailed();
        break;
      case FAILED_ONLY:
        builder.buildFailedOnly();
        break;
      case NOT_BUILD_ONLY:
        builder.buildNotBuildOnly();
        break;
      case NOT_BUILT:
        builder.buildNotBuilt();
        break;
      case UNSTABLE:
        builder.buildUnstable();
        break;
      case UNSTABLE_ONLY:
        builder.buildUnstableOnly();
        break;
    }

    rsp.forwardToPreviousPage(req);
  }