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) 

Source Link

Usage

From source file:org.camunda.connect.httpclient.impl.AbstractHttpConnector.java

protected <T extends HttpRequestBase> void applyPayload(T httpRequest, Q request) {
    if (httpMethodSupportsPayload(httpRequest)) {
        if (request.getPayload() != null) {
            ByteArrayInputStream payload = new ByteArrayInputStream(
                    request.getPayload().getBytes(Charset.forName("utf-8")));
            ((HttpEntityEnclosingRequestBase) httpRequest).setEntity(new InputStreamEntity(payload));
        }/* www.ja v a 2s .c  o  m*/
    } else if (request.getPayload() != null) {
        LOG.payloadIgnoredForHttpMethod(request.getMethod());
    }
}

From source file:com.teradata.tempto.internal.hadoop.hdfs.WebHDFSClient.java

@Override
public void saveFile(String path, String username, InputStream input) {
    try {//w w  w . ja va2  s. c  om
        saveFile(path, username, new BufferedHttpEntity(new InputStreamEntity(input)));
    } catch (IOException e) {
        throw new RuntimeException("Could not create buffered http entity", e);
    }
}

From source file:integration.report.ReportIT.java

private void createOaiDcObject(String oaiDcId, InputStream src) throws Exception {
    HttpPost post = new HttpPost(serverAddress + "/");
    post.addHeader("Slug", oaiDcId);
    post.addHeader("Content-Type", "application/octet-stream");
    post.setEntity(new InputStreamEntity(src));
    HttpResponse resp = this.client.execute(post);
    assertEquals(201, resp.getStatusLine().getStatusCode());
    post.releaseConnection();/*  w  w  w  .java 2 s  . co  m*/
}

From source file:org.jboss.pnc.auth.keycloakutil.util.HttpUtil.java

public static HeadersBodyStatus doRequest(String type, String url, HeadersBody request) throws IOException {
    HttpRequestBase req;/* w ww  .j a v  a2s . co  m*/
    switch (type) {
    case "get":
        req = new HttpGet(url);
        break;
    case "post":
        req = new HttpPost(url);
        break;
    case "put":
        req = new HttpPut(url);
        break;
    case "delete":
        req = new HttpDelete(url);
        break;
    case "options":
        req = new HttpOptions(url);
        break;
    case "head":
        req = new HttpHead(url);
        break;
    default:
        throw new RuntimeException("Method not supported: " + type);
    }
    addHeaders(req, request.getHeaders());

    if (request.getBody() != null) {
        if (req instanceof HttpEntityEnclosingRequestBase == false) {
            throw new RuntimeException("Request type does not support body: " + type);
        }
        ((HttpEntityEnclosingRequestBase) req).setEntity(new InputStreamEntity(request.getBody()));
    }

    HttpResponse res = getHttpClient().execute(req);
    InputStream responseStream = null;
    if (res.getEntity() != null) {
        responseStream = res.getEntity().getContent();
    } else {
        responseStream = new InputStream() {
            @Override
            public int read() throws IOException {
                return -1;
            }
        };
    }

    Headers headers = new Headers();
    HeaderIterator it = res.headerIterator();
    while (it.hasNext()) {
        org.apache.http.Header header = it.nextHeader();
        headers.add(header.getName(), header.getValue());
    }

    return new HeadersBodyStatus(res.getStatusLine().toString(), headers, responseStream);
}

From source file:org.fcrepo.camel.FcrepoClient.java

/**
 * Make a PATCH request//from   w  w w . j a v  a  2s . c om
 * Please note: the body should have an application/sparql-update content-type
 * @param url the URL of the resource to PATCH
 * @param body the body to be sent to the repository
 * @return the repository response
 * @throws FcrepoOperationFailedException when the underlying HTTP request results in an error
 */
public FcrepoResponse patch(final URI url, final InputStream body) throws FcrepoOperationFailedException {

    final HttpMethods method = HttpMethods.PATCH;
    final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) method.createRequest(url);

    request.addHeader(CONTENT_TYPE, "application/sparql-update");
    request.setEntity(new InputStreamEntity(body));

    LOGGER.debug("Fcrepo PATCH request headers: {}", request.getAllHeaders());

    final HttpResponse response = executeRequest(request);

    LOGGER.debug("Fcrepo PATCH request returned status [{}]", response.getStatusLine().getStatusCode());

    return fcrepoGenericResponse(url, response, throwExceptionOnFailure);
}

From source file:mx.ipn.escom.supernaut.nile.logic.CommonBean.java

@Override
@PrePassivate/*  www  .ja va 2  s  .c om*/
public void commitModel() {
    HttpPut request = new HttpPut(baseUri + getPkAsParams());
    request.addHeader(JSON_OUT_HEAD);
    HttpResponse response;
    try {
        request.setEntity(new InputStreamEntity(streamedMarshall()));
        response = client.execute(HOST, request);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
    StatusLine statusLine = response.getStatusLine();
    if (statusLine.getStatusCode() < 200 || statusLine.getStatusCode() > 299)
        throw new RuntimeException(statusLine.getReasonPhrase());
}

From source file:com.seleniumtests.connectors.selenium.SeleniumRobotGridConnector.java

/**
 * Upload a file given file path//  www.j av  a 2  s .  c om
 * @param filePath
 */
@Override
public void uploadFile(String filePath) {
    try (CloseableHttpClient client = HttpClients.createDefault();) {
        // zip file
        List<File> appFiles = new ArrayList<>();
        appFiles.add(new File(filePath));
        File zipFile = FileUtility.createZipArchiveFromFiles(appFiles);

        HttpHost serverHost = new HttpHost(hubUrl.getHost(), hubUrl.getPort());
        URIBuilder builder = new URIBuilder();
        builder.setPath("/grid/admin/FileServlet/");
        builder.addParameter("output", "app");
        HttpPost httpPost = new HttpPost(builder.build());
        httpPost.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.OCTET_STREAM.toString());
        FileInputStream fileInputStream = new FileInputStream(zipFile);
        InputStreamEntity entity = new InputStreamEntity(fileInputStream);
        httpPost.setEntity(entity);

        CloseableHttpResponse response = client.execute(serverHost, httpPost);
        if (response.getStatusLine().getStatusCode() != 200) {
            throw new SeleniumGridException(
                    "could not upload file: " + response.getStatusLine().getReasonPhrase());
        } else {
            // TODO call remote API
            throw new NotImplementedException("call remote Robot to really upload file");
        }

    } catch (IOException | URISyntaxException e) {
        throw new SeleniumGridException("could not upload file", e);
    }
}

From source file:com.vsct.dt.hesperides.indexation.ElasticSearchIndexationExecutor.java

public void reset() throws IOException {
    /* Reset the index */
    HttpDelete deleteIndex = null;//w w w .j a v a2 s.c o m
    try {
        deleteIndex = new HttpDelete("/" + elasticSearchClient.getIndex());
        elasticSearchClient.getClient().execute(elasticSearchClient.getHost(), deleteIndex);
    } catch (final Exception e) {
        LOGGER.info(
                "Could not delete elastic search index. This mostly happens when there is no index already");
    } finally {
        if (deleteIndex != null) {
            deleteIndex.releaseConnection();
        }
    }

    LOGGER.debug("Deleted Hesperides index {}", elasticSearchClient.getIndex());

    /* Add global mapping */
    HttpPut putGlobalMapping = null;
    try (InputStream globalMappingFile = this.getClass().getClassLoader()
            .getResourceAsStream("elasticsearch/global_mapping.json")) {

        putGlobalMapping = new HttpPut("/" + elasticSearchClient.getIndex());

        putGlobalMapping.setEntity(new InputStreamEntity(globalMappingFile));
        elasticSearchClient.getClient().execute(elasticSearchClient.getHost(), putGlobalMapping);

        LOGGER.debug("Put new global mapping in {}", elasticSearchClient.getIndex());
    } finally {
        if (putGlobalMapping != null) {
            putGlobalMapping.releaseConnection();
        }
    }

    /* Add documents mapping
     */
    for (final Mapping mapping : MAPPINGS) {

        HttpPut putMapping = null;
        try (InputStream mappingFile = this.getClass().getClassLoader().getResourceAsStream(mapping.resource)) {

            putMapping = new HttpPut(
                    "/" + elasticSearchClient.getIndex() + "/" + mapping.documentName + "/_mapping");
            putMapping.setEntity(new InputStreamEntity(mappingFile));
            elasticSearchClient.getClient().execute(elasticSearchClient.getHost(), putMapping);

            LOGGER.debug("Put new mapping in {}", mapping.documentName);
        } finally {
            if (putMapping != null) {
                putMapping.releaseConnection();
            }
        }

    }

}

From source file:com.helger.httpclient.HttpClientHelper.java

@Nullable
public static HttpEntity createParameterEntity(@Nullable final Map<String, String> aMap,
        @Nonnull final Charset aCharset) {
    ValueEnforcer.notNull(aCharset, "Charset");
    if (aMap == null || aMap.isEmpty())
        return null;

    try (final NonBlockingByteArrayOutputStream aBAOS = new NonBlockingByteArrayOutputStream(1024)) {
        final URLCodec aURLCodec = new URLCodec();
        boolean bFirst = true;
        for (final Map.Entry<String, String> aEntry : aMap.entrySet()) {
            if (bFirst)
                bFirst = false;/*from   w  w w .  j  a  v  a2 s . co  m*/
            else
                aBAOS.write('&');

            // Key must be present
            final String sKey = aEntry.getKey();
            aURLCodec.encode(sKey.getBytes(aCharset), aBAOS);

            // Value is optional
            final String sValue = aEntry.getValue();
            if (StringHelper.hasText(sValue)) {
                aBAOS.write('=');
                aURLCodec.encode(sValue.getBytes(aCharset), aBAOS);
            }
        }
        return new InputStreamEntity(aBAOS.getAsInputStream());
    }
}