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

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

Introduction

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

Prototype

public BufferedHttpEntity(HttpEntity httpEntity) throws IOException 

Source Link

Usage

From source file:com.pyj.http.AsyncHttpResponseHandler.java

void sendResponseMessage(HttpResponse response, int reqType) {
    StatusLine status = response.getStatusLine();
    //      response.getAllHeaders()[1].get
    try {/*from  w w  w  . j  a  v a2  s .  c  om*/
        HttpEntity entity = null;
        HttpEntity temp = response.getEntity();

        if (temp != null) {
            entity = new BufferedHttpEntity(temp);
        }
        if (status.getStatusCode() >= 300) {
            String responseBody = EntityUtils.toString(entity, "UTF-8");
            sendFailureMessage(new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()),
                    responseBody, reqType);
        } else {
            //            byte[] result = EntityUtils.toByteArray(response.getEntity());
            sendSuccessMessage(status.getStatusCode(), response.getAllHeaders(), entity, reqType);
        }
    } catch (IOException e) {
        sendFailureMessage(e, (String) null, reqType);
    }
}

From source file:com.nloko.android.Utils.java

public static InputStream downloadPictureAsStream(String url) throws IOException {
    if (url == null) {
        throw new IllegalArgumentException("url");
    }//from   ww w  .j av  a 2s.c  o  m

    HttpClient httpclient = null;
    InputStream stream = null;
    try {
        HttpParams params = new BasicHttpParams();
        // Set the timeout in milliseconds until a connection is established.
        HttpConnectionParams.setConnectionTimeout(params, 5000);
        // Set the default socket timeout (SO_TIMEOUT) 
        // in milliseconds which is the timeout for waiting for data.
        HttpConnectionParams.setSoTimeout(params, 10000);

        httpclient = new DefaultHttpClient(params);
        HttpGet httpget = new HttpGet(url);

        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            BufferedHttpEntity buff = new BufferedHttpEntity(entity);
            stream = buff.getContent();
            //   image = BitmapFactory.decodeStream(stream);
        }
    } catch (IOException ex) {
        Log.e(null, android.util.Log.getStackTraceString(ex));
        throw ex;
    } finally {
        try {
            if (httpclient != null) {
                httpclient.getConnectionManager().shutdown();
            }
            if (stream != null) {
                stream.close();
            }
        } catch (Exception e) {
        }
    }

    return stream;
}

From source file:com.axibase.tsd.client.PlainSender.java

@Override
public void run() {
    if (messages == null) {
        messages = new LinkedBlockingQueue<String>();
    }//from  w w w  .j a  v  a2  s  . co m
    HttpPost httpPost = null;
    try {
        SslConfigurator sslConfig = SslConfigurator.newInstance().securityProtocol("SSL");
        connectionManager = HttpClient.createConnectionManager(clientConfiguration, sslConfig);
        connectionManager.setDefaultConnectionConfig(ConnectionConfig.custom().setBufferSize(SMALL).build());
        httpClient = HttpClients.custom().setConnectionManager(connectionManager).build();
        httpPost = new HttpPost(fullUrl());
        httpPost.setHeader("Authorization", "Basic " + DatatypeConverter.printBase64Binary(
                (clientConfiguration.getUsername() + ":" + clientConfiguration.getPassword()).getBytes()));
        httpPost.setEntity(new BufferedHttpEntity(this));
    } catch (Throwable e) {
        log.error("Could not create http client: ", e);
        latch.countDown();
        close();
        return;
    }
    try {
        log.info("Start writing commands to {}", fullUrl());
        state = SenderState.WORKING;
        latch.countDown();
        response = httpClient.execute(httpPost);
    } catch (Throwable e) {
        log.error("Could not execute HTTP POST: {}", httpPost, e);
    } finally {
        log.warn("Http post execution is finished, close sender");
        close();
    }
}

From source file:com.example.openfirechat.http.AsyncHttpResponseHandler.java

void sendResponseMessage(HttpResponse response) {
    StatusLine status = response.getStatusLine();
    String responseBody = null;//w ww  . j a  va2  s  .com
    try {
        HttpEntity entity = null;
        HttpEntity temp = response.getEntity();
        if (temp != null) {
            entity = new BufferedHttpEntity(temp);
        }
        responseBody = DecryptUtil.decrytEntity(entity);
    } catch (IOException e) {
        sendFailureMessage(e, null);
    }

    if (status.getStatusCode() >= 300) {
        sendFailureMessage(new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()),
                responseBody);
    } else {
        sendSuccessMessage(responseBody);
    }
}

From source file:com.ytrain.mutrain.utils.asynchttp.AsyncHttpResponseHandler.java

/**? to AsyncHttpRequest
 * //from  w  ww .ja  va  2s.c o  m
 */
