Search in sources :

Example 41 with HttpResponseException

use of org.apache.http.client.HttpResponseException 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

HttpResponseException (org.apache.http.client.HttpResponseException)41 StatusLine (org.apache.http.StatusLine)23 HttpEntity (org.apache.http.HttpEntity)17 IOException (java.io.IOException)16 Header (org.apache.http.Header)5 BasicResponseHandler (org.apache.http.impl.client.BasicResponseHandler)5 URISyntaxException (java.net.URISyntaxException)4 HttpClient (org.apache.http.client.HttpClient)4 HttpGet (org.apache.http.client.methods.HttpGet)4 BufferedHttpEntity (org.apache.http.entity.BufferedHttpEntity)4 Test (org.junit.Test)4 URI (java.net.URI)3 PatternSyntaxException (java.util.regex.PatternSyntaxException)3 HttpResponse (org.apache.http.HttpResponse)3 SharedPreferences (android.content.SharedPreferences)2 ApplicationException (com.github.hakko.musiccabinet.exception.ApplicationException)2 TextMessage (com.nyaruka.androidrelay.data.TextMessage)2 TextMessageHelper (com.nyaruka.androidrelay.data.TextMessageHelper)2 JenkinsServer (com.offbytwo.jenkins.JenkinsServer)2 Job (com.offbytwo.jenkins.model.Job)2