Example usage for org.apache.http.entity StringEntity StringEntity

List of usage examples for org.apache.http.entity StringEntity StringEntity

Introduction

In this page you can find the example usage for org.apache.http.entity StringEntity StringEntity.

Prototype

public StringEntity(String str) throws UnsupportedEncodingException 

Source Link

Usage

From source file:im.dadoo.logger.client.impl.DefaultLoggerClient.java

@Override
public void send(final Log log) {
    logger.info(String.format("??,:%s", log.toString()));
    try {/*from  w  w w.j a v  a2 s. co m*/
        this.post.setEntity(new StringEntity(this.mapper.writeValueAsString(log)));
        if (!this.httpClient.isRunning()) {
            this.httpClient.start();
        }
        this.httpClient.execute(this.post, new FutureCallback<HttpResponse>() {
            @Override
            public void completed(HttpResponse response) {
                HttpEntity entity = response.getEntity();
                try {
                    String rs = EntityUtils.toString(entity);
                    logger.info(rs);
                    Boolean result = mapper.readValue(rs, Boolean.class);
                    if (result) {
                        logger.info(String.format(",?:%s", log.toPropertyString()));
                    } else {
                        logger.warn(
                                String.format(",?:%s", log.toPropertyString()));
                    }
                } catch (IOException ex) {
                    logger.error(String.format("IO,?:%s", log.toPropertyString()),
                            ex);
                } catch (ParseException ex) {
                    logger.error(String.format("???,?:%s",
                            log.toPropertyString()), ex);
                } finally {
                    EntityUtils.consumeQuietly(entity);
                }
            }

            @Override
            public void failed(Exception ex) {
                logger.error(String.format("??,?:%s", log.toPropertyString()), ex);
            }

            @Override
            public void cancelled() {
                logger.warn(String.format("???,?:%s", log.toPropertyString()));
            }
        });
    } catch (JsonProcessingException ex) {
        logger.error(
                String.format("??json,?:%s", log.toPropertyString()),
                ex);
    } catch (UnsupportedEncodingException ex) {
        logger.error(String.format("????,?:%s", log.toPropertyString()), ex);
    }
}

From source file:org.sharetask.controller.TaskControllerIT.java

@Test
public void testAddComment() throws IOException {
    //given//from  w  w w .  j  av  a2  s  . c om
    final HttpPost httpPost = new HttpPost(URL_TASK + "/1/comment");
    httpPost.addHeader(new BasicHeader("Content-Type", "application/json"));
    final StringEntity httpEntity = new StringEntity("{\"comment\":\"test comment\"}");
    httpPost.setEntity(httpEntity);

    //when
    final HttpResponse response = getClient().execute(httpPost);

    //then
    Assert.assertEquals(HttpStatus.OK.value(), response.getStatusLine().getStatusCode());
}

From source file:io.crate.integrationtests.BlobHttpClient.java

public CloseableHttpResponse put(String table, String body) throws IOException {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    String digest = Hex.encodeHexString(Blobs.digest(body));
    String url = Blobs.url(address, table + "/" + digest);
    HttpPut httpPut = new HttpPut(url);
    httpPut.setEntity(new StringEntity(body));
    return httpClient.execute(httpPut);
}

From source file:org.fcrepo.it.BasicIT.java

private static void updateProperties(final String pid, final String sparql) throws IOException {
    final HttpPatch patch = new HttpPatch(getURIForPid(pid));
    patch.setHeader("Content-type", "application/sparql-update");
    patch.setEntity(new StringEntity(sparql));
    Assert.assertEquals(204, getStatusCode(patch));
}

From source file:ca.ualberta.cs.swapmyride.Misc.SaveUserRunnable.java

