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:eu.europa.ec.markt.dss.validation102853.https.CommonDataLoader.java

/**
 * This method retrieves data using HTTP or HTTPS protocol and 'get' method.
 *
 * @param urlString to access//w w  w. j a v  a  2  s  .com
 * @return {@code byte} array of obtained data or {@code null}
 * @throws DSSException in case of any exception
 */
protected byte[] httpGet(final String urlString) throws DSSException {

    final URI uri = DSSUtils.toUri(urlString.trim());
    HttpGet httpGet = null;
    HttpResponse httpResponse = null;
    try {

        httpGet = new HttpGet(uri);
        defineContentType(httpGet);
        defineContentTransferEncoding(httpGet);
        httpResponse = getHttpResponse(httpGet, uri);
        final byte[] returnedBytes = readHttpResponse(uri, httpResponse);
        return returnedBytes;
    } finally {
        if (httpGet != null) {
            httpGet.releaseConnection();
        }
        if (httpResponse != null) {
            EntityUtils.consumeQuietly(httpResponse.getEntity());
        }
    }
}

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

@Test
public void testAllowHeader() throws Exception {
    HttpResponse response = httpGET(baseURL + "/ResetDataSource", 405);
    Header[] headers = response.getHeaders("Allow");
    assertEquals("POST", headers[0].getValue());
    EntityUtils.consumeQuietly(response.getEntity());
}

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

@Test
@Ignore//w  ww .ja v  a  2  s. c  o  m
public void testFunctionImport() throws Exception {
    //TODO: fails because of lack of geometery support
    HttpResponse response = httpGET(baseURL + "/GetNearestAirport(lat=23.0,lon=34.0)", 200);
    EntityUtils.consumeQuietly(response.getEntity());
}

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

@Test
public void testBadReferences() throws Exception {
    HttpResponse response = httpGET(baseURL + "/People('russelwhyte')/$ref", 405);
    EntityUtils.consumeQuietly(response.getEntity());
}

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

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

    LOG.debug("Fetching data via POST from url " + url);

    HttpPost httpRequest = null;/*from  www .  ja v  a  2 s .  com*/
    HttpResponse httpResponse = null;

    try {
        final URI uri = URI.create(url.trim());
        httpRequest = new HttpPost(uri);

        // 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 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 = getHttpResponse(httpRequest, url);

        final byte[] returnedBytes = readHttpResponse(url, httpResponse);
        return returnedBytes;
    } finally {
        if (httpRequest != null) {
            httpRequest.releaseConnection();
        }
        if (httpResponse != null) {
            EntityUtils.consumeQuietly(httpResponse.getEntity());
        }
    }
}

From source file:org.fcrepo.test.api.TestRESTAPI.java

protected void verifyNoAuthFailOnAPIAAuth(URI url) throws Exception {
    if (this.getAuthAccess()) {
        HttpGet get = new HttpGet(url);
        HttpResponse response = getOrDelete(get, false, false);
        int status = response.getStatusLine().getStatusCode();
        EntityUtils.consumeQuietly(response.getEntity());
        assertEquals(SC_UNAUTHORIZED, status);
    }//from  w  w  w.  j a va 2s  . c o  m
}

From source file:org.codelibs.fess.crawler.client.http.HcHttpClient.java

protected void processRobotsTxt(final String url) {
    if (StringUtil.isBlank(url)) {
        throw new CrawlerSystemException("url is null or empty.");
    }// w w  w  .ja  v  a2 s . c  o  m

    if (robotsTxtHelper == null || !robotsTxtHelper.isEnabled()) {
        // not support robots.txt
        return;
    }

    // crawler context
    final CrawlerContext crawlerContext = CrawlingParameterUtil.getCrawlerContext();
    if (crawlerContext == null) {
        // wrong state
        return;
    }

    final int idx = url.indexOf('/', url.indexOf("://") + 3);
    String hostUrl;
    if (idx >= 0) {
        hostUrl = url.substring(0, idx);
    } else {
        hostUrl = url;
    }
    final String robotTxtUrl = hostUrl + "/robots.txt";

    // check url
    if (crawlerContext.getRobotsTxtUrlSet().contains(robotTxtUrl)) {
        if (logger.isDebugEnabled()) {
            logger.debug(robotTxtUrl + " is already visited.");
        }
        return;
    }

    if (logger.isInfoEnabled()) {
        logger.info("Checking URL: " + robotTxtUrl);
    }
    // add url to a set
    crawlerContext.getRobotsTxtUrlSet().add(robotTxtUrl);

    final HttpGet httpGet = new HttpGet(robotTxtUrl);

    // request header
    for (final Header header : requestHeaderList) {
        httpGet.addHeader(header);
    }

    HttpEntity httpEntity = null;
    try {
        // get a content
        final HttpResponse response = executeHttpClient(httpGet);
        httpEntity = response.getEntity();

        final int httpStatusCode = response.getStatusLine().getStatusCode();
        if (httpStatusCode == 200) {

            // check file size
            final Header contentLengthHeader = response.getFirstHeader("Content-Length");
            if (contentLengthHeader != null) {
                final String value = contentLengthHeader.getValue();
                final long contentLength = Long.parseLong(value);
                if (contentLengthHelper != null) {
                    final long maxLength = contentLengthHelper.getMaxLength("text/plain");
                    if (contentLength > maxLength) {
                        throw new MaxLengthExceededException("The content length (" + contentLength
                                + " byte) is over " + maxLength + " byte. The url is " + robotTxtUrl);
                    }
                }
            }

            if (httpEntity != null) {
                final RobotsTxt robotsTxt = robotsTxtHelper.parse(httpEntity.getContent());
                if (robotsTxt != null) {
                    final String[] sitemaps = robotsTxt.getSitemaps();
                    if (sitemaps.length > 0) {
                        crawlerContext.addSitemaps(sitemaps);
                    }

                    final RobotsTxt.Directive directive = robotsTxt.getMatchedDirective(userAgent);
                    if (directive != null) {
                        if (useRobotsTxtDisallows) {
                            for (String urlPattern : directive.getDisallows()) {
                                if (StringUtil.isNotBlank(urlPattern)) {
                                    urlPattern = convertRobotsTxtPathPattern(urlPattern);
                                    crawlerContext.getUrlFilter().addExclude(hostUrl + urlPattern);
                                }
                            }
                        }
                        if (useRobotsTxtAllows) {
                            for (String urlPattern : directive.getAllows()) {
                                if (StringUtil.isNotBlank(urlPattern)) {
                                    urlPattern = convertRobotsTxtPathPattern(urlPattern);
                                    crawlerContext.getUrlFilter().addInclude(hostUrl + urlPattern);
                                }
                            }
                        }
                    }
                }
            }
        }
    } catch (final CrawlerSystemException e) {
        httpGet.abort();
        throw e;
    } catch (final Exception e) {
        httpGet.abort();
        throw new CrawlingAccessException("Could not process " + robotTxtUrl + ". ", e);
    } finally {
        EntityUtils.consumeQuietly(httpEntity);
    }
}

