Java Code Examples for org.kohsuke.stapler.StaplerRequest#getHeader()

The following examples show how to use org.kohsuke.stapler.StaplerRequest#getHeader() . 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: ApiHead.java    From blueocean-plugin with MIT License 6 votes vote down vote up
/**
 * Exposes all {@link ApiRoutable}s to URL space.
 *
 * @param route current URL route handled by ApiHead
 * @return {@link ApiRoutable} object
 */
public ApiRoutable getDynamic(String route) {
    setApis();
    StaplerRequest request = Stapler.getCurrentRequest();
    String m = request.getMethod();
    if(m.equalsIgnoreCase("POST") || m.equalsIgnoreCase("PUT") || m.equalsIgnoreCase("PATCH")) {
        String header = request.getHeader("Content-Type");
        if(header == null || !header.contains("application/json")) {
            throw new ServiceException(415, "Content-Type: application/json required");
        }
    }

    ApiRoutable apiRoutable = apis.get(route);

    //JENKINS-46025 - Avoid caching REST API responses for IE
    StaplerResponse response = Stapler.getCurrentResponse();
    if (response != null && !response.containsHeader("Cache-Control")) {
        response.setHeader("Cache-Control", "no-cache, no-store, no-transform");
    }

    return apiRoutable;
}
 
Example 2
Source File: ActionResolver.java    From gitlab-plugin with GNU General Public License v2.0 6 votes vote down vote up
private WebHookAction onPost(Item project, StaplerRequest request) {
    String eventHeader = request.getHeader("X-Gitlab-Event");
    if (eventHeader == null) {
        LOGGER.log(Level.FINE, "Missing X-Gitlab-Event header");
        return new NoopAction();
    }
    String tokenHeader = request.getHeader("X-Gitlab-Token");
    switch (eventHeader) {
        case "Merge Request Hook":
            return new MergeRequestBuildAction(project, getRequestBody(request), tokenHeader);
        case "Push Hook":
        case "Tag Push Hook":
            return new PushBuildAction(project, getRequestBody(request), tokenHeader);
        case "Note Hook":
            return new NoteBuildAction(project, getRequestBody(request), tokenHeader);
        case "Pipeline Hook":
            return new PipelineBuildAction(project, getRequestBody(request), tokenHeader);
        case "System Hook":
            return onSystemHook(project, getRequestBody(request), tokenHeader);
        default:
            LOGGER.log(Level.FINE, "Unsupported X-Gitlab-Event header: {0}", eventHeader);
            return new NoopAction();
    }
}
 
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: 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 5
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 6
Source File: AbstractScm.java    From blueocean-plugin with MIT License 5 votes vote down vote up
protected static @CheckForNull String getCredentialIdFromRequest(@Nonnull StaplerRequest request){
    String credentialId = request.getParameter(CREDENTIAL_ID);

    if(credentialId == null){
        credentialId = request.getHeader(X_CREDENTIAL_ID);
    }
    return credentialId;
}
 
Example 7
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");
}
 
Example 8
Source File: BrowserAndOperatingSystemAnalyticsProperties.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Override
public Map<String, Object> properties(TrackRequest req) {
    StaplerRequest httpReq = Stapler.getCurrentRequest();
    if (PARSER == null || httpReq == null) {
        return null;
    }
    String userAgent = httpReq.getHeader("User-Agent");
    if (userAgent == null) {
        return null;
    }
    Client client = PARSER.parse(userAgent);
    String browserFamily = client.userAgent.family;
    // If we can't find the browser family then we shouldn't record anything
    if (browserFamily == null) {
        return null;
    }
    Map<String, Object> props = Maps.newHashMap();
    props.put("browserFamily", browserFamily);
    String browserVersionMajor = client.userAgent.major;
    // Versions are useful if they are available
    if (isNotEmpty(browserVersionMajor)) {
        props.put("browserVersionMajor", browserVersionMajor);
    }
    String browserVersionMinor = client.userAgent.minor;
    if (isNotEmpty(browserVersionMinor)) {
        props.put("browserVersionMinor", browserVersionMinor);
    }
    // If the operating system is available lets use that
    String operatingSystemFamily = client.os.family;
    if (isNotEmpty(operatingSystemFamily)) {
        props.put("osFamily", operatingSystemFamily);
        String osVersionMajor = client.os.major;
        if (isNotEmpty(osVersionMajor)) {
            props.put("osVersionMajor", osVersionMajor);
        }
        String osVersionMinor = client.os.minor;
        if (isNotEmpty(osVersionMinor)) {
            props.put("osVersionMinor", osVersionMinor);
        }
    }
    return props;
}