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

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

Introduction

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

Prototype

public InputStream getContent() throws IOException 

Source Link

Usage

From source file:co.aurasphere.botmill.skype.util.network.NetworkUtils.java

/**
 * To string entity.// w w  w  . j a  v  a  2  s .  com
 *
 * @param object the object
 * @return the string entity
 */
private static StringEntity toStringEntity(Object object) {
    StringEntity input = null;
    try {
        String json = JsonUtils.toJson(object);
        input = new StringEntity(json);
        input.setContentType("application/json");
        logger.debug("Request: {}", inputStreamToString(input.getContent()));
    } catch (Exception e) {
        logger.error("Error during JSON message creation: ", e);
    }
    return input;
}

From source file:co.aurasphere.botmill.telegram.internal.util.network.NetworkUtils.java

/**
 * Utility method that converts an object to its StringEntity
 * representation.//from   w ww.  j av a 2 s  . c  om
 * 
 * @param object
 *            the object to convert to a StringEntity.
 * @return a {@link StringEntity} object containing the object JSON.
 */
private static StringEntity toStringEntity(Object object) {
    StringEntity input = null;
    try {
        String json = JsonUtils.toJson(object);
        input = new StringEntity(json, "UTF-8");
        input.setContentType("application/json");
        logger.debug("Request: {}", inputStreamToString(input.getContent()));
    } catch (Exception e) {
        logger.error("Error during JSON message creation: ", e);
    }
    return input;
}

From source file:com.glaf.core.util.http.HttpClientUtils.java

public static String doPost(String url, String data, String contentType, String encoding) {
    StringBuffer buffer = new StringBuffer();
    InputStreamReader is = null;/*from  w w w  . ja  va 2s.c o m*/
    BufferedReader reader = null;
    BasicCookieStore cookieStore = new BasicCookieStore();
    HttpClientBuilder builder = HttpClientBuilder.create();
    CloseableHttpClient client = builder.setDefaultCookieStore(cookieStore).build();
    try {
        HttpPost post = new HttpPost(url);
        if (data != null) {
            StringEntity entity = new StringEntity(data, encoding);
            post.setHeader("Content-Type", contentType);
            post.setEntity(entity);
        }
        HttpResponse response = client.execute(post);
        HttpEntity entity = response.getEntity();
        is = new InputStreamReader(entity.getContent(), encoding);
        reader = new BufferedReader(is);
        String tmp = reader.readLine();
        while (tmp != null) {
            buffer.append(tmp);
            tmp = reader.readLine();
        }
    } catch (IOException ex) {
        ex.printStackTrace();
        throw new RuntimeException(ex);
    } finally {
        IOUtils.closeStream(reader);
        IOUtils.closeStream(is);
        try {
            client.close();
        } catch (IOException ex) {
        }
    }
    return buffer.toString();
}

From source file:co.aurasphere.botmill.fb.internal.util.network.FbBotMillNetworkController.java

/**
 * Utility method that converts an object to its StringEntity
 * representation./*from  w ww.j a  v  a2s .  com*/
 * 
 * @param object
 *            the object to convert to a StringEntity.
 * @return a {@link StringEntity} object containing the object JSON.
 */
private static StringEntity toStringEntity(Object object) {
    StringEntity input = null;
    try {
        String json = FbBotMillJsonUtils.toJson(object);
        input = new StringEntity(json, "UTF-8");
        input.setContentType("application/json");
        logger.debug("Request: {}", inputStreamToString(input.getContent()));
    } catch (Exception e) {
        logger.error("Error during JSON message creation: ", e);
    }
    return input;
}

From source file:co.aurasphere.botmill.fb.internal.util.network.NetworkUtils.java

/**
 * Utility method that converts an object to its StringEntity
 * representation./*  w ww . j av  a2  s.co  m*/
 * 
 * @param object
 *            the object to convert to a StringEntity.
 * @return a {@link StringEntity} object containing the object JSON.
 */
