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:gertjvr.slacknotifier.SlackApiProcessor.java

public void sendNotification(String url, SlackNotificationMessage notification) throws IOException {
    String content = new Gson().toJson(notification);
    logger.debug(String.format("sendNotification: %s", content));

    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(url);

    httpPost.setEntity(new StringEntity(content, ContentType.APPLICATION_JSON));
    CloseableHttpResponse response = httpClient.execute(httpPost);

    try {/* w w  w  .  j a v  a  2s  . c om*/
        HttpEntity entity = response.getEntity();
        EntityUtils.consume(entity);
    } catch (Exception ex) {
        logger.error(String.format("sendNotification %s", ex.getMessage()), ex);
    } finally {
        response.close();
    }
}

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

/**
 * @throws MLHttpClientException//from   w  w  w .j a va2s  .c  o m
 * @throws IOException
 */
@Test(description = "Delete a dataset with a known ID")
public void testDeleteVersionSet() throws MLHttpClientException, IOException, JSONException {
    CloseableHttpResponse response = mlHttpclient
            .doHttpDelete("/api/datasets/versions/" + MLIntegrationTestConstants.VERSIONSET_ID);
    assertEquals("Unexpected response received", Response.Status.OK.getStatusCode(),
            response.getStatusLine().getStatusCode());
    response.close();
}

From source file:org.eclipse.wst.json.core.internal.download.HttpClientProvider.java

private static File download(File file, URL url) {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    CloseableHttpResponse response = null;
    OutputStream out = null;/* www  . j  av  a 2 s  .  co  m*/
    file.getParentFile().mkdirs();
    try {
        HttpHost target = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
        Builder builder = RequestConfig.custom();
        HttpHost proxy = getProxy(target);
        if (proxy != null) {
            builder = builder.setProxy(proxy);
        }
        RequestConfig config = builder.build();
        HttpGet request = new HttpGet(url.toURI());
        request.setConfig(config);
        response = httpclient.execute(target, request);
        InputStream in = response.getEntity().getContent();
        out = new BufferedOutputStream(new FileOutputStream(file));
        copy(in, out);
        return file;
    } catch (Exception e) {
        logWarning(e);
        ;
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
            }
        }
        if (response != null) {
            try {
                response.close();
            } catch (IOException e) {
            }
        }
        try {
            httpclient.close();
        } catch (IOException e) {
        }
    }
    return null;
}

From source file:org.wso2.carbon.ml.analysis.test.GetAnalysesTestCase.java

/**
 * Test retrieving all analyzes.//w  ww  . j  av  a2  s  .c o  m
 * 
 * @throws MLHttpClientException
 * @throws IOException
 */
@Test(description = "Get all analyses")
public void testGetAllAnalyzesOfProject() throws MLHttpClientException, IOException {
    CloseableHttpResponse response = mlHttpclient.doHttpGet("/api/projects/" + projectId + "/analyses");
    assertEquals("Unexpected response received", Response.Status.OK.getStatusCode(),
            response.getStatusLine().getStatusCode());
    response.close();
}

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

/**
 * @throws MLHttpClientException// w ww. j a va 2 s . c o  m
 * @throws IOException
 */
@Test(description = "Delete a dataset with a known ID")
public void testDeleteDataset() throws MLHttpClientException, IOException, JSONException {
    CloseableHttpResponse response = mlHttpclient
            .doHttpDelete("/api/datasets/" + MLIntegrationTestConstants.DATASET_ID_DIABETES);
    assertEquals("Unexpected response received", Response.Status.OK.getStatusCode(),
            response.getStatusLine().getStatusCode());
    response.close();

    response = mlHttpclient.doHttpDelete("/api/datasets/" + MLIntegrationTestConstants.DATASET_ID_DAS);
    assertEquals("Unexpected response received", Response.Status.OK.getStatusCode(),
            response.getStatusLine().getStatusCode());
    response.close();
}

From source file:org.wso2.carbon.ml.configs.test.ConfigurationAPITestCase.java

/**
 * Test retrieving an algorithm config by name.
 * /*from w ww  .j  a  va 2 s .c o  m*/
 * @throws MLHttpClientException 
 * @throws IOException 
 */
@Test(description = "Retrieve an algorithm")
public void testGetAlgorithmConfig() throws MLHttpClientException, IOException {
    CloseableHttpResponse response = mlHttpclient.doHttpGet("/api/configs/algorithms/LOGISTIC_REGRESSION");
    assertEquals("Unexpected response received", Response.Status.OK.getStatusCode(),
            response.getStatusLine().getStatusCode());
    response.close();
}

From source file:org.wso2.carbon.ml.analysis.test.FeatureProcessingTestCase.java

/**
 * Test adding default values to customized features an analysis.
 * @throws MLHttpClientException //from w ww. j a  va  2 s  .  c  o  m
 * @throws IOException
 */
@Test(priority = 1, description = "Add default values to customized features")
public void testAddDefaultsToCustomizedFeatures() throws MLHttpClientException, IOException {
    CloseableHttpResponse response = mlHttpclient.setFeatureDefaults(analysisId);
    assertEquals("Unexpected response received", Response.Status.OK.getStatusCode(),
            response.getStatusLine().getStatusCode());
    response.close();
}

From source file:myexamples.MyExamples.java

public static String getTweet() throws IOException {
    String result = "";
    HttpGet httpGet = new HttpGet("http://localhost:9200/gb/tweet/9");
    CloseableHttpResponse response1 = httpclient.execute(httpGet);
    // The underlying HTTP connection is still held by the response object
    // to allow the response content to be streamed directly from the network socket.
    // In order to ensure correct deallocation of system resources
    // the user MUST call CloseableHttpResponse#close() from a finally clause.
    // Please note that if response content is not fully consumed the underlying
    // connection cannot be safely re-used and will be shut down and discarded
    // by the connection manager.
    try {// w w w  . j a  v a 2 s .co m
        System.out.println(response1.getStatusLine());
        HttpEntity entity1 = response1.getEntity();
        // do something useful with the response body
        // and ensure it is fully consumed
        if (entity1 != null) {
            long len = entity1.getContentLength();
            if (len != -1 && len < 2048) {
                //      System.out.println(EntityUtils.toString(entity1));
                result = EntityUtils.toString(entity1);
            } else {
                System.out.println("entity length=" + len);
            }
        }

        EntityUtils.consume(entity1);
    } finally {
        response1.close();
    }
    return result;
}

From source file:fr.lissi.belilif.om2m.rest.WebServiceActions.java

/**
 * Do get.//  w  w w .  j a va2  s.c o  m
 *
 * @param uri
 *            the uri
 * @param headers
 *            the headers
 * @return the string
 * @throws Exception
 *             the exception
 */
public static String doGet(URI uri, HashMap<String, String> headers) throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    String respString = null;
    try {
        /*
         * HttpClient provides URIBuilder utility class to simplify creation and modification of request URIs.
         * 
         * URI uri = new URIBuilder() .setScheme("http") .setHost("hc.apache.org/") // .setPath("/search") // .setParameter("q",
         * "httpclient") // .setParameter("btnG", "Google Search") // .setParameter("aq", "f") // .setParameter("oq", "") .build();
         */

        HttpGet httpGet = new HttpGet(uri);

        for (String key : headers.keySet()) {
            httpGet.addHeader(key, headers.get(key));
        }

        CloseableHttpResponse response1 = httpclient.execute(httpGet);
        // The underlying HTTP connection is still held by the response object
        // to allow the response content to be streamed directly from the network socket.
        // In order to ensure correct deallocation of system resources
        // the user MUST call CloseableHttpResponse#close() from a finally clause.
        // Please note that if response content is not fully consumed the underlying
        // connection cannot be safely re-used and will be shut down and discarded
        // by the connection manager.

        try {
            System.out.println(response1.getStatusLine());
            HttpEntity entity = response1.getEntity();
            // do something useful with the response body
            if (entity != null) {
                respString = EntityUtils.toString(entity);
            }
            // and ensure it is fully consumed
            EntityUtils.consume(entity);
        } finally {
            response1.close();
        }
    } finally {
        httpclient.close();
    }
    return respString;
}

From source file:org.wso2.carbon.ml.configs.test.ConfigurationAPITestCase.java

/**
 * Test retrieving a non-existing algorithm.
 * //from   w w w.jav a  2s .c om
 * @throws MLHttpClientException 
 * @throws IOException
 */
@Test(description = "Retrieve a non-existing algorithm")
public void testGetNonExistingAnalysis() throws MLHttpClientException, IOException {
    CloseableHttpResponse response = mlHttpclient.doHttpGet("/api/configs/algorithms/" + "nonExistinfAlgo");
    assertEquals("Unexpected response received", Response.Status.NOT_FOUND.getStatusCode(),
            response.getStatusLine().getStatusCode());
    response.close();
}