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.CappedBufferHttpEntityTest.java

@Test
public void testB_C1_PC_IB__C1_XC__C2_FC() throws IOException {
    String data = "0123456789";
    BasicHttpEntity basic;/*from   w  w w  . j  a v a2s  . c  om*/
    CappedBufferHttpEntity replay;
    InputStream stream;
    String text;

    basic = new BasicHttpEntity();
    basic.setContent(new ByteArrayInputStream(data.getBytes(UTF8)));
    replay = new CappedBufferHttpEntity(basic, 20);

    stream = replay.getContent();
    text = blockRead(stream, UTF8, 7, 2);
    assertThat(text, is("0123456"));
    stream.close();

    stream = replay.getContent();
    text = blockRead(stream, UTF8, -1, 7);
    assertThat(text, is("0123456789"));
}

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

/**
* @brief            ??/*from  w ww. j a  v a2  s . c om*/
* @param subAccountSid   ??   
* @param subToken      ??
* @param voipAccount   VoIP?
* @param from         ??
* @param to            ??
* @return Httppost       ???
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
public String CallBack(String subAccountSid, String subToken, String voipAccount, String from, 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 = subAccountSid + subToken + timestamp;
        // MD5
        EncryptUtil eu = new EncryptUtil();
        String signature = eu.md5Digest(sig);

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

        String body = null;
        if (!JsonUtils.JSON_ENABLE) {

            // Body
            body = (new StringBuilder("<?xml version='1.0' encoding='utf-8'?><CallBack>").append("<from>")
                    .append(from).append("</from>").append("<to>").append(to).append("</to></CallBack>")
                    .toString());
        } else {
            CallBackRequest callBackRequest = new CallBackRequest(subAccountSid, voipAccount, from, to);
            Gson gson = new Gson();
            body = gson.toJson(callBackRequest);
        }
        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.PartiallyRepeatableHttpEntityTest.java

@Test
public void testB_C1_PC_IB__C1_XC__C2_FC() throws IOException {
    String data = "0123456789";
    BasicHttpEntity basic;//from   ww  w.j  a  va2 s .com
    PartiallyRepeatableHttpEntity replay;
    InputStream stream;
    String text;

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

    stream = replay.getContent();
    text = blockRead(stream, UTF8, 7, 2);
    assertThat(text, is("0123456"));
    stream.close();

    stream = replay.getContent();
    text = blockRead(stream, UTF8, -1, 7);
    assertThat(text, is("0123456789"));
}

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

@Test
public void testS_C1_PC_OB__C1_XC__C2_AC__EE() throws IOException {
    String data = "0123456789";
    BasicHttpEntity basic;/*www  .  j  a v  a 2s. co m*/
    CappedBufferHttpEntity replay;
    InputStream stream;
    String text;

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

    try {
        stream = replay.getContent();
    } catch (IOException e) {
        // Expected.
    }
}

From source file:com.joyent.manta.client.multipart.ServerSideMultipartManagerTest.java

private ServerSideMultipartManager buildMockManager(final StatusLine statusLine, final String responseBody,
        final Consumer<CloseableHttpResponse> responseConfigCallback) throws IOException {
    final ConfigContext config = testConfigContext();
    final CloseableHttpResponse response = mock(CloseableHttpResponse.class);

    when(response.getStatusLine()).thenReturn(statusLine);

    if (responseConfigCallback != null) {
        responseConfigCallback.accept(response);
    }// w  w  w . java2s . co  m

    if (responseBody != null) {
        final BasicHttpEntity entity = new BasicHttpEntity();
        entity.setContent(IOUtils.toInputStream(responseBody, StandardCharsets.UTF_8));
        when(response.getEntity()).thenReturn(entity);
    }

    final MantaConnectionContext connectionContext = mock(MantaConnectionContext.class);
    final CloseableHttpClient fakeClient = new FakeCloseableHttpClient(response);
    when(connectionContext.getHttpClient()).thenReturn(fakeClient);

    final HttpHelper httpHelper = mock(HttpHelper.class);
    when(httpHelper.getConnectionContext()).thenReturn(connectionContext);
    when(httpHelper.getRequestFactory()).thenReturn(new MantaHttpRequestFactory(config.getMantaURL()));
    when(httpHelper.executeRequest(any(HttpUriRequest.class), any())).thenReturn(response);

    final MantaClient client = mock(MantaClient.class);
    return new ServerSideMultipartManager(config, httpHelper, client);
}

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

@Test
public void testS_C1_PC_OB__C1_XC__C2_AC__EE() throws IOException {
    String data = "0123456789";
    BasicHttpEntity basic;/*w  ww  . j  a va2 s. com*/
    PartiallyRepeatableHttpEntity replay;
    InputStream stream;
    String text;

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

    stream = replay.getContent();
    text = byteRead(stream, 7);
    assertThat(text, is("0123456"));
    stream.close();

    try {
        replay.getContent();
        fail("Expected IOException");
    } catch (IOException e) {
        // Expected.
    }
}

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

@Test
public void testB_C1_PC_OB__C1_XC__C2_AC__EE() throws IOException {
    String data = "0123456789";
    BasicHttpEntity basic;// w  w w  .j  ava  2s .  c  o  m
    CappedBufferHttpEntity replay;
    InputStream stream;
    String text;

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

    stream = replay.getContent();
    try {
        text = blockRead(stream, UTF8, 7, 2);
        fail("Expected IOException");
    } catch (IOException e) {
        // Expected.
    }
}

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

@Test
public void testB_C1_PC_OB__C1_XC__C2_AC__EE() throws IOException {
    String data = "0123456789";
    BasicHttpEntity basic;/*from ww  w.  j  a v  a  2  s . c  o m*/
    PartiallyRepeatableHttpEntity replay;
    InputStream stream;
    String text;

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

    stream = replay.getContent();
    text = blockRead(stream, UTF8, 7, 2);
    assertThat(text, is("0123456"));
    stream.close();

    try {
        replay.getContent();
        fail("Expected IOException");
    } catch (IOException e) {
        // Expected.
    }
}