public void run() {
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpAdd = new HttpPost(url);
    HttpResponse response;/*from w  ww  .ja  v  a 2s . co  m*/
    StringEntity stringEntity = null;

    try {
        stringEntity = new StringEntity(gson.toJson(user));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    httpAdd.setEntity(stringEntity);
    httpAdd.setHeader("Accept", "application/json");

    //possible IO Error and an HTTP apache error
    try {
        response = httpClient.execute(httpAdd);
        String status = response.getStatusLine().toString();
        Log.i("NetworkDataManager", status);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:io.undertow.servlet.test.streams.ServletInputStreamEarlyCloseTestCase.java

@Test
public void testServletInputStreamEarlyClose() throws Exception {

    DeploymentUtils.setupServlet(new ServletInfo(SERVLET, EarlyCloseServlet.class).addMapping("/" + SERVLET));
    TestHttpClient client = new TestHttpClient();
    try {//from ww w .  j  a  v  a 2 s .c om
        String uri = DefaultServer.getDefaultServerURL() + "/servletContext/" + SERVLET;
        HttpPost post = new HttpPost(uri);
        post.setEntity(new StringEntity("A non-empty request body"));
        HttpResponse result = client.execute(post);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);

        result = client.execute(post);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);

        result = client.execute(post);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
    } finally {
        client.getConnectionManager().shutdown();
    }
}

From source file:retsys.client.http.HttpHelper.java

public HttpPost getHttpPostObj(String operation, String body) throws IOException {
    HttpPost post = null;//ww w. j  ava 2  s  .  co m

    post = new HttpPost(getHttpUrl(hostName, hostPort, context) + "/" + operation);

    StringEntity se = new StringEntity(body);
    se.setContentEncoding("UTF-8");
    se.setContentType("application/json");

    post.setEntity(se);

    return post;
}

From source file:tech.beshu.ror.integration.ClosedIndicesTests.java

private static void insertDoc(String docName, RestClient restClient) {
    if (adminClient == null) {
        adminClient = restClient;//  w  w w. j a v a 2 s . c  o  m
    }

    String path = "/" + IDX_PREFIX + docName + "/documents/doc-" + docName;
    try {

        HttpPut request = new HttpPut(restClient.from(path));
        request.setHeader("Content-Type", "application/json");
        request.setHeader("refresh", "true");
        request.setHeader("timeout", "50s");
        request.setEntity(new StringEntity("{\"title\": \"" + docName + "\"}"));
        System.out.println(body(restClient.execute(request)));

    } catch (Exception e) {
        e.printStackTrace();
        throw new IllegalStateException("Test problem", e);
    }

    // Polling phase.. #TODO is there a better way?
    try {
        HttpResponse response;
        do {
            HttpHead request = new HttpHead(restClient.from(path));
            request.setHeader("x-api-key", "p");
            response = restClient.execute(request);
            System.out.println(
                    "polling for " + docName + ".. result: " + response.getStatusLine().getReasonPhrase());
            Thread.sleep(200);
        } while (response.getStatusLine().getStatusCode() != 200);
    } catch (Exception e) {
        e.printStackTrace();
        throw new IllegalStateException("Cannot configure test case", e);
    }
}

From source file:ca.ualberta.cs.swapmyride.Misc.SavePhotoRunnable.java

public void run() {
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpAdd = new HttpPost(url);
    HttpResponse response;//from  w  w  w  .  j a  va2s. com
    StringEntity stringEntity = null;

    try {
        stringEntity = new StringEntity(gson.toJson(photo));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    httpAdd.setEntity(stringEntity);
    httpAdd.setHeader("Accept", "application/json");

    //possible IO Error and an HTTP apache error
    try {
        response = httpClient.execute(httpAdd);
        String status = response.getStatusLine().toString();
        Log.i("NetworkDataManager", status);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.envirocar.app.dao.remote.RemoteUserDAO.java

@Override
public void updateUser(User user) throws UserUpdateException, UnauthorizedException {
    HttpPut put = new HttpPut(ECApplication.BASE_URL + "/users/" + user.getUsername());
    try {/*from  www .j av a  2 s.c  o  m*/
        put.setEntity(new StringEntity(user.toJson()));
        super.executePayloadRequest(put);
    } catch (UnsupportedEncodingException e) {
        throw new UserUpdateException(e);
    } catch (JSONException e) {
        throw new UserUpdateException(e);
    } catch (NotConnectedException e) {
        throw new UserUpdateException(e);
    } catch (ResourceConflictException e) {
        throw new UserUpdateException(e);
    }
}