void sendResponseMessage(HttpResponse response) {
    StatusLine status = response.getStatusLine();
    String responseBody = null;
    try {
        HttpEntity entity = null;
        HttpEntity temp = response.getEntity();
        if (temp != null) {
            entity = new BufferedHttpEntity(temp);
            responseBody = EntityUtils.toString(entity, "UTF-8");
        }
    } catch (IOException e) {
        sendFailureMessage(e, (String) null);
    }

    if (status.getStatusCode() >= 300) {
        sendFailureMessage(new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()),
                responseBody);
    } else {
        sendSuccessMessage(status.getStatusCode(), response.getAllHeaders(), responseBody);
    }
}

From source file:com.guster.brandon.library.webservice.RequestHandler.java

/**
 * Main method of sending HTTP Request to the server
 * @param rq Apache HTTP Base Request/*from w w w  .  ja v a  2  s  .  c o m*/
 * @return Response, if no response from the server or no internet connection,
 *          this object will return null
 * @throws IOException
 */
private Response send(HttpRequestBase rq) throws IOException {
    //Log.d("NISSAN", "WebService: Connecting to url... " + rq.getURI());
    HttpResponse httpResponse = httpClient.execute(rq);
    HttpEntity entity = httpResponse.getEntity();
    BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
    InputStream inputStream = bufHttpEntity.getContent();
    InputStream rawInputStream = bufHttpEntity.getContent();

    // variables to be put into Response
    int statusCode = httpResponse.getStatusLine().getStatusCode();
    String statusDesc = httpResponse.getStatusLine().getReasonPhrase();
    long contentLength = entity.getContentLength();

    // onPrepare the response
    Response responseObject = new Response();
    responseObject.setStatusCode(statusCode);
    responseObject.setStatusDesc(statusDesc);
    responseObject.setContentLength(contentLength);
    responseObject.setRawResponse(rawInputStream);
    responseObject.setContentEncoding(entity.getContentEncoding());
    responseObject.setContentType(entity.getContentType());
    responseObject.setUrl(rq.getURI().toString());

    return responseObject;
}

From source file:be.cytomine.client.HttpClient.java

public int post(byte[] data) throws IOException {
    log.debug("POST " + URL.toString());
    HttpPost httpPost = new HttpPost(URL.toString());
    if (isAuthByPrivateKey) {
        httpPost.setHeaders(headersArray);
    }//from   w ww. j ava  2  s .c o m
    log.debug("Post send :" + data.length);

    InputStreamEntity reqEntity = new InputStreamEntity(new ByteArrayInputStream(data), data.length);
    reqEntity.setContentType("binary/octet-stream");
    reqEntity.setChunked(false);
    BufferedHttpEntity myEntity = null;
    try {
        myEntity = new BufferedHttpEntity(reqEntity);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        log.error(e);
    }

    httpPost.setEntity(myEntity);
    response = client.execute(targetHost, httpPost, localcontext);
    return response.getStatusLine().getStatusCode();
}

From source file:com.corebase.android.framework.http.client.AsyncHttpResponseHandler.java

protected byte[] getByteArrayAndSendMessage(Context context, HttpResponse response) {
    StatusLine status = response.getStatusLine();
    byte[] responseBody = null;
    try {//  ww w.j av a 2 s. c  o m
        HttpEntity entity = null;
        HttpEntity temp = response.getEntity();
        if (temp != null) {
            entity = new BufferedHttpEntity(temp);
            // pcgroup?1MNULL
            long length = entity.getContentLength();
            if (length >= MAX) {
                return null;
            }
        }
        responseBody = EntityUtils.toByteArray(entity);

    } catch (IOException e) {
        sendFailureMessage(context, e, (String) null);
        return null;
    }

    Log.v("Http StatusCode", status.getStatusCode() + "");
    if (status.getStatusCode() >= 300) {
        sendFailureMessage(context, new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()),
                (String) null);
        return null;
    }
    if (null == responseBody) {
        // ????
        sendSuccessMessage(context, SUCCESS_STATUS_CODE, null);
        return null;
    }
    return responseBody;
}

From source file:com.sinacloud.scs.http.HttpRequestFactory.java

/**
 * Utility function for creating a new BufferedEntity and wrapping any errors
 * as an AmazonClientException./*from w w w .j av  a2  s.com*/
 *
 * @param entity
 *            The HTTP entity to wrap with a buffered HTTP entity.
 *
 * @return A new BufferedHttpEntity wrapping the specified entity.
 */
private HttpEntity newBufferedHttpEntity(HttpEntity entity) {
    try {
        return new BufferedHttpEntity(entity);
    } catch (IOException e) {
        throw new SCSClientException("Unable to create HTTP entity: " + e.getMessage(), e);
    }
}

From source file:com.gistlabs.mechanize.impl.MechanizeAgent.java

protected HttpResponse execute(final HttpClient client, final HttpRequestBase request) throws Exception {
    HttpContext context = new BasicHttpContext();
    HttpResponse response = requestChain.execute(request, context);

    if (context.getAttribute("Location") != null)
        response.setHeader(MECHANIZE_LOCATION, (String) context.getAttribute("Location"));

    response.setEntity(new BufferedHttpEntity(response.getEntity()));

    return response;
}