Example usage for org.apache.http.client.methods CloseableHttpResponse close

List of usage examples for org.apache.http.client.methods CloseableHttpResponse close

Introduction

In this page you can find the example usage for org.apache.http.client.methods CloseableHttpResponse close.

Prototype

public void close() throws IOException;

Source Link

Document

Closes this stream and releases any system resources associated with it.

Usage

From source file:cn.org.once.cstack.cli.rest.RestUtils.java

/**
 * sendDeleteCommand/*from w  w  w .  j  av  a2 s.c  o  m*/
 *
 * @param url
 * @return
 */
public Map<String, String> sendDeleteCommand(String url, Map<String, Object> credentials)
        throws ManagerResponseException {
    Map<String, String> response = new HashMap<String, String>();
    CloseableHttpClient httpclient = HttpClients.createDefault();

    HttpDelete httpDelete = new HttpDelete(url);
    CloseableHttpResponse httpResponse;
    try {
        httpResponse = httpclient.execute(httpDelete, localContext);
        ResponseHandler<String> handler = new CustomResponseErrorHandler();
        String body = handler.handleResponse(httpResponse);
        response.put("body", body);
        httpResponse.close();
    } catch (Exception e) {
        throw new ManagerResponseException(e.getMessage(), e);
    }

    return response;
}

From source file:org.wso2.carbon.ml.dataset.test.GetDatasetsTestCase.java

/**
 * @throws MLHttpClientException /*from   w w  w  .java  2  s  .c om*/
 * @throws IOException 
 */
@Test(description = "Get chart sample points of a dataset version", dependsOnMethods = "testGetVersionSetsOfdataset")
public void testGetChartSamplePointsOfVersionSet() throws MLHttpClientException, IOException {
    CloseableHttpResponse response = mlHttpclient
            .doHttpGet("/api/datasets/versions/" + MLIntegrationTestConstants.VERSIONSET_ID + "/charts");
    assertEquals("Unexpected response received", Response.Status.OK.getStatusCode(),
            response.getStatusLine().getStatusCode());
    response.close();
}

From source file:com.certivox.net.HTTPConnector.java

public HTTPResponse sendRequest(String serviceURL, String http_method, String requestBody,
        Hashtable<String, String> requestProperties) throws IOException {
    HttpRequestBase httpRequest = HttpRequestFactory.createRequest(serviceURL, http_method, requestBody,
            requestProperties);/*from  ww w. ja va 2s .  co  m*/
    CloseableHttpResponse response = httpClient.execute(httpRequest, HttpClientContext.create());
    try {
        HttpEntity entity = response.getEntity();
        InputStream is = entity.getContent();
        try {
            return new HTTPResponse(response.getStatusLine().getStatusCode(),
                    StringUtils.convertStreamToString(is));
        } finally {
            is.close();
        }
    } finally {
        response.close();
    }
}

From source file:com.srotya.sidewinder.cluster.storage.ClusteredMemStorageEngine.java

@Override
public boolean checkIfExists(String dbName) throws Exception {
    List<String> proxies = new ArrayList<>();
    dbName = decodeDbAndProxyNames(proxies, dbName);
    if (proxies.size() > 0) {
        return local.checkIfExists(dbName);
    } else {/*from w  ww .  j  a va  2s  . c  o m*/
        boolean localResult = local.checkIfExists(dbName);
        for (Entry<Integer, WorkerEntry> entry : columbus.getWorkerMap().entrySet()) {
            if (entry.getKey() != columbus.getSelfWorkerId()) {
                String newDbName = encodeDbAndProxyName(dbName, String.valueOf(columbus.getSelfWorkerId()));
                // http call
                CloseableHttpClient client = Utils.getClient(
                        "http://" + entry.getValue().getWorkerAddress().getHostAddress() + ":8080/", 5000,
                        5000);
                // Grafan API
                HttpGet post = new HttpGet("http://" + entry.getValue().getWorkerAddress().getHostAddress()
                        + ":8080/" + newDbName + "/hc");
                CloseableHttpResponse result = client.execute(post);
                if (result.getStatusLine().getStatusCode() == 200) {
                    localResult = true;
                    result.close();
                    client.close();
                    break;
                }
            }
        }
        return localResult;
    }
}

From source file:com.srotya.sidewinder.cluster.storage.ClusteredMemStorageEngine.java

@Override
public boolean checkIfExists(String dbName, String measurement) throws Exception {
    List<String> proxies = new ArrayList<>();
    dbName = decodeDbAndProxyNames(proxies, dbName);
    if (proxies.size() > 0) {
        return local.checkIfExists(dbName);
    } else {//from w w  w .j a  v  a2 s  .  co m
        boolean localResult = local.checkIfExists(dbName);
        for (Entry<Integer, WorkerEntry> entry : columbus.getWorkerMap().entrySet()) {
            if (entry.getKey() != columbus.getSelfWorkerId()) {
                String newDbName = encodeDbAndProxyName(dbName, String.valueOf(columbus.getSelfWorkerId()));
                // http call
                CloseableHttpClient client = Utils.getClient(
                        "http://" + entry.getValue().getWorkerAddress().getHostAddress() + ":8080/", 5000,
                        5000);

                // MeasurementOpsApi
                HttpGet post = new HttpGet("http://" + entry.getValue().getWorkerAddress().getHostAddress()
                        + ":8080/database/" + newDbName + "/" + measurement + "/check");
                CloseableHttpResponse result = client.execute(post);
                if (result.getStatusLine().getStatusCode() == 200) {
                    localResult = true;
                    result.close();
                    client.close();
                    break;
                }
            }
        }
        return localResult;
    }
}

From source file:org.wso2.carbon.ml.dataset.test.GetDatasetsTestCase.java

/**
 * @throws MLHttpClientException //from  w  ww .j a v  a  2 s.co m
 * @throws IOException 
 */
@Test(description = "Get version set with a version", dependsOnMethods = "testGetVersionSetsOfdataset")
public void testGetVersionSetWithVersion() throws MLHttpClientException, IOException {
    CloseableHttpResponse response = mlHttpclient
            .doHttpGet("/api/datasets/" + MLIntegrationTestConstants.DATASET_ID_DIABETES + "/versions/1.0");
    assertEquals("Unexpected response received", Response.Status.OK.getStatusCode(),
            response.getStatusLine().getStatusCode());
    response.close();
}

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

/**
 *
 * @param restPath/*ww  w  . j  a v a  2s .  com*/
 * @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:org.helm.notation2.wsadapter.NucleotideWSSaver.java

/**
 * Adds or updates a single nucleotide to the nucleotide store using the URL configured in
 * {@code MonomerStoreConfiguration}.//from  w  w  w  .j a v a 2  s . c  o m
 * 
 * @param nucleotide to save
 */
public String saveNucleotideToStore(Nucleotide nucleotide) {
    String res = "";
    CloseableHttpResponse response = null;

    try {
        response = WSAdapterUtils.putResource(nucleotide.toJSON(),
                MonomerStoreConfiguration.getInstance().getWebserviceNucleotidesPutFullURL());
        LOG.debug(response.getStatusLine().toString());

        EntityUtils.consume(response.getEntity());

    } catch (Exception e) {
        LOG.error("Saving nucleotide failed!", e);
        return "";
    } finally {
        try {
            if (response != null) {
                response.close();
            }
        } catch (IOException e) {
            LOG.debug("Closing resources failed.", e);
            return res;
        }
    }

    return res;
}

From source file:io.cloudslang.content.httpclient.CSHttpClient.java

private void checkKeepAlive(HttpRequestBase httpRequestBase, PoolingHttpClientConnectionManager connManager,
        String keepAliveInput, CloseableHttpResponse httpResponse) {
    boolean keepAlive = StringUtils.isBlank(keepAliveInput) || Boolean.parseBoolean(keepAliveInput);
    if (keepAlive) {
        httpRequestBase.releaseConnection();
    } else {/*w  w  w  .  j av a  2  s. c  o m*/
        try {
            httpResponse.close();
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
        httpRequestBase.releaseConnection();
        connManager.closeExpiredConnections();
    }
}

From source file:com.dnastack.bob.service.fetcher.util.HttpUtils.java

/**
 * Executes GET/POST and obtain the response.
 *
 * @param request request/*  ww  w  .  j a va  2s  .c  o  m*/
 *
 * @return response
 */
public String executeRequest(HttpRequestBase request) {
    String response = null;

    CloseableHttpResponse res = null;
    try {
        res = httpClient.execute(request);
        StatusLine line = res.getStatusLine();
        int status = line.getStatusCode();
        HttpEntity entity = res.getEntity();
        response = (entity == null) ? null : EntityUtils.toString(entity);
    } catch (IOException ex) {
        logger.error(ex.getMessage());
    } finally {
        try {
            if (res != null) {
                res.close();
            }
        } catch (IOException ex) {
            logger.error(ex.getMessage());
        }
    }
    return response;
}