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.cloudbees.servlet.filters.PrivateAppFilterIntegratedTest.java

private void authentication_scenario(PrivateAppFilter.AuthenticationEntryPoint authenticationEntryPoint)
        throws Exception {
    privateAppFilter.setAuthenticationEntryPoint(authenticationEntryPoint);

    {//from   w  w w. ja v  a  2s  . c o m
        // AUTHENTICATION REQUEST
        HttpRequest request;
        switch (authenticationEntryPoint) {
        case BASIC_AUTH:
            request = new HttpGet("/");
            request.addHeader("Authorization", buildBasicAuthHeader());
            break;
        case HTTP_PARAM_AUTH:
            URI uri = new URIBuilder("/")
                    .addParameter(privateAppFilter.getAuthenticationParameterName(), secretKey).build();
            request = new HttpGet(uri);
            break;
        case HTTP_HEADER_AUTH:
            request = new HttpGet("/");
            request.addHeader(privateAppFilter.getAuthenticationHeaderName(), secretKey);
            break;
        default:
            throw new IllegalStateException();
        }
        HttpResponse response = httpClient.execute(httpHost, request);

        assertThat(response.getStatusLine().getStatusCode(), equalTo(HttpServletResponse.SC_OK));
        assertThat(response.containsHeader("x-response"), is(true));

        dumpHttpResponse(response);

        EntityUtils.consumeQuietly(response.getEntity());
    }

    {
        // ALREADY AUTHENTICATED REQUEST
        HttpGet request = new HttpGet("/");
        HttpResponse response = httpClient.execute(httpHost, request);

        assertThat(response.getStatusLine().getStatusCode(), equalTo(HttpServletResponse.SC_OK));
        assertThat(response.containsHeader("x-response"), is(true));

        dumpHttpResponse(response);

        EntityUtils.consumeQuietly(response.getEntity());
    }
}

From source file:nl.opengeogroep.filesetsync.client.FilesetSyncer.java

private void retrieveFilesetList() throws IOException {

    final boolean cachedFileList = state.getFileListDate() != null
            && state.getFileListRemotePath().equals(fs.getRemote())
            && (!fs.isHash() || state.isFileListHashed()) && SyncJobState.haveCachedFileList(fs.getName());

    String s = "Retrieving file list";
    if (cachedFileList) {
        s += String.format(" (last file list cached at %s)", FormatUtil.dateToString(state.getFileListDate()));
    }/*w  w  w.  j  a  v  a 2s .com*/
    action(s);

    try (CloseableHttpClient httpClient = HttpClientUtil.get()) {
        HttpUriRequest get = RequestBuilder.get().setUri(serverUrl + "list/" + fs.getRemote())
                .addParameter("hash", fs.isHash() + "").addParameter("regexp", fs.getRegexp()).build();
        if (cachedFileList) {
            get.addHeader(HttpHeaders.IF_MODIFIED_SINCE, HttpUtil.formatDate(state.getFileListDate()));
        }
        addExtraHeaders(get);
        // Request poorly encoded text format
        get.addHeader(HttpHeaders.ACCEPT, "text/plain");

        ResponseHandler<List<FileRecord>> rh = new ResponseHandler<List<FileRecord>>() {
            @Override
            public List handleResponse(HttpResponse hr) throws ClientProtocolException, IOException {
                log.info("< " + hr.getStatusLine());

                int status = hr.getStatusLine().getStatusCode();
                if (status == SC_NOT_MODIFIED) {
                    return null;
                } else if (status >= SC_OK && status < 300) {
                    HttpEntity entity = hr.getEntity();
                    if (entity == null) {
                        throw new ClientProtocolException("No response entity, invalid server URL?");
                    }
                    try (InputStream in = entity.getContent()) {
                        return Protocol.decodeFilelist(in);
                    }
                } else {
                    if (log.isTraceEnabled()) {
                        String entity = hr.getEntity() == null ? null : EntityUtils.toString(hr.getEntity());
                        log.trace("Response body: " + entity);
                    } else {
                        EntityUtils.consumeQuietly(hr.getEntity());
                    }
                    throw new ClientProtocolException("Server error: " + hr.getStatusLine());
                }
            }
        };

        log.info("> " + get.getRequestLine());
        fileList = httpClient.execute(get, rh);

        if (fileList == null) {
            log.info("Cached file list is up-to-date");

            fileList = SyncJobState.readCachedFileList(fs.getName());
        } else {
            log.info("Filelist returned " + fileList.size() + " files");

            /* Calculate last modified client-side, requires less server
             * memory
             */

            long lastModified = -1;
            for (FileRecord fr : fileList) {
                lastModified = Math.max(lastModified, fr.getLastModified());
            }
            if (lastModified != -1) {
                state.setFileListRemotePath(fs.getRemote());
                state.setFileListDate(new Date(lastModified));
                state.setFileListHashed(fs.isHash());
                SyncJobState.writeCachedFileList(fs.getName(), fileList);
                SyncJobStatePersistence.persist();
            }
        }
    }
}

