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:org.openehealth.ipf.tutorials.ref.Client.java

public void execute(InputStream input) throws Exception {
    HttpPost method = new HttpPost(serverUrl.toString());
    method.setEntity(new InputStreamEntity(input, contentType));
    CloseableHttpResponse response = client.execute(method);

    try {/*from  w  ww  . j  a  v a  2 s.co m*/
        InputStream responseStream = response.getEntity().getContent();
        handler.handleResponse(responseStream);
        IOUtils.closeQuietly(responseStream);
    } finally {
        method.releaseConnection();
    }

}

From source file:com.abc.turkey.web.unfreeze.UnfreezeController.java

@RequestMapping(value = "/cap_union_getsig_new")
public void getNewSig(HttpServletRequest request, HttpServletResponse response) {
    String url = captcha_prefix + "cap_union_getsig_new?" + request.getQueryString();
    HttpGet httpGet = new HttpGet(url);
    try {//from   www  .j ava 2  s. co  m
        CloseableHttpResponse response2 = httpClient.execute(httpGet);
        response2.getEntity().writeTo(response.getOutputStream());
        response2.close();
    } catch (Exception e) {
        throw new ServiceException(e.getMessage(), e.getCause());
    }
}

From source file:com.abc.turkey.web.unfreeze.UnfreezeController.java

@RequestMapping(value = "/cap_union_getcapbysig_new")
public void getCaptchaImage(HttpServletRequest request, HttpServletResponse response) {
    String url = captcha_prefix + "cap_union_getcapbysig_new?" + request.getQueryString();
    HttpGet httpGet = new HttpGet(url);
    try {/*from   w  w  w.j  a  v  a2s  .  c o m*/
        CloseableHttpResponse response2 = httpClient.execute(httpGet);
        response2.getEntity().writeTo(response.getOutputStream());
        response2.close();
    } catch (Exception e) {
        throw new ServiceException(e.getMessage(), e.getCause());
    }
}

From source file:com.abc.turkey.web.unfreeze.UnfreezeController.java

@RequestMapping(value = "/cap_union_verify_new")
public void capVerify(HttpServletRequest request, HttpServletResponse response) {
    String url = captcha_prefix + "cap_union_verify_new?" + request.getQueryString();
    HttpGet httpGet = new HttpGet(url);
    try {/*from   w w  w .  ja  v a 2 s .  c  om*/
        CloseableHttpResponse response2 = httpClient.execute(httpGet);
        response2.getEntity().writeTo(response.getOutputStream());
        response2.close();
    } catch (Exception e) {
        throw new ServiceException(e.getMessage(), e.getCause());
    }
}

From source file:sms.SendRequest.java

/**
 * /*from w ww .j av a 2  s  . c  o  m*/
 * @param moId
 * @param sender
 * @param serviceId
 * @param receiver
 * @param contentType
 * @param messageType
 * @param username
 * @param messageIndex
 * @param message
 * @param operator
 * @param commandCode
 * @return
 * @throws URISyntaxException
 * @throws IOException 
 */
public String sendRequests(String moId, String sender, String serviceId, String message, String operator,
        String commandCode, String username) throws URISyntaxException, IOException {
    Date d = new Date();
    SimpleDateFormat fm = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
    URI uri = new URIBuilder().setScheme("http").setHost("222.255.167.6:8080").setPath("/SMS/Ctnet/sms")
            .setParameter("mo_id", moId).setParameter("username", username)
            .setParameter("key", Common.getMD5(username + "|" + moId + "|ctnet2015"))
            .setParameter("sender", sender).setParameter("serviceId", serviceId)
            .setParameter("message", message).setParameter("operator", operator)
            .setParameter("commandCode", commandCode).build();
    HttpGet httpPost = new HttpGet(uri);
    httpPost.addHeader("content-type", "application/x-www-form-urlencoded");
    CloseableHttpClient httpclient = HttpClients.createDefault();
    CloseableHttpResponse response = httpclient.execute(httpPost);

    try {
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            long len = entity.getContentLength();
            if (len != -1 && len < 2048) {
                return EntityUtils.toString(entity);
            } else {
                // Stream content out
            }
        }
    } finally {
        response.close();
    }
    return "";
}

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

