Example #1
0
  /** Accepts submission from the configuration page. */
  @RequirePOST
  public synchronized void doConfigSubmit(StaplerRequest req, StaplerResponse rsp)
      throws IOException, ServletException, FormException {
    checkPermission(CONFIGURE);

    description = req.getParameter("description");

    keepDependencies = req.getParameter("keepDependencies") != null;

    try {
      JSONObject json = req.getSubmittedForm();

      setDisplayName(json.optString("displayNameOrNull"));

      if (req.getParameter("logrotate") != null)
        logRotator = LogRotator.DESCRIPTOR.newInstance(req, json.getJSONObject("logrotate"));
      else logRotator = null;

      DescribableList<JobProperty<?>, JobPropertyDescriptor> t =
          new DescribableList<JobProperty<?>, JobPropertyDescriptor>(NOOP, getAllProperties());
      t.rebuild(
          req,
          json.optJSONObject("properties"),
          JobPropertyDescriptor.getPropertyDescriptors(Job.this.getClass()));
      properties.clear();
      for (JobProperty p : t) {
        p.setOwner(this);
        properties.add(p);
      }

      submit(req, rsp);

      save();
      ItemListener.fireOnUpdated(this);

      String newName = req.getParameter("name");
      final ProjectNamingStrategy namingStrategy = Jenkins.getInstance().getProjectNamingStrategy();
      if (newName != null && !newName.equals(name)) {
        // check this error early to avoid HTTP response splitting.
        Jenkins.checkGoodName(newName);
        namingStrategy.checkName(newName);
        rsp.sendRedirect("rename?newName=" + URLEncoder.encode(newName, "UTF-8"));
      } else {
        if (namingStrategy.isForceExistingJobs()) {
          namingStrategy.checkName(name);
        }
        FormApply.success(".").generateResponse(req, rsp, null);
      }
    } catch (JSONException e) {
      StringWriter sw = new StringWriter();
      PrintWriter pw = new PrintWriter(sw);
      pw.println("Failed to parse form data. Please report this problem as a bug");
      pw.println("JSON=" + req.getSubmittedForm());
      pw.println();
      e.printStackTrace(pw);

      rsp.setStatus(SC_BAD_REQUEST);
      sendError(sw.toString(), req, rsp, true);
    }
  }
Example #2
0
  /** Debug command to attach to a running instance. */
  public void doAttach(StaplerRequest req, StaplerResponse rsp, @QueryParameter String id)
      throws ServletException, IOException, AmazonClientException {
    checkPermission(PROVISION);
    SlaveTemplate t = getTemplates().get(0);

    StringWriter sw = new StringWriter();
    StreamTaskListener listener = new StreamTaskListener(sw);
    EC2AbstractSlave node = t.attach(id, listener);
    Hudson.getInstance().addNode(node);

    rsp.sendRedirect2(req.getContextPath() + "/computer/" + node.getNodeName());
  }
Example #3
0
  /** Renames this job. */
  @RequirePOST
  public /* not synchronized. see renameTo() */ void doDoRename(
      StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
    // rename is essentially delete followed by a create
    checkPermission(CREATE);
    checkPermission(DELETE);

    String newName = req.getParameter("newName");
    Jenkins.checkGoodName(newName);

    if (isBuilding()) {
      // redirect to page explaining that we can't rename now
      rsp.sendRedirect("rename?newName=" + URLEncoder.encode(newName, "UTF-8"));
      return;
    }

    renameTo(newName);
    // send to the new job page
    // note we can't use getUrl() because that would pick up old name in the
    // Ancestor.getUrl()
    rsp.sendRedirect2("../" + newName);
  }
Example #4
0
  /** Accepts and serves the job description */
  public void doDescription(StaplerRequest req, StaplerResponse rsp) throws IOException {
    if (req.getMethod().equals("GET")) {
      // read
      rsp.setContentType("text/plain;charset=UTF-8");
      rsp.getWriter().write(Util.fixNull(this.getDescription()));
      return;
    }
    if (req.getMethod().equals("POST")) {
      checkPermission(CONFIGURE);

      // submission
      if (req.getParameter("description") != null) {
        this.setDescription(req.getParameter("description"));
        rsp.sendError(SC_NO_CONTENT);
        return;
      }
    }

    // huh?
    rsp.sendError(SC_BAD_REQUEST);
  }
Example #5
0
  public HttpResponse doProvision(@QueryParameter String template)
      throws ServletException, IOException {
    checkPermission(PROVISION);
    if (template == null) {
      throw HttpResponses.error(SC_BAD_REQUEST, "The 'template' query parameter is missing");
    }
    SlaveTemplate t = getTemplate(template);
    if (t == null) {
      throw HttpResponses.error(SC_BAD_REQUEST, "No such template: " + template);
    }

    StringWriter sw = new StringWriter();
    StreamTaskListener listener = new StreamTaskListener(sw);
    try {
      EC2AbstractSlave node = t.provision(listener);
      Hudson.getInstance().addNode(node);

      return HttpResponses.redirectViaContextPath("/computer/" + node.getNodeName());
    } catch (AmazonClientException e) {
      throw HttpResponses.error(SC_INTERNAL_SERVER_ERROR, e);
    }
  }