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:com.example.chengcheng.network.httpstacks.HttpUrlConnStack.java

/**
 * HTTP????,??//ww w.  java  2s.com
 * @param connection 
 * @return HttpEntity
 */
private HttpEntity entityFromURLConnwction(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream = null;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException e) {
        e.printStackTrace();
        inputStream = connection.getErrorStream();
    }

    // TODO : GZIP 
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());

    return entity;
}

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

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

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

    String output;

    output = byteRead(replay.getContent(), -1);
    assertThat(output, is(data));

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

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

@Test
public void testS_C1_FC_OB__C2_AC__EE() throws Exception {
    String data = "0123456789";
    BasicHttpEntity basic;/* w w w. j  a va  2s.  c  o m*/
    CappedBufferHttpEntity replay;

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

    String output;

    try {
        output = byteRead(replay.getContent(), -1);
        fail("Expected IOException");
    } catch (IOException e) {
        // Expected.
    }

}

From source file:org.jenkinsci.plugins.relution_publisher.net.requests.ZeroCopyFileRequestProducer.java

@Override
public HttpRequest generateRequest() throws IOException, HttpException {
    final BasicHttpEntity entity = new BasicHttpEntity();

    entity.setContentLength(this.getContentLength());
    entity.setContentType(this.getContentType());
    entity.setChunked(false);//ww  w  .j a  v a2 s  . c om

    return this.createRequest(this.mRequest.getUri(), entity);
}

From source file:org.qucosa.camel.component.fcrepo3.Fcrepo3APIAccess.java

public void modifyDatastream(String pid, String dsid, Boolean versionable, InputStream content)
        throws IOException {
    HttpResponse response = null;//from  w  w w  .ja v  a 2 s  .  c  o m
    try {
        BasicHttpEntity entity = new BasicHttpEntity();
        entity.setContent(content);
        // Entity needs to be buffered because Fedora might reply in a way
        // forces resubmitting the entity
        BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(entity);

        URIBuilder uriBuilder = new URIBuilder(
                format(FEDORA_DATASTREAM_MODIFICATION_URI_PATTERN, host, port, pid, dsid));
        if (versionable != null) {
            uriBuilder.addParameter("versionable", String.valueOf(versionable));
        }
        URI uri = uriBuilder.build();

        HttpPut put = new HttpPut(uri);
        put.setEntity(bufferedHttpEntity);
        response = httpClient.execute(put);

        if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            throw new IOException(format("Cannot modify datastream %s of object %s. Server responded: %s", dsid,
                    pid, response.getStatusLine()));
        }
    } catch (URISyntaxException e) {
        throw new IOException("Cannot ", e);
    } finally {
        consumeResponseEntity(response);
    }
}

From source file:com.baasbox.android.HttpUrlConnectionClient.java

private HttpEntity asEntity(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream in;//from www  .  j a va  2s. c  o  m
    try {
        in = connection.getInputStream();
    } catch (IOException e) {
        in = connection.getErrorStream();
    }
    entity.setContent(in);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}

From source file:net.kungfoo.grizzly.proxy.impl.ConnectingHandler.java

/**
 * Triggered when the connection is ready to send an HTTP request.
 *
 * @see NHttpClientConnection/*  w ww. j a  va2 s.  co m*/
 *
 * @param conn HTTP connection that is ready to send an HTTP request
 */
public void requestReady(final NHttpClientConnection conn) {
    System.out.println(conn + " [proxy->origin] request ready");

    HttpContext context = conn.getContext();
    ProxyProcessingInfo proxyTask = (ProxyProcessingInfo) context.getAttribute(ProxyProcessingInfo.ATTRIB);

    // TODO: change it to ReentrantLock
    synchronized (proxyTask) {
        if (requestReadyValidateConnectionState(proxyTask))
            return;

        HttpRequest request = proxyTask.getRequest();
        if (request == null) {
            throw new IllegalStateException("HTTP request is null");
        }

        requestReadyCleanUpHeaders(request);

        HttpHost targetHost = proxyTask.getTarget();

        try {

            request.setParams(new DefaultedHttpParams(request.getParams(), this.params));

            // Pre-process HTTP request
            context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
            context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, targetHost);

            this.httpProcessor.process(request, context);
            // and send it to the origin server
            Request originalRequest = proxyTask.getOriginalRequest();
            int length = originalRequest.getContentLength();
            if (length > 0) {
                BasicHttpEntity httpEntity = new BasicHttpEntity();
                httpEntity.setContentLength(originalRequest.getContentLengthLong());
                /*
                          httpEntity.setContent(((InternalInputBuffer) originalRequest.getInputBuffer()).getInputStream());
                          ((BasicHttpEntityEnclosingRequest) request).setEntity(httpEntity);
                */
            }
            conn.submitRequest(request);
            // Update connection state
            proxyTask.setOriginState(ConnState.REQUEST_SENT);

            System.out.println(conn + " [proxy->origin] >> " + request.getRequestLine().toString());

        } catch (IOException ex) {
            shutdownConnection(conn);
        } catch (HttpException ex) {
            shutdownConnection(conn);
        }

    }
}

From source file:cn.geowind.takeout.verify.JsonRestApi.java

/**
 * @brief ??/*from   w w w .ja  v a 2 s  . c  o m*/
 * @param accountSid
 *            ?
 * @param authToken
 *            ?
 * @param appId
 *            id
 * @param to
 *            ?
 * @return HttpPost ???
 * @throws NoSuchAlgorithmException
 * @throws KeyManagementException
 */
public String sendTemplateSMS(String accountSid, String authToken, String appId, String to, String code)
        throws NoSuchAlgorithmException, KeyManagementException {
    String result = "";
    // HttpClient
    CcopHttpClient chc = new CcopHttpClient();
    DefaultHttpClient httpclient = chc.registerSSL("app.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);

        StringBuffer sb = new StringBuffer();
        String url = sb.append(SMS_BASE_URL).append(SMS_VERSION).append("/Accounts/").append(accountSid)
                .append(SMS).append(TEMPLATE_SMS).append("?sig=").append(signature).toString();
        // HttpPost
        HttpPost httppost = new HttpPost(url);
        setHttpHeader(httppost);
        String src = accountSid + ":" + timestamp;
        String auth = eu.base64Encoder(src);
        httppost.setHeader("Authorization", auth);// base64(Id + ? +
        // )
        JSONObject obj = new JSONObject();
        obj.put("to", to);
        obj.put("appId", appId);
        obj.put("templateId", SMS_TEMPLATE_ID);
        JSONArray replace = new JSONArray();
        replace.put(code);
        obj.put("datas", replace);
        String data = obj.toString();

        System.out.println("---------------------------- SendSMS for JSON begin----------------------------");
        System.out.println("data: " + data);

        BasicHttpEntity requestBody = new BasicHttpEntity();
        requestBody.setContent(new ByteArrayInputStream(data.getBytes("UTF-8")));
        requestBody.setContentLength(data.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:ste.web.http.api.BugFreeApiHandlerExec.java

@Test
public void content_type_is_json_if_not_already_set() throws Exception {
    response.setEntity(new BasicHttpEntity());
    handler.handle(request(TEST_URI_ITEMS1), response, context);
    then(response.getEntity().getContentType().getValue()).isEqualTo("application/json");
}