@Override
public ForwardConfig execute(ActionMapping mapping, DynaActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws SQLException, RuntimeException {
    try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {

        //get request data
        String id_usuario = form.getItem("id_usuario");
        String id_sala = form.getItem("id_sala");
        String authToken = String.valueOf(request.getSession().getAttribute("token"));

        URIBuilder builder = new URIBuilder();
        builder.setScheme("http").setHost("25.136.78.82").setPort(8080)
                .setPath("/usuarios-salas/" + id_usuario + "/" + id_sala);

        HttpDelete delete = new HttpDelete();
        delete.setURI(builder.build());/*from  w  ww.  j av a2s  .  co  m*/
        delete.addHeader("Authorization", "BEARER " + authToken);
        delete.addHeader("accept", "application/json");

        CloseableHttpResponse deleteResponse = httpClient.execute(delete);

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

        if (responseStatus.getStatusCode() != 200) {
            throw new RuntimeException(restResp);
        }

        return mapping.getForwardByName("success");

    } catch (IOException | URISyntaxException | RuntimeException e) {
        String id_usuario = form.getItem("id_usuario");
        request.setAttribute("message",
                "Error al intentar expulsar usuario: " + id_usuario + "; " + e.getMessage());
        response.setStatus(400);
        return mapping.getForwardByName("failure");
    }
}

From source file:goofyhts.torrentkinesis.http.client.DefaultHttpClient.java

@Override
public String getURL(String url) {
    System.out.println("Request=" + url);
    String responseString = null;
    try {//from ww w.  ja va  2s . c om
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response = client.execute(httpGet, context);
        responseString = IOUtils.toString(response.getEntity().getContent());
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return responseString;
}

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

@Test
public void testPost() throws IOException {

    List<NameValuePair> formparams = new ArrayList<NameValuePair>();
    formparams.add(new BasicNameValuePair("extTID", "3"));
    formparams.add(new BasicNameValuePair("extAction", "exceptionFormPostController"));
    formparams.add(new BasicNameValuePair("extMethod", "throwAException"));
    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   www.j  a  va  2 s .  co  m
    String responseString = EntityUtils.toString(entity);
    ObjectMapper mapper = new ObjectMapper();

    Map<String, Object> rootAsMap = mapper.readValue(responseString, Map.class);
    assertThat(rootAsMap).hasSize(6);
    assertThat(rootAsMap.get("method")).isEqualTo("throwAException");
    assertThat(rootAsMap.get("type")).isEqualTo("exception");
    assertThat(rootAsMap.get("action")).isEqualTo("exceptionFormPostController");
    assertThat(rootAsMap.get("tid")).isEqualTo(3);
    assertThat(rootAsMap.get("message")).isEqualTo("a null pointer");
    assertThat(rootAsMap.get("where")).isNull();

    @SuppressWarnings("unchecked")
    Map<String, Object> result = (Map<String, Object>) rootAsMap.get("result");
    assertThat(result).hasSize(1);
    assertThat((Boolean) result.get("success")).isFalse();
    IOUtils.closeQuietly(response);
}

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

@Override
public ForwardConfig execute(ActionMapping mapping, DynaActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws SQLException, RuntimeException {
    try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
        //prepare http get
        SalaBean sala = (SalaBean) request.getSession().getAttribute("sala");
        String authToken = String.valueOf(request.getSession().getAttribute("token"));

        URIBuilder builder = new URIBuilder();
        builder.setScheme("http").setHost("25.136.78.82").setPort(8080)
                .setPath("/usuarios-salas/sala/" + sala.getId());

        HttpGet getRequest = new HttpGet();
        getRequest.setURI(builder.build());
        getRequest.addHeader("Authorization", "BEARER " + authToken);
        getRequest.addHeader("accept", "application/json");

        CloseableHttpResponse getResponse = httpClient.execute(getRequest);
        HttpEntity responseEntity = getResponse.getEntity();
        StatusLine responseStatus = getResponse.getStatusLine();
        String restResp = EntityUtils.toString(responseEntity);

        if (responseStatus.getStatusCode() != 200) {
            throw new RuntimeException(restResp);
        }/*from   www  . ja va 2  s  .  co m*/
        //parse message data from response
        Gson gson = new Gson();
        UsuarioBean[] usrList = gson.fromJson(restResp, UsuarioBean[].class);
        request.setAttribute("usuarios", usrList);

        return mapping.getForwardByName("success");

    } catch (IOException | URISyntaxException | RuntimeException e) {
        request.setAttribute("message", "Error al intentar listar Usuarios " + e.getMessage());
        response.setStatus(400);
        return mapping.getForwardByName("failure");
    }

}

From source file:org.activiti.rest.dmn.service.api.repository.DecisionTableModelResourceTest.java

@DmnDeploymentAnnotation(resources = { "org/activiti/rest/dmn/service/api/repository/simple.dmn" })
public void testGetDecisionTableModel() throws Exception {

    DecisionTable decisionTable = dmnRepositoryService.createDecisionTableQuery().singleResult();

    HttpGet httpGet = new HttpGet(SERVER_URL_PREFIX + DmnRestUrls
            .createRelativeResourceUrl(DmnRestUrls.URL_DECISION_TABLE_MODEL, decisionTable.getId()));
    CloseableHttpResponse response = executeRequest(httpGet, HttpStatus.SC_OK);

    // Check "OK" status
    JsonNode resultNode = objectMapper.readTree(response.getEntity().getContent());
    closeResponse(response);/*from www. j  av a 2 s  .co m*/
    assertNotNull(resultNode);
    JsonNode currentDecisionTable = resultNode.get("currentDecisionTable");
    assertNotNull(currentDecisionTable);
    assertEquals("decisionTable", currentDecisionTable.get("id").textValue());
}