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

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

Introduction

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

Prototype

public BasicHttpEntity() 

Source Link

Usage

From source file:org.apache.hadoop.gateway.dispatch.PartiallyRepeatableHttpEntityTest.java

@Test
public void testB_C1_PC_IB__C2_PC_OB__C1_AC__EE() throws IOException {
    String data = "0123456789";
    BasicHttpEntity basic;// w w w  .j  a  va 2  s. c om
    PartiallyRepeatableHttpEntity replay;
    InputStream stream1, stream2;
    String text;

    basic = new BasicHttpEntity();
    basic.setContent(new ByteArrayInputStream(data.getBytes(UTF8)));
    replay = new PartiallyRepeatableHttpEntity(basic, 5);

    stream1 = replay.getContent();
    text = blockRead(stream1, UTF8, 3, 2);
    assertThat(text, is("012"));

    stream2 = replay.getContent();
    text = blockRead(stream2, UTF8, 6, 4);
    assertThat(text, is("012345"));

    try {
        blockRead(stream1, UTF8, 6, 4);
        fail("Expected IOException");
    } catch (IOException e) {
        // Expected.
    }
}

From source file:org.apache.hadoop.gateway.dispatch.CappedBufferHttpEntityTest.java

@Test
public void testIsRepeatable() throws Exception {
    String text = "0123456789";
    BasicHttpEntity basic;//from ww w .  ja v a  2s.c o  m
    CappedBufferHttpEntity replay;

    basic = new BasicHttpEntity();
    basic.setContent(new ByteArrayInputStream(text.getBytes(UTF8)));
    replay = new CappedBufferHttpEntity(basic);
    assertThat(replay.isRepeatable(), is(true));

    basic = new BasicHttpEntity();
    basic.setContent(new ByteArrayInputStream(text.getBytes(UTF8)));
    BufferedHttpEntity buffered = new BufferedHttpEntity(basic);
    replay = new CappedBufferHttpEntity(buffered);
    assertThat(replay.isRepeatable(), is(true));
}

From source file:org.apache.hadoop.gateway.dispatch.PartiallyRepeatableHttpEntityTest.java

@Test
public void testWriteTo() throws Exception {
    String input = "0123456789";
    BasicHttpEntity basic;/*from  www.  j ava2  s  .c o m*/
    PartiallyRepeatableHttpEntity replay;

    basic = new BasicHttpEntity();
    basic.setContent(new ByteArrayInputStream(input.getBytes(UTF8)));
    replay = new PartiallyRepeatableHttpEntity(basic, 5);

    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    replay.writeTo(buffer);
    String output = new String(buffer.toByteArray(), UTF8);
    assertThat(output, is(input));
}

From source file:jscover.server.PersistentStaticHttpServer.java

protected void sendOkContent(HttpServerConnection conn) throws IOException, HttpException {
    // send a 200 OK with the static content
    BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
    BasicHttpEntity entity = new BasicHttpEntity();
    byte[] message = content.getBytes(Charset.forName("UTF-8"));
    entity.setContent(new ByteArrayInputStream(message));
    entity.setContentLength(message.length);
    response.setEntity(entity);/*from w  ww  . j  a v  a 2 s.  c  o m*/

    // force Content-Length header so the client doesn't expect us to close the connection to end the response
    response.addHeader("Content-Length", String.valueOf(message.length));

    conn.sendResponseHeader(response);
    conn.sendResponseEntity(response);
    conn.flush();
    logger.log(FINE, "Sent 200 OK");
}

From source file:com.tmount.business.cloopen.restAPI.RestAPI.java

/**
* @brief            ?/*from ww w . j av  a 2s.c o  m*/
* @param accountSid      ?
* @param authToken      ?
* @param appId         id
* @param mediaName      ??
* @param mediaTxt      
* @param playTimes      
* @param to            ???
*/
public String LandingCalls(String accountSid, String authToken, String appId, String mediaName,
        String playTimes, String to) throws NoSuchAlgorithmException, KeyManagementException {
    // ?
    String result = "";
    // HttpClient
    CcopHttpClient chc = new CcopHttpClient();
    DefaultHttpClient httpclient = chc.registerSSL("sandboxapp.cloopen.com", "TLS", 8883, "https");
    try {
        // URL
        String timestamp = DateUtil.dateToStr(new Date(), DateUtil.DATE_TIME_NO_SLASH);
        // md5(Id +? + )
        String sig = accountSid + authToken + timestamp;
        // MD5
        EncryptUtil eu = new EncryptUtil();
        String signature = eu.md5Digest(sig);

        String url = (new StringBuilder(hostname)).append(":").append(port).append("/").append(softVer)
                .append("/Accounts/").append(accountSid).append("/Calls/LandingCalls?sig=").append(signature)
                .toString();
        // HttpPost
        HttpPost httppost = new HttpPost(url);
        setHttpHeader(httppost);
        String src = accountSid + ":" + timestamp;
        // base64(Id + ? +)
        String auth = eu.base64Encoder(src);
        httppost.setHeader("Authorization", auth);

        String body = null;
        if (!JsonUtils.JSON_ENABLE) {
            // Body
            StringBuilder bodyBuilder = new StringBuilder(
                    "<?xml version='1.0' encoding='utf-8'?><LandingCall>");
            bodyBuilder.append("<appId>").append(appId).append("</appId>");
            bodyBuilder.append("<mediaName>").append(mediaName).append("</mediaName>");
            bodyBuilder.append("<playTimes>").append(playTimes).append("</playTimes>");
            bodyBuilder.append("<to>").append(to).append("</to></LandingCall>");
            body = bodyBuilder.toString();

        } else {
            LandingCallsRequest landingCallsRequest = new LandingCallsRequest(appId, mediaName, playTimes, to);
            Gson gson = new Gson();
            body = gson.toJson(landingCallsRequest);
        }
        System.out.println("CreateSubAccount : request _" + body);

        BasicHttpEntity requestBody = new BasicHttpEntity();
        requestBody.setContent(new ByteArrayInputStream(body.getBytes("UTF-8")));
        requestBody.setContentLength(body.getBytes("UTF-8").length);
        httppost.setEntity(requestBody);

        // 
        HttpResponse response = httpclient.execute(httppost);

        // ???
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            result = EntityUtils.toString(entity, "UTF-8");
        }
        // ?HTTP??
        EntityUtils.consume(entity);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // 
        httpclient.getConnectionManager().shutdown();
    }
    return result;
}