From source file:org.apache.olingo.server.example.TripPinServiceTest.java

@Test
public void testReadEntityWithNonExistingKey() throws Exception {
    HttpResponse response = httpGET(baseURL + "/Airlines('OO')", 404);
    EntityUtils.consumeQuietly(response.getEntity());
}

From source file:com.hortonworks.registries.auth.client.AuthenticatorTestCase.java

private void doHttpClientRequest(HttpClient httpClient, HttpUriRequest request) throws Exception {
    HttpResponse response = null;//www  .ja  va 2  s  . c o m
    try {
        response = httpClient.execute(request);
        final int httpStatus = response.getStatusLine().getStatusCode();
        Assert.assertEquals(HttpURLConnection.HTTP_OK, httpStatus);
    } finally {
        if (response != null)
            EntityUtils.consumeQuietly(response.getEntity());
    }
}

From source file:com.flurry.proguard.UploadMapping.java

/**
 * Convert a HTTP response to JSON//from  ww  w .  j  ava 2 s .  c o  m
 *
 * @param httpEntity the response body
 * @return a JSON object
 */
private static JSONObject getJsonFromEntity(HttpEntity httpEntity) {
    try {
        return new JSONObject(EntityUtils.toString(httpEntity));
    } catch (IOException e) {
        failWithError("Cannot read HttpEntity {}", httpEntity, e);
        return null;
    } finally {
        EntityUtils.consumeQuietly(httpEntity);
    }
}

From source file:pl.psnc.synat.wrdz.mdz.integrity.IntegrityProcessorBean.java

/**
 * Stores the given http entity in a temporary file.
 * // w ww . j  a va2s  . c o m
 * @param entity
 *            entity to be stored
 * @return the temporary file containing the entity data
 * @throws IOException
 *             if any read/write errors occur
 */
private File storeTemporarily(HttpEntity entity) throws IOException {
    File file = File.createTempFile("integrity", null);
    file.deleteOnExit();

    try {

        FileOutputStream stream = new FileOutputStream(file);
        try {
            entity.writeTo(stream);
        } finally {
            IOUtils.closeQuietly(stream);
        }

    } catch (IOException e) {
        file.delete();
        throw e;
    } finally {
        EntityUtils.consumeQuietly(entity);
    }

    return file;
}

From source file:net.yacy.grid.http.ClientConnection.java

/**
 * get a redirect for an url: this method shall be called if it is expected that a url
 * is redirected to another url. This method then discovers the redirect.
 * @param urlstring//from  w  w  w.ja v  a2 s.  c o m
 * @param useAuthentication
 * @return the redirect url for the given urlstring
 * @throws IOException if the url is not redirected
 */
public static String getRedirect(String urlstring) throws IOException {
    HttpGet get = new HttpGet(urlstring);
    get.setConfig(RequestConfig.custom().setRedirectsEnabled(false).build());
    get.setHeader("User-Agent",
            ClientIdentification.getAgent(ClientIdentification.yacyInternetCrawlerAgentName).userAgent);
    CloseableHttpClient httpClient = getClosableHttpClient();
    HttpResponse httpResponse = httpClient.execute(get);
    HttpEntity httpEntity = httpResponse.getEntity();
    if (httpEntity != null) {
        if (httpResponse.getStatusLine().getStatusCode() == 301) {
            for (Header header : httpResponse.getAllHeaders()) {
                if (header.getName().equalsIgnoreCase("location")) {
                    EntityUtils.consumeQuietly(httpEntity);
                    return header.getValue();
                }
            }
            EntityUtils.consumeQuietly(httpEntity);
            throw new IOException("redirect for  " + urlstring + ": no location attribute found");
        } else {
            EntityUtils.consumeQuietly(httpEntity);
            throw new IOException(
                    "no redirect for  " + urlstring + " fail: " + httpResponse.getStatusLine().getStatusCode()
                            + ": " + httpResponse.getStatusLine().getReasonPhrase());
        }
    } else {
        throw new IOException("client connection to " + urlstring + " fail: no connection");
    }
}

