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:com.blacklocus.sample.time.ConnectionTest.java

private void makeConnection() {
    requestPool.submit(new ExceptingRunnable() {
        @Override//  w  ww .j  a va2 s .  c  o m
        public void go() throws Exception {
            HttpResponse res = null;
            try {
                res = httpClient.execute(new HttpGet("http://127.0.0.1:8080/wait/" + wait));
                System.out.println(IOUtils.toString(res.getEntity().getContent()));
                counter.getAndIncrement();
            } finally {
                if (res != null) {
                    EntityUtils.consumeQuietly(res.getEntity());
                }
            }
        }
    });
}

From source file:org.caratarse.auth.client.CaratarseAuthClient0100Test.java

@Before
public void beforeEach() throws IOException {
    httpClient = new DefaultHttpClient();
    HttpDelete deleteRequest = new HttpDelete(BASE_URL + "/populates.json");
    HttpResponse response = httpClient.execute(deleteRequest);
    assertEquals(204, response.getStatusLine().getStatusCode());
    EntityUtils.consumeQuietly(response.getEntity());
    HttpGet getRequest = new HttpGet(BASE_URL + "/populates.json");
    response = httpClient.execute(getRequest);
    assertEquals(200, response.getStatusLine().getStatusCode());
    EntityUtils.consumeQuietly(response.getEntity());
}

From source file:com.kixeye.relax.HttpResponse.java

/**
 * @param result//from www.  j  a  v a 2 s. c o  m
 * @param serDe
 * @param objectType
 * @throws IOException
 */
protected HttpResponse(org.apache.http.HttpResponse result, RestClientSerDe serDe, Class<T> objectType)
        throws IOException {
    this.statusCode = result.getStatusLine().getStatusCode();

    Header[] headers = result.getAllHeaders();
    if (headers != null) {
        for (Header header : headers) {
            List<String> headerValues = this.headers.get(header.getName());
            if (headerValues == null) {
                headerValues = new ArrayList<>();
                this.headers.put(header.getName(), headerValues);
            }
            headerValues.add(header.getValue());
        }
    }

    HttpEntity entity = result.getEntity();
    byte[] data = null;

    if (entity != null) {
        data = EntityUtils.toByteArray(entity);
        EntityUtils.consumeQuietly(entity);
    }

    Header contentTypeHeader = result.getFirstHeader("Content-Type");

    if (!Void.class.equals(objectType)) {
        this.body = new SerializedObject<>(serDe,
                contentTypeHeader != null ? contentTypeHeader.getValue() : null, data, objectType);
    } else {
        this.body = null;
    }
}

From source file:com.arcbees.vcs.AbstractVcsApi.java

protected <T> T processResponse(HttpClientWrapper httpClient, HttpUriRequest request, Credentials credentials,
        Gson gson, Class<T> clazz) throws IOException {
    try {//  ww w.  j  av  a 2s. c  om
        HttpResponse httpResponse = doExecuteRequest(httpClient, request, credentials);

        HttpEntity entity = httpResponse.getEntity();
        if (entity == null) {
            throw new IOException(
                    "Failed to complete request. Empty response. Status: " + httpResponse.getStatusLine());
        }

        try {
            return readEntity(clazz, entity, gson);
        } finally {
            EntityUtils.consumeQuietly(entity);
        }
    } finally {
        request.abort();
    }
}

From source file:com.vmware.content.samples.client.util.HttpUtil.java

/**
 * Uploads a file from local storage to a given HTTP URI.
 *
 * @param localFile local storage path to the file to upload.
 * @param uploadUri HTTP URI where the file needs to be uploaded.
 * @throws java.security.NoSuchAlgorithmException
 * @throws java.security.KeyStoreException
 * @throws java.security.KeyManagementException
 * @throws java.io.IOException/*from w ww  .  jav  a2s  . c om*/
 */
public static void uploadFileToUri(File localFile, URI uploadUri)
        throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException, IOException {
    CloseableHttpClient httpClient = getCloseableHttpClient();
    HttpPut request = new HttpPut(uploadUri);
    HttpEntity content = new FileEntity(localFile);
    request.setEntity(content);
    HttpResponse response = httpClient.execute(request);
    EntityUtils.consumeQuietly(response.getEntity());
}

From source file:org.alfresco.provision.WARDeployService.java

public JSONArray readJSONArray(final HttpEntity entity) {
    String rsp = null;/*from   w ww  . ja v a2 s .co  m*/
    try {
        rsp = EntityUtils.toString(entity, "UTF-8");
    } catch (Throwable ex) {
        throw new RuntimeException("Failed to read HTTP entity stream.", ex);
    } finally {
        EntityUtils.consumeQuietly(entity);
    }
    try {
        JSONParser parser = new JSONParser();
        JSONArray result = (JSONArray) parser.parse(rsp);
        return result;
    } catch (Throwable e) {
        throw new RuntimeException("Failed to convert response to JSON: \n" + "   Response: \r\n" + rsp, e);
    }
}

