Search in sources :

Example 1 with EntityBuilder

use of org.apache.http.client.entity.EntityBuilder in project bender by Nextdoor.

the class HttpTransportTest method getMockClientWithResponse.

private HttpClient getMockClientWithResponse(byte[] respPayload, ContentType contentType, int status, boolean compressed) throws IOException {
    HttpClient mockClient = mock(HttpClient.class);
    HttpResponse mockResponse = mock(HttpResponse.class);
    StatusLine mockStatusLine = mock(StatusLine.class);
    doReturn("expected failure").when(mockStatusLine).getReasonPhrase();
    doReturn(status).when(mockStatusLine).getStatusCode();
    doReturn(mockStatusLine).when(mockResponse).getStatusLine();
    EntityBuilder eb = EntityBuilder.create().setBinary(respPayload).setContentType(contentType);
    HttpEntity he;
    if (compressed) {
        eb.setContentEncoding("gzip");
        he = new GzipDecompressingEntity(eb.build());
    } else {
        he = eb.build();
    }
    doReturn(he).when(mockResponse).getEntity();
    doReturn(mockResponse).when(mockClient).execute(any(HttpPost.class));
    return mockClient;
}
Also used : StatusLine(org.apache.http.StatusLine) HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) GzipDecompressingEntity(org.apache.http.client.entity.GzipDecompressingEntity) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) EntityBuilder(org.apache.http.client.entity.EntityBuilder)

Example 2 with EntityBuilder

use of org.apache.http.client.entity.EntityBuilder in project openstack4j by ContainX.

the class HttpCommand method execute.

/**
 * Executes the command and returns the Response
 *
 * @return the response
 * @throws Exception
 */
public CloseableHttpResponse execute() throws Exception {
    EntityBuilder builder = null;
    if (request.getEntity() != null) {
        if (InputStream.class.isAssignableFrom(request.getEntity().getClass())) {
            InputStreamEntity ise = new InputStreamEntity((InputStream) request.getEntity(), ContentType.create(request.getContentType()));
            ((HttpEntityEnclosingRequestBase) clientReq).setEntity(ise);
        } else {
            builder = EntityBuilder.create().setContentType(ContentType.create(request.getContentType(), "UTF-8")).setText(ObjectMapperSingleton.getContext(request.getEntity().getClass()).writer().writeValueAsString(request.getEntity()));
        }
    } else if (request.hasJson()) {
        builder = EntityBuilder.create().setContentType(ContentType.APPLICATION_JSON).setText(request.getJson());
    }
    if (builder != null && clientReq instanceof HttpEntityEnclosingRequestBase)
        ((HttpEntityEnclosingRequestBase) clientReq).setEntity(builder.build());
    return client.execute(clientReq);
}
Also used : EntityBuilder(org.apache.http.client.entity.EntityBuilder) InputStreamEntity(org.apache.http.entity.InputStreamEntity)

Aggregations

EntityBuilder (org.apache.http.client.entity.EntityBuilder)2 HttpEntity (org.apache.http.HttpEntity)1 HttpResponse (org.apache.http.HttpResponse)1 StatusLine (org.apache.http.StatusLine)1 HttpClient (org.apache.http.client.HttpClient)1 GzipDecompressingEntity (org.apache.http.client.entity.GzipDecompressingEntity)1 HttpPost (org.apache.http.client.methods.HttpPost)1 InputStreamEntity (org.apache.http.entity.InputStreamEntity)1