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

The following examples show how to use org.kohsuke.stapler.StaplerResponse#setStatus() . 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: BitbucketServerEndpoint.java    From blueocean-plugin with MIT License 6 votes vote down vote up
/**
 * Validates this server endpoint. Checks availability and version requirement.
 * @return If valid HttpStatus 200, if unsupported version then 428 and if unreachable then 400 error code is returned.
 */
@GET
@WebMethod(name="validate")
public HttpResponse validate(){
    String version = BitbucketServerApi.getVersion(apiUrl);
    if(!BitbucketServerApi.isSupportedVersion(version)){
        throw new ServiceException.PreconditionRequired(
                Messages.bbserver_version_validation_error(
                        version, BitbucketServerApi.MINIMUM_SUPPORTED_VERSION));
    }
    return new HttpResponse(){
        @Override
        public void generateResponse(StaplerRequest req, StaplerResponse rsp, Object node) throws IOException, ServletException {
            rsp.setStatus(200);
        }
    };
}
 
Example 2
Source File: LogResource.java    From blueocean-plugin with MIT License 6 votes vote down vote up
private void writeLog(StaplerRequest req, StaplerResponse rsp) {
    try {
        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);

        writeLogs(req, rsp);
    } catch (IOException e) {
        throw new ServiceException.UnexpectedErrorException("Failed to get logText: " + e.getMessage(), e);
    }
}
 
Example 3
Source File: StatusImage.java    From jenkins-status-badges-plugin with MIT License 6 votes vote down vote up
@Override
public void generateResponse( StaplerRequest req, StaplerResponse rsp, Object node )
    throws IOException, ServletException
{
    String v = req.getHeader( "If-None-Match" );
    if ( etag.equals( v ) )
    {
        rsp.setStatus( SC_NOT_MODIFIED );
        return;
    }

    rsp.setHeader( "ETag", etag );
    rsp.setHeader( "Expires", "Fri, 01 Jan 1984 00:00:00 GMT" );
    rsp.setHeader( "Cache-Control", "no-cache, private" );
    rsp.setHeader( "Content-Type", "image/svg+xml;charset=utf-8" );
    rsp.setHeader( "Content-Length", length );
    rsp.getOutputStream().write( payload.getBytes() );
}
 
Example 4
Source File: ServiceException.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(status);
    rsp.setContentType("application/json");
    PrintWriter w = rsp.getWriter();
    w.write(toJson());
    w.close();
}
 
Example 5
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 6
Source File: BlueI18n.java    From blueocean-plugin with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void generateResponse(StaplerRequest req, StaplerResponse rsp, Object node) throws IOException, ServletException {
    rsp.setStatus(statusCode);
    rsp.setContentType("application/json; charset=UTF-8");
    if (bundleCacheEntry != null) {
        // Set pugin version info that can be used by the browser to
        // determine if it wants to use the resource bundle, or not.
        // The versions may not match (in theory - should never happen),
        // in which case the browser might not want to use the bundle data.
        jsonObject.put("plugin-version-requested", bundleCacheEntry.bundleParams.pluginVersion);
        PluginWrapper pluginWrapper = bundleCacheEntry.bundleParams.getPlugin();
        if(pluginWrapper != null) {
            jsonObject.put("plugin-version-actual", pluginWrapper.getVersion());
        }

        if (bundleCacheEntry.bundleParams.isBrowserCacheable()) {
            // Set the expiry to one year.
            rsp.setHeader("Cache-Control", "public, max-age=31536000");
        } else if (!bundleCacheEntry.bundleParams.isMatchingPluginVersionInstalled()) {
            // This should never really happen if things are installed properly
            // and the UI is coded up properly, with proper access to the installed
            // plugin version.
            LOGGER.log(Level.WARNING, String.format("Unexpected request for Blue Ocean i18n resource bundle '%s'. Installed plugin version '%s' does not match.",
                bundleCacheEntry.bundleParams, pluginWrapper!= null ? pluginWrapper.getVersion() : "unknown"));
        }
    }

    byte[] bytes = jsonObject.toString().getBytes(UTF8);
    rsp.setContentLength(bytes.length);
    rsp.getOutputStream().write(bytes);
}
 
Example 7
Source File: FavoriteContainerImpl.java    From blueocean-plugin with MIT License 5 votes vote down vote up
/**
 * Delete all of the user's favorites.
 */
@WebMethod(name = "") @DELETE
public void doDelete(StaplerResponse resp) throws Favorites.FavoriteException {
    for (final Item favorite: Favorites.getFavorites(user.user)) {
        Favorites.removeFavorite(user.user, favorite);
    }
    resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
}
 
Example 8
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 9
Source File: AbstractScm.java    From blueocean-plugin with MIT License 5 votes vote down vote up
protected HttpResponse createResponse(final String credentialId) {
    return new HttpResponse() {
        @Override
        public void generateResponse(StaplerRequest req, StaplerResponse rsp, Object node) throws IOException, ServletException {
            rsp.setStatus(200);
            rsp.getWriter().print(JsonConverter.toJson(ImmutableMap.of("credentialId", credentialId)));
        }
    };
}
 
Example 10
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());
}
 
Example 11
Source File: FillErrorResponse.java    From github-branch-source-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(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    rsp.setContentType("text/html;charset=UTF-8");
    rsp.setHeader("X-Jenkins-Select-Error", clearList ? "clear" : "retain");
    rsp.getWriter().print(
            "<div class='error'><img src='" + req.getContextPath()
                    + Jenkins.RESOURCE_PATH + "/images/none.gif' height=16 width=1>" + Util.escape(getMessage()) +
                    "</div>");

}
 
Example 12
Source File: TestResultProjectAction.java    From junit-plugin with MIT License 5 votes vote down vote up
/**
 * Display the test result trend.
 */
public void doTrend( StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException {
    AbstractTestResultAction a = getLastTestResultAction();
    if(a!=null)
        a.doGraph(req,rsp);
    else
        rsp.setStatus(HttpServletResponse.SC_NOT_FOUND);
}
 
Example 13
Source File: TestResultProjectAction.java    From junit-plugin with MIT License 5 votes vote down vote up
/**
 * Generates the clickable map HTML fragment for {@link #doTrend(StaplerRequest, StaplerResponse)}.
 */
public void doTrendMap( StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException {
    AbstractTestResultAction a = getLastTestResultAction();
    if(a!=null)
        a.doGraphMap(req,rsp);
    else
        rsp.setStatus(HttpServletResponse.SC_NOT_FOUND);
}
 
Example 14
Source File: BitbucketServerEndpoint.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@WebMethod(name="") @DELETE
public void doDelete(StaplerResponse resp) {
    final BitbucketEndpointConfiguration config = BitbucketEndpointConfiguration.get();
    config.removeEndpoint(getApiUrl());
    resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
}