Example #1
0
 /** Sends out the raw polling log output. */
 public void doPollingLog(StaplerRequest req, StaplerResponse rsp) throws IOException {
   rsp.setContentType("text/plain;charset=UTF-8");
   // Prevent jelly from flushing stream so Content-Length header can be added afterwards
   FlushProofOutputStream out = new FlushProofOutputStream(rsp.getCompressedOutputStream(req));
   getPollingLogText().writeLogTo(0, out);
   out.close();
 }
Example #2
0
    @WebMethod(name = "heapdump.hprof")
    public void doHeapDump(StaplerRequest req, StaplerResponse rsp)
        throws IOException, InterruptedException {
      owner.checkPermission(Jenkins.RUN_SCRIPTS);
      rsp.setContentType("application/octet-stream");

      FilePath dump = obtain();
      try {
        dump.copyTo(rsp.getCompressedOutputStream(req));
      } finally {
        dump.delete();
      }
    }
 /** Transformation of html code which modify all formular's items to read-only */
 public void transformToReadOnly(StaplerRequest request, StaplerResponse response)
     throws IOException {
   try {
     DefaultScriptInvoker invoker = new DefaultScriptInvoker();
     ByteArrayOutputStream out = new ByteArrayOutputStream();
     HTMLWriterOutput xmlOutput = HTMLWriterOutput.create(out);
     Script script = compileScript();
     xmlOutput.useHTML(true);
     invoker.invokeScript(request, response, script, project, xmlOutput);
     String page = ReadOnlyUtil.transformInputsToReadOnly(out.toString("UTF-8"), null);
     OutputStream output = response.getCompressedOutputStream(request);
     output.write(page.getBytes());
     output.close();
   } catch (Exception ex) {
     ex.printStackTrace(new PrintStream(response.getOutputStream()));
   }
 }
Example #4
0
  /** Exposes the bean as XML. */
  public void doXml(
      StaplerRequest req,
      StaplerResponse rsp,
      @QueryParameter String xpath,
      @QueryParameter String wrapper,
      @QueryParameter String tree,
      @QueryParameter int depth)
      throws IOException, ServletException {
    setHeaders(rsp);

    String[] excludes = req.getParameterValues("exclude");

    if (xpath == null && excludes == null) {
      // serve the whole thing
      rsp.serveExposedBean(req, bean, Flavor.XML);
      return;
    }

    StringWriter sw = new StringWriter();

    // first write to String
    Model p = MODEL_BUILDER.get(bean.getClass());
    TreePruner pruner = (tree != null) ? new NamedPathPruner(tree) : new ByDepth(1 - depth);
    p.writeTo(bean, pruner, Flavor.XML.createDataWriter(bean, sw));

    // apply XPath
    Object result;
    try {
      Document dom = new SAXReader().read(new StringReader(sw.toString()));

      // apply exclusions
      if (excludes != null) {
        for (String exclude : excludes) {
          List<org.dom4j.Node> list = (List<org.dom4j.Node>) dom.selectNodes(exclude);
          for (org.dom4j.Node n : list) {
            Element parent = n.getParent();
            if (parent != null) parent.remove(n);
          }
        }
      }

      if (xpath == null) {
        result = dom;
      } else {
        List list = dom.selectNodes(xpath);
        if (wrapper != null) {
          Element root = DocumentFactory.getInstance().createElement(wrapper);
          for (Object o : list) {
            if (o instanceof String) {
              root.addText(o.toString());
            } else {
              root.add(((org.dom4j.Node) o).detach());
            }
          }
          result = root;
        } else if (list.isEmpty()) {
          rsp.setStatus(HttpServletResponse.SC_NOT_FOUND);
          rsp.getWriter().print(Messages.Api_NoXPathMatch(xpath));
          return;
        } else if (list.size() > 1) {
          rsp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
          rsp.getWriter().print(Messages.Api_MultipleMatch(xpath, list.size()));
          return;
        } else {
          result = list.get(0);
        }
      }

    } catch (DocumentException e) {
      LOGGER.log(Level.FINER, "Failed to do XPath/wrapper handling. XML is as follows:" + sw, e);
      throw new IOException2(
          "Failed to do XPath/wrapper handling. Turn on FINER logging to view XML.", e);
    }

    OutputStream o = rsp.getCompressedOutputStream(req);
    try {
      if (result instanceof CharacterData
          || result instanceof String
          || result instanceof Number
          || result instanceof Boolean) {
        if (INSECURE) {
          rsp.setContentType("text/plain;charset=UTF-8");
          String text =
              result instanceof CharacterData
                  ? ((CharacterData) result).getText()
                  : result.toString();
          o.write(text.getBytes("UTF-8"));
        } else {
          rsp.sendError(
              HttpURLConnection.HTTP_FORBIDDEN,
              "primitive XPath result sets forbidden; can use -Dhudson.model.Api.INSECURE=true if you run without security");
        }
        return;
      }

      // otherwise XML
      rsp.setContentType("application/xml;charset=UTF-8");
      new XMLWriter(o).write(result);
    } finally {
      o.close();
    }
  }