Example usage for org.apache.http.util EntityUtils consume

List of usage examples for org.apache.http.util EntityUtils consume

Introduction

In this page you can find the example usage for org.apache.http.util EntityUtils consume.

Prototype

public static void consume(HttpEntity httpEntity) throws IOException 

Source Link

Usage

From source file:crawler.java.edu.uci.ics.crawler4j.fetcher.PageFetchResult.java

public void discardContentIfNotConsumed() {
    try {/*w w  w .ja v a2s .  c  o m*/
        if (entity != null) {
            EntityUtils.consume(entity);
        }
    } catch (IOException ignored) {
        // We can EOFException (extends IOException) exception. It can happen on compressed streams which are not
        // repeatable
        // We can ignore this exception. It can happen if the stream is closed.
    } catch (Exception e) {
        logger.warn("Unexpected error occurred while trying to discard content", e);
    }
}

From source file:de.drv.dsrv.spoc.web.service.impl.SpocResponseHandler.java

@Override
public StreamSource handleResponse(final HttpResponse httpResponse) throws IOException {

    final HttpEntity httpResponseEntity = getAndCheckHttpEntity(httpResponse);

    // Kopiert den InputStream der Response, damit die Connection
    // geschlossen werden kann.
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    IOUtils.copy(httpResponseEntity.getContent(), outputStream);
    EntityUtils.consume(httpResponseEntity);
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());

    // Initialisiert die zurueck gegebene Source.
    StreamSource responseSource;/*from  w  ww  .j a  v  a 2  s  .  com*/
    final Charset charset = getCharsetFromResponse(httpResponseEntity);
    if (charset != null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("F\u00fcr die Antwort des Fachverfahrens wird das Charset >" + charset.name()
                    + "< aus dem Content-Type Header verwendet.");
        }
        responseSource = new StreamSource(new InputStreamReader(inputStream, charset));
    } else {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Die Antwort des Fachverfahren hat kein Charset im Content-Type Header spezifiziert."
                    + " Der Payload wird dem XML-Parser ohne Charset \u00fcbergeben,"
                    + " dieses sollte aber im XML-Prolog spezifiziert sein.");
        }
        responseSource = new StreamSource(inputStream);
    }

    return responseSource;
}

From source file:org.springframework.ws.transport.http.HttpComponentsConnection.java

@Override
public void onClose() throws IOException {
    if (httpResponse != null && httpResponse.getEntity() != null) {
        EntityUtils.consume(httpResponse.getEntity());
    }/*from   ww  w.  ja va 2s . com*/
}

From source file:com.crawler.app.fetcher.PageFetchResult.java

public void discardContentIfNotConsumed() {
    try {//from  ww  w  .j  av a2s  .c  o m
        if (entity != null) {
            EntityUtils.consume(entity);
        }
    } catch (IOException e) {
        // We can EOFException (extends IOException) exception. It can happen on compressed streams which are not repeatable
        // We can ignore this exception. It can happen if the stream is closed.
    } catch (Exception e) {
        logger.warn("Unexpected error occurred while trying to discard content", e);
    }
}

From source file:asterixReadOnlyClient.AsterixReadOnlyClientUtility.java

@Override
public void executeQuery(int qid, int vid, String qBody) {
    String content = null;/*w w w . ja v  a  2s. c  o  m*/
    long rspTime = Constants.INVALID_TIME;
    try {
        roBuilder.setParameter("query", qBody);
        URI uri = roBuilder.build();
        httpGet.setURI(uri);

        long s = System.currentTimeMillis();
        HttpResponse response = httpclient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        content = EntityUtils.toString(entity);
        EntityUtils.consume(entity);
        long e = System.currentTimeMillis();

        rspTime = (e - s);

    } catch (Exception ex) {
        System.err.println("Problem in read-only query execution against Asterix");
        ex.printStackTrace();
        updateStat(qid, vid, Constants.INVALID_TIME);
        return;
    }
    updateStat(qid, vid, rspTime);
    if (resPw != null) {
        resPw.println(qid);
        resPw.println("Ver " + vid);
        resPw.println(qBody + "\n");
        if (dumpResults) {
            resPw.println(content + "\n");
        }
    }
    System.out.println("Q" + qid + " version " + vid + "\t" + rspTime); //trace the progress

}

From source file:com.abc.client.abcAuth.AbcAuthClient.java

public void login() throws IOException, AbcAuthClientException {
    HttpResponse response = null;// w  w w.  j a  v a 2  s  .co  m
    try {
        HttpGet method = new HttpGet(authURL);
        method.setHeader(X_STORAGE_USER, username);
        method.setHeader(X_STORAGE_PASS, password);
        response = client.execute(method);
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            authToken = response.getFirstHeader(X_AUTH_TOKEN) != null
                    ? response.getFirstHeader(X_AUTH_TOKEN).getValue()
                    : null;
            return;
        }
        throw new AbcAuthClientException(response.getStatusLine().getStatusCode(),
                response.getStatusLine().getReasonPhrase());
    } finally {
        if (response != null)
            EntityUtils.consume(response.getEntity());
    }
}

From source file:com.netflix.http4.NamedConnectionPoolTest.java

