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

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

Introduction

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

Prototype

public static void consumeQuietly(HttpEntity httpEntity) 

Source Link

Usage

From source file:org.commonjava.aprox.client.core.helper.HttpResources.java

public static void cleanupResources(final HttpRequest request, final HttpResponse response,
        final CloseableHttpClient client) {
    final Logger logger = LoggerFactory.getLogger(HttpResources.class);
    logger.info("CLEANING UP RESOURCES via: {}" + Thread.currentThread().getStackTrace()[1]);

    if (response != null && response.getEntity() != null) {
        EntityUtils.consumeQuietly(response.getEntity());

        if (response instanceof CloseableHttpResponse) {
            closeQuietly((CloseableHttpResponse) response);
        }//from   www.  j  a  v  a2  s .  c  o  m
    }

    if (request != null) {
        if (request instanceof AbstractExecutionAwareRequest) {
            ((AbstractExecutionAwareRequest) request).reset();
        }
    }

    if (client != null) {
        closeQuietly(client);
    }

    logger.info("DONE: CLEANING UP RESOURCES");
}

From source file:com.messagemedia.restapi.client.v1.internal.RestResponse.java

public RestResponse(HttpResponse response) throws RestApiException {
    this.resultCode = response.getStatusLine().getStatusCode();
    for (Header header : response.getAllHeaders()) {
        headers.put(header.getName(), header.getValue());
    }//from  ww  w .  j  av  a  2s  .c  om
    if (response.getEntity() != null) {
        try {
            this.resultBytes = EntityUtils.toByteArray(response.getEntity());
        } catch (IOException e) {
            throw new RestApiException("Error reading response", e);
        } finally {
            EntityUtils.consumeQuietly(response.getEntity());
        }
    }
}

From source file:org.commonjava.indy.client.core.helper.HttpResources.java

public static void cleanupResources(final HttpRequest request, final HttpResponse response,
        final CloseableHttpClient client) {
    final Logger logger = LoggerFactory.getLogger(HttpResources.class);
    logger.info("CLEANING UP RESOURCES via: {}", Thread.currentThread().getStackTrace()[2]);

    if (response != null && response.getEntity() != null) {
        EntityUtils.consumeQuietly(response.getEntity());

        if (response instanceof CloseableHttpResponse) {
            closeQuietly((CloseableHttpResponse) response);
        }/*  w  w w.  j  a v a2s  .  c o m*/
    }

    if (request != null) {
        if (request instanceof AbstractExecutionAwareRequest) {
            ((AbstractExecutionAwareRequest) request).reset();
        }
    }

    if (client != null) {
        closeQuietly(client);
    }

    logger.info("DONE: CLEANING UP RESOURCES");
}

From source file:org.elasticsearch.test.rest.client.RestTestResponse.java

public RestTestResponse(Response response) throws IOException {
    this.response = response;
    if (response.getEntity() != null) {
        try {/*from   w w  w  .  j av  a  2  s.c  om*/
            this.body = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
        } catch (IOException e) {
            EntityUtils.consumeQuietly(response.getEntity());
            throw new RuntimeException(e);
        } finally {
            IOUtils.closeWhileHandlingException(response);
        }
    } else {
        this.body = null;
    }
    parseResponseBody();
}

From source file:org.wso2.carbon.device.mgt.iot.input.adapter.mqtt.util.MQTTUtil.java

public static String getResponseString(HttpResponse httpResponse) throws IOException {
    BufferedReader br = null;//from   www  .ja  v  a 2 s . c o m
    try {
        br = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
        String readLine;
        String response = "";
        while (((readLine = br.readLine()) != null)) {
            response += readLine;
        }
        return response;
    } finally {
        EntityUtils.consumeQuietly(httpResponse.getEntity());
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                log.warn("Error while closing the connection! " + e.getMessage());
            }
        }
    }
}

From source file:com.meplato.store2.ApacheHttpResponse.java

/**
 * Instantiates a new instance of ApacheHttpResponse,
 * then close the response.//from   ww  w.  jav  a 2s .c o  m
 *
 * @param response the HTTP response from the ApacheHttpClient.
 * @throws ServiceException if e.g. serialization of the response fails.
 */
public ApacheHttpResponse(CloseableHttpResponse response) throws ServiceException {
    this.statusCode = response.getStatusLine().getStatusCode();

    HttpEntity entity = response.getEntity();
    if (entity != null) {
        try {
            ContentType contentType = ContentType.getOrDefault(entity);
            Charset charset = contentType.getCharset();
            this.body = EntityUtils.toString(entity, charset);
        } catch (IOException e) {
            EntityUtils.consumeQuietly(entity);
            throw new ServiceException("Error deserializing data", null, e);
        } finally {
            try {
                response.close();
            } catch (IOException e) {
            }
        }
    } else {
        this.body = null;
    }
}

From source file:org.elasticsearch.test.rest.client.http.HttpResponse.java

HttpResponse(HttpUriRequest httpRequest, CloseableHttpResponse httpResponse) {
    this.httpRequest = httpRequest;
    this.statusCode = httpResponse.getStatusLine().getStatusCode();
    this.reasonPhrase = httpResponse.getStatusLine().getReasonPhrase();
    if (httpResponse.getEntity() != null) {
        try {//from www  . j  a  va 2s .  c om
            this.body = EntityUtils.toString(httpResponse.getEntity(), HttpRequestBuilder.DEFAULT_CHARSET);
        } catch (IOException e) {
            EntityUtils.consumeQuietly(httpResponse.getEntity());
            throw new RuntimeException(e);
        } finally {
            try {
                httpResponse.close();
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }
        }
    } else {
        this.body = null;
    }
}

From source file:org.apache.brooklyn.util.http.executor.apacheclient.HttpResponseWrapper.java

@Override
public void close() throws IOException {
    Maybe<org.apache.http.HttpResponse> apacheResponse = delegate.getResponse();
    if (apacheResponse.isPresent()) {
        HttpEntity entity = apacheResponse.get().getEntity();
        if (entity != null) {
            EntityUtils.consumeQuietly(apacheResponse.get().getEntity());
        }/*from  w  ww. j  a v  a 2 s.c o  m*/
    }
}

From source file:io.wcm.devops.maven.nodejsproxy.health.NodeJsDistHealthCheck.java

@Override
protected Result check() throws Exception {
    String url = config.getNodeJsBinariesRootUrl();

    log.info("Validate file: {}", url);
    HttpGet get = new HttpGet(url);
    HttpResponse response = httpClient.execute(get);
    try {/*from   w ww  .  j a  va 2s.  com*/
        if (response.getStatusLine().getStatusCode() == HttpServletResponse.SC_OK) {
            return Result.healthy();
        } else {
            return Result.unhealthy("Got status code " + response.getStatusLine().getStatusCode()
                    + " when accessing URL " + url);
        }
    } finally {
        EntityUtils.consumeQuietly(response.getEntity());
    }
}

From source file:org.commonjava.test.http.TestHttpServerTest.java

@Test
public void simpleDownload() throws Exception {
    final String subPath = "/path/to/something.txt";
    final String content = "this is the content";
    final String url = server.formatUrl(subPath);
    final String path = server.formatPath(subPath);
    server.expect(url, 200, content);/*from   w  w  w  . ja  v a 2s. c  o  m*/

    final HttpGet request = new HttpGet(url);
    final CloseableHttpClient client = HttpClients.createDefault();
    CloseableHttpResponse response = null;

    InputStream stream = null;
    try {
        response = client.execute(request);
        stream = response.getEntity().getContent();
        final String result = IOUtils.toString(stream);

        assertThat(result, notNullValue());
        assertThat(result, equalTo(content));
    } finally {
        IOUtils.closeQuietly(stream);
        if (response != null && response.getEntity() != null) {
            EntityUtils.consumeQuietly(response.getEntity());
            IOUtils.closeQuietly(response);
        }

        if (request != null) {
            request.reset();
        }

        if (client != null) {
            IOUtils.closeQuietly(client);
        }
    }

    System.out.println(server.getAccessesByPathKey());

    final String key = server.getAccessKey(CommonMethod.GET.name(), path);
    System.out.println("Getting accesses for: '" + key + "'");
    assertThat(server.getAccessesByPathKey().get(key), equalTo(1));
}