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:com.emc.esu.api.rest.CommonsInputStreamWrapper.java

@Override
protected void finalize() throws Throwable {
    if (response != null) {
        l4j.warn("Warning: connection was not closed!");
        try {//from   w w w.  java  2  s  .c o m
            EntityUtils.consume(response.getEntity());
            response = null;
        } catch (Exception e) {
            // Ignore
        }
    }
    super.finalize();
}

From source file:asterixUpdateClient.AsterixUpdateClientUtility.java

@Override
public void executeUpdate(int qid, Update update) {
    long rspTime = Constants.INVALID_TIME;
    String updateBody = null;/*from   w  ww.  ja  v  a 2 s .  c o m*/
    HttpResponse response;
    try {
        updateBody = ((AqlUpdate) update).printAqlStatement();
        httpPost.setEntity(new StringEntity(updateBody));

        long s = System.currentTimeMillis();
        response = httpclient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        EntityUtils.consume(entity);
        long e = System.currentTimeMillis();
        rspTime = (e - s);
    } catch (Exception e) {
        System.err.println("Problem in running update " + qid + " against Asterixdb !");
        updateStat(qid, 0, Constants.INVALID_TIME);
        return;
    }

    int statusCode = response.getStatusLine().getStatusCode();
    if (statusCode != HttpStatus.SC_OK) {
        System.err.println("Update " + qid + " against Asterixdb returned http error code");
        rspTime = Constants.INVALID_TIME;
    }
    updateStat(qid, 0, rspTime);

    if (++counter % TRACE_PACE == 0) {
        System.out.println(
                counter + " Updates done - last one took\t" + rspTime + " ms\tStatus-Code\t" + statusCode);
    }

}

From source file:com.msds.km.service.Impl.YunmaiAPIDrivingLicenseRecognitionServcieiImpl.java

@Override
protected DrivingLicense recognitionInternal(File file) throws RecognitionException {
    try {//from   ww w . ja  v a  2s.  c  o  m
        HttpPost httppost = new HttpPost(POST_URL);
        FileBody fileBody = new FileBody(file);

        HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("img", fileBody)
                .addTextBody("action", "driving").addTextBody("callbackurl", "/idcard/").build();

        httppost.setEntity(reqEntity);

        CloseableHttpResponse response = httpclient.execute(httppost);
        try {
            String content = EntityUtils.toString(response.getEntity());
            EntityUtils.consume(response.getEntity());
            return this.parseDrivingLicense(content);
        } catch (IOException e) {
            throw new RecognitionException(
                    "can not post request to the url:" + POST_URL + ", please check the network.", e);
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                throw new RecognitionException(
                        "can not post request to the url:" + POST_URL + ", please check the network.", e);
            }
        }
    } catch (FileNotFoundException e) {
        throw new RecognitionException(
                "the file can not founded:" + file.getAbsolutePath() + ", please check the file.", e);
    } catch (ClientProtocolException e) {
        throw new RecognitionException(
                "can not post request to the url:" + POST_URL + ", please check the network.", e);
    } catch (IOException e) {
        throw new RecognitionException(
                "can not post request to the url:" + POST_URL + ", please check the network.", e);
    }
}

From source file:org.eclipse.lyo.testsuite.oslcv2.ServiceProviderCatalogBaseTests.java

@Test
public void invalidContentTypeGivesNotSupportedOPTIONAL() throws IOException {
    HttpResponse resp = OSLCUtils.getResponseFromUrl(setupBaseUrl, currentUrl, basicCreds,
            "invalid/content-type", headers);
    if (resp.getEntity() != null) {
        String respType = "";
        if (resp.getEntity().getContentType() != null) {
            respType = resp.getEntity().getContentType().getValue();
        }/*www .  ja  va  2 s. c o  m*/
        EntityUtils.consume(resp.getEntity());
        assertTrue(
                "Expected 406 but received " + resp.getStatusLine()
                        + " or Content-type='invalid/content-type' but received " + respType,
                resp.getStatusLine().getStatusCode() == 406 || respType.contains("invalid/content-type"));
    }
}

From source file:it.uiExtension.UiExtensionsTest.java

/**
 * SONAR-3555/* w w  w .  j  a va 2 s  .com*/
 */