@Test
public void testConnectionPoolCounters() throws Exception {
    // LogManager.getRootLogger().setLevel((Level)Level.DEBUG);
    NFHttpClient client = NFHttpClientFactory.getNamedNFHttpClient("google-NamedConnectionPoolTest");
    assertTrue(client.getConnectionManager() instanceof MonitoredConnectionManager);
    MonitoredConnectionManager connectionPoolManager = (MonitoredConnectionManager) client
            .getConnectionManager();/*from w  w w . j a  va2 s  . c  om*/
    connectionPoolManager.setDefaultMaxPerRoute(100);
    connectionPoolManager.setMaxTotal(200);
    assertTrue(connectionPoolManager.getConnectionPool() instanceof NamedConnectionPool);
    NamedConnectionPool connectionPool = (NamedConnectionPool) connectionPoolManager.getConnectionPool();
    System.out.println("Entries created: " + connectionPool.getCreatedEntryCount());
    System.out.println("Requests count: " + connectionPool.getRequestsCount());
    System.out.println("Free entries: " + connectionPool.getFreeEntryCount());
    System.out.println("Deleted :" + connectionPool.getDeleteCount());
    System.out.println("Released: " + connectionPool.getReleaseCount());
    for (int i = 0; i < 10; i++) {
        HttpUriRequest request = new HttpGet("http://www.google.com/");
        HttpResponse response = client.execute(request);
        EntityUtils.consume(response.getEntity());
        int statusCode = response.getStatusLine().getStatusCode();
        assertTrue(statusCode == 200 || statusCode == 302);
        Thread.sleep(500);
    }
    System.out.println("Entries created: " + connectionPool.getCreatedEntryCount());
    System.out.println("Requests count: " + connectionPool.getRequestsCount());
    System.out.println("Free entries: " + connectionPool.getFreeEntryCount());
    System.out.println("Deleted :" + connectionPool.getDeleteCount());
    System.out.println("Released: " + connectionPool.getReleaseCount());
    assertTrue(connectionPool.getCreatedEntryCount() >= 1);
    assertTrue(connectionPool.getRequestsCount() >= 10);
    assertTrue(connectionPool.getFreeEntryCount() >= 9);
    assertEquals(0, connectionPool.getDeleteCount());
    assertEquals(connectionPool.getReleaseCount(), connectionPool.getRequestsCount());
    assertEquals(connectionPool.getRequestsCount(),
            connectionPool.getCreatedEntryCount() + connectionPool.getFreeEntryCount());
    ConfigurationManager.getConfigInstance().setProperty(
            "google-NamedConnectionPoolTest.ribbon." + CommonClientConfigKey.MaxTotalHttpConnections.key(),
            "50");
    ConfigurationManager.getConfigInstance().setProperty(
            "google-NamedConnectionPoolTest.ribbon." + CommonClientConfigKey.MaxHttpConnectionsPerHost.key(),
            "10");
    assertEquals(50, connectionPoolManager.getMaxTotal());
    assertEquals(10, connectionPoolManager.getDefaultMaxPerRoute());
}

From source file:com.autonomousturk.crawler.fetcher.PageFetchResult.java

public void discardContentIfNotConsumed() {
    try {/* ww  w .  ja va  2s . c  o  m*/
        if (entity != null) {
            EntityUtils.consume(entity);
        }
    } catch (EOFException e) {
        // We can ignore this exception. It can happen on compressed streams
        // which are not repeatable
    } catch (IOException e) {
        // We can ignore this exception. It can happen if the stream is
        // closed.
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:tech.beshu.ror.utils.integration.ElasticsearchTweetsInitializer.java

private void createMessage(RestClient client, String endpoint, String id, String user, String message) {
    try {/*w w  w  . j a va2  s  .c o  m*/
        HttpPut httpPut = new HttpPut(client.from(endpoint + id));
        httpPut.setHeader("Content-Type", "application/json");
        httpPut.setEntity(new StringEntity("{\n" + "\"user\" : \"" + user + "\",\n" + "\"post_date\" : \""
                + LocalDateTime.now().format(DateTimeFormatter.BASIC_ISO_DATE) + "\",\n" + "\"message\" : \""
                + message + "\"\n" + "}"));
        EntityUtils.consume(client.execute(httpPut).getEntity());
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Creating message failed", e);
    }
}

From source file:guru.nidi.ramltester.loader.FormLoginUrlFetcher.java

@Override
public InputStream fetchFromUrl(CloseableHttpClient client, String base, String name) {
    try {/* w  w  w  .  j ava  2 s.  co  m*/
        final HttpPost login = new HttpPost(base + "/" + loginUrl);
        List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair(loginField, this.login));
        params.add(new BasicNameValuePair(passwordField, password));
        postProcessLoginParameters(params);
        login.setEntity(new UrlEncodedFormEntity(params));
        final CloseableHttpResponse getResult = client.execute(postProcessLogin(login));
        if (getResult.getStatusLine().getStatusCode() != HttpStatus.SC_MOVED_TEMPORARILY) {
            throw new RamlLoader.ResourceNotFoundException(name,
                    "Could not login: " + getResult.getStatusLine().toString());
        }
        EntityUtils.consume(getResult.getEntity());
        return super.fetchFromUrl(client, base + "/" + loadPath, name);
    } catch (IOException e) {
        throw new RamlLoader.ResourceNotFoundException(name, e);
    }
}