From source file:com.meltmedia.cadmium.core.github.ApiClient.java

public String[] getAuthorizedOrgs() throws Exception {
    HttpClient client = createHttpClient();

    HttpGet get = new HttpGet("https://api.github.com/user/orgs");
    addAuthHeader(get);/* w  ww  . ja  v a 2  s  .com*/
    Set<String> orgs = new TreeSet<String>();
    HttpResponse response = null;
    try {
        response = client.execute(get);
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            String responseStr = EntityUtils.toString(response.getEntity());
            List<Org> retOrgs = new Gson().fromJson(responseStr, new TypeToken<List<Org>>() {
            }.getType());
            for (Org org : retOrgs) {
                orgs.add(org.login);
            }
        } else {
            EntityUtils.consumeQuietly(response.getEntity());
            log.warn("Github get authorized orgs service returned with a status of {}, {}",
                    response.getStatusLine().getStatusCode(), token);
            throw new Exception("Request to github authorized orgs service failed");
        }
    } finally {
        get.releaseConnection();
        if (response != null) {
            try {
                ((CloseableHttpResponse) response).close();
            } catch (IOException e) {
            }
        }
        if (client != null) {
            try {
                ((CloseableHttpClient) client).close();
            } catch (Throwable t) {
            }
        }
    }
    return orgs.toArray(new String[] {});
}

From source file:io.openvidu.java.client.OpenVidu.java

/**
 * Stops the recording of a {@link io.openvidu.java.client.Session}
 *
 * @param recordingId The id property of the recording you want to stop
 *
 * @return The stopped recording//from w ww  .j a  v a2 s.com
 * 
 * @throws OpenViduJavaClientException
 * @throws OpenViduHttpException       Value returned from
 *                                     {@link io.openvidu.java.client.OpenViduHttpException#getStatus()}
 *                                     <ul>
 *                                     <li><code>404</code>: no recording exists
 *                                     for the passed <i>recordingId</i></li>
 *                                     <li><code>406</code>: recording has
 *                                     <i>starting</i> status. Wait until
 *                                     <i>started</i> status before stopping the
 *                                     recording</li>
 *                                     </ul>
 */
public Recording stopRecording(String recordingId) throws OpenViduJavaClientException, OpenViduHttpException {
    HttpPost request = new HttpPost(
            OpenVidu.urlOpenViduServer + API_RECORDINGS + API_RECORDINGS_STOP + "/" + recordingId);
    HttpResponse response;
    try {
        response = OpenVidu.httpClient.execute(request);
    } catch (IOException e) {
        throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
    }

    try {
        int statusCode = response.getStatusLine().getStatusCode();
        if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
            Recording r = new Recording(httpResponseToJson(response));
            Session activeSession = OpenVidu.activeSessions.get(r.getSessionId());
            if (activeSession != null) {
                activeSession.setIsBeingRecorded(false);
            } else {
                log.warn("No active session found for sessionId '" + r.getSessionId()
                        + "'. This instance of OpenVidu Java Client didn't create this session");
            }
            return r;
        } else {
            throw new OpenViduHttpException(statusCode);
        }
    } finally {
        EntityUtils.consumeQuietly(response.getEntity());
    }
}

From source file:org.openrdf.http.client.SesameSession.java

public synchronized void commitTransaction() throws OpenRDFException, IOException, UnauthorizedException {
    checkRepositoryURL();/*  w ww.j  a va2 s .co  m*/

    if (transactionURL == null) {
        throw new IllegalStateException("Transaction URL has not been set");
    }

    HttpPost method = null;
    try {
        URIBuilder url = new URIBuilder(transactionURL);
        url.addParameter(Protocol.ACTION_PARAM_NAME, Action.COMMIT.toString());
        method = new HttpPost(url.build());
    } catch (URISyntaxException e) {
        logger.error("could not create URL for transaction commit", e);
        throw new RuntimeException(e);
    }

    final HttpResponse response = execute(method);
    try {
        int code = response.getStatusLine().getStatusCode();
        if (code == HttpURLConnection.HTTP_OK) {
            // we're done.
            transactionURL = null;
        } else {
            throw new RepositoryException("unable to commit transaction. HTTP error code " + code);
        }
    } finally {
        EntityUtils.consumeQuietly(response.getEntity());
    }

}