Search in sources :

Example 1 with NByteArrayEntity

use of org.apache.http.nio.entity.NByteArrayEntity in project spring-framework by spring-projects.

the class HttpComponentsAsyncClientHttpRequest method executeInternal.

@Override
protected ListenableFuture<ClientHttpResponse> executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException {
    HttpComponentsClientHttpRequest.addHeaders(this.httpRequest, headers);
    if (this.httpRequest instanceof HttpEntityEnclosingRequest) {
        HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest) this.httpRequest;
        HttpEntity requestEntity = new NByteArrayEntity(bufferedOutput);
        entityEnclosingRequest.setEntity(requestEntity);
    }
    HttpResponseFutureCallback callback = new HttpResponseFutureCallback(this.httpRequest);
    Future<HttpResponse> futureResponse = this.httpClient.execute(this.httpRequest, this.httpContext, callback);
    return new ClientHttpResponseFuture(futureResponse, callback);
}
Also used : HttpEntity(org.apache.http.HttpEntity) NByteArrayEntity(org.apache.http.nio.entity.NByteArrayEntity) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) HttpResponse(org.apache.http.HttpResponse)

Example 2 with NByteArrayEntity

use of org.apache.http.nio.entity.NByteArrayEntity in project elasticsearch by elastic.

the class RequestLoggerTests method testTraceRequest.

public void testTraceRequest() throws IOException, URISyntaxException {
    HttpHost host = new HttpHost("localhost", 9200, randomBoolean() ? "http" : "https");
    String expectedEndpoint = "/index/type/_api";
    URI uri;
    if (randomBoolean()) {
        uri = new URI(expectedEndpoint);
    } else {
        uri = new URI("index/type/_api");
    }
    HttpUriRequest request = randomHttpRequest(uri);
    String expected = "curl -iX " + request.getMethod() + " '" + host + expectedEndpoint + "'";
    boolean hasBody = request instanceof HttpEntityEnclosingRequest && randomBoolean();
    String requestBody = "{ \"field\": \"value\" }";
    if (hasBody) {
        expected += " -d '" + requestBody + "'";
        HttpEntityEnclosingRequest enclosingRequest = (HttpEntityEnclosingRequest) request;
        HttpEntity entity;
        switch(randomIntBetween(0, 4)) {
            case 0:
                entity = new StringEntity(requestBody, ContentType.APPLICATION_JSON);
                break;
            case 1:
                entity = new InputStreamEntity(new ByteArrayInputStream(requestBody.getBytes(StandardCharsets.UTF_8)), ContentType.APPLICATION_JSON);
                break;
            case 2:
                entity = new NStringEntity(requestBody, ContentType.APPLICATION_JSON);
                break;
            case 3:
                entity = new NByteArrayEntity(requestBody.getBytes(StandardCharsets.UTF_8), ContentType.APPLICATION_JSON);
                break;
            case 4:
                // Evil entity without a charset
                entity = new StringEntity(requestBody, ContentType.create("application/json", (Charset) null));
                break;
            default:
                throw new UnsupportedOperationException();
        }
        enclosingRequest.setEntity(entity);
    }
    String traceRequest = RequestLogger.buildTraceRequest(request, host);
    assertThat(traceRequest, equalTo(expected));
    if (hasBody) {
        //check that the body is still readable as most entities are not repeatable
        String body = EntityUtils.toString(((HttpEntityEnclosingRequest) request).getEntity(), StandardCharsets.UTF_8);
        assertThat(body, equalTo(requestBody));
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpEntity(org.apache.http.HttpEntity) URI(java.net.URI) InputStreamEntity(org.apache.http.entity.InputStreamEntity) NStringEntity(org.apache.http.nio.entity.NStringEntity) StringEntity(org.apache.http.entity.StringEntity) NStringEntity(org.apache.http.nio.entity.NStringEntity) NByteArrayEntity(org.apache.http.nio.entity.NByteArrayEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpHost(org.apache.http.HttpHost) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest)

Aggregations

HttpEntity (org.apache.http.HttpEntity)2 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)2 NByteArrayEntity (org.apache.http.nio.entity.NByteArrayEntity)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 URI (java.net.URI)1 HttpHost (org.apache.http.HttpHost)1 HttpResponse (org.apache.http.HttpResponse)1 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)1 InputStreamEntity (org.apache.http.entity.InputStreamEntity)1 StringEntity (org.apache.http.entity.StringEntity)1 NStringEntity (org.apache.http.nio.entity.NStringEntity)1