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:jp.classmethod.aws.brian.BrianClient.java

@Override
public void deleteTrigger(TriggerKey key) throws BrianClientException, BrianServerException {
    logger.debug("delete trigger: {}", key);
    HttpResponse httpResponse = null;//ww w  .  j a  va  2 s .  co  m
    try {
        String path = String.format("/triggers/%s/%s", key.getGroup(), key.getName());
        URI uri = new URI(scheme, null, hostname, port, path, null, null);
        HttpUriRequest httpRequest = RequestBuilder.delete().setUri(uri).build();
        httpResponse = httpClientExecute(httpRequest);
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        if (statusCode == HttpStatus.SC_OK) {
            logger.info("trigger deleted: {}", key);
            return;
        } else if (statusCode >= 500) {
            throw new BrianServerException("status = " + statusCode);
        } else if (statusCode == HttpStatus.SC_NOT_FOUND) {
            throw new BrianClientException(String.format("triggerKey (%s/%s) is not found",
                    new Object[] { key.getGroup(), key.getName() }));
        } else if (statusCode >= 400) {
            throw new BrianClientException("status = " + statusCode);
        } else {
            throw new Error("status = " + statusCode);
        }
    } catch (JsonProcessingException e) {
        throw new BrianServerException(e);
    } catch (URISyntaxException e) {
        throw new IllegalStateException(e);
    } catch (IOException e) {
        throw new BrianServerException(e);
    } catch (IllegalStateException e) {
        throw new Error(e);
    } finally {
        if (httpResponse != null) {
            EntityUtils.consumeQuietly(httpResponse.getEntity());
        }
    }
}

From source file:org.seasar.robot.client.http.HcHttpClient.java

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

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

    // robot context
    final S2RobotContext robotContext = CrawlingParameterUtil.getRobotContext();
    if (robotContext == 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 (robotContext.getRobotTxtUrlSet().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
    robotContext.getRobotTxtUrlSet().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) {
                        robotContext.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);
                                    robotContext.getUrlFilter().addExclude(hostUrl + urlPattern);
                                }
                            }
                        }
                        if (useRobotsTxtAllows) {
                            for (String urlPattern : directive.getAllows()) {
                                if (StringUtil.isNotBlank(urlPattern)) {
                                    urlPattern = convertRobotsTxtPathPattern(urlPattern);
                                    robotContext.getUrlFilter().addInclude(hostUrl + urlPattern);
                                }
                            }
                        }
                    }
                }
            }
        }
    } catch (final RobotSystemException e) {
        httpGet.abort();
        throw e;
    } catch (final Exception e) {
        httpGet.abort();
        throw new RobotCrawlAccessException("Could not process " + robotTxtUrl + ". ", e);
    } finally {
        EntityUtils.consumeQuietly(httpEntity);
    }
}

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

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

    if (LOG.isDebugEnabled()) {
        LOG.debug("Fetching data via POST from url " + url);
    }/*from  w w w  .  j  a  v a 2 s. co m*/
    HttpPost httpPost = null;
    HttpResponse httpResponse = null;
    try {

        final URI uri = DSSUtils.toUri(url.trim());
        httpPost = 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(requestBytes);

        final HttpEntity httpEntity = new InputStreamEntity(bis, requestBytes.length);
        final HttpEntity requestEntity = new BufferedHttpEntity(httpEntity);
        httpPost.setEntity(requestEntity);
        defineContentType(httpPost);
        defineContentTransferEncoding(httpPost);
        httpResponse = getHttpResponse(httpPost, uri);
        final byte[] returnedBytes = readHttpResponse(uri, httpResponse);
        return returnedBytes;
    } catch (IOException e) {
        throw new DSSException(e);
    } finally {
        if (httpPost != null) {
            httpPost.releaseConnection();
        }
        if (httpResponse != null) {
            EntityUtils.consumeQuietly(httpResponse.getEntity());
        }
    }
}

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

public synchronized void rollbackTransaction() throws OpenRDFException, IOException, UnauthorizedException {
    checkRepositoryURL();/*from w  w w .  j ava  2 s. co  m*/

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

    String requestURL = appendAction(transactionURL, Action.ROLLBACK);
    HttpPost method = new HttpPost(requestURL);

    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 rollback transaction. HTTP error code " + code);
        }
    } finally {
        EntityUtils.consumeQuietly(response.getEntity());
    }
}

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

public Integer[] getAuthorizedTeamsInOrg(String org) throws Exception {
    HttpClient client = createHttpClient();

    HttpGet get = new HttpGet("https://api.github.com/orgs/" + org + "/teams");
    addAuthHeader(get);/*from   w  w w  .  j  av a 2 s  . c  om*/
    Set<Integer> teamIds = new TreeSet<Integer>();
    HttpResponse response = null;
    try {
        response = client.execute(get);
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            String responseStr = EntityUtils.toString(response.getEntity());
            List<Team> teams = new Gson().fromJson(responseStr, new TypeToken<List<Team>>() {
            }.getType());
            for (Team team : teams) {
                teamIds.add(team.id);
            }
        } else {
            EntityUtils.consumeQuietly(response.getEntity());
            log.warn("Github get authorized teams service returned with a status of {}, {}",
                    response.getStatusLine().getStatusCode(), token);
            throw new Exception("Request to github authorized teams 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 teamIds.toArray(new Integer[] {});
}

From source file:org.eclipse.rdf4j.http.client.RDF4JProtocolSession.java

public synchronized void commitTransaction() throws RDF4JException, IOException, UnauthorizedException {
    checkRepositoryURL();//from   w w  w. j a  v  a2  s .  c o m

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

    HttpPut method = null;
    try {
        URIBuilder url = new URIBuilder(transactionURL);
        url.addParameter(Protocol.ACTION_PARAM_NAME, Action.COMMIT.toString());
        method = new HttpPut(url.build());

        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());
        }
    } catch (URISyntaxException e) {
        logger.error("could not create URL for transaction commit", e);
        throw new RuntimeException(e);
    } finally {
        if (method != null) {
            method.reset();
        }
    }
}

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

/**
 * Gets an existing recording/*from  ww w  .  j a  v  a 2s  .com*/
 *
 * @param recordingId The id property of the recording you want to retrieve
 * 
 * @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>
 *                                     </ul>
 */
public Recording getRecording(String recordingId) throws OpenViduJavaClientException, OpenViduHttpException {
    HttpGet request = new HttpGet(OpenVidu.urlOpenViduServer + API_RECORDINGS + "/" + 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)) {
            return new Recording(httpResponseToJson(response));
        } else {
            throw new OpenViduHttpException(statusCode);
        }
    } finally {
        EntityUtils.consumeQuietly(response.getEntity());
    }
}

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

@SuppressWarnings("unchecked")
private void getSessionIdHttp() throws OpenViduJavaClientException, OpenViduHttpException {
    if (this.hasSessionId()) {
        return;/*from w w  w.j a v  a2s .  c  om*/
    }

    HttpPost request = new HttpPost(OpenVidu.urlOpenViduServer + OpenVidu.API_SESSIONS);

    JSONObject json = new JSONObject();
    json.put("mediaMode", properties.mediaMode().name());
    json.put("recordingMode", properties.recordingMode().name());
    json.put("defaultOutputMode", properties.defaultOutputMode().name());
    json.put("defaultRecordingLayout", properties.defaultRecordingLayout().name());
    json.put("defaultCustomLayout", properties.defaultCustomLayout());
    json.put("customSessionId", properties.customSessionId());
    StringEntity params = null;
    try {
        params = new StringEntity(json.toString());
    } catch (UnsupportedEncodingException e1) {
        throw new OpenViduJavaClientException(e1.getMessage(), e1.getCause());
    }

    request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
    request.setEntity(params);

    HttpResponse response;
    try {
        response = OpenVidu.httpClient.execute(request);
    } catch (IOException e2) {
        throw new OpenViduJavaClientException(e2.getMessage(), e2.getCause());
    }
    try {
        int statusCode = response.getStatusLine().getStatusCode();
        if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
            JSONObject responseJson = httpResponseToJson(response);
            this.sessionId = (String) responseJson.get("id");
            this.createdAt = (long) responseJson.get("createdAt");
            log.info("Session '{}' created", this.sessionId);
        } else if (statusCode == org.apache.http.HttpStatus.SC_CONFLICT) {
            // 'customSessionId' already existed
            this.sessionId = properties.customSessionId();
        } else {
            throw new OpenViduHttpException(statusCode);
        }
    } finally {
        EntityUtils.consumeQuietly(response.getEntity());
    }
}

From source file:jp.classmethod.aws.brian.BrianClient.java

@Override
public void forceFireTrigger(TriggerKey key) throws BrianClientException, BrianServerException {
    logger.debug("force fire: {}", key);
    HttpResponse httpResponse = null;/*from www  . java 2 s  .  com*/
    try {
        String path = String.format("/triggers/%s/%s", key.getGroup(), key.getName());
        URI uri = new URI(scheme, null, hostname, port, path, null, null);
        HttpUriRequest httpRequest = RequestBuilder.post().setUri(uri).build();
        httpResponse = httpClientExecute(httpRequest);
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        if (statusCode == HttpStatus.SC_OK) {
            logger.info("trigger force fired: {}", key);
            return;
        } else if (statusCode >= 500) {
            throw new BrianServerException("status = " + statusCode);
        } else if (statusCode == HttpStatus.SC_NOT_FOUND) {
            throw new BrianClientException(String.format("triggerKey (%s/%s) is not found",
                    new Object[] { key.getGroup(), key.getName() }));
        } else if (statusCode >= 400) {
            throw new BrianClientException("status = " + statusCode);
        } else {
            throw new Error("status = " + statusCode);
        }
    } catch (JsonProcessingException e) {
        throw new BrianServerException(e);
    } catch (URISyntaxException e) {
        throw new IllegalStateException(e);
    } catch (IOException e) {
        throw new BrianServerException(e);
    } catch (IllegalStateException e) {
        throw new Error(e);
    } finally {
        if (httpResponse != null) {
            EntityUtils.consumeQuietly(httpResponse.getEntity());
        }
    }
}

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

@Test
public void testListMethodsForSDep() throws Exception {
    URI url = getURI(String.format("/objects/%s/methods/fedora-system:3", DEMO_REST_PID.toString()));
    verifyNoAuthFailOnAPIAAuth(url);//from ww  w .  j av  a 2s .c  o m
    HttpGet get = new HttpGet(url);
    HttpResponse response;
    int status = 0;
    get = new HttpGet(url);
    response = getOrDelete(get, getAuthAccess(), false);
    status = response.getStatusLine().getStatusCode();
    EntityUtils.consumeQuietly(response.getEntity());
    assertEquals(SC_OK, status);

    url = getURI(String.format("/objects/%s/methods/fedora-system:3?format=xml", DEMO_REST_PID.toString()));
    verifyNoAuthFailOnAPIAAuth(url);
    get = new HttpGet(url);
    response = getOrDelete(get, getAuthAccess(), true);
    assertEquals(SC_OK, response.getStatusLine().getStatusCode());
    String responseXML = readString(response);
    assertTrue(responseXML.contains("sDef=\"fedora-system:3\""));

    url = getURI(String.format("/objects/%s/methods/fedora-system:3?asOfDateTime=%s", DEMO_REST_PID.toString(),
            datetime));
    verifyNoAuthFailOnAPIAAuth(url);
    get = new HttpGet(url);
    response = getOrDelete(get, getAuthAccess(), false);
    status = response.getStatusLine().getStatusCode();
    EntityUtils.consumeQuietly(response.getEntity());
    assertEquals(SC_OK, status);
}