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.opinionlab.woa.WallOfAwesomeTest.java

@Test
public void hello() throws IOException {
    CloseableHttpResponse response = HttpClients.createDefault()
            .execute(new HttpGet("http://localhost:9999/hello"));
    assertEquals("Hello World!", IOUtils.toString(response.getEntity().getContent()));
}

From source file:ar.edu.ubp.das.src.chat.actions.ValidAction.java

@Override
public ForwardConfig execute(ActionMapping mapping, DynaActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws SQLException, RuntimeException {
    try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
        HttpPost httpPost = new HttpPost("http://25.136.78.82:8080/login/");
        List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("nombre_usuario", form.getItem("user")));
        params.add(new BasicNameValuePair("password", form.getItem("pw")));
        httpPost.setEntity(new UrlEncodedFormEntity(params));
        CloseableHttpResponse postResponse = httpClient.execute(httpPost);

        HttpEntity responseEntity = postResponse.getEntity();
        StatusLine responseStatus = postResponse.getStatusLine();
        String restResp = EntityUtils.toString(responseEntity);

        if (responseStatus.getStatusCode() != 200) {
            System.out.println(restResp);
            throw new RuntimeException("Los datos ingresados son incorrectos");
        }//from   w  w w  .j a v a2 s  .  c o m

        Header authHeader = postResponse.getFirstHeader("Auth-Token");
        String headerValue = authHeader != null ? authHeader.getValue() : "";

        HttpPost adminPost = new HttpPost("http://25.136.78.82:8080/login/admin");
        adminPost.addHeader("Authorization", "BEARER " + headerValue);
        adminPost.addHeader("accept", "application/json");

        postResponse = httpClient.execute(adminPost);
        responseEntity = postResponse.getEntity();
        responseStatus = postResponse.getStatusLine();

        if (responseStatus.getStatusCode() != 200) {
            throw new RuntimeException("Accesso restringido a Administradores");
        }

        request.getSession().setAttribute("user", restResp);
        request.getSession().setAttribute("token", headerValue);
        request.getSession().setAttribute("login_tmst", String.valueOf(System.currentTimeMillis()));

        return mapping.getForwardByName("success");

    } catch (IOException | RuntimeException e) {
        request.setAttribute("message", "Error al realizar login: " + e.getMessage());
        response.setStatus(401);
        return mapping.getForwardByName("failure");
    }
}

From source file:com.baidubce.http.BceHttpResponse.java

public BceHttpResponse(CloseableHttpResponse httpResponse) throws IOException {
    this.httpResponse = httpResponse;
    HttpEntity entity = httpResponse.getEntity();
    if (entity != null && entity.isStreaming()) {
        this.content = entity.getContent();
    }//w w w .j  a  v a 2  s .co  m
}

From source file:com.collaide.fileuploader.requests.repository.SendFileThread.java

@Override
public void run() {
    try {/*from  ww w.ja  v  a2 s.  c o m*/
        logger.debug("start sending file");
        CloseableHttpResponse response = httpClient.execute(httpPost, httpContext);
        logger.debug("executing the query...");
        try {
            response.getEntity();
        } finally {
            response.close();
        }
        logger.debug("stop sending file");

    } catch (IOException ex) {
        logger.debug("error while creating a file " + ex);
    }
}

From source file:org.jboss.quickstarts.wfk.travelagent.flight.FlightService.java

JSONObject findById(Long id) {

    try {//from  w w w . ja  v a  2 s .  c om
        URI uri = new URIBuilder().setScheme("http").setHost("jbosscontactsangularjs-110336260.rhcloud.com")
                .setPath("/rest/flights/id/" + id.toString())
                //.setParameter("id", id.toString())
                .build();
        HttpGet req = new HttpGet(uri);
        CloseableHttpResponse response = httpClient.execute(req);
        String responseBody = EntityUtils.toString(response.getEntity());
        JSONObject responseJson = new JSONObject(responseBody);

        HttpClientUtils.closeQuietly(response);
        return responseJson;
    } catch (Exception e) {
        log.info(e.toString());
        return null;
    }

}

From source file:io.github.discovermovies.datacollector.movie.network.TheMovieDbApi.java

private String getResponseFromServer(String url) throws IOException {
    HttpGet request = new HttpGet(url);
    if (Application.DEBUG) {
        System.out.print("\nDownloading");
    }//from ww  w  .j av a  2s  .  c om
    CloseableHttpResponse response = client.execute(request);
    BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
    String line;
    String output = "";
    while ((line = br.readLine()) != null) {
        output += line;
    }
    request.releaseConnection();
    if (Application.DEBUG) {
        System.out.println();
    }
    return output;

}

From source file:com.apm4all.tracy.TracyCloseableHttpClientPublisher.java

private String extractPostResponse(CloseableHttpResponse response) throws ParseException, IOException {
    StringBuilder sb = new StringBuilder(1024);
    HttpEntity entity = response.getEntity();
    sb.append(response.getStatusLine());
    sb.append(" ");
    sb.append(EntityUtils.toString(entity, StandardCharsets.UTF_8));
    EntityUtils.consume(entity);/*  www .jav a2s .  c  o  m*/
    return sb.toString();
}

From source file:ch.ralscha.extdirectspring_itest.ExceptionFormPostServiceTest.java

@Test
public void testNonExistingMethod() throws IOException {

    List<NameValuePair> formparams = new ArrayList<NameValuePair>();
    formparams.add(new BasicNameValuePair("extTID", "3"));
    formparams.add(new BasicNameValuePair("extAction", "exceptionFormPostService"));
    formparams.add(new BasicNameValuePair("extMethod", "throwAExceptionNotExists"));
    formparams.add(new BasicNameValuePair("extType", "rpc"));
    formparams.add(new BasicNameValuePair("extUpload", "false"));

    UrlEncodedFormEntity postEntity = new UrlEncodedFormEntity(formparams, "UTF-8");

    this.post.setEntity(postEntity);

    CloseableHttpResponse response = this.client.execute(this.post);
    HttpEntity entity = response.getEntity();
    assertThat(entity).isNotNull();/*from   w w  w  .  ja  v a2s .c  om*/
    String responseString = EntityUtils.toString(entity);
    ObjectMapper mapper = new ObjectMapper();

    Map<String, Object> rootAsMap = mapper.readValue(responseString, Map.class);
    assertThat(rootAsMap).hasSize(5);
    assertThat(rootAsMap.get("method")).isEqualTo("throwAExceptionNotExists");
    assertThat(rootAsMap.get("type")).isEqualTo("exception");
    assertThat(rootAsMap.get("action")).isEqualTo("exceptionFormPostService");
    assertThat(rootAsMap.get("tid")).isEqualTo(3);
    assertThat(rootAsMap.get("message")).isEqualTo("Server Error");
    IOUtils.closeQuietly(response);
}

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

@Override
public String getJsonFromCAdvisor(String containerId) {
    String result = "";
    try {// w w w  . j  av  a 2s  .  c o  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;
}