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

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

Introduction

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

Prototype

public InputStreamEntity(InputStream inputStream, ContentType contentType) 

Source Link

Usage

From source file:com.hdsfed.cometapi.HCPClient.java

public String HttpPutHCPContent(String xml_content, URL url) throws Exception {
    return HttpPutHCPContent(new InputStreamEntity(new ByteArrayInputStream(xml_content.getBytes()), -1), url);
}

From source file:com.clarionmedia.infinitum.http.rest.impl.CachingEnabledRestfulClient.java

@Override
public RestResponse executePut(String uri, InputStream messageBody, int messageBodyLength, String contentType,
        Map<String, String> headers) {
    HttpPut httpPut = new HttpPut(uri);
    for (Entry<String, String> header : headers.entrySet()) {
        httpPut.addHeader(header.getKey(), header.getValue());
    }/*from w  w  w . j  a v a 2  s  . co  m*/
    httpPut.addHeader("content-type", contentType);
    httpPut.setEntity(new InputStreamEntity(messageBody, messageBodyLength));
    try {
        RequestWrapper request = new RequestWrapper(httpPut);
        return executeRequest(new HashableHttpRequest(request));
    } catch (ProtocolException e) {
        throw new InfinitumRuntimeException("Unable to execute request", e);
    }
}

From source file:org.hardisonbrewing.s3j.FileSyncer.java

public void put(String path, InputStream inputStream, long length) throws Exception {

    String url = getUrl(path);//from   w  w w  .j a v a 2s.  co  m

    HttpPut httpRequest = new HttpPut(url);
    httpRequest.addHeader(HTTP.EXPECT_DIRECTIVE, HTTP.EXPECT_CONTINUE);
    addHeaders(httpRequest, path);

    System.out.println("Uploading: " + url);
    HttpUtil.printHeaders(httpRequest);

    if (md5Digest == null) {
        md5Digest = MessageDigest.getInstance("MD5");
    } else {
        md5Digest.reset();
    }

    HttpResponse httpResponse;
    HttpEntity httpEntity = null;

    try {
        inputStream = new ProgressInputStream(inputStream, length);
        inputStream = new DigestInputStream(inputStream, md5Digest);
        httpRequest.setEntity(new InputStreamEntity(inputStream, length));
        httpResponse = httpClient.execute(httpRequest);
        httpEntity = httpResponse.getEntity();
    } finally {
        EntityUtils.consume(httpEntity);
    }

    System.out.println("  Response Headers");
    HttpUtil.printHeaders(httpResponse);

    HttpUtil.validateResponseCode(httpResponse);

    byte[] digest = md5Digest.digest();
    String digestHex = Hex.encodeHexString(digest);

    String etag = getHeaderValue(httpResponse, "ETag");

    if (!etag.equals(digestHex)) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("Uploaded digest (");
        stringBuffer.append(digestHex);
        stringBuffer.append(") does not match ETag (");
        stringBuffer.append(etag);
        stringBuffer.append(").");
        throw new IOException(stringBuffer.toString());
    }
}

From source file:com.cloudant.client.org.lightcouch.CouchDbClientBase.java

/**
 * Performs a HTTP PUT request, saves an attachment.
 *
 * @return {@link Response}//from   w  w  w.jav  a2 s . com
 */
Response put(URI uri, InputStream instream, String contentType) {
    HttpResponse response = null;
    try {
        final HttpPut httpPut = new HttpPut(uri);
        final InputStreamEntity entity = new InputStreamEntity(instream, -1);
        entity.setContentType(contentType);
        httpPut.setEntity(entity);
        response = executeRequest(httpPut);
        return getResponse(response, Response.class, getGson());
    } finally {
        close(response);
    }
}

From source file:com.hdsfed.cometapi.HCPClient.java

public String HttpPutHCPContent(File inFile, URL url) throws FileNotFoundException, Exception {
    return HttpPutHCPContent(new InputStreamEntity(new FileInputStream(inFile), -1), url);
}

