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

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

Introduction

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

Prototype

public void setContent(InputStream inputStream) 

Source Link

Usage

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

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

    if (onBehalfOfHeader != null && !onBehalfOfHeader.isEmpty()) {
        httpPut.setHeader("X-On-Behalf-Of", onBehalfOfHeader);
    }//w  ww. j  a  v a  2  s .c  o  m

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

    HttpResponse response = httpClient.execute(httpPut);
    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("Content type is '{}'", deposit.getContentType());
            log.debug("Posting to SWORD collection '{}'", deposit.getCollection());
        }
        log.debug(response.toString());
    }

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

    return response;
}

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);
    }//  w ww  .ja v  a  2s  . c o  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:com.gooddata.http.client.GoodDataHttpClientTest.java

private HttpResponse createResponse(int status, String body, String reasonPhrase) {
    HttpResponse response = new BasicHttpResponse(
            new BasicStatusLine(new ProtocolVersion("https", 1, 1), status, reasonPhrase));
    BasicHttpEntity entity = new BasicHttpEntity();
    entity.setContent(new ByteArrayInputStream(body.getBytes()));
    response.setEntity(entity);//from w w  w. ja v a2s  . c om
    return response;
}

From source file:com.gooddata.http.client.LoginSSTRetrievalStrategyTest.java

private void prepareLoginFailureResponse() throws IOException, ClientProtocolException {
    statusLine = new BasicStatusLine(new ProtocolVersion("https", 1, 1), HttpStatus.SC_UNAUTHORIZED,
            "Unauthorized");
    final HttpResponse response = new BasicHttpResponse(statusLine);
    response.setHeader("X-GDC-Request", REQUEST_ID);
    BasicHttpEntity entity = new BasicHttpEntity();
    entity.setContent(new ByteArrayInputStream(FAILURE_REASON.getBytes()));
    response.setEntity(entity);//w w  w  . j a  va 2s.c  om
    when(httpClient.execute(any(HttpHost.class), any(HttpPost.class))).thenReturn(response);
    sstStrategy.setLogger(logger);
}

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 .  jav a  2  s  .  c om*/
    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:org.apache.nifi.processors.aws.wag.TestInvokeAmazonGatewayApiMock.java

@Test
public void testSendAttributes() 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 w  w  .  j  a  va 2 s. c  om
    Mockito.doReturn(resp).when(mockSdkClient).execute(any(HttpUriRequest.class), any(HttpContext.class));

    // add dynamic property
    runner.setProperty("dynamicHeader", "yes!");
    // set the regex
    runner.setProperty(InvokeAWSGatewayApi.PROP_ATTRIBUTES_TO_SEND, "F.*");

    final Map<String, String> attributes = new HashMap<>();
    attributes.put(CoreAttributes.MIME_TYPE.key(), "application/plain-text");
    attributes.put("Foo", "Bar");
    runner.enqueue("Hello".getBytes("UTF-8"), attributes);
    // execute
    runner.assertValid();
    runner.run(1);

    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.getFirstHeader("dynamicHeader").getValue().equals("yes!")
                && x.getFirstHeader("Foo").getValue().equals("Bar")
                && x.getURI().toString().equals("https://foobar.execute-api.us-east-1.amazonaws.com/TEST");
    })), any(HttpContext.class));
    // check
    runner.assertTransferCount(InvokeAWSGatewayApi.REL_SUCCESS_REQ, 1);
    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:org.apache.nifi.processors.aws.wag.TestInvokeAmazonGatewayApiMock.java

@Test
public void testSendQueryParams() 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  a  v a 2  s .  co  m*/
    Mockito.doReturn(resp).when(mockSdkClient).execute(any(HttpUriRequest.class), any(HttpContext.class));

    // add dynamic property
    runner.setProperty("dynamicHeader", "yes!");
    runner.setProperty(InvokeAWSGatewayApi.PROP_QUERY_PARAMS, "apples=oranges&dogs=cats");

    // set the regex
    runner.setProperty(InvokeAWSGatewayApi.PROP_ATTRIBUTES_TO_SEND, "F.*");

    final Map<String, String> attributes = new HashMap<>();
    attributes.put(CoreAttributes.MIME_TYPE.key(), "application/plain-text");
    attributes.put("Foo", "Bar");
    runner.enqueue("Hello".getBytes("UTF-8"), attributes);
    // execute
    runner.assertValid();
    runner.run(1);

    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.getFirstHeader("dynamicHeader").getValue().equals("yes!")
                && x.getFirstHeader("Foo").getValue().equals("Bar") && x.getURI().toString().equals(
                        "https://foobar.execute-api.us-east-1.amazonaws.com/TEST?dogs=cats&apples=oranges");
    })), any(HttpContext.class));
    // check
    runner.assertTransferCount(InvokeAWSGatewayApi.REL_SUCCESS_REQ, 1);
    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.overture.questdroid.utility.ExtHttpClientStack.java

private HttpEntity convertEntityNewToOld(HttpEntity ent) throws IllegalStateException, IOException {

    BasicHttpEntity ret = new BasicHttpEntity();
    if (ent != null) {
        ret.setContent(ent.getContent());
        ret.setContentLength(ent.getContentLength());
        Header h;//from   www  . j  a  va2  s.  c o  m
        h = ent.getContentEncoding();
        if (h != null) {
            ret.setContentEncoding(convertheaderNewToOld(h));
        }
        h = ent.getContentType();
        if (h != null) {
            ret.setContentType(convertheaderNewToOld(h));
        }
    }

    return ret;
}

From source file:carleton150.edu.carleton.carleton150.CertificateManagement.ExtHttpClientStack.java

private org.apache.http.HttpEntity convertEntityNewToOld(HttpEntity ent)
        throws IllegalStateException, IOException {

    BasicHttpEntity ret = new BasicHttpEntity();
    if (ent != null) {
        ret.setContent(ent.getContent());
        ret.setContentLength(ent.getContentLength());
        Header h;//  w  w  w.ja va  2s .  c o m
        h = ent.getContentEncoding();
        if (h != null) {
            ret.setContentEncoding(convertheaderNewToOld(h));
        }
        h = ent.getContentType();
        if (h != null) {
            ret.setContentType(convertheaderNewToOld(h));
        }
    }

    return ret;
}

From source file:com.baasbox.android.net.OkClient.java

private HttpEntity asEntity(Response resp) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream = resp.body().byteStream();
    entity.setContent(inputStream);
    entity.setContentLength(resp.body().contentLength());
    String ctnt = resp.body().contentType().toString();
    entity.setContentType(ctnt);/*from w w  w . j a  v a 2 s  .  c  o m*/
    return entity;
}