Example usage for org.apache.http.entity BasicHttpEntity BasicHttpEntity

List of usage examples for org.apache.http.entity BasicHttpEntity BasicHttpEntity

Introduction

In this page you can find the example usage for org.apache.http.entity BasicHttpEntity BasicHttpEntity.

Prototype

public BasicHttpEntity() 

Source Link

Usage

From source file:org.apache.nifi.processors.aws.wag.TestInvokeAmazonGatewayApiMock.java

@Test
public void testGetApiSimple() throws Exception {

    HttpResponse resp = new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "OK"));
    BasicHttpEntity entity = new BasicHttpEntity();
    entity.setContent(new ByteArrayInputStream("test payload".getBytes()));
    resp.setEntity(entity);//from  w ww. j av  a 2  s  . co m
    Mockito.doReturn(resp).when(mockSdkClient).execute(any(HttpUriRequest.class), any(HttpContext.class));

    // execute
    runner.assertValid();
    runner.run(1);

    // check
    Mockito.verify(mockSdkClient, times(1)).execute(argThat(new RequestMatcher<HttpUriRequest>(x -> {
        return x.getMethod().equals("GET") && x.getFirstHeader("x-api-key").getValue().equals("abcd")
                && x.getFirstHeader("Authorization").getValue().startsWith("AWS4")
                && x.getURI().toString().equals("https://foobar.execute-api.us-east-1.amazonaws.com/TEST");
    })), any(HttpContext.class));

    runner.assertTransferCount(InvokeAWSGatewayApi.REL_SUCCESS_REQ, 0);
    runner.assertTransferCount(InvokeAWSGatewayApi.REL_RESPONSE, 1);
    runner.assertTransferCount(InvokeAWSGatewayApi.REL_RETRY, 0);
    runner.assertTransferCount(InvokeAWSGatewayApi.REL_NO_RETRY, 0);
    runner.assertTransferCount(InvokeAWSGatewayApi.REL_FAILURE, 0);

    final List<MockFlowFile> flowFiles = runner.getFlowFilesForRelationship(InvokeAWSGatewayApi.REL_RESPONSE);
    final MockFlowFile ff0 = flowFiles.get(0);

    ff0.assertAttributeEquals(InvokeAWSGatewayApi.STATUS_CODE, "200");
    ff0.assertContentEquals("test payload");
    ff0.assertAttributeExists(InvokeAWSGatewayApi.TRANSACTION_ID);
    ff0.assertAttributeEquals(InvokeAWSGatewayApi.RESOURCE_NAME_ATTR, "/TEST");
}

From source file:com.nexmo.client.voice.endpoints.ReadCallMethodTest.java

@Test
public void parseResponse() throws Exception {
    HttpResponse stubResponse = new BasicHttpResponse(
            new BasicStatusLine(new ProtocolVersion("1.1", 1, 1), 200, "OK"));

    String json = "      {\n" + "        \"uuid\": \"93137ee3-580e-45f7-a61a-e0b5716000ef\",\n"
            + "        \"status\": \"completed\",\n" + "        \"direction\": \"outbound\",\n"
            + "        \"rate\": \"0.02400000\",\n" + "        \"price\": \"0.00280000\",\n"
            + "        \"duration\": \"7\",\n" + "        \"network\": \"23410\",\n"
            + "        \"conversation_uuid\": \"aa17bd11-c895-4225-840d-30dc38c31e50\",\n"
            + "        \"start_time\": \"2017-01-13T13:55:02.000Z\",\n"
            + "        \"end_time\": \"2017-01-13T13:55:09.000Z\",\n" + "        \"to\": {\n"
            + "          \"type\": \"phone\",\n" + "          \"number\": \"447700900104\"\n" + "        },\n"
            + "        \"from\": {\n" + "          \"type\": \"phone\",\n"
            + "          \"number\": \"447700900105\"\n" + "        },\n" + "        \"_links\": {\n"
            + "          \"self\": {\n"
            + "            \"href\": \"/v1/calls/93137ee3-580e-45f7-a61a-e0b5716000ef\"\n" + "          }\n"
            + "        }\n" + "      }\n";
    InputStream jsonStream = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));
    BasicHttpEntity entity = new BasicHttpEntity();
    entity.setContent(jsonStream);//from   ww w . jav  a  2 s  .c  o m
    stubResponse.setEntity(entity);

    CallInfo record = method.parseResponse(stubResponse);
    assertEquals("93137ee3-580e-45f7-a61a-e0b5716000ef", record.getUuid());
}

From source file:com.eTilbudsavis.etasdk.network.impl.HttpURLNetwork.java

private static HttpEntity getEntity(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;//w ww .  ja va2  s . c o  m
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}

From source file:io.cloudslang.content.httpclient.consume.HttpResponseConsumerTest.java

private void setHttpResponseEntity(String contentType) {
    BasicHttpEntity entity = new BasicHttpEntity();
    entity.setContent(inputStreamMock);/*from  w ww. j a va2  s.c o  m*/
    Header contentTypeHeader = new HeaderEntity("Content-Type", contentType);
    entity.setContentType(contentTypeHeader);
    when(httpResponseMock.getEntity()).thenReturn(entity);
}

From source file:org.apache.synapse.samples.framework.clients.BasicHttpClient.java

/**
 * Make a HTTP POST request on the specified URL.
 *
 * @param url A valid HTTP URL/* w ww . j a  va2s . co m*/
 * @param payload An array of bytes to be posted to the URL (message body)
 * @param contentType Content type of the message body
 * @param headers A map of HTTP headers to be set on the outgoing request
 * @return A HttpResponse object
 * @throws Exception If an error occurs while making the HTTP call
 */
public HttpResponse doPost(String url, byte[] payload, String contentType, Map<String, String> headers)
        throws Exception {
    CloseableHttpClient client = HttpClientBuilder.create().build();
    try {
        HttpPost post = new HttpPost(url);
        if (headers != null) {
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                post.setHeader(entry.getKey(), entry.getValue());
            }
        }
        BasicHttpEntity entity = new BasicHttpEntity();
        entity.setContentType(contentType);
        entity.setContent(new ByteArrayInputStream(payload));
        post.setEntity(entity);
        return new HttpResponse(client.execute(post));
    } finally {
        client.close();
    }
}

From source file:org.qucosa.camel.component.sword.SwordConnection.java

public HttpResponse deposit(SwordDeposit deposit, Boolean noop, String slugHeader, String onBehalfOfHeader)
        throws Exception {
    URIBuilder uriBuilder = new URIBuilder(url + "/" + deposit.getCollection());
    HttpPost httpPost = new HttpPost(uriBuilder.build());
    httpPost.setHeader("X-No-Op", String.valueOf(noop));
    httpPost.setHeader("Content-Type", deposit.getContentType());

    if (onBehalfOfHeader != null && !onBehalfOfHeader.isEmpty()) {
        httpPost.setHeader("X-On-Behalf-Of", onBehalfOfHeader);
    }//from  w  ww.j ava  2 s  .co  m

    if (slugHeader != null && !slugHeader.isEmpty()) {
        httpPost.setHeader("Slug", slugHeader);
    }

    BasicHttpEntity httpEntity = new BasicHttpEntity();
    httpEntity.setContent(toInputStream(deposit.getBody()));
    httpEntity.setContentType(deposit.getContentType());
    httpPost.setEntity(new BufferedHttpEntity(httpEntity));

    HttpResponse response = httpClient.execute(httpPost);
    EntityUtils.consume(response.getEntity());

    if (log.isDebugEnabled()) {
        if (noop) {
            log.debug("SWORD parameter 'X-No-Op' is '{}'", noop);
            log.debug("SWORD parameter 'X-On-Behalf-Of' is '{}'", onBehalfOfHeader);
            log.debug("Slug header is '{}'", slugHeader);
            log.debug("Content type is '{}'", deposit.getContentType());
            log.debug("Posting to SWORD collection '{}'", deposit.getCollection());
        }
        log.debug(response.toString());
    }

    if (response.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED) {
        String reason = response.getStatusLine().getReasonPhrase();
        throw new Exception(reason);
    }

    return response;
}

From source file:org.aerogear.android.impl.core.HttpRestProvider.java

private void addBodyRequest(HttpEntityEnclosingRequestBase requestBase, String data) {
    BasicHttpEntity entity = new BasicHttpEntity();
    entity.setContent(new ByteArrayInputStream(data.getBytes()));
    requestBase.setEntity(entity);//from   w w  w  .  j av a2 s.c o  m
}

From source file:com.objectivetruth.uoitlibrarybooking.app.networking.OkHttp3Stack.java

private static HttpEntity entityFromOkHttpResponse(Response r) throws IOException {
    BasicHttpEntity entity = new BasicHttpEntity();
    ResponseBody body = r.body();//from w  ww  .  java 2s  . com
    entity.setContent(body.byteStream());
    entity.setContentLength(body.contentLength());
    entity.setContentEncoding(r.header("Content-Encoding"));

    if (body.contentType() != null) {
        entity.setContentType(body.contentType().type());
    }
    return entity;
}

From source file:org.apache.hadoop.gateway.dispatch.CappedBufferHttpEntityTest.java

@Test
public void testS__C1_FC_OB() throws IOException {
    String data = "0123456789";
    BasicHttpEntity basic;/*from www .  java  2 s . co  m*/
    CappedBufferHttpEntity replay;

    basic = new BasicHttpEntity();
    basic.setContent(new ByteArrayInputStream(data.getBytes("UTF-8")));
    replay = new CappedBufferHttpEntity(basic, 5);

    String output;

    try {
        output = byteRead(replay.getContent(), -1);
        fail("expected IOException");
    } catch (IOException e) {
        // expected
    }
}