From source file:project.cs.netinfservice.netinf.node.search.UrlSearchService.java

/**
 * Creates the search request that is going to be sent to the NRS.
 * //  w ww .j  a v a  2  s. c o m
 * @param url
 *      URL that is going to be searched for
 * @return
 *      HTTP Post representing the search request
 * @throws UnsupportedEncodingException
 *      In case UTF-8 is not supported
 */
private HttpPost createSearch(String url) throws UnsupportedEncodingException {
    // Create URI to look like http://host:port/netinfproto/search
    String uri = HTTP + getHost() + ":" + getPort() + "/netinfproto/search";

    // Create the HTTP Post object with the uri from above
    HttpPost post = new HttpPost(uri);

    // Build additional URI
    StringBuilder query = new StringBuilder();

    // Add msgId
    query.append("?msgid=");
    query.append(Integer.toString(mRandomGenerator.nextInt(MSG_ID_MAX)));

    // Add tokens
    query.append("&tokens=");
    query.append(URLEncoder.encode(url, "UTF-8")); // URL must be encoded to work

    // Add ext
    query.append("&ext=");
    query.append("empty");

    // Put the full url in its own object. Should look like this:
    // http://host:port/netinfproto/search/?msgid=MSGID&tokens=TOKENS&ext=EXT
    String fullUrl = query.toString();

    // Create new entity
    HttpEntity newEntity = new InputStreamEntity(new ByteArrayInputStream(fullUrl.getBytes()),
            fullUrl.getBytes().length);

    // Add header
    post.addHeader("Content-Type", "application/x-www-form-urlencoded");

    // set post entity
    post.setEntity(newEntity);

    // return HTTP POST object with search
    return post;
}

From source file:com.cloudant.mazha.HttpRequests.java

protected void setEntity(HttpEntityEnclosingRequestBase httpRequest, String contentType, InputStream is,
        long contentLength) {
    InputStreamEntity entity = new InputStreamEntity(is, contentLength);
    entity.setContentType(contentType);/*from   w  w w. j  av a  2s. c o m*/
    httpRequest.setEntity(entity);
}

From source file:cn.tiup.httpproxy.ProxyServlet.java

protected HttpRequest newProxyRequestWithEntity(String method, String proxyRequestUri,
        HttpServletRequest servletRequest) throws IOException {
    HttpEntityEnclosingRequest eProxyRequest = new BasicHttpEntityEnclosingRequest(method, proxyRequestUri);
    // Add the input entity (streamed)
    //  note: we don't bother ensuring we close the servletInputStream since the container handles it
    eProxyRequest.setEntity(/*from   ww w.j  a  v a  2 s  .  co  m*/
            new InputStreamEntity(servletRequest.getInputStream(), getContentLength(servletRequest)));
    return eProxyRequest;
}

From source file:org.gitana.platform.client.support.RemoteImpl.java

@Override
public void upload(String uri, InputStream in, long length, String mimetype) throws Exception {
    InputStreamEntity entity = new InputStreamEntity(in, length);
    entity.setContentType(mimetype);//w w w  . ja v a 2s  . c o m

    String URL = buildURL(uri, false);
    HttpPost httpPost = new HttpPost(URL);
    httpPost.setEntity(entity);

    HttpResponse httpResponse = invoker.execute(httpPost);
    if (!HttpUtil.isOk(httpResponse)) {
        throw new RuntimeException("Upload failed: " + EntityUtils.toString(httpResponse.getEntity()));
    }

    // consume the response fully so that the client connection can be reused
    EntityUtils.consume(httpResponse.getEntity());
}

From source file:org.apache.camel.component.cxf.jaxrs.simplebinding.CxfRsConsumerSimpleBindingTest.java

@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(/*from www .  j ava  2s .c  o  m*/
            new InputStreamEntity(this.getClass().getClassLoader().getResourceAsStream("java.jpg"), 100));
    HttpResponse response = httpclient.execute(post);
    assertEquals(200, response.getStatusLine().getStatusCode());
}