Search in sources :

Example 11 with Request

use of org.apache.http.client.fluent.Request in project gerrit by GerritCodeReview.

the class CorsIT method preflightOk.

@Test
public void preflightOk() throws Exception {
    Result change = createChange();
    String origin = "http://example.com";
    Request req = Request.Options(adminRestSession.url() + "/a/changes/" + change.getChangeId() + "/detail");
    req.addHeader(ORIGIN, origin);
    req.addHeader(ACCESS_CONTROL_REQUEST_METHOD, "GET");
    req.addHeader(ACCESS_CONTROL_REQUEST_HEADERS, "X-Requested-With");
    RestResponse res = adminRestSession.execute(req);
    res.assertOK();
    checkCors(res, true, origin);
}
Also used : RestResponse(com.google.gerrit.acceptance.RestResponse) Request(org.apache.http.client.fluent.Request) Result(com.google.gerrit.acceptance.PushOneCommit.Result) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 12 with Request

use of org.apache.http.client.fluent.Request in project blueocean-plugin by jenkinsci.

the class HttpRequest method executeInternal.

private Content executeInternal() throws IOException {
    String uriPath = urlParts.size() > 0 ? UriTemplate.fromTemplate(requestUrl).expand(urlParts) : requestUrl;
    URIBuilder uri;
    String fullUrl;
    try {
        uri = new URIBuilder(baseUrl + uriPath);
        fullUrl = uri.toString();
    } catch (URISyntaxException ex) {
        throw new RuntimeException("could not parse request URL: " + baseUrl + requestUrl, ex);
    }
    logger.info("request url: " + fullUrl);
    Request request;
    switch(method) {
        case GET:
            request = Request.Get(fullUrl);
            break;
        case POST:
            request = Request.Post(fullUrl);
            break;
        case PUT:
            request = Request.Put(fullUrl);
            break;
        case PATCH:
            request = Request.Patch(fullUrl);
            break;
        case DELETE:
            request = Request.Delete(fullUrl);
            break;
        default:
            throw new RuntimeException("Invalid method: " + method);
    }
    headers.forEach(request::setHeader);
    if (requestBody != null) {
        request.bodyString(requestBody, ContentType.parse(contentType));
    }
    Executor exec = Executor.newInstance();
    // use 'Authorization: Basic' for username/password
    if (StringUtils.isNotEmpty(username) && StringUtils.isNotEmpty(password)) {
        String authHost = uri.getPort() != -1 ? String.format("%s:%s", uri.getHost(), uri.getPort()) : uri.getHost();
        exec.authPreemptive(authHost).auth(username, password);
    }
    try {
        Response response = exec.execute(request);
        HttpResponse httpResponse = response.returnResponse();
        int returnedStatusCode = httpResponse.getStatusLine().getStatusCode();
        if (expectedStatus != returnedStatusCode) {
            throw new RuntimeException(String.format("Status code %s did not match expected %s", returnedStatusCode, expectedStatus));
        }
        // manually build content to avoid 'already consumed' exception from response.returnContent()
        return new ContentResponseHandler().handleEntity(httpResponse.getEntity());
    } catch (HttpResponseException ex) {
        throw new RuntimeException("Unexpected error during request", ex);
    }
}
Also used : HttpResponse(org.apache.http.HttpResponse) Response(org.apache.http.client.fluent.Response) ContentResponseHandler(org.apache.http.client.fluent.ContentResponseHandler) Executor(org.apache.http.client.fluent.Executor) Request(org.apache.http.client.fluent.Request) HttpResponse(org.apache.http.HttpResponse) HttpResponseException(org.apache.http.client.HttpResponseException) URISyntaxException(java.net.URISyntaxException) URIBuilder(org.apache.http.client.utils.URIBuilder)

Aggregations

Request (org.apache.http.client.fluent.Request)12 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)4 Result (com.google.gerrit.acceptance.PushOneCommit.Result)4 URI (java.net.URI)4 Test (org.junit.Test)4 HttpResponse (org.apache.http.HttpResponse)3 HttpResponseException (org.apache.http.client.HttpResponseException)3 BasicHeader (org.apache.http.message.BasicHeader)3 Executor (org.apache.http.client.fluent.Executor)2 Response (org.apache.http.client.fluent.Response)2 StringEntity (org.apache.http.entity.StringEntity)2 RestResponse (com.google.gerrit.acceptance.RestResponse)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URISyntaxException (java.net.URISyntaxException)1 HttpEntity (org.apache.http.HttpEntity)1 NameValuePair (org.apache.http.NameValuePair)1 ClientProtocolException (org.apache.http.client.ClientProtocolException)1 ContentResponseHandler (org.apache.http.client.fluent.ContentResponseHandler)1 URIBuilder (org.apache.http.client.utils.URIBuilder)1