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:org.chaplib.HttpResource.java

private void consumeBodyOf(HttpResponse resp) {
    HttpEntity entity = resp.getEntity();
    if (entity == null)
        return;//  www. j a v a  2 s .c  om
    try {
        EntityUtils.consume(entity);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.everit.osgi.authentication.cas.tests.SampleApp.java

public static void pingCasLoginUrl(final BundleContext bundleContext) throws Exception {
    CloseableHttpClient httpClient = new SecureHttpClient(null, bundleContext).getHttpClient();

    HttpGet httpGet = new HttpGet(CAS_LOGIN_URL + "?" + LOCALE);
    HttpResponse httpResponse = null;/*from ww  w.  ja  v a  2  s . c o  m*/
    try {
        httpResponse = httpClient.execute(httpGet);
        Assert.assertEquals(CAS_PING_FAILURE_MESSAGE, HttpServletResponse.SC_OK,
                httpResponse.getStatusLine().getStatusCode());
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(CAS_PING_FAILURE_MESSAGE);
    } finally {
        if (httpResponse != null) {
            EntityUtils.consume(httpResponse.getEntity());
        }
        httpClient.close();
    }
}

From source file:org.sonatype.nexus.repositories.metadata.Hc4RawTransport.java

@Override
public byte[] readRawData(final String path) throws IOException {
    final HttpGet get = new HttpGet(createUrlWithPath(path));
    get.setHeader("Accept", ContentType.APPLICATION_XML.getMimeType());
    final HttpResponse response = httpClient.execute(get);
    try {/*w  w  w  . ja  v a2s . c  o m*/
        final int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode == 200 && response.getEntity() != null) {
            return EntityUtils.toByteArray(response.getEntity());
        } else if (statusCode == 404) {
            return null;
        } else {
            throw new IOException("The response was not successful: " + response.getStatusLine());
        }
    } finally {
        EntityUtils.consume(response.getEntity());
    }
}

From source file:eu.seaclouds.platform.planner.core.HttpHelper.java

/**
 *
 * @param restPath/* www .  j  ava2s.c  o  m*/
 * @param params
 * @return
 */
public String getRequest(String restPath, List<NameValuePair> params) {

    HttpGet httpGet = new HttpGet(prepareRequestURL(restPath, params));

    CloseableHttpResponse response = null;
    try {
        response = httpclient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        String content = new Scanner(entity.getContent()).useDelimiter("\\Z").next();
        EntityUtils.consume(entity);
        return content;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } finally {
        try {
            response.close();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}

From source file:eu.prestoprime.p4gui.connection.SearchConnection.java

public static ArrayList<String> searchByDC(P4Service service, String title, String description, String format,
        String identifier) {//from   w  w w.  ja  v a 2s.co m
    ArrayList<String> records = new ArrayList<String>();
    try {
        String path = service.getURL() + "/search/?" + "title=" + title + "&description=" + description
                + "&format=" + format + "&identifier=" + identifier;
        P4HttpClient client = new P4HttpClient(service.getUserID());
        HttpRequestBase request = new HttpGet(path);
        HttpResponse response = client.executeRequest(request);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream is = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            String line = null;
            while ((line = reader.readLine()) != null) {
                records.add(line);
            }
            is.close();
        }
        EntityUtils.consume(entity);
    } catch (Exception e) {
        e.printStackTrace();
    }
    Collections.reverse(records);
    return records;
}

From source file:com.mycompany.projecta.Test.java

public void Auth() throws Exception {

    // Credentials
    String username = "fernandoadp";
    String password = "ADP.2017";

    // Jenkins url
    String jenkinsUrl = "http://localhost:8080";

    // Build name
    String jobName = "ServiceA";

    // Build token
    String buildToken = "build";

    // Create your httpclient
    DefaultHttpClient client = new DefaultHttpClient();

    // Then provide the right credentials *******************
    client.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
            new org.apache.http.auth.UsernamePasswordCredentials(username, password));
    //client.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), (Credentials) new UsernamePasswordCredentials(username, password));

    // Generate BASIC scheme object and stick it to the execution context
    BasicScheme basicAuth = new BasicScheme();
    BasicHttpContext context = new BasicHttpContext();
    context.setAttribute("preemptive-auth", basicAuth);

    // Add as the first (because of the zero) request interceptor
    // It will first intercept the request and preemptively initialize the authentication scheme if there is not
    client.addRequestInterceptor(new PreemptiveAuth(), 0);

    // You get request that will start the build
    String getUrl = jenkinsUrl + "/job/" + jobName + "/build?token=" + buildToken;
    HttpGet get = new HttpGet(getUrl);

    try {/*from w  ww . ja v  a2 s  .  com*/
        // Execute your request with the given context
        HttpResponse response = client.execute(get, context);
        HttpEntity entity = response.getEntity();
        EntityUtils.consume(entity);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:com.github.parisoft.resty.entity.EntityReaderImpl.java

@Override
public String getEntityAsString() throws IOException {
    if (body == null) {
        final HttpEntity entity = httpResponse.getEntity();

        if (entity == null) {
            return body = "";
        }/*w w w . jav  a 2 s  . c  om*/

        try {
            final HttpEntity entityWrapper = isGzipEncoding() ? new GzipDecompressingEntity(entity) : entity;
            final ByteArrayOutputStream output = new ByteArrayOutputStream();

            entityWrapper.writeTo(output);

            body = output.toString(getContentCharSet().toString());
        } finally {
            EntityUtils.consume(entity);
        }
    }

    return body;
}

From source file:pydio.sdk.java.http.CountingMultipartRequestEntity.java

public void consumeContent() throws IOException {
    //this.delegate.consumeContent();
    EntityUtils.consume(delegate);

}

From source file:org.dthume.spring.http.client.httpcomponents.HttpComponentsClientHttpResponse.java

/** {@inheritDoc} */
public void close() {
    try {//from w  ww  .j av  a 2  s .  com
        EntityUtils.consume(response.getEntity());
    } catch (IOException e) {
        throw new HttpComponentsClientException("Caught exception consuming response content", e);
    }
}

From source file:com.soulgalore.crawler.core.assets.impl.HTTPClientAssetFetcher.java

@Override
public AssetResponse getAsset(CrawlerURL url, Map<String, String> requestHeaders) {

    final HttpGet get = new HttpGet(url.getUri());

    for (String key : requestHeaders.keySet()) {
        get.setHeader(key, requestHeaders.get(key));
    }//from   w ww.  j  a v  a  2s.  c om

    HttpEntity entity = null;

    final long start = System.currentTimeMillis();
    try {

        final HttpResponse resp = httpClient.execute(get);
        final long time = System.currentTimeMillis() - start;
        final int sc = resp.getStatusLine().getStatusCode();
        entity = resp.getEntity();
        EntityUtils.consume(entity);
        return new AssetResponse(url.getUrl(), url.getReferer(), sc, time);

    } catch (ConnectTimeoutException e) {
        return new AssetResponse(url.getUrl(), url.getReferer(),
                StatusCode.SC_SERVER_RESPONSE_TIMEOUT.getCode(), System.currentTimeMillis() - start);
    } catch (SocketTimeoutException e) {
        return new AssetResponse(url.getUrl(), url.getReferer(),
                StatusCode.SC_SERVER_RESPONSE_TIMEOUT.getCode(), System.currentTimeMillis() - start);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
        return new AssetResponse(url.getUrl(), url.getReferer(),
                StatusCode.SC_SERVER_RESPONSE_UNKNOWN.getCode(), -1);
    } catch (IOException e) {
        e.printStackTrace();
        return new AssetResponse(url.getUrl(), url.getReferer(),
                StatusCode.SC_SERVER_RESPONSE_UNKNOWN.getCode(), -1);
    }

    finally {

        get.releaseConnection();

    }

}