private static StringEntity toStringEntity(Object object) {
    StringEntity input = null;
    try {
        String json = JsonUtils.toJson(object);
        input = new StringEntity(json, StandardCharsets.UTF_8);
        input.setContentType("application/json");
        logger.debug("Request: {}", inputStreamToString(input.getContent()));
    } catch (Exception e) {
        logger.error("Error during JSON message creation: ", e);
    }
    return input;
}

From source file:com.nominanuda.springsoy.SoySourceTest.java

@Test
public void testJs() throws Exception {
    SoyJsTemplateServer soyJsTemplateServer = new SoyJsTemplateServer();
    soyJsTemplateServer.setSoySource(soySource);
    HttpRequest req = new HttpGet("/somepath/foo.soy.js?lang=en");
    DataObjectURISpec spec = new DataObjectURISpec("/somepath/{tpl **.soy.js}?{lang en|it}");
    DataObject cmd = spec.match(req.getRequestLine().getUri());
    CommandRequestHandlerAdapter adapter = new CommandRequestHandlerAdapter();
    StringEntity se = (StringEntity) adapter.invoke(soyJsTemplateServer, req, cmd);
    String jsFile = new IOHelper().readAndClose(se.getContent(), HttpProtocol.CS_UTF_8);
    Assert.assertTrue(jsFile.contains("examples.simple.helloWorld2 = function"));
    Assert.assertEquals(HttpProtocol.CT_TEXT_JAVASCRIPT_CS_UTF8, se.getContentType().getValue());
}

From source file:de.devbliss.apitester.factory.impl.EntityBuilderTest.java

@Test
public void buildEntityFromStringPayload() throws Exception {
    final String payload = "spitting cobra";
    StringEntity result = entityBuilder.buildEntity(payload);
    assertEquals("text/plain", result.getContentType().getValue());
    assertEquals(payload, IOUtils.toString(result.getContent()));
}

From source file:de.devbliss.apitester.factory.impl.EntityBuilderTest.java

@Test
public void buildEntityFromObjectPayload() throws Exception {
    // prepare payload:
    SpittingCobra payload = new SpittingCobra();
    final String name = "Bob";
    payload.setName(name);// w ww  . j a v  a 2s . c o m

    // build entity:
    StringEntity result = entityBuilder.buildEntity(payload);
    assertEquals("application/json", result.getContentType().getValue());
    assertEquals("{\"name\":\"" + name + "\"}", IOUtils.toString(result.getContent()));
}

From source file:org.pentaho.di.engine.configuration.impl.pentaho.scheduler.SchedulerRequestTest.java

private boolean compareContentOfStringEntities(StringEntity entity1, StringEntity entity2) {
    if (entity1.getContentLength() == entity2.getContentLength()) {
        try (InputStream stream1 = entity1.getContent(); InputStream stream2 = entity2.getContent()) {
            while (stream1.available() > 0) {
                if (stream1.read() != stream2.read()) {
                    return false;
                }// w  w  w. j a va2  s.c o m
            }
            return true;
        } catch (IOException e) {
            return false;
        }
    } else {
        return false;
    }
}

From source file:boosta.artem.services.TaskQuery.java

public int addTask() throws UnsupportedEncodingException, IOException, ParseException {

    int task_id = 0;

    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost("http://62.210.82.210:9091/API");
    StringEntity requestEntity = new StringEntity(this.task.toJSON(), "UTF-8");
    requestEntity.setContentType("application/json");
    System.out.println(getStringFromInputStream(requestEntity.getContent()) + "\n");
    httpPost.setEntity(requestEntity);/*from   w w w  .  jav  a 2 s  .co  m*/
    HttpResponse response = httpClient.execute(httpPost);

    InputStream in = response.getEntity().getContent();

    String resp = getStringFromInputStream(in);
    //resp.getBytes("UTF-8");

    //resp = resp.getBytes(resp).toString();

    System.out.println(resp);

    JSONParser parser = new JSONParser();

    JSONObject json = (JSONObject) parser.parse(resp);
    //json.toJSONString();
    task_id = Integer.parseInt(json.get("data").toString());

    System.out.println(task_id);

    return task_id;
}