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);
}
}
Aggregations