Search in sources :

Example 1 with CharSpool

use of org.kohsuke.stapler.framework.io.CharSpool in project blueocean-plugin by jenkinsci.

the class NodeLogResource method doIndex.

public void doIndex(StaplerRequest req, StaplerResponse rsp, @Header("Accept") AcceptHeader accept) {
    String download = req.getParameter("download");
    if ("true".equalsIgnoreCase(download)) {
        rsp.setHeader("Content-Disposition", "attachment; filename=log.txt");
    }
    rsp.setContentType("text/plain;charset=UTF-8");
    rsp.setStatus(HttpServletResponse.SC_OK);
    long count = 0;
    try (CharSpool spool = new CharSpool()) {
        for (BluePipelineStep blueStep : steps) {
            if (blueStep instanceof PipelineStepImpl) {
                PipelineStepImpl step = (PipelineStepImpl) blueStep;
                final FlowNodeWrapper node = step.getFlowNodeWrapper();
                if (step.getFlowNodeWrapper().isLoggable()) {
                    count += node.getNode().getAction(LogAction.class).getLogText().writeLogTo(0, spool);
                    String errorLog = node.blockError();
                    if (errorLog != null) {
                        count += appendError(errorLog, new WriterOutputStream(spool));
                    }
                } else {
                    String errorLog = step.getFlowNodeWrapper().nodeError();
                    if (errorLog == null) {
                        errorLog = step.getFlowNodeWrapper().blockError();
                    }
                    if (errorLog != null) {
                        count += appendError(errorLog, new WriterOutputStream(spool));
                    }
                }
            }
        }
        Writer writer;
        if (count > 0) {
            writer = (count > 4096) ? rsp.getCompressedWriter(req) : rsp.getWriter();
            spool.flush();
            spool.writeTo(new LineEndNormalizingWriter(writer));
            rsp.addHeader("X-Text-Size", String.valueOf(count));
            writer.close();
        }
    } catch (IOException e) {
        throw new ServiceException.UnexpectedErrorException("Error reading log");
    }
}
Also used : IOException(java.io.IOException) WriterOutputStream(org.kohsuke.stapler.framework.io.WriterOutputStream) LineEndNormalizingWriter(org.kohsuke.stapler.framework.io.LineEndNormalizingWriter) BluePipelineStep(io.jenkins.blueocean.rest.model.BluePipelineStep) LogAction(org.jenkinsci.plugins.workflow.actions.LogAction) ServiceException(io.jenkins.blueocean.commons.ServiceException) CharSpool(org.kohsuke.stapler.framework.io.CharSpool) LineEndNormalizingWriter(org.kohsuke.stapler.framework.io.LineEndNormalizingWriter) Writer(java.io.Writer)

Example 2 with CharSpool

use of org.kohsuke.stapler.framework.io.CharSpool in project blueocean-plugin by jenkinsci.

the class LogResource method writeLogs.

private void writeLogs(StaplerRequest req, StaplerResponse rsp) throws IOException {
    long threshold = DEFAULT_LOG_THREASHOLD * 1024;
    String s = req.getParameter("thresholdInKB");
    if (s != null) {
        threshold = Long.parseLong(s) * 1024;
    }
    long offset;
    if (req.getParameter("start") != null) {
        offset = Long.parseLong(req.getParameter("start"));
    } else if (logText.length() > threshold) {
        offset = logText.length() - threshold;
    } else {
        offset = 0;
    }
    CharSpool spool = new CharSpool();
    long r = logText.writeLogTo(offset, spool);
    Writer w = createWriter(req, rsp, r - offset);
    spool.writeTo(new LineEndNormalizingWriter(w));
    if (!logText.isComplete()) {
        rsp.addHeader("X-More-Data", "true");
    } else {
        int text = appenderLogReader.read();
        while (text != -1) {
            w.write(text);
            r++;
            text = appenderLogReader.read();
        }
    }
    rsp.addHeader("X-Text-Size", String.valueOf(r));
    w.close();
}
Also used : CharSpool(org.kohsuke.stapler.framework.io.CharSpool) LineEndNormalizingWriter(org.kohsuke.stapler.framework.io.LineEndNormalizingWriter) LineEndNormalizingWriter(org.kohsuke.stapler.framework.io.LineEndNormalizingWriter) Writer(java.io.Writer)

Aggregations

Writer (java.io.Writer)2 CharSpool (org.kohsuke.stapler.framework.io.CharSpool)2 LineEndNormalizingWriter (org.kohsuke.stapler.framework.io.LineEndNormalizingWriter)2 ServiceException (io.jenkins.blueocean.commons.ServiceException)1 BluePipelineStep (io.jenkins.blueocean.rest.model.BluePipelineStep)1 IOException (java.io.IOException)1 LogAction (org.jenkinsci.plugins.workflow.actions.LogAction)1 WriterOutputStream (org.kohsuke.stapler.framework.io.WriterOutputStream)1