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.dream.messaging.sender.http.HttpClientTemplate.java

/**
 * byte?/*from   ww  w.j ava  2s . c  o m*/
 * @param uri
 * @param input
 * @param headers
 * @param params
 * @return
 * @throws IOException
 */
public byte[] executeHttpPost(String uri, byte[] input, Map headers, Map params) throws IOException {
    return executeHttpPost1(uri,
            (input != null ? new InputStreamEntity(new ByteArrayInputStream(input), input.length) : null),
            headers, params);
}

From source file:com.kolich.http.common.HttpClient4ClosureBase.java

public T post(final HttpPost post, final InputStream is, final long length, final String contentType,
        final HttpContext context) {
    if (is != null) {
        final InputStreamEntity entity = new InputStreamEntity(is, length);
        if (contentType != null) {
            entity.setContentType(contentType);
        }/*from  w w  w.  j  ava  2s. c om*/
        post.setEntity(entity);
    }
    return request(post, context);
}

From source file:org.dataconservancy.dcs.ingest.client.impl.SwordClientManager.java

public DepositInfo deposit(InputStream content, String contentType, String packaging,
        Map<String, String> metadata) throws PackageException {

    if (metadata == null) {
        metadata = new HashMap<String, String>();
    }//ww  w .j  a v  a2  s .co m

    HttpPost post = new HttpPost(collectionUrl);
    for (Map.Entry<String, String> val : metadata.entrySet()) {
        if (!val.getKey().equals(HttpHeaderUtil.CONTENT_LENGTH)) {
            post.setHeader(val.getKey(), val.getValue());
        }
    }

    post.setHeader(HttpHeaderUtil.CONTENT_TYPE, contentType);
    if (packaging != null) {
        post.setHeader(PACKAGING, packaging);
    }

    if (!metadata.containsKey(HttpHeaderUtil.CONTENT_LENGTH)) {
        BasicHttpEntity entity = new BasicHttpEntity();
        entity.setContent(content);
        post.setEntity(entity);
    } else {
        post.setEntity(new InputStreamEntity(content, new Long(metadata.get(HttpHeaderUtil.CONTENT_LENGTH))));
    }

    return execute(post);
}

From source file:net.gcolin.httpquery.HttpHandlerImpl.java

@Override
public Request post(String uri, InputStream inStream) {
    HttpPost post = new HttpPost(uri);
    post.setEntity(new InputStreamEntity(inStream, -1));
    return new RequestImpl(post);
}

From source file:com.woonoz.proxy.servlet.HttpPostRequestHandler.java

private HttpEntity createHttpEntity(HttpServletRequest request, HttpPost httpPost)
        throws FileUploadException, IOException {
    if (ServletFileUpload.isMultipartContent(request)) {
        return createMultipartEntity(request, httpPost);
    } else {//from ww w  .j  a v a 2s .  c o m
        return new BufferedHttpEntity(
                new InputStreamEntity(request.getInputStream(), request.getContentLength()));
    }
}

From source file:org.elasticsearch.client.RequestLoggerTests.java

public void testTraceRequest() throws IOException, URISyntaxException {
    HttpHost host = new HttpHost("localhost", 9200, randomBoolean() ? "http" : "https");
    String expectedEndpoint = "/index/type/_api";
    URI uri;//from  w  ww  .  j  ava2s . c o m
    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));
    }
}

From source file:de.dentrassi.pm.jenkins.UploaderV2.java

@Override
public void upload(final File file, final String filename) throws IOException {
    final URI uri = makeUrl(filename);
    final HttpPut httppost = new HttpPut(uri);

    final InputStream stream = new FileInputStream(file);
    try {/*from  w w w. j  a  v  a2  s .  co  m*/
        httppost.setEntity(new InputStreamEntity(stream, file.length()));

        final HttpResponse response = this.client.execute(httppost);
        final HttpEntity resEntity = response.getEntity();

        if (resEntity != null) {
            switch (response.getStatusLine().getStatusCode()) {
            case 200:
                addUploadedArtifacts(filename, resEntity);
                break;
            default:
                addUploadFailure(filename, response);
                break;
            }
        }
    } finally {
        stream.close();
    }
}

From source file:com.aliyun.android.oss.task.UploadPartTask.java

/**
 * HttpPut//  www  .j  a  va  2 s. co  m
 */
protected HttpUriRequest generateHttpRequest() {
    String requestUri = this.getOSSEndPoint()
            + httpTool.generateCanonicalizedResource("/" + OSSHttpTool.encodeUri(objectKey));
    HttpPut httpPut = new HttpPut(requestUri);

    String resource = httpTool.generateCanonicalizedResource("/" + bucketName + "/" + objectKey);
    String dateStr = Helper.getGMTDate();
    String authorization = OSSHttpTool.generateAuthorization(accessId, accessKey, httpMethod.toString(), "", "",
            dateStr, "", resource);

    httpPut.setHeader(AUTHORIZATION, authorization);
    httpPut.setHeader(DATE, dateStr);

    httpPut.setHeader(UPLOAD_ID, httpTool.getUploadId());
    httpPut.setHeader(PART_NUMBER, Integer.toString(httpTool.getPartNumber()));

    InputStreamEntity entity = new InputStreamEntity(part.getStream(), part.getSize());
    httpPut.setEntity(entity);

    return httpPut;
}

From source file:com.ibm.stocator.fs.swift.SwiftOutputStream.java

/**
 * Default constructor/*from w  ww  . ja va2 s  .co m*/
 *
 * @param account JOSS account object
 * @param url URL connection
 * @param contentType content type
 * @param metadata input metadata
 * @param connectionManager SwiftConnectionManager
 * @throws IOException if error
 */
public SwiftOutputStream(JossAccount account, URL url, final String contentType, Map<String, String> metadata,
        SwiftConnectionManager connectionManager) throws IOException {
    mUrl = url;
    totalWritten = 0;
    mAccount = account;
    client = connectionManager.createHttpConnection();
    request = new HttpPut(mUrl.toString());
    request.addHeader("X-Auth-Token", account.getAuthToken());
    if (metadata != null && !metadata.isEmpty()) {
        for (Map.Entry<String, String> entry : metadata.entrySet()) {
            request.addHeader("X-Object-Meta-" + entry.getKey(), entry.getValue());
        }
    }

    PipedOutputStream out = new PipedOutputStream();
    final PipedInputStream in = new PipedInputStream();
    out.connect(in);
    execService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    mOutputStream = out;
    Callable<Void> task = new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            InputStreamEntity entity = new InputStreamEntity(in, -1);
            entity.setChunked(true);
            entity.setContentType(contentType);
            request.setEntity(entity);

            LOG.debug("HTTP PUT request {}", mUrl.toString());
            HttpResponse response = client.execute(request);
            int responseCode = response.getStatusLine().getStatusCode();
            LOG.debug("HTTP PUT response {}. Response code {}", mUrl.toString(), responseCode);
            if (responseCode == 401) { // Unauthorized error
                mAccount.authenticate();
                request.removeHeaders("X-Auth-Token");
                request.addHeader("X-Auth-Token", mAccount.getAuthToken());
                LOG.warn("Token recreated for {}.  Retry request", mUrl.toString());
                response = client.execute(request);
                responseCode = response.getStatusLine().getStatusCode();
            }
            if (responseCode >= 400) { // Code may have changed from retrying
                throw new IOException("HTTP Error: " + responseCode + " Reason: "
                        + response.getStatusLine().getReasonPhrase());
            }

            return null;
        }
    };
    futureTask = execService.submit(task);
}