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:org.digidoc4j.impl.bdoc.SKOcspDataLoader.java

@Override
public byte[] post(final String url, final byte[] content) throws DSSException {
    logger.info("Getting OCSP response from " + url);

    HttpPost httpRequest = null;//from  w w w. j  a  v a  2  s.  c  o m
    HttpResponse httpResponse = null;

    try {
        final URI uri = URI.create(url.trim());
        httpRequest = new HttpPost(uri);
        httpRequest.setHeader("User-Agent", userAgent);

        // 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 httpEntity = new InputStreamEntity(bis, content.length);
        final HttpEntity requestEntity = new BufferedHttpEntity(httpEntity);
        httpRequest.setEntity(requestEntity);
        if (contentType != null) {
            httpRequest.setHeader(CONTENT_TYPE, contentType);
        }

        httpResponse = getHttpResponse(httpRequest, url);

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

From source file:de.ii.xtraplatform.ogc.api.gml.parser.GMLParser.java

public void parse(ListenableFuture<HttpEntity> entity, String ns, String ft) throws ExecutionException {

    QName featureType = new QName(ns, ft);

    LOGGER.debug("Parsing GetFeature response for '{}'", ft);
    try {/*from   w w  w .  j  a v a 2  s .  c  o m*/

        ListenableFuture<SMInputCursor> rootFuture = Futures.transform(entity,
                new Function<HttpEntity, SMInputCursor>() {
                    @Override
                    public SMInputCursor apply(HttpEntity e) {
                        try {
                            return staxFactory.rootElementCursor(e.getContent()).advance();
                        } catch (IOException | IllegalStateException | XMLStreamException ex) {
                            LOGGER.debug("Error parsing WFS GetFeature (IOException) {}", ex.getMessage());
                            return null;
                        }
                    }
                });

        parseRoot(rootFuture, ns, ft);
    } finally {
        try {
            EntityUtils.consumeQuietly(entity.get());
        } catch (InterruptedException ex) {

        }
    }
}

From source file:de.ii.xtraplatform.ogc.api.gml.parser.GMLSchemaParser.java

public void parse(HttpEntity entity, Map<String, List<String>> elements) throws SchemaParseException {
    try {//from   w  w w . j a  v a2s  . c om
        InputSource is = new InputSource(entity.getContent());
        parse(is, elements);
    } catch (IOException ex) {
        LOGGER.error("Error parsing application schema. {}", ex);
        throw new SchemaParseException("Error parsing application schema. {}", ex.getMessage());
    } finally {
        EntityUtils.consumeQuietly(entity);
    }
}

From source file:httputils.RavelloHttpClient.java

public JsonObject publishBlueprint(String applicationName, int blueprintId, int stopTime, int startupDelay,
        String preferredCloud, String preferredRegion, boolean startAllVms, boolean costOptimized)
        throws RavelloException, InterruptedException {
    JsonObject value = null;/*from  w w w  .  j a  v  a2s.c  o  m*/
    HttpResponse response = null;

    try {
        response = this.getBlueprint(blueprintId);
        if (!HttpUtil.verifyResponseWithoutConsuming(response)) {
            EntityUtils.consumeQuietly(response.getEntity());
            throw new RavelloException("Failed to get blueprint number " + blueprintId + " error: "
                    + response.getStatusLine().toString());
        }
        JsonObject vmTemp = HttpUtil.convertResponseToJson(response);
        EntityUtils.consume(response.getEntity());

        JsonBuilderFactory factory = Json.createBuilderFactory(null);
        Iterator<Map.Entry<String, JsonValue>> it = vmTemp.entrySet().iterator();
        JsonObjectBuilder builder = factory.createObjectBuilder();
        Map.Entry<String, JsonValue> ent;
        while (it.hasNext()) {
            ent = it.next();
            if (!ent.getKey().equals("id") && !ent.getKey().equals("owner")) {
                builder.add(ent.getKey(), ent.getValue());
            }
        }
        builder.add("name", applicationName);
        value = builder.build();
        vmTemp = null;

        response = this.createApplication(value);
        this.verifyResponseAndConsume(response, "Failed to create application - error: ");

        value = HttpUtil.convertResponseToJson(response);
        EntityUtils.consumeQuietly(response.getEntity());

        int appId = value.getInt("id");

        if (costOptimized) {
            value = factory.createObjectBuilder().add("startAllVms", startAllVms).build();
        } else {
            value = factory.createObjectBuilder().add("startAllVms", startAllVms)
                    .add("preferredCloud", preferredCloud).add("preferredRegion", preferredRegion)
                    .add("optimizationLevel", "PERFORMANCE_OPTIMIZED").build();
        }
        response = this.post("/applications/" + appId + "/publish", value);
        this.verifyResponseAndConsume(response, "Failed to publish application - error: ");

        value = factory.createObjectBuilder().add("expirationFromNowSeconds", stopTime).build();
        response = this.post("/applications/" + appId + "/setExpiration", value);
        if (!HttpUtil.verifyResponseAndConsume(response)) {
            throw new RavelloException("Failed to set expiration time for application - error: "
                    + response.getStatusLine().toString() + "\n"
                    + "THIS ERROR MAY CAUSE APPLICATION TO RUN INDEFINITELY - MAKE SURE TO CHECK IT STOPPED");
        }

        if (!startAllVms) {
            response = this.getApplication(appId);
            if (!HttpUtil.verifyResponseWithoutConsuming(response)) {
                EntityUtils.consumeQuietly(response.getEntity());
                throw new RavelloException(
                        "Failed to get application status - error: " + response.getStatusLine().toString());
            }
            value = HttpUtil.convertResponseToJson(response);

            return value;
        }

        String state;
        JsonArray jArr;
        boolean allStarted;
        while (true) {
            allStarted = true;
            response = this.getApplication(appId);
            if (!HttpUtil.verifyResponseWithoutConsuming(response)) {
                EntityUtils.consumeQuietly(response.getEntity());
                throw new RavelloException(
                        "Failed to get application status - error: " + response.getStatusLine().toString());
            }
            value = HttpUtil.convertResponseToJson(response);

            jArr = value.getJsonObject("deployment").getJsonArray("vms");
            for (int jt = 0; jt < jArr.size(); jt++) {
                state = jArr.getJsonObject(jt).getString("state");
                allStarted = state.equals("STARTED");
                if (state.equals("ERROR")) {
                    throw new RavelloException(
                            "vm" + jArr.getJsonObject(jt).getString("name") + " failed to start");
                }
                if (!allStarted) {
                    break;
                }
            }

            if (allStarted) {
                break;
            } else {
                EntityUtils.consumeQuietly(response.getEntity());
                Thread.sleep(20000);
            }
        }

    } catch (ClientProtocolException e) {
        throw new RavelloException("ClientProtocolException - " + e.getMessage());
    } catch (IOException e) {
        throw new RavelloException("IOException - " + e.getMessage());
    } catch (NullPointerException e) {
        throw new RavelloException("NullPointerException - " + e.getMessage());
    }

    Thread.sleep(startupDelay * 1000);

    return value;
}

From source file:com.github.brandtg.pantopod.crawler.CrawlingEventHandler.java

@Override
public Set<CrawlEvent> handle(CrawlEvent event) throws Exception {
    Set<CrawlEvent> nextEvents = new HashSet<>();

    // Get url//  w w  w .  j  av a2 s  .com
    URI url = URI.create(event.getUrl());
    Document dom = null;
    boolean created = false;
    if (!checkErrors || !hasError(url)) {
        HttpGet req = new HttpGet(url);
        HttpResponse res = httpClient.execute(req);

        try {
            if (res.getStatusLine().getStatusCode() == 200) {
                byte[] domBytes = IOUtils.toByteArray(res.getEntity().getContent());
                created = handleData(url, domBytes);
                dom = Jsoup.parse(new String(domBytes));
            } else {
                LOG.error("Error for {} #=> {}", url, res.getStatusLine().getStatusCode());
                markError(url, res.getStatusLine().getStatusCode());
            }
        } finally {
            if (res.getEntity() != null) {
                EntityUtils.consumeQuietly(res.getEntity());
            }
        }
    }

    // Extract links
    if ((created || traverseDuplicates) && dom != null) {
        for (Element element : dom.select("a")) {
            String href = element.attr("href");
            if (href != null) {
                URI nextUri = getNextUri(url, href, event.getChroot());
                if (shouldExplore(nextUri) && isSameDomain(url, nextUri) && isDifferentPage(url, nextUri)) {
                    CrawlEvent nextEvent = new CrawlEvent(event);
                    nextEvent.setUrl(nextUri.toString());
                    nextEvent.setParentUrl(event.getUrl());
                    nextEvent.setDepth(event.getDepth() + 1);
                    nextEvents.add(nextEvent);
                    LOG.debug("Exploring {}", nextUri);
                } else {
                    LOG.debug("Skipping {}", nextUri);
                }
            }
        }
    }

    return nextEvents;
}

From source file:org.keycloak.testsuite.saml.ConcurrentAuthnRequestTest.java

public static void performLogin(HttpUriRequest post, URI samlEndpoint, String relayState, Document samlRequest,
        CloseableHttpResponse response, final CloseableHttpClient client, UserRepresentation user,
        RedirectStrategyWithSwitchableFollowRedirect strategy) {
    try {/* w  w  w.ja  va 2  s.co m*/
        HttpClientContext context = HttpClientContext.create();
        response = client.execute(post, context);

        String loginPageText = EntityUtils.toString(response.getEntity(), "UTF-8");
        response.close();

        HttpUriRequest loginRequest = LoginBuilder.handleLoginPage(user, loginPageText);

        strategy.setRedirectable(false);
        response = client.execute(loginRequest, context);
        response.close();
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        if (response != null) {
            EntityUtils.consumeQuietly(response.getEntity());
            try {
                response.close();
            } catch (IOException ex) {
            }
        }
    }
}

From source file:im.dadoo.logger.client.impl.DefaultLoggerClient.java

@Override
public void send(final Log log) {
    logger.info(String.format("??,:%s", log.toString()));
    try {/* w  ww .  j a  v a 2s. c o m*/
        this.post.setEntity(new StringEntity(this.mapper.writeValueAsString(log)));
        if (!this.httpClient.isRunning()) {
            this.httpClient.start();
        }
        this.httpClient.execute(this.post, new FutureCallback<HttpResponse>() {
            @Override
            public void completed(HttpResponse response) {
                HttpEntity entity = response.getEntity();
                try {
                    String rs = EntityUtils.toString(entity);
                    logger.info(rs);
                    Boolean result = mapper.readValue(rs, Boolean.class);
                    if (result) {
                        logger.info(String.format(",?:%s", log.toPropertyString()));
                    } else {
                        logger.warn(
                                String.format(",?:%s", log.toPropertyString()));
                    }
                } catch (IOException ex) {
                    logger.error(String.format("IO,?:%s", log.toPropertyString()),
                            ex);
                } catch (ParseException ex) {
                    logger.error(String.format("???,?:%s",
                            log.toPropertyString()), ex);
                } finally {
                    EntityUtils.consumeQuietly(entity);
                }
            }

            @Override
            public void failed(Exception ex) {
                logger.error(String.format("??,?:%s", log.toPropertyString()), ex);
            }

            @Override
            public void cancelled() {
                logger.warn(String.format("???,?:%s", log.toPropertyString()));
            }
        });
    } catch (JsonProcessingException ex) {
        logger.error(
                String.format("??json,?:%s", log.toPropertyString()),
                ex);
    } catch (UnsupportedEncodingException ex) {
        logger.error(String.format("????,?:%s", log.toPropertyString()), ex);
    }
}

From source file:de.ii.xtraplatform.ogc.api.gml.parser.OGCEntityResolver.java

@Override
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {

    // TODO: temporary basic auth hack
    // protected schema files
    if (systemId != null && systemId.startsWith("https://") && useBasicAuth) {
        HttpResponse response = null;//from   ww  w.j a  v  a2  s. c  om
        LOGGER.debug("resolving protected schema: {}", systemId);
        try {
            HttpGet httpGet = new HttpGet(systemId);

            String basic_auth = new String(Base64.encodeBase64((user + ":" + password).getBytes()));
            httpGet.addHeader("Authorization", "Basic " + basic_auth);

            response = untrustedSslHttpClient.execute(httpGet, new BasicHttpContext());
            String stringFromStream = CharStreams
                    .toString(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
            InputSource is = new InputSource(new StringReader(stringFromStream));
            is.setSystemId(systemId);

            return is;

        } catch (IOException ex) {
            ex.printStackTrace();
            LOGGER.error("Error parsing application schema. {}", ex);
            throw new SchemaParseException("Error parsing application schema. {}", ex.getMessage());
        } finally {
            if (response != null) {
                EntityUtils.consumeQuietly(response.getEntity());
            }
        }
    }

    //LOGGER.info(" --- {} --- {} ", systemId, publicId);
    if (publicId != null && publicId.equals("http://www.opengis.net/gml")) {
        if (!isAvailable(systemId)) {
            return new InputSource("http://schemas.opengis.net/gml/3.1.1/base/gml.xsd");
        }
    }
    if (publicId != null && publicId.equals("http://www.opengis.net/gml/3.2")) {
        if (!isAvailable(systemId)) {
            return new InputSource("http://schemas.opengis.net/gml/3.2.1/gml.xsd");
        }
    }
    if (publicId != null && publicId.equals("http://www.w3.org/1999/xlink")) {
        if (!isAvailable(systemId)) {
            return new InputSource("http://www.w3.org/1999/xlink.xsd");
        }
    }

    if (publicId != null && publicId.equals("http://www.aixm.aero/schema/5.1")) {
        if (!isAvailable(systemId)) {
            return new InputSource("http://www.aixm.aero/gallery/content/public/schema/5.1/AIXM_Features.xsd");
        }
    }

    if (systemId != null) {
        // Workaround for broken Schema in dwd-WFS
        if (systemId.endsWith("gml.xsd") && systemId.contains("kunden.dwd.de")) {
            return new InputSource("http://schemas.opengis.net/gml/3.2.1/gml.xsd");
        }

        /*if (systemId.endsWith("basicTypes.xsd") && redirect.contains("http://www.opengis.net/gml/3.2")) {
         return new InputSource("http://schemas.opengis.net/gml/3.2.1/basicTypes.xsd");
         }
                
         if (systemId.endsWith("xlinks.xsd") && redirect.contains("http://www.w3.org/1999/xlink")) {
         return new InputSource("http://www.w3.org/1999/xlink.xsd");
         }*/
        // workaround for A4I DescribeFeatureType (seen in 10.2.1)
        // also occurs with native XtraServer, moved to general workarounds
        if (publicId == null
                && systemId.contains("&REQUEST=DescribeFeatureType&TYPENAMES=ns:AbstractFeature")) {
            return createFakeSchema("http://www.opengis.net/gml/3.2");
        }

        // A4I workarounds
        if (systemId.contains("/exts/InspireFeatureDownload/service")) {
            String url = systemId;
            // workaround for A4I 10.1 SP1 (Patch1) blank encoding bug in GET parameters
            if (url.contains("OUTPUT_FORMAT=")) {
                int start = url.indexOf("OUTPUT_FORMAT=") + 13;
                int end = url.indexOf("&", start);
                String out = url.substring(start, end).replaceAll("%20", "");
                url = url.substring(0, start) + out + url.substring(end);
            }

            if (!url.equals(systemId)) {
                LOGGER.debug("original systemId: {}", systemId);
                LOGGER.debug("changed systemId: {}", url);
                return new InputSource(url);
            }
        }
    }

    // ignore multiple imports into the same namespace
    if (publicId != null) {
        if (!uris.containsKey(publicId)) {
            uris.put(publicId, systemId);
        }
        if (systemId != null && !systemId.equals(uris.get(publicId))) {
            return createFakeSchema(publicId);
        }
    }

    return null;
}

From source file:com.griddynamics.jagger.invoker.http.ApacheAbstractHttpInvoker.java

@Override
public final HttpResponse invoke(Q query, String endpoint) throws InvocationException {
    Preconditions.checkNotNull(query);/*from   w  w w . j av  a  2 s.  co  m*/
    Preconditions.checkNotNull(endpoint);

    HttpRequestBase method = null;
    HttpEntity response = null;
    try {
        method = getHttpMethod(query, endpoint);
        method.setParams(getHttpClientParams(query));

        org.apache.http.HttpResponse httpResponse = httpClient.execute(method);
        response = httpResponse.getEntity();
        return HttpResponse.create(httpResponse.getStatusLine().getStatusCode(),
                EntityUtils.toString(response));
    } catch (Exception e) {
        if (method != null) {
            log.debug("Error during invocation with URL: " + method.getURI() + ", endpoint: " + endpoint
                    + ", query: " + query, e);
        } else {
            log.debug("Error during invocation with: endpoint: " + endpoint + ", query: " + query, e);
        }
        throw new InvocationException("InvocationException : ", e);
    } finally {
        EntityUtils.consumeQuietly(response);
    }
}

From source file:org.phenotips.pingback.internal.client.data.IPPingDataProvider.java

@Override
public Map<String, Object> provideData() {
    Map<String, Object> jsonMap = new HashMap<>();

    CloseableHttpResponse response = null;
    try {/*from w ww  .  j a  v a 2  s  . c om*/
        String uri = configuration.getProperty(IP_FETCH_URL_PROPERTY);
        HttpGet method = new HttpGet(uri);
        RequestConfig config = RequestConfig.custom().setSocketTimeout(2000).build();
        method.setConfig(config);
        response = this.client.execute(method);
        JSONObject obj = new JSONObject(IOUtils.toString(response.getEntity().getContent()));

        if (obj.has(PROPERTY_IP)) {
            jsonMap.put(PROPERTY_IP, obj.get(PROPERTY_IP));
        }
    } catch (Exception e) {
        logWarning("Making IP request failed.", e);
    } finally {
        if (response != null) {
            try {
                EntityUtils.consumeQuietly(response.getEntity());
                response.close();
            } catch (IOException ex) {
                // Not dangerous
            }
        }
    }

    return jsonMap;
}