Java Code Examples for org.kohsuke.stapler.StaplerResponse#addHeader()

The following examples show how to use org.kohsuke.stapler.StaplerResponse#addHeader() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GitLabAvatarCache.java    From gitlab-branch-source-plugin with MIT License 6 votes vote down vote up
@Override
public void generateResponse(StaplerRequest req, StaplerResponse rsp, Object node)
    throws IOException, ServletException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {
        ImageIO.write(image, "png", bos);
    } finally {
        if (flushImage) {
            image.flush();
        }
    }
    final byte[] bytes = bos.toByteArray();
    if (lastModified > 0) {
        rsp.addDateHeader("Last-Modified", lastModified);
    }
    rsp.addHeader("Cache-control", cacheControl);
    rsp.setContentType("image/png");
    rsp.setContentLength(bytes.length);
    rsp.getOutputStream().write(bytes);
}
 
Example 2
Source File: ConfigurationAsCode.java    From configuration-as-code-plugin with MIT License 5 votes vote down vote up
/**
 * Export live jenkins instance configuration as Yaml
 * @throws Exception
 */
@RequirePOST
@Restricted(NoExternalUse.class)
public void doExport(StaplerRequest req, StaplerResponse res) throws Exception {
    if (!Jenkins.get().hasPermission(Jenkins.SYSTEM_READ)) {
        res.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }

    res.setContentType("application/x-yaml; charset=utf-8");
    res.addHeader("Content-Disposition", "attachment; filename=jenkins.yaml");
    export(res.getOutputStream());
}
 
Example 3
Source File: JwtToken.java    From blueocean-plugin with MIT License 5 votes vote down vote up
/**
 * Writes the token as an HTTP response.
 */
@Override
public void generateResponse(StaplerRequest req, StaplerResponse rsp, Object node) throws IOException, ServletException {
    rsp.setStatus(204);
    /* https://httpstatuses.com/204 No Content
    The server has successfully fulfilled the request and
    that there is no additional content to send in the response payload body */
    rsp.addHeader(X_BLUEOCEAN_JWT, sign());
}
 
Example 4
Source File: LogResource.java    From blueocean-plugin with MIT License 5 votes vote down vote up
private void writeLogs(StaplerRequest req, StaplerResponse rsp) throws IOException {
    long threshold = DEFAULT_LOG_THRESHOLD * 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));
    rsp.addHeader("X-Text-Delivered", String.valueOf(r - offset));
    w.close();

}
 
Example 5
Source File: CreateResponse.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Override
public void generateResponse(StaplerRequest req, StaplerResponse rsp, Object node) throws IOException, ServletException {
    rsp.setStatus(201);
    String location = String.format("%s%s", req.getRootPath(), payload.getLink());
    rsp.addHeader("Location", location);

    // Writes payload as a tree response
    Export.doJson(req, rsp, payload);
}
 
Example 6
Source File: GogsWebHook.java    From gogs-webhook-plugin with MIT License 5 votes vote down vote up
/**
 * Exit the WebHook
 *
 * @param result GogsResults
 */
private void exitWebHook(GogsResults result, StaplerResponse resp) throws IOException {
    if (result.getStatus() != 200) {
        LOGGER.warning(result.getMessage());
    }
    //noinspection MismatchedQueryAndUpdateOfCollection
    JSONObject json = new JSONObject();
    json.element("result", result.getStatus() == 200 ? "OK" : "ERROR");
    json.element("message", result.getMessage());
    resp.setStatus(result.getStatus());
    resp.addHeader("Content-Type", "application/json");
    PrintWriter printer = resp.getWriter();
    printer.print(json.toString());
}