From source file:com.threadswarm.imagefeedarchiver.driver.CommandLineDriver.java

private RssChannel fetchRssChannel(URI targetUri) throws IOException, FeedParserException {
    RssChannel rssChannel = null;//w w  w.  jav  a 2  s . c  o m
    HttpEntity responseEntity = null;
    try {
        LOGGER.info("Attempting to fetch feed from URI: {}", targetUri.toString());
        HttpGet rssFeedGet = new HttpGet(targetUri);
        if (doNotTrackRequested) {
            LOGGER.debug("Adding 'DNT' header to feed-fetch request");
            rssFeedGet.addHeader(DNT_HEADER);
        }
        rssFeedGet.addHeader(RSS_ACCEPT_HEADER);

        HttpResponse imageResponse = httpClient.execute(rssFeedGet);
        responseEntity = imageResponse.getEntity();
        String rssFeedXmlString = EntityUtils.toString(responseEntity);
        RssDOMFeedParser parser = new RssDOMFeedParser();
        rssChannel = parser.readFeed(rssFeedXmlString);
    } finally {
        EntityUtils.consumeQuietly(responseEntity);
    }

    return rssChannel;
}

From source file:eu.europa.ec.markt.dss.validation102853.https.FileCacheDataLoader.java

@Override
public byte[] post(final String urlString, final byte[] content) throws DSSException {

    final String fileName = ResourceLoader.getNormalizedFileName(urlString);

    // The length for the InputStreamEntity is needed, because some receivers (on the other side) need this
    // information.
    // To determine the length, we cannot read the content-stream up to the end and re-use it afterwards.
    // This is because, it may not be possible to reset the stream (= go to position 0).
    // So, the solution is to cache temporarily the complete content data (as we do not expect much here) in a
    // byte-array.
    final byte[] digest = DSSUtils.digest(DigestAlgorithm.MD5, content);
    final String digestHexEncoded = DSSUtils.toHex(digest);
    final String cacheFileName = fileName + "." + digestHexEncoded;
    final File file = getCacheFile(cacheFileName);
    if (file.exists()) {

        LOG.debug("Cached file was used");
        final byte[] byteArray = DSSUtils.toByteArray(file);
        return byteArray;
    } else {/*from w ww . ja  v a  2  s  .c  o m*/

        LOG.debug("There is no cached file!");
    }

    final byte[] returnedBytes;
    if (!isNetworkProtocol(urlString)) {

        final String resourcePath = resourceLoader.getAbsoluteResourceFolder(urlString.trim());
        final File fileResource = new File(resourcePath);
        returnedBytes = DSSUtils.toByteArray(fileResource);
        return returnedBytes;
    }

    HttpPost httpRequest = null;
    HttpResponse httpResponse = null;
    try {

        final URI uri = URI.create(urlString.trim());
        httpRequest = new HttpPost(uri);

        final ByteArrayInputStream bis = new ByteArrayInputStream(content);

        final HttpEntity requestEntity = new InputStreamEntity(bis, content.length);
        httpRequest.setEntity(requestEntity);
        if (contentType != null) {
            httpRequest.setHeader(CONTENT_TYPE, contentType);
        }

        httpResponse = super.getHttpResponse(httpRequest, urlString);

        returnedBytes = readHttpResponse(urlString, httpResponse);
        if (returnedBytes.length != 0) {

            final File cacheFile = getCacheFile(cacheFileName);
            DSSUtils.saveToFile(returnedBytes, cacheFile);
        }
    } finally {
        if (httpRequest != null) {
            httpRequest.releaseConnection();
        }
        if (httpResponse != null) {
            EntityUtils.consumeQuietly(httpResponse.getEntity());
        }
    }
    return returnedBytes;
}