Search in sources :

Example 1 with ContentResponseHandler

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);
}
Also used : HttpResponse(org.apache.http.HttpResponse) Response(org.apache.http.client.fluent.Response) ContentResponseHandler(org.apache.http.client.fluent.ContentResponseHandler) JSONObject(org.json.JSONObject) Request(org.apache.http.client.fluent.Request) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI)

Example 2 with ContentResponseHandler

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

Example 3 with ContentResponseHandler

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;
}
Also used : ContentResponseHandler(org.apache.http.client.fluent.ContentResponseHandler) JSONObject(org.json.JSONObject) Request(org.apache.http.client.fluent.Request) ArrayList(java.util.ArrayList)

Aggregations

ContentResponseHandler (org.apache.http.client.fluent.ContentResponseHandler)3 Request (org.apache.http.client.fluent.Request)3 HttpResponse (org.apache.http.HttpResponse)2 Response (org.apache.http.client.fluent.Response)2 JSONObject (org.json.JSONObject)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 HttpResponseException (org.apache.http.client.HttpResponseException)1 Executor (org.apache.http.client.fluent.Executor)1 URIBuilder (org.apache.http.client.utils.URIBuilder)1