use of org.apache.http.client.fluent.ContentResponseHandler in project dcos-commons by mesosphere.
the class CertificateAuthorityClient method doPostRequest.
private JSONObject doPostRequest(String path, JSONObject data) throws Exception {
Request request = Request.Post(new URI(DcosConstants.CA_BASE_URI + path)).bodyString(data.toString(), ContentType.APPLICATION_JSON);
Response response = httpExecutor.execute(request);
HttpResponse httpResponse = response.returnResponse();
handleResponseStatusLine(httpResponse.getStatusLine(), 200);
String responseContent = new ContentResponseHandler().handleEntity(httpResponse.getEntity()).asString();
return new JSONObject(responseContent);
}
use of org.apache.http.client.fluent.ContentResponseHandler 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);
}
}
use of org.apache.http.client.fluent.ContentResponseHandler in project dcos-commons by mesosphere.
the class SecretsClient method list.
/**
* List all secrets paths for given path.
*
* @param path location to list
* @return list of nested paths
* @throws IOException if the list operation failed to complete
*/
public Collection<String> list(String path) throws IOException {
Request httpRequest = Request.Get(uriForPath(String.format("%s?list=true", path)));
String responseContent = new ContentResponseHandler().handleEntity(query("list", path, httpRequest, 200).getEntity()).asString();
JSONObject data = new JSONObject(responseContent);
ArrayList<String> secrets = new ArrayList<>();
data.getJSONArray("array").iterator().forEachRemaining(secret -> secrets.add((String) secret));
return secrets;
}
Aggregations