@Test
public void content_type_of_static_files_is_set() throws Exception {
    HttpClient httpclient = new DefaultHttpClient();
    try {
        HttpGet get = new HttpGet(orchestrator.getServer().getUrl() + "/static/uiextensionsplugin/cute.jpg");
        HttpResponse response = httpclient.execute(get);
        assertThat(response.getLastHeader("Content-Type").getValue()).isEqualTo("image/jpeg");

        EntityUtils.consume(response.getEntity());

    } finally {
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:org.callimachusproject.client.AuthenticationClientExecChain.java

public CloseableHttpResponse execute(final HttpRoute route, final HttpRequestWrapper original,
        final HttpClientContext context, final HttpExecutionAware execAware) throws IOException, HttpException {
    final HttpRequestWrapper request = HttpRequestWrapper.wrap(original);
    final RequestConfig config = context.getRequestConfig();

    CloseableHttpResponse response = null;
    while (true) {
        if (execAware != null && execAware.isAborted()) {
            throw new RequestAbortedException("Request aborted");
        }//  w  w  w .j a va2 s .co m

        this.authenticator.generateAuthResponse(route, request, context);

        response = delegate.execute(route, request, context, execAware);

        if (config.isAuthenticationEnabled()
                && authenticator.needAuthentication(route, request, response, context)) {
            EntityUtils.consume(response.getEntity());
        } else {
            return response;
        }
    }
}

From source file:org.wuspba.ctams.ui.server.ServerUtils.java

public static String get(URI uri) throws IOException {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpGet httpGet = new HttpGet(uri);

    String ret;/*from   www . j av a  2  s .  c  om*/

    try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
        HttpEntity entity = response.getEntity();

        ret = ServerUtils.convertEntity(entity);

        EntityUtils.consume(entity);
    }

    return ret;
}

From source file:com.helger.peppol.httpclient.SMPHttpResponseHandlerWriteOperations.java

@Override
@Nullable/*from w  w w  .j a  v  a 2 s  .c  om*/
public Object handleEntity(@Nonnull final HttpEntity aEntity) throws IOException {
    EntityUtils.consume(aEntity);
    return null;
}

From source file:nl.salp.warcraft4j.io.CachedHttpDataReader.java

/**
 * Read the data from a file (direct blocking http read).
 *
 * @param url The URL of the file to read.
 *
 * @return The file contents./*from ww w  . j a va2s.c o  m*/
 *
 * @throws DataReadingException When reading the file failed.
 */
private static byte[] getData(String url) throws DataReadingException {
    try (CloseableHttpClient httpClient = HttpClients.createDefault();
            CloseableHttpResponse response = httpClient.execute(new HttpGet(URI.create(url)))) {
        StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() > 300) {
            throw new DataReadingException(String.format("Error opening HTTP data reader for %s: error %d: %s",
                    url, statusLine.getStatusCode(), statusLine.getReasonPhrase()));
        }
        HttpEntity entity = response.getEntity();
        if (entity == null) {
            throw new DataReadingException(format("HTTP data reader received no response from for %s", url));
        }
        byte[] data = EntityUtils.toByteArray(entity);
        EntityUtils.consume(entity);
        return data;
    } catch (IOException e) {
        throw new DataReadingException(e);
    }
}

From source file:com.networknt.light.server.handler.loader.Loader.java

public static void login(String host, String userId, String password) throws Exception {
    Map<String, Object> inputMap = new HashMap<String, Object>();
    inputMap.put("category", "user");
    inputMap.put("name", "signInUser");
    inputMap.put("readOnly", false);
    Map<String, Object> data = new HashMap<String, Object>();
    data.put("userIdEmail", userId);
    data.put("password", password);
    data.put("rememberMe", true);
    data.put("clientId", "example@Browser");
    inputMap.put("data", data);

    HttpPost httpPost = new HttpPost(host + "/api/rs");
    StringEntity input = new StringEntity(
            ServiceLocator.getInstance().getMapper().writeValueAsString(inputMap));
    input.setContentType("application/json");
    httpPost.setEntity(input);//from  w  w w .j ava  2 s.  c  o m
    CloseableHttpResponse response = httpclient.execute(httpPost);

    try {

        //System.out.println(response.getStatusLine());
        HttpEntity entity = response.getEntity();
        BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent()));
        String json = "";
        String line = "";
        while ((line = rd.readLine()) != null) {
            json = json + line;
        }
        //System.out.println("json = " + json);
        Map<String, Object> jsonMap = ServiceLocator.getInstance().getMapper().readValue(json,
                new TypeReference<HashMap<String, Object>>() {
                });
        jwt = (String) jsonMap.get("accessToken");
        EntityUtils.consume(entity);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        response.close();
    }
}