From source file:org.apache.hadoop.gateway.dispatch.CappedBufferHttpEntityTest.java

@Test
public void testIsChunked() throws Exception {
    String input = "0123456789";
    BasicHttpEntity basic;/*from   w  ww.  j av a2  s  . com*/
    CappedBufferHttpEntity replay;

    basic = new BasicHttpEntity();
    basic.setContent(new ByteArrayInputStream(input.getBytes(UTF8)));
    replay = new CappedBufferHttpEntity(basic, 5);
    assertThat(replay.isChunked(), is(false));

    basic = new BasicHttpEntity();
    basic.setContent(new ByteArrayInputStream(input.getBytes(UTF8)));
    basic.setChunked(true);
    replay = new CappedBufferHttpEntity(basic, 5);
    assertThat(replay.isChunked(), is(true));
}

From source file:org.apache.hadoop.gateway.dispatch.PartiallyRepeatableHttpEntityTest.java

@Test
public void testIsRepeatable() throws Exception {
    String text = "0123456789";
    BasicHttpEntity basic;/*from w w w .  j  ava 2  s .com*/
    PartiallyRepeatableHttpEntity replay;

    basic = new BasicHttpEntity();
    basic.setContent(new ByteArrayInputStream(text.getBytes(UTF8)));
    replay = new PartiallyRepeatableHttpEntity(basic);
    assertThat(replay.isRepeatable(), is(true));

    basic = new BasicHttpEntity();
    basic.setContent(new ByteArrayInputStream(text.getBytes(UTF8)));
    BufferedHttpEntity buffered = new BufferedHttpEntity(basic);
    replay = new PartiallyRepeatableHttpEntity(buffered);
    assertThat(replay.isRepeatable(), is(true));
}

From source file:org.apache.hadoop.gateway.dispatch.CappedBufferHttpEntityTest.java

@Test
public void testGetContentLength() throws Exception {
    String input = "0123456789";
    BasicHttpEntity basic;// www . j  av  a2s .  c  o m
    CappedBufferHttpEntity replay;

    basic = new BasicHttpEntity();
    basic.setContent(new ByteArrayInputStream(input.getBytes(UTF8)));
    replay = new CappedBufferHttpEntity(basic, 5);
    assertThat(replay.getContentLength(), is(-1L));

    basic = new BasicHttpEntity();
    basic.setContent(new ByteArrayInputStream(input.getBytes(UTF8)));
    basic.setContentLength(input.length());
    replay = new CappedBufferHttpEntity(basic, 5);
    assertThat(replay.getContentLength(), is(10L));
}

From source file:org.apache.hadoop.gateway.dispatch.PartiallyRepeatableHttpEntityTest.java

@Test
public void testIsChunked() throws Exception {
    String input = "0123456789";
    BasicHttpEntity basic;/*w  ww  .  ja  v  a  2  s  .c o  m*/
    PartiallyRepeatableHttpEntity replay;

    basic = new BasicHttpEntity();
    basic.setContent(new ByteArrayInputStream(input.getBytes(UTF8)));
    replay = new PartiallyRepeatableHttpEntity(basic, 5);
    assertThat(replay.isChunked(), is(false));

    basic = new BasicHttpEntity();
    basic.setContent(new ByteArrayInputStream(input.getBytes(UTF8)));
    basic.setChunked(true);
    replay = new PartiallyRepeatableHttpEntity(basic, 5);
    assertThat(replay.isChunked(), is(true));
}

From source file:org.apache.hadoop.gateway.dispatch.CappedBufferHttpEntityTest.java

@Test
public void testGetContentType() throws Exception {
    String input = "0123456789";
    BasicHttpEntity basic;/*w w  w.  j a v a 2s.com*/
    CappedBufferHttpEntity replay;

    basic = new BasicHttpEntity();
    basic.setContent(new ByteArrayInputStream(input.getBytes(UTF8)));
    replay = new CappedBufferHttpEntity(basic, 5);
    assertThat(replay.getContentType(), nullValue());

    basic = new BasicHttpEntity();
    basic.setContent(new ByteArrayInputStream(input.getBytes(UTF8)));
    basic.setContentType(ContentType.APPLICATION_JSON.getMimeType());
    replay = new CappedBufferHttpEntity(basic, 5);
    assertThat(replay.getContentType().getValue(), is("application/json"));
}