Java Code Examples for hudson.util.HttpResponses#error()

The following examples show how to use hudson.util.HttpResponses#error() . 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: GitLabWebHookAction.java    From gitlab-branch-source-plugin with MIT License 5 votes vote down vote up
public HttpResponse doPost(StaplerRequest request) throws IOException, GitLabApiException {
    if (!request.getMethod().equals("POST")) {
        return HttpResponses
            .error(HttpServletResponse.SC_BAD_REQUEST,
                "Only POST requests are supported, this was a " + request.getMethod()
                    + " request");
    }
    if (!"application/json".equals(request.getContentType())) {
        return HttpResponses
            .error(HttpServletResponse.SC_BAD_REQUEST,
                "Only application/json content is supported, this was " + request
                    .getContentType());
    }
    String type = request.getHeader("X-Gitlab-Event");
    if (StringUtils.isBlank(type)) {
        return HttpResponses.error(HttpServletResponse.SC_BAD_REQUEST,
            "Expecting a GitLab event, missing expected X-Gitlab-Event header");
    }
    String secretToken = request.getHeader("X-Gitlab-Token");
    if(!isValidToken(secretToken)) {
        return HttpResponses.error(HttpServletResponse.SC_UNAUTHORIZED,
            "Expecting a valid secret token");
    }
    String origin = SCMEvent.originOf(request);
    WebHookManager webHookManager = new WebHookManager();
    webHookManager.addListener(new GitLabWebHookListener(origin));
    webHookManager.handleEvent(request);
    return HttpResponses.ok(); // TODO find a better response
}
 
Example 2
Source File: GitLabSystemHookAction.java    From gitlab-branch-source-plugin with MIT License 5 votes vote down vote up
public HttpResponse doPost(StaplerRequest request) throws GitLabApiException {
    if (!request.getMethod().equals("POST")) {
        return HttpResponses
            .error(HttpServletResponse.SC_BAD_REQUEST,
                "Only POST requests are supported, this was a " + request.getMethod()
                    + " request");
    }
    if (!"application/json".equals(request.getContentType())) {
        return HttpResponses
            .error(HttpServletResponse.SC_BAD_REQUEST,
                "Only application/json content is supported, this was " + request
                    .getContentType());
    }
    String type = request.getHeader("X-Gitlab-Event");
    if (StringUtils.isBlank(type)) {
        return HttpResponses.error(HttpServletResponse.SC_BAD_REQUEST,
            "Expecting a GitLab event, missing expected X-Gitlab-Event header");
    }
    String secretToken = request.getHeader("X-Gitlab-Token");
    if(!isValidToken(secretToken)) {
        return HttpResponses.error(HttpServletResponse.SC_UNAUTHORIZED,
            "Expecting a valid secret token");
    }
    String origin = SCMEvent.originOf(request);
    SystemHookManager systemHookManager = new SystemHookManager();
    systemHookManager.addListener(new GitLabSystemHookListener(origin));
    systemHookManager.handleEvent(request);
    return HttpResponses.ok(); // TODO find a better response
}
 
Example 3
Source File: BuildPageRedirectAction.java    From gitlab-plugin with GNU General Public License v2.0 5 votes vote down vote up
public void execute(StaplerResponse response) {
    if (build != null) {
        try {
            response.sendRedirect2(Jenkins.getInstance().getRootUrl() + build.getUrl());
        } catch (IOException e) {
            try {
                response.sendRedirect2(Jenkins.getInstance().getRootUrl() + build.getBuildStatusUrl());
            } catch (IOException e1) {
                throw HttpResponses.error(500, "Failed to redirect to build page");
            }
        }
    }
}
 
Example 4
Source File: StatusPngAction.java    From gitlab-plugin with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void writeStatusBody(StaplerResponse response, Run<?, ?> build, BuildStatus status) {
    try {
        response.setHeader("Expires", "Fri, 01 Jan 1984 00:00:00 GMT");
        response.setHeader("Cache-Control", "no-cache, private");
        response.setHeader("Content-Type", "image/png");
        IOUtils.copy(getStatusImage(status), response.getOutputStream());
        response.flushBuffer();
    } catch (Exception e) {
        throw HttpResponses.error(500, "Could not generate response.");
    }
}
 
Example 5
Source File: StatusJsonAction.java    From gitlab-plugin with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void writeStatusBody(StaplerResponse response, Run<?, ?> build, BuildStatus status) {
    try {
        JSONObject object = new JSONObject();
        object.put("sha", sha1);
        if (build != null) {
            object.put("id", build.getNumber());
        }
        object.put("status", status.getValue());
        writeBody(response, object);
    } catch (IOException e) {
        throw HttpResponses.error(500, "Failed to generate response");
    }
}
 
Example 6
Source File: BuildStatusAction.java    From gitlab-plugin with GNU General Public License v2.0 5 votes vote down vote up
public void execute(StaplerResponse response) {
    SCMTriggerItem item = SCMTriggerItem.SCMTriggerItems.asSCMTriggerItem(project);
    if (!hasGitSCM(item)) {
        throw HttpResponses.error(409, "The project has no GitSCM configured");
    }
    writeStatusBody(response, build, getStatus(build));
}
 
Example 7
Source File: ActionResolver.java    From gitlab-plugin with GNU General Public License v2.0 5 votes vote down vote up
private String getRequestBody(StaplerRequest request) {
    String requestBody;
    try {
        Charset charset = request.getCharacterEncoding() == null ?  UTF_8 : Charset.forName(request.getCharacterEncoding());
        requestBody = IOUtils.toString(request.getInputStream(), charset);
    } catch (IOException e) {
        throw HttpResponses.error(500, "Failed to read request body");
    }
    return requestBody;
}
 
Example 8
Source File: GiteaWebhookAction.java    From gitea-plugin with MIT License 4 votes vote down vote up
public HttpResponse doPost(StaplerRequest request) throws IOException {
    String origin = SCMEvent.originOf(request);
    if (!request.getMethod().equals("POST")) {
        LOGGER.log(Level.FINE, "Received {0} request (expecting POST) from {1}",
                new Object[]{request.getMethod(), origin});
        return HttpResponses
                .error(HttpServletResponse.SC_BAD_REQUEST,
                        "Only POST requests are supported, this was a " + request.getMethod() + " request");
    }
    if (!"application/json".equals(request.getContentType())) {
        LOGGER.log(Level.FINE, "Received {0} body (expecting application/json) from {1}",
                new Object[]{request.getContentType(), origin});
        return HttpResponses
                .error(HttpServletResponse.SC_BAD_REQUEST,
                        "Only application/json content is supported, this was " + request.getContentType());
    }
    String type = request.getHeader("X-Gitea-Event");
    if (StringUtils.isBlank(type)) {
        LOGGER.log(Level.FINE, "Received request without X-Gitea-Event header from {1}",
                new Object[]{request.getContentType(), origin});
        return HttpResponses.error(HttpServletResponse.SC_BAD_REQUEST,
                "Expecting a Gitea event, missing expected X-Gitea-Event header");
    }
    LOGGER.log(Level.FINER, "Received {0} event from {1}", new Object[]{
            request.getContentType(), origin
    });
    boolean processed = false;
    for (GiteaWebhookHandler<?, ?> h : ExtensionList.lookup(GiteaWebhookHandler.class)) {
        if (h.matches(type)) {
            LOGGER.log(Level.FINER, "Processing {0} event from {1} with {2}",
                    new Object[]{type, origin, h});
            h.process(request.getInputStream(), origin);
            processed = true;
        }
    }
    if (!processed) {
        LOGGER.log(Level.INFO, "Received hook payload with unknown type: {0} from {1}",
                new Object[]{type, origin});
    }
    return HttpResponses.text(processed ? "Processed" : "Ignored");
}