From source file:org.opennms.poller.remote.MetadataUtils.java

public static Map<String, String> fetchGeodata() {
    final Map<String, String> ret = new HashMap<>();
    final String url = "http://freegeoip.net/xml/";

    final CloseableHttpClient httpclient = HttpClients.createDefault();
    final HttpGet get = new HttpGet(url);
    CloseableHttpResponse response = null;

    try {//from w  w  w .j av  a 2 s.  c om
        response = httpclient.execute(get);

        final HttpEntity entity = response.getEntity();
        final String xml = EntityUtils.toString(entity);
        System.err.println("xml = " + xml);
        final GeodataResponse geoResponse = JaxbUtils.unmarshal(GeodataResponse.class, xml);
        ret.put("external-ip-address", InetAddressUtils.str(geoResponse.getIp()));
        ret.put("country-code", geoResponse.getCountryCode());
        ret.put("region-code", geoResponse.getRegionCode());
        ret.put("city", geoResponse.getCity());
        ret.put("zip-code", geoResponse.getZipCode());
        ret.put("time-zone", geoResponse.getTimeZone());
        ret.put("latitude", geoResponse.getLatitude() == null ? null : geoResponse.getLatitude().toString());
        ret.put("longitude", geoResponse.getLongitude() == null ? null : geoResponse.getLongitude().toString());
        EntityUtils.consumeQuietly(entity);
    } catch (final Exception e) {
        LOG.debug("Failed to get GeoIP data from " + url, e);
    } finally {
        IOUtils.closeQuietly(response);
    }

    return ret;
}

From source file:org.elasticsearch.test.rest.yaml.ClientYamlTestResponse.java

ClientYamlTestResponse(Response response) throws IOException {
    this.response = response;
    if (response.getEntity() != null) {
        String contentType = response.getHeader("Content-Type");
        this.bodyContentType = XContentType.fromMediaTypeOrFormat(contentType);
        try {/*from   w w  w.  j a  v a 2 s  .c o m*/
            byte[] bytes = EntityUtils.toByteArray(response.getEntity());
            //skip parsing if we got text back (e.g. if we called _cat apis)
            if (bodyContentType != null) {
                this.parsedResponse = ObjectPath.createFromXContent(bodyContentType.xContent(),
                        new BytesArray(bytes));
            }
            this.body = bytes;
        } catch (IOException e) {
            EntityUtils.consumeQuietly(response.getEntity());
            throw e;
        }
    } else {
        this.body = null;
        this.bodyContentType = null;
    }
}

From source file:org.alfresco.provision.BMService.java

/**
 * Parses http response stream into a {@link JSONObject}.
 * /* ww w  .  ja v a 2s .c  o  m*/
 * @param stream
 *            Http response entity
 * @return {@link JSONObject} response
 */
public JSONObject readStream(final HttpEntity entity) {
    String rsp = null;
    try {
        rsp = EntityUtils.toString(entity, "UTF-8");
    } catch (Throwable ex) {
        throw new RuntimeException("Failed to read HTTP entity stream.", ex);
    } finally {
        EntityUtils.consumeQuietly(entity);
    }
    try {
        JSONParser parser = new JSONParser();
        JSONObject result = (JSONObject) parser.parse(rsp);
        return result;
    } catch (Throwable e) {
        throw new RuntimeException("Failed to convert response to JSON: \n" + "   Response: \r\n" + rsp, e);
    }
}

From source file:net.sf.jasperreports.data.http.HttpDataConnection.java

@Override
public InputStream getInputStream() {
    try {//from   ww  w  .  j a v a 2 s  . c  o  m
        response = httpClient.execute(request);
        StatusLine status = response.getStatusLine();
        if (log.isDebugEnabled()) {
            log.debug("HTTP response status " + status);
        }

        HttpEntity entity = response.getEntity();
        if (entity == null) {
            throw new JRRuntimeException(EXCEPTION_MESSAGE_KEY_NO_RESPONSE, (Object[]) null);
        }

        if (status.getStatusCode() >= 300) {
            EntityUtils.consumeQuietly(entity);
            //FIXME include request URI in the exception?  that might be a security issue
            throw new JRRuntimeException(EXCEPTION_MESSAGE_KEY_STATUS_CODE_ERROR, new Object[] { status });
        }

        return entity.getContent();
    } catch (ClientProtocolException e) {
        throw new JRRuntimeException(e);
    } catch (IOException e) {
        throw new JRRuntimeException(e);
    }
}