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

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

Introduction

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

Prototype

HttpEntity getEntity();

Source Link

Usage

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 {// w w w.jav  a2 s.c  o m
        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.commonjava.test.http.TestHttpServerTest.java

@Test
public void simpleDownload() throws Exception {
    final String subPath = "/path/to/something.txt";
    final String content = "this is the content";
    final String url = server.formatUrl(subPath);
    final String path = server.formatPath(subPath);
    server.expect(url, 200, content);//from w ww  .  ja  va 2  s .  c o m

    final HttpGet request = new HttpGet(url);
    final CloseableHttpClient client = HttpClients.createDefault();
    CloseableHttpResponse response = null;

    InputStream stream = null;
    try {
        response = client.execute(request);
        stream = response.getEntity().getContent();
        final String result = IOUtils.toString(stream);

        assertThat(result, notNullValue());
        assertThat(result, equalTo(content));
    } finally {
        IOUtils.closeQuietly(stream);
        if (response != null && response.getEntity() != null) {
            EntityUtils.consumeQuietly(response.getEntity());
            IOUtils.closeQuietly(response);
        }

        if (request != null) {
            request.reset();
        }

        if (client != null) {
            IOUtils.closeQuietly(client);
        }
    }

    System.out.println(server.getAccessesByPathKey());

    final String key = server.getAccessKey(CommonMethod.GET.name(), path);
    System.out.println("Getting accesses for: '" + key + "'");
    assertThat(server.getAccessesByPathKey().get(key), equalTo(1));
}

From source file:com.abc.turkey.service.unfreeze.SmsService.java

private String checkRes(String res, HttpUriRequest lastRequest) throws Exception {
    String errorCode = JSON.parseObject(res).getString("error");
    if (errorCode == null) {
        return res;
    }/* w w  w  .j a v a2 s. c  om*/
    if (errorCode.equals("10001")) {
        // ,,??
        login();
        CloseableHttpResponse response = httpclient.execute(lastRequest);
        res = EntityUtils.toString(response.getEntity(), "utf8");
        logger.debug(res);
        response.close();
    } else {
        // throw new ServiceException("Sms api error, code: " + errorCode);
    }
    return res;
}

From source file:HelloWorld.HelloWorldTest.java

@Test
public void postPeopleTest() throws URISyntaxException, IOException {
    URI uri = new URI(baseURI + "api/person");
    HttpPost post = new HttpPost(uri);
    Person person = new Person();
    person.firstName = "Sally";
    person.lastName = "Smith";
    person.age = 25;//from   www.  j  a v  a2 s  . c om
    person.married = false;
    person.birthDate = new Date();

    Gson gson = new GsonBuilder().setDateFormat("MM/dd/yyyy").create();
    String json = gson.toJson(person);
    post.setEntity(new StringEntity(json));

    CloseableHttpResponse response = client.execute(post);
    assertEquals(201, response.getStatusLine().getStatusCode());
    assertEquals("Person creation success!", EntityUtils.toString(response.getEntity(), "UTF-8"));
}

From source file:pl.wavesoftware.wfirma.api.simple.mapper.SimpleGateway.java

private String handleCode200OK(CloseableHttpResponse response) throws IOException, WFirmaException {
    HttpEntity entity = response.getEntity();
    String content = EntityUtils.toString(entity, Charsets.UTF_8);
    for (ResponseListener responseListener : listeners) {
        responseListener.responseRecived(content);
    }/*  w  w  w.ja  v a2s.  c  o m*/
    return statusParser.checkedForStatus(credentials.getConsumerKey(), content);
}

From source file:io.crate.protocols.http.CrateHttpsTransportIntegrationTest.java

@Test
public void testBlobLayer() throws IOException {
    try {/*from w  w w  .  j  av  a  2s.  c om*/
        execute("create blob table test");
        final String blob = StringUtils.repeat("abcdefghijklmnopqrstuvwxyz", 1024 * 600);
        String blobUrl = upload("test", blob);
        assertThat(blobUrl, not(isEmptyOrNullString()));
        HttpGet httpGet = new HttpGet(blobUrl);
        CloseableHttpResponse response = httpClient.execute(httpGet);
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertThat(response.getEntity().getContentLength(), is((long) blob.length()));
    } finally {
        execute("drop table if exists test");
    }
}

From source file:callbacks.AuthenticationCallback.java

@Override
public Boolean checkCredentials(final ClientCredentialsData clientCredentialsData)
        throws AuthenticationException {

    /*TODO: In a production ready plugin you should cache here if possible!!
    You can use hte @Cache Annotation for that or implement caching yourself */

    final HttpPost httpPost = new HttpPost(MOCK_HTTP_SERVICE);

    CloseableHttpResponse response = null;
    try {//ww  w  .  j  a  v  a 2 s . c o  m
        response = httpClient.execute(httpPost);
        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            final HashMap result = objectMapper.readValue(EntityUtils.toString(entity), HashMap.class);
            if ("valid".equals(result.get("auth"))) {
                return true;
            }
        }
    } catch (JsonParseException e) {
        log.error("Error while parsing the results from the webservice call", e);
    } catch (JsonMappingException e) {
        log.error("Error while parsing the results from the webservice call", e);
    } catch (ClientProtocolException e) {
        log.error("There was an error in the HTTP communication with the server", e);
    } catch (IOException e) {
        log.error("Could not get valid results from the webservice", e);
    } finally {
        closeGracefully(response);
    }
    //If we're getting here, we didn't get a valid response from the webservice
    return false;
}

From source file:com.abc.turkey.service.unfreeze.SmsService.java

private void login() {
    try {/*from   ww w.  j  a v  a  2 s  .com*/
        HttpPost httpPost = new HttpPost(loginUrl);
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("username", "yuzhibo468"));
        nvps.add(new BasicNameValuePair("password", "qq123654"));
        httpPost.setEntity(new UrlEncodedFormEntity(nvps));
        CloseableHttpResponse response = httpclient.execute(httpPost);
        String res = EntityUtils.toString(response.getEntity(), "utf8");
        logger.debug(res);
        response.close();
    } catch (Exception e) {
        throw new ServiceException("Sms api login error!");
    }
}