Search in sources :

Example 1 with InputStreamEntity

use of org.apache.http.entity.InputStreamEntity in project camel by apache.

the class CxfRsConsumerSimpleBindingTest method testUploadInputStream.

@Test
public void testUploadInputStream() throws Exception {
    HttpPost post = new HttpPost("http://localhost:" + PORT_PATH + "/rest/customerservice/customers/123/image_inputstream");
    post.addHeader("Content-Type", "image/jpeg");
    post.addHeader("Accept", "text/xml");
    post.setEntity(new InputStreamEntity(this.getClass().getClassLoader().getResourceAsStream("java.jpg"), 100));
    HttpResponse response = httpclient.execute(post);
    assertEquals(200, response.getStatusLine().getStatusCode());
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpResponse(org.apache.http.HttpResponse) InputStreamEntity(org.apache.http.entity.InputStreamEntity) Test(org.junit.Test)

Example 2 with InputStreamEntity

use of org.apache.http.entity.InputStreamEntity in project camel by apache.

the class CxfRsConsumerSimpleBindingTest method testUploadDataHandler.

@Test
public void testUploadDataHandler() throws Exception {
    HttpPost post = new HttpPost("http://localhost:" + PORT_PATH + "/rest/customerservice/customers/123/image_datahandler");
    post.addHeader("Content-Type", "image/jpeg");
    post.addHeader("Accept", "text/xml");
    post.setEntity(new InputStreamEntity(this.getClass().getClassLoader().getResourceAsStream("java.jpg"), 100));
    HttpResponse response = httpclient.execute(post);
    assertEquals(200, response.getStatusLine().getStatusCode());
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpResponse(org.apache.http.HttpResponse) InputStreamEntity(org.apache.http.entity.InputStreamEntity) Test(org.junit.Test)

Example 3 with InputStreamEntity

use of org.apache.http.entity.InputStreamEntity in project camel by apache.

the class HttpEntityConverter method asHttpEntity.

private static HttpEntity asHttpEntity(InputStream in, Exchange exchange) throws IOException {
    InputStreamEntity entity;
    if (!exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) {
        String contentEncoding = exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class);
        InputStream stream = GZIPHelper.compressGzip(contentEncoding, in);
        entity = new InputStreamEntity(stream, stream instanceof ByteArrayInputStream ? stream.available() != 0 ? stream.available() : -1 : -1);
    } else {
        Message inMessage = exchange.getIn();
        String length = inMessage.getHeader(Exchange.CONTENT_LENGTH, String.class);
        if (ObjectHelper.isEmpty(length)) {
            entity = new InputStreamEntity(in, -1);
        } else {
            entity = new InputStreamEntity(in, Long.parseLong(length));
        }
    }
    if (exchange != null) {
        String contentEncoding = exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class);
        String contentType = ExchangeHelper.getContentType(exchange);
        entity.setContentEncoding(contentEncoding);
        entity.setContentType(contentType);
    }
    return entity;
}
Also used : Message(org.apache.camel.Message) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) InputStreamEntity(org.apache.http.entity.InputStreamEntity)

Example 4 with InputStreamEntity

use of org.apache.http.entity.InputStreamEntity in project camel by apache.

the class HttpEntityConverter method asHttpEntity.

private static HttpEntity asHttpEntity(byte[] data, Exchange exchange) throws Exception {
    AbstractHttpEntity entity;
    if (exchange != null && !exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) {
        String contentEncoding = exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class);
        InputStream stream = GZIPHelper.compressGzip(contentEncoding, data);
        entity = new InputStreamEntity(stream, stream instanceof ByteArrayInputStream ? stream.available() != 0 ? stream.available() : -1 : -1);
    } else {
        // create the Repeatable HttpEntity
        entity = new ByteArrayEntity(data);
    }
    if (exchange != null) {
        String contentEncoding = exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class);
        String contentType = ExchangeHelper.getContentType(exchange);
        entity.setContentEncoding(contentEncoding);
        entity.setContentType(contentType);
    }
    return entity;
}
Also used : ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) InputStreamEntity(org.apache.http.entity.InputStreamEntity)

Example 5 with InputStreamEntity

use of org.apache.http.entity.InputStreamEntity in project hbase by apache.

the class Client method put.

/**
   * Send a PUT request
   * @param cluster the cluster definition
   * @param path the path or URI
   * @param headers the HTTP headers to include, <tt>Content-Type</tt> must be
   * supplied
   * @param content the content bytes
   * @return a Response object with response detail
   * @throws IOException
   */
public Response put(Cluster cluster, String path, Header[] headers, byte[] content) throws IOException {
    HttpPut method = new HttpPut(path);
    try {
        method.setEntity(new InputStreamEntity(new ByteArrayInputStream(content), content.length));
        HttpResponse resp = execute(cluster, method, headers, path);
        headers = resp.getAllHeaders();
        content = getResponseBody(resp);
        return new Response(resp.getStatusLine().getStatusCode(), headers, content);
    } finally {
        method.releaseConnection();
    }
}
Also used : HttpResponse(org.apache.http.HttpResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpResponse(org.apache.http.HttpResponse) HttpPut(org.apache.http.client.methods.HttpPut) InputStreamEntity(org.apache.http.entity.InputStreamEntity)

Aggregations

InputStreamEntity (org.apache.http.entity.InputStreamEntity)41 ByteArrayInputStream (java.io.ByteArrayInputStream)15 HttpPost (org.apache.http.client.methods.HttpPost)14 HttpResponse (org.apache.http.HttpResponse)12 InputStream (java.io.InputStream)10 Test (org.junit.Test)10 HttpEntity (org.apache.http.HttpEntity)8 IOException (java.io.IOException)7 HttpPut (org.apache.http.client.methods.HttpPut)7 Map (java.util.Map)4 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)4 HttpClient (org.apache.http.client.HttpClient)4 StringEntity (org.apache.http.entity.StringEntity)4 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)4 URISyntaxException (java.net.URISyntaxException)3 HashMap (java.util.HashMap)3 Header (org.apache.http.Header)3 ProtocolVersion (org.apache.http.ProtocolVersion)3 HttpGet (org.apache.http.client.methods.HttpGet)3 BasicHttpEntityEnclosingRequest (org.apache.http.message.BasicHttpEntityEnclosingRequest)3