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:org.wso2.carbon.ml.analysis.test.AddFeaturesTestCase.java

/**
 * Test adding customized features an analysis.
 * //www .j  a  v  a2  s.c o m
 * @throws IOException
 * @throws MLHttpClientException 
 */
@Test(description = "Add customized features")
public void testAddCustomizedFeatures() throws MLHttpClientException, IOException {
    String payload = "[{\"type\" :\"CATEGORICAL\",\"include\" : true,\"imputeOption\":\"DISCARD\",\"name\":\""
            + "Cover_Type\"}]";
    CloseableHttpResponse response = mlHttpclient
            .doHttpPost("/api/analyses/" + MLIntegrationTestConstants.ANALYSIS_ID + "/features", payload);
    assertEquals("Unexpected response recieved", Response.Status.OK.getStatusCode(),
            response.getStatusLine().getStatusCode());
    response.close();
}

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

/**
 * Test setting customized hyper-parameters of an analysis.
 * //w ww  . jav  a2  s.c om
 * @throws IOException
 * @throws MLHttpClientException 
 */
@Test(description = "Set customized hyperparameters", dependsOnMethods = "testSetDefaultHyperparameters")
public void testSetCustomizedHyperParameters() throws IOException, MLHttpClientException {
    String payload = "[{\"key\" :\"Learning_Rate\",\"value\" : \"0.1\"},{\"key\":\"Iterations\",\"value\":\"100\"}]";
    CloseableHttpResponse response = mlHttpclient
            .doHttpPost("/api/analyses/" + MLIntegrationTestConstants.ANALYSIS_ID + "/hyperParams", payload);
    assertEquals("Unexpected response recieved", Response.Status.OK.getStatusCode(),
            response.getStatusLine().getStatusCode());
    response.close();
}

From source file:cn.org.once.cstack.service.impl.MonitoringServiceImpl.java

@Override
public String getJsonFromCAdvisor(String containerId) {
    String result = "";
    try {/*  ww  w.  j  av a 2s.co m*/
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpget = new HttpGet(cAdvisorURL + "/api/v1.3/containers/docker/" + containerId);
        CloseableHttpResponse response = httpclient.execute(httpget);
        try {
            result = EntityUtils.toString(response.getEntity());
            if (logger.isDebugEnabled()) {
                logger.debug(result);
            }
        } finally {
            response.close();
        }
    } catch (Exception e) {
        logger.error(containerId, e);
    }
    return result;
}

From source file:cn.org.once.cstack.service.impl.MonitoringServiceImpl.java

@Override
public String getJsonMachineFromCAdvisor() {
    String result = "";
    try {/*  w  w w .j ava2  s. c o  m*/
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpget = new HttpGet(cAdvisorURL + "/api/v1.3/machine");
        CloseableHttpResponse response = httpclient.execute(httpget);
        try {
            result = EntityUtils.toString(response.getEntity());
            if (logger.isDebugEnabled()) {
                logger.debug(result);
            }
        } finally {
            response.close();
        }
    } catch (Exception e) {
        logger.error("" + e);
    }
    return result;
}

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

/**
 * Test setting default values to hyper-parameters of an analysis.
 * //from w ww  .j a va2  s.co  m
 * @throws MLHttpClientException 
 * @throws IOException
 */
@Test(description = "Set default values to hyperparameters")
public void testSetDefaultHyperparameters() throws MLHttpClientException, IOException {
    CloseableHttpResponse response = mlHttpclient.doHttpPost(
            "/api/analyses/" + MLIntegrationTestConstants.ANALYSIS_ID + "/hyperParams/defaults", null);
    assertEquals("Unexpected response recieved", Response.Status.OK.getStatusCode(),
            response.getStatusLine().getStatusCode());
    response.close();
}

From source file:org.wuspba.ctams.ws.ITRosterController.java

private static void add(Roster roster) throws Exception {
    CTAMSDocument doc = new CTAMSDocument();
    doc.getRosters().add(roster);//from  w w w.j a va  2s .c o  m
    String xml = XMLUtils.marshal(doc);

    CloseableHttpClient httpclient = HttpClients.createDefault();

    URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).build();

    HttpPost httpPost = new HttpPost(uri);

    StringEntity xmlEntity = new StringEntity(xml, ContentType.APPLICATION_XML);

    CloseableHttpResponse response = null;

    try {
        httpPost.setEntity(xmlEntity);
        response = httpclient.execute(httpPost);

        assertEquals(IntegrationTestUtils.OK_STRING, response.getStatusLine().toString());

        HttpEntity responseEntity = response.getEntity();

        doc = IntegrationTestUtils.convertEntity(responseEntity);

        roster.setId(doc.getRosters().get(0).getId());

        EntityUtils.consume(responseEntity);
    } catch (UnsupportedEncodingException ex) {
        LOG.error("Unsupported coding", ex);
    } catch (IOException ioex) {
        LOG.error("IOException", ioex);
    } finally {
        if (response != null) {
            try {
                response.close();
            } catch (IOException ex) {
                LOG.error("Could not close response", ex);
            }
        }
    }
}

From source file:RestApiHttpClient.java

/**
 * Make a REST-Api Call with given HTTP-Method, Path and Data
 * //w  w  w  . j a  v a  2s.c om
 * @param method HTTP-Method (GET, POST, PUT and DELETE)
 * @param path String URL path with additional Parameters (articles/3)
 * @param data String POST or PUT data or null
 * @return String
 * @throws IOException in case of a problem or the connection was aborted
 */
public String call(RestApiHttpClient.HTTP_METHOD method, String path, String data) throws IOException {
    HttpRequestBase httpquery;

    switch (method) {
    case POST: {
        httpquery = new HttpPost(this.apiEndpoint + path);
        StringEntity sendData = new StringEntity(data,
                ContentType.create(ContentType.APPLICATION_JSON.getMimeType(), "UTF-8"));

        ((HttpPost) httpquery).setEntity(sendData);

        break;
    }
    case PUT: {
        httpquery = new HttpPut(this.apiEndpoint + path);
        if (data != null) {
            StringEntity sendData = new StringEntity(data,
                    ContentType.create(ContentType.APPLICATION_JSON.getMimeType(), "UTF-8"));
            ((HttpPut) httpquery).setEntity(sendData);
        }

        break;
    }
    case DELETE: {
        httpquery = new HttpDelete(this.apiEndpoint + path);
        break;
    }
    default:
        httpquery = new HttpGet(this.apiEndpoint.toString() + path);
        break;
    }

    CloseableHttpResponse response = httpclient.execute(httpquery, this.localContext);

    String result = EntityUtils.toString(response.getEntity());
    response.close();

    return result;
}

From source file:org.superbiz.CdiEventRealmTest.java

@Test
public void notAuthenticated() throws IOException {
    final CloseableHttpClient client = HttpClients.createDefault();

    final HttpGet httpGet = new HttpGet(webapp.toExternalForm() + "hello");
    final CloseableHttpResponse resp = client.execute(httpGet);
    try {/*from   ww  w. j  a v  a  2s  . c  om*/
        // Without login, it fails with a 403, not authorized
        assertEquals(403, resp.getStatusLine().getStatusCode());

    } finally {
        resp.close();
    }
}

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

/**
 * Test creating an analysis without the Name.
 * /*from  ww  w. j  a  va 2s.  c om*/
 * @throws MLHttpClientException 
 * @throws IOException 
 */
@Test(description = "Create an analysis without a name")
public void testCreateAnalysisWithoutName() throws MLHttpClientException, IOException {
    CloseableHttpResponse response = mlHttpclient.createAnalysis(null,
            MLIntegrationTestConstants.PROJECT_ID_DIABETES);
    assertEquals("Unexpected response received", Response.Status.BAD_REQUEST.getStatusCode(),
            response.getStatusLine().getStatusCode());
    response.close();
}

From source file:co.cask.cdap.client.rest.RestStreamWriter.java

private ListenableFuture<Void> write(HttpEntity entity, Map<String, String> headers) {
    final HttpPost postRequest = new HttpPost(restClient.getBaseURL()
            .resolve(String.format("/%s/streams/%s", restClient.getVersion(), streamName)));

    for (Map.Entry<String, String> entry : headers.entrySet()) {
        postRequest.setHeader(streamName + "." + entry.getKey(), entry.getValue());
    }//from w  w  w  .jav a  2  s  .  co  m

    postRequest.setEntity(entity);

    return pool.submit(new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            CloseableHttpResponse response = restClient.execute(postRequest);
            try {
                LOG.info("Write stream execute with response: " + response);
                RestClient.responseCodeAnalysis(response);
            } finally {
                response.close();
            }
            return null;
        }
    });
}