Search in sources :

Example 1 with WriterOutputStream

use of org.kohsuke.stapler.framework.io.WriterOutputStream 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 WriterOutputStream

use of org.kohsuke.stapler.framework.io.WriterOutputStream in project hudson-2.x by hudson.

the class LargeText method writeLogTo.

/**
     * Writes the tail portion of the file to the {@link Writer}.
     *
     * <p>
     * The text file is assumed to be in the system default encoding.
     *
     * @param start
     *      The byte offset in the input file where the write operation starts.
     *
     * @return
     *      if the file is still being written, this method writes the file
     *      until the last newline character and returns the offset to start
     *      the next write operation.
     */
public long writeLogTo(long start, Writer w) throws IOException {
    CountingOutputStream os = new CountingOutputStream(new WriterOutputStream(w));
    Session f = source.open();
    f.skip(start);
    if (completed) {
        // write everything till EOF
        byte[] buf = new byte[1024];
        int sz;
        while ((sz = f.read(buf)) >= 0) os.write(buf, 0, sz);
    } else {
        ByteBuf buf = new ByteBuf(null, f);
        HeadMark head = new HeadMark(buf);
        TailMark tail = new TailMark(buf);
        while (tail.moveToNextLine(f)) {
            head.moveTo(tail, os);
        }
        head.finish(os);
    }
    f.close();
    os.flush();
    return os.getCount() + start;
}
Also used : CountingOutputStream(org.apache.commons.io.output.CountingOutputStream) WriterOutputStream(org.kohsuke.stapler.framework.io.WriterOutputStream)

Aggregations

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