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.yihaodian.performance.UploadReportMojo.java

@Override
public void execute() throws MojoExecutionException {
    File file = new File("target/perf4junit/report.xml");
    FileInputStream fis;/*from w  w w. j a  v  a2s . co  m*/
    BufferedInputStream bis;
    try {
        fis = new FileInputStream(file);
        bis = new BufferedInputStream(fis);

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost http = new HttpPost(endPoint);
        http.setEntity(new InputStreamEntity(bis, file.length()));
        HttpResponse response = httpclient.execute(http);
        getLog().info(endPoint);
        bis.close();
        fis.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:marytts.server.http.MaryHttpServerUtils.java

public static void toHttpResponse(InputStream stream, HttpResponse response, String contentType,
        long streamLength) throws IOException {
    InputStreamEntity body = new InputStreamEntity(stream, streamLength);
    body.setContentType(contentType);// w w w. jav a2s  .c  o m
    response.setEntity(body);
    response.setStatusCode(HttpStatus.SC_OK);
}

From source file:org.elasticsearch.client.sniff.ElasticsearchNodesSnifferParseTests.java

private void checkFile(String file, Node... expected) throws IOException {
    InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(file);
    if (in == null) {
        throw new IllegalArgumentException("Couldn't find [" + file + "]");
    }//from  www  .  ja  va  2s. c  o m
    try {
        HttpEntity entity = new InputStreamEntity(in, ContentType.APPLICATION_JSON);
        List<Node> nodes = ElasticsearchNodesSniffer.readHosts(entity, Scheme.HTTP, new JsonFactory());
        /*
         * Use these assertions because the error messages are nicer
         * than hasItems and we know the results are in order because
         * that is how we generated the file.
         */
        assertThat(nodes, hasSize(expected.length));
        for (int i = 0; i < expected.length; i++) {
            assertEquals(expected[i], nodes.get(i));
        }
    } finally {
        in.close();
    }
}

From source file:com.autonomy.aci.client.services.impl.DocumentProcessorTest.java

@Test(expected = FactoryConfigurationError.class)
public void testConvertACIResponseToDOMInvalidDocumentBuilderFactory()
        throws AciErrorException, IOException, ProcessorException {
    // Set a duff property for the DocumentBuilderFactory...
    System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "com.autonomy.DuffDocumentBuilderFactory");

    try {/*from  w  w  w . j  a  v a  2s  .co  m*/
        // Setup with a proper XML response file...
        final BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
        response.setEntity(new InputStreamEntity(getClass().getResourceAsStream("/GetVersion.xml"), -1));

        // Set the AciResponseInputStream...
        final AciResponseInputStream stream = new AciResponseInputStreamImpl(response);

        // Process...
        processor.process(stream);
        fail("Should have raised an ProcessorException.");
    } finally {
        // Remove the duff system property...
        System.clearProperty("javax.xml.parsers.DocumentBuilderFactory");
    }
}

From source file:org.digidoc4j.impl.bdoc.SKOcspDataLoader.java

@Override
public byte[] post(final String url, final byte[] content) throws DSSException {
    logger.info("Getting OCSP response from " + url);

    HttpPost httpRequest = null;/*from ww  w.ja  v a 2  s  .  c  o  m*/
    HttpResponse httpResponse = null;

    try {
        final URI uri = URI.create(url.trim());
        httpRequest = new HttpPost(uri);
        httpRequest.setHeader("User-Agent", userAgent);

        // The length for the InputStreamEntity is needed, because some receivers (on the other side) need this information.
        // To determine the length, we cannot read the content-stream up to the end and re-use it afterwards.
        // This is because, it may not be possible to reset the stream (= go to position 0).
        // So, the solution is to cache temporarily the complete content data (as we do not expect much here) in a byte-array.
        final ByteArrayInputStream bis = new ByteArrayInputStream(content);

        final HttpEntity httpEntity = new InputStreamEntity(bis, content.length);
        final HttpEntity requestEntity = new BufferedHttpEntity(httpEntity);
        httpRequest.setEntity(requestEntity);
        if (contentType != null) {
            httpRequest.setHeader(CONTENT_TYPE, contentType);
        }

        httpResponse = getHttpResponse(httpRequest, url);

        final byte[] returnedBytes = readHttpResponse(url, httpResponse);
        return returnedBytes;
    } catch (IOException e) {
        throw new DSSException(e);
    } finally {
        if (httpRequest != null) {
            httpRequest.releaseConnection();
        }
        if (httpResponse != null) {
            EntityUtils.consumeQuietly(httpResponse.getEntity());
        }
    }
}

From source file:com.nominanuda.web.mvc.URLStreamer.java

public HttpResponse handle(HttpRequest request) throws IOException {
    URL url = getURL(request);//  www.  j a  va  2 s.  c o m
    URLConnection conn = url.openConnection();
    conn.connect();
    int len = conn.getContentLength();
    InputStream is = conn.getInputStream();
    String ce = conn.getContentEncoding();
    String ct = determineContentType(url, conn);
    if (len < 0) {
        byte[] content = ioHelper.readAndClose(is);
        is = new ByteArrayInputStream(content);
        len = content.length;
    }
    StatusLine statusline = httpCoreHelper.statusLine(SC_OK);
    HttpResponse resp = new BasicHttpResponse(statusline);
    resp.setEntity(new InputStreamEntity(is, len));
    httpCoreHelper.setContentType(resp, ct);
    httpCoreHelper.setContentLength(resp, len);//TODO not needed ??
    if (ce != null) {
        httpCoreHelper.setContentEncoding(resp, ce);
    }
    return resp;
}

From source file:org.openehealth.ipf.tutorials.ref.Client.java

public void execute(InputStream input) throws Exception {
    HttpPost method = new HttpPost(serverUrl.toString());
    method.setEntity(new InputStreamEntity(input, contentType));
    CloseableHttpResponse response = client.execute(method);

    try {/*from  w  w  w. ja  va2  s .  c o  m*/
        InputStream responseStream = response.getEntity().getContent();
        handler.handleResponse(responseStream);
        IOUtils.closeQuietly(responseStream);
    } finally {
        method.releaseConnection();
    }

}

From source file:com.example.testwebservice2.TestWebserviceActivity.java

private void putRequest() {
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(URL);
    try {//from  w  w  w.jav a  2  s.  c  om
        byte[] b = beanConvertXml().getBytes("utf-8");
        InputStream is = new ByteArrayInputStream(b, 0, b.length);
        InputStreamEntity re = new InputStreamEntity(is, is.available());//ContentType.create(XmlContentType, WPCharset)
        re.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, XmlContentType));
        httpPost.setEntity(re);
        HttpResponse response = httpClient.execute(httpPost);
        if (response.getStatusLine().getStatusCode() == 200) {
            // getEntity 
            String result = EntityUtils.toString(response.getEntity());
            System.out.println("result:" + result);
            Toast.makeText(TestWebserviceActivity.this,
                    "result:" + response.getStatusLine().getStatusCode() + result, Toast.LENGTH_SHORT).show();
        }
        System.out.println("response   " + response.getEntity());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.apache.solr.client.solrj.SolrSchemalessExampleTests.java

@Test
public void testArbitraryJsonIndexing() throws Exception {
    HttpSolrServer server = (HttpSolrServer) getSolrServer();
    server.deleteByQuery("*:*");
    server.commit();//from  w  w  w .j a  va2 s .co  m
    assertNumFound("*:*", 0); // make sure it got in

    // two docs, one with uniqueKey, another without it
    String json = "{\"id\":\"abc1\", \"name\": \"name1\"} {\"name\" : \"name2\"}";
    HttpClient httpClient = server.getHttpClient();
    HttpPost post = new HttpPost(server.getBaseURL() + "/update/json/docs");
    post.setHeader("Content-Type", "application/json");
    post.setEntity(new InputStreamEntity(new ByteArrayInputStream(json.getBytes("UTF-8")), -1));
    HttpResponse response = httpClient.execute(post);
    assertEquals(200, response.getStatusLine().getStatusCode());
    server.commit();
    assertNumFound("*:*", 2);
}

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

public void testResponseException() throws IOException {
    ProtocolVersion protocolVersion = new ProtocolVersion("http", 1, 1);
    StatusLine statusLine = new BasicStatusLine(protocolVersion, 500, "Internal Server Error");
    HttpResponse httpResponse = new BasicHttpResponse(statusLine);

    String responseBody = "{\"error\":{\"root_cause\": {}}}";
    boolean hasBody = getRandom().nextBoolean();
    if (hasBody) {
        HttpEntity entity;/*w  w w .j av  a2 s  . c  o  m*/
        if (getRandom().nextBoolean()) {
            entity = new StringEntity(responseBody, ContentType.APPLICATION_JSON);
        } else {
            //test a non repeatable entity
            entity = new InputStreamEntity(
                    new ByteArrayInputStream(responseBody.getBytes(StandardCharsets.UTF_8)),
                    ContentType.APPLICATION_JSON);
        }
        httpResponse.setEntity(entity);
    }

    RequestLine requestLine = new BasicRequestLine("GET", "/", protocolVersion);
    HttpHost httpHost = new HttpHost("localhost", 9200);
    Response response = new Response(requestLine, httpHost, httpResponse);
    ResponseException responseException = new ResponseException(response);

    assertSame(response, responseException.getResponse());
    if (hasBody) {
        assertEquals(responseBody, EntityUtils.toString(responseException.getResponse().getEntity()));
    } else {
        assertNull(responseException.getResponse().getEntity());
    }

    String message = response.getRequestLine().getMethod() + " " + response.getHost()
            + response.getRequestLine().getUri() + ": " + response.getStatusLine().toString();
    if (hasBody) {
        message += "\n" + responseBody;
    }
    assertEquals(message, responseException.getMessage());
}