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.cloudsimulator.utility.RestAPI.java

public static ResponseMessageString receiveString(final String restAPIURI, final String username,
        final String password, final String typeOfString, final String charset) throws IOException {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    CloseableHttpResponse httpResponse = null;

    ResponseMessageString responseMessageString = null;

    httpResponse = getRequestBasicAuth(httpClient, escapeURI(restAPIURI), username, password, typeOfString);

    if (httpResponse != null) {
        if (httpResponse.getStatusLine() != null) {
            if (httpResponse.getEntity() != null) {
                responseMessageString = new ResponseMessageString(httpResponse.getStatusLine().getStatusCode(),
                        httpResponse.getStatusLine().getReasonPhrase(),
                        IOUtils.toString(httpResponse.getEntity().getContent(), charset));
            } else {
                responseMessageString = new ResponseMessageString(httpResponse.getStatusLine().getStatusCode(),
                        httpResponse.getStatusLine().getReasonPhrase(), null);
            }/*from ww  w.j av a2s .co m*/

        } else {
            if (httpResponse.getEntity() != null) {
                responseMessageString = new ResponseMessageString(null, null,
                        IOUtils.toString(httpResponse.getEntity().getContent(), charset));
            }
        }

        httpResponse.close();
    }

    httpClient.close();
    return responseMessageString;

}

From source file:org.activiti.rest.content.service.api.BaseSpringContentRestTestCase.java

public void closeResponse(CloseableHttpResponse response) {
    if (response != null) {
        try {/*from   w w  w .  ja  va 2 s  .  c  o m*/
            response.close();
        } catch (IOException e) {
            fail("Could not close http connection");
        }
    }
}

From source file:com.whizzosoftware.hobson.rest.v1.resource.device.MediaProxyResource.java

@Override
public Representation head() {
    HobsonRestContext ctx = HobsonRestContext.createContext(this, getRequest());
    HobsonVariable hvar = variableManager.getDeviceVariable(ctx.getUserId(), ctx.getHubId(),
            getAttribute("pluginId"), getAttribute("deviceId"), getAttribute("mediaId"));
    if (hvar != null && hvar.getValue() != null) {
        try {/* w w  w.jav a2s. c o  m*/
            final HttpProps httpProps = createHttpGet(hvar.getValue().toString());

            try {
                final CloseableHttpResponse response = httpProps.client.execute(httpProps.httpGet);
                getResponse().setStatus(new Status(response.getStatusLine().getStatusCode()));
                response.close();
                return new EmptyRepresentation();
            } catch (IOException e) {
                throw new HobsonRuntimeException(e.getLocalizedMessage(), e);
            } finally {
                try {
                    httpProps.client.close();
                } catch (IOException e) {
                    logger.warn("Error closing HttpClient", e);
                }
            }
        } catch (ParseException | URISyntaxException e) {
            logger.error("Error obtaining media stream from device", e);
            throw new HobsonRuntimeException(e.getLocalizedMessage(), e);
        }
    }

    getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
    return new EmptyRepresentation();
}

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

/**
 * Test retrieving a non-existing analysis.
 * /* w  w  w .  j a  va  2s.  c  o  m*/
 * @throws MLHttpClientException
 * @throws IOException
 */
@Test(description = "Retrieve a non-existing analysis")
public void testGetNonExistingAnalysis() throws MLHttpClientException, IOException {
    CloseableHttpResponse response = mlHttpclient
            .doHttpGet("/api/projects/" + projectId + "/analyses/" + "nonExistingAnalysisName");
    assertEquals("Unexpected response received", Response.Status.NOT_FOUND.getStatusCode(),
            response.getStatusLine().getStatusCode());
    response.close();
}

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

/**
 * /* w  w  w  . ja  v a 2 s  .co  m*/
 * @throws MLHttpClientException
 * @throws IOException
 */
@Test(priority = 3, description = "Get all summarized features")
public void testGetSummarizedFeatures() throws MLHttpClientException, IOException {
    CloseableHttpResponse response = mlHttpclient
            .doHttpGet("/api/analyses/" + analysisId + "/summarizedFeatures");
    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 customized features an analysis.
 * /*from w  ww.  j a v a2 s .  c  om*/
 * @throws IOException
 * @throws MLHttpClientException 
 */
@Test(priority = 2, description = "Add customized features")
public void testAddCustomizedFeatures() throws MLHttpClientException, IOException {
    String payload = "[{\"type\" :\"NUMERICAL\",\"include\" : false,\"imputeOption\":\"DISCARD\",\"name\":\""
            + "Age\"}]";
    CloseableHttpResponse response = mlHttpclient.doHttpPost("/api/analyses/" + analysisId + "/features",
            payload);
    assertEquals("Unexpected response received", Response.Status.OK.getStatusCode(),
            response.getStatusLine().getStatusCode());
    response.close();
}

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

/**
 * //from w w  w  . j a v a  2  s. co m
 * @throws MLHttpClientException
 * @throws IOException
 */
@Test(priority = 4, description = "Get summary stats - with feature")
public void testGetSummaryStatsWithFeature() throws MLHttpClientException, IOException {
    CloseableHttpResponse response = mlHttpclient
            .doHttpGet("/api/analyses/" + analysisId + "/stats?feature=Class");
    assertEquals("Unexpected response received", Response.Status.OK.getStatusCode(),
            response.getStatusLine().getStatusCode());
    response.close();
}

From source file:com.comcast.cdn.traffic_control.traffic_router.core.external.DeliveryServicesTest.java

@Test
public void itReturnsIdOfValidDeliveryService() throws Exception {
    String encodedUrl = URLEncoder.encode("http://trafficrouter01.steering-target-1.thecdn.example.com/stuff",
            "utf-8");
    HttpGet httpGet = new HttpGet("http://localhost:3333/crs/deliveryservices?url=" + encodedUrl);

    CloseableHttpResponse response = null;
    try {//from  ww w . ja v a 2s.  c  om
        response = closeableHttpClient.execute(httpGet);
        String responseBody = EntityUtils.toString(response.getEntity());
        assertThat(responseBody, equalTo("{\"id\":\"steering-target-1\"}"));
    } finally {
        if (response != null)
            response.close();
    }
}

From source file:org.wso2.carbon.ml.project.test.MLProjectsTestCase.java

/**
 * Test retrieving all projects.//  w w  w . ja  va2  s.  c  om
 * 
 * @throws MLHttpClientException
 * @throws IOException
 */
@Test(priority = 5, description = "Retrieve all projects")
public void testGetAllProjects() throws MLHttpClientException, IOException {
    CloseableHttpResponse response = mlHttpclient.doHttpGet("/api/projects");
    assertEquals("Unexpected response received", Response.Status.OK.getStatusCode(),
            response.getStatusLine().getStatusCode());
    response.close();
}

From source file:org.apache.hadoop.gateway.SecureClusterTest.java

@Test
public void basicGetUserHomeRequest() throws Exception {
    setupLogging();/*from   ww  w. j a  va 2  s.  c  o  m*/
    CloseableHttpClient client = getHttpClient();
    String method = "GET";
    String uri = driver.getClusterUrl() + "/webhdfs/v1?op=GETHOMEDIRECTORY";
    HttpHost target = new HttpHost("localhost", driver.getGatewayPort(), "http");
    HttpRequest request = new BasicHttpRequest(method, uri);
    CloseableHttpResponse response = client.execute(target, request);
    String json = EntityUtils.toString(response.getEntity());
    response.close();
    //    System.out.println(json);
    assertEquals("{\"Path\":\"/user/" + userName + "\"}", json);
}