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.alading.library.util.http.BinaryHttpResponseHandler.java

@Override
protected void sendResponseMessage(HttpResponse response, String requset) {
    StatusLine status = response.getStatusLine();
    Header[] contentTypeHeaders = response.getHeaders("Content-Type");
    byte[] responseBody = null;
    if (contentTypeHeaders.length != 1) {
        //malformed/ambiguous HTTP Header, ABORT!
        sendFailureMessage(new HttpResponseException(status.getStatusCode(),
                "None, or more than one, Content-Type Header found!"), responseBody, requset);
        return;/*from   w ww .  j a v a2  s.com*/
    }
    Header contentTypeHeader = contentTypeHeaders[0];
    boolean foundAllowedContentType = false;
    for (String anAllowedContentType : mAllowedContentTypes) {
        if (Pattern.matches(anAllowedContentType, contentTypeHeader.getValue())) {
            foundAllowedContentType = true;
        }
    }
    if (!foundAllowedContentType) {
        //Content-Type not in allowed list, ABORT!
        sendFailureMessage(new HttpResponseException(status.getStatusCode(), "Content-Type not allowed!"),
                responseBody, requset);
        return;
    }
    try {
        HttpEntity entity = null;
        HttpEntity temp = response.getEntity();
        if (temp != null) {
            entity = new BufferedHttpEntity(temp);
        }
        responseBody = EntityUtils.toByteArray(entity);
    } catch (IOException e) {
        sendFailureMessage(e, (byte[]) null, requset);
    }

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

From source file:com.loopj.android.http.ImageHttpResponseHandler.java

void sendResponseMessage(HttpResponse response) {
    StatusLine status = response.getStatusLine();
    Header[] contentTypeHeaders = response.getHeaders("Content-Type");
    byte[] responseBody = null;
    if (contentTypeHeaders.length != 1) {
        //malformed/ambiguous HTTP Header, ABORT!
        sendFailureMessage(new HttpResponseException(status.getStatusCode(),
                "None, or more than one, Content-Type Header found!"), responseBody);
        return;/*from   w w w.  j  ava  2 s. c  o  m*/
    }
    Header contentTypeHeader = contentTypeHeaders[0];
    boolean foundAllowedContentType = false;
    for (String anAllowedContentType : mAllowedContentTypes) {
        if (anAllowedContentType.equals(contentTypeHeader.getValue())) {
            foundAllowedContentType = true;
        }
    }
    if (!foundAllowedContentType) {
        //Content-Type not in allowed list, ABORT!
        sendFailureMessage(new HttpResponseException(status.getStatusCode(), "Content-Type not allowed!"),
                responseBody);
        return;
    }
    try {
        HttpEntity entity = null;
        HttpEntity temp = response.getEntity();
        if (temp != null) {
            entity = new BufferedHttpEntity(temp);
        }
        responseBody = EntityUtils.toByteArray(entity);
    } catch (IOException e) {
        sendFailureMessage(e, (byte[]) null);
    }

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

From source file:com.wallpaper.core.loopj.android.http.AsyncHttpResponseHandler.java

void sendResponseMessage(HttpResponse response) {
    StatusLine status = response.getStatusLine();
    String responseBody = null;/*  w  w  w .  j ava2s .c  o m*/
    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(responseBody);
    }
}

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;/* www.  j a  v  a2 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.orange.common.android.network.http.CustomBinaryHttpResponseHandler.java

void sendResponseMessage(HttpResponse response) {
    StatusLine status = response.getStatusLine();
    Header[] contentTypeHeaders = response.getHeaders("Content-Type");
    byte[] responseBody = null;
    //        if(contentTypeHeaders.length != 1) {
    //            //malformed/ambiguous HTTP Header, ABORT!
    //            sendFailureMessage(new HttpResponseException(status.getStatusCode(), "None, or more than one, Content-Type Header found!"), responseBody);
    //            return;
    //        }//from   w  w  w .  jav a  2  s.c o m
    //        Header contentTypeHeader = contentTypeHeaders[0];

    //        boolean foundAllowedContentType = false;
    //        for(String anAllowedContentType : mAllowedContentTypes) {
    //            if(anAllowedContentType.equals(contentTypeHeader.getValue())) {
    //                foundAllowedContentType = true;
    //            }
    //        }
    //        boolean foundAllowedContentType = true;   // disable this feature
    //        if(!foundAllowedContentType) {
    //            //Content-Type not in allowed list, ABORT!
    //            sendFailureMessage(new HttpResponseException(status.getStatusCode(), "Content-Type not allowed!"), responseBody);
    //            return;
    //        }
    try {

        HttpEntity entity = null;
        HttpEntity temp = response.getEntity();
        if (temp != null) {
            entity = new BufferedHttpEntity(temp);
        }
        responseBody = EntityUtils.toByteArray(entity);

        /*
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        InputStream inputStream = new BufferedInputStream(response.getEntity().getContent());
        int offset = -1;
        byte[] buffer = new byte[10240];
        while ((offset = inputStream.read(buffer, 0, 10240)) != -1) {                            
           output.write(buffer, 0, offset);
           output.flush();
                
           if (isStop){
         Log.d(TAG, "<http> detect stop flag, stop receive data");
         break;
           }
        }
                
        responseBody = output.toByteArray();
                
        output.close();
           inputStream.close();
                   
           if (isStop){
         return;
           }           
           */

    } catch (IOException e) {
        sendFailureMessage(e, (byte[]) null);
    } catch (Exception e) {
        sendFailureMessage(e, (byte[]) null);
    }

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

From source file:io.uploader.drive.drive.largefile.DriveResumableUpload.java

public boolean checkMD5(String md5) throws IOException {
    logger.info("Querying metadata of completed upload...");

    Preconditions.checkState(org.apache.commons.lang3.StringUtils.isNotEmpty(md5));

    CloseableHttpClient httpclient = null;
    CloseableHttpResponse response = null;
    try {/*from  w ww .  j av a2s .c  o  m*/
        httpclient = getHttpClient();
        BasicHttpRequest httpreq = new BasicHttpRequest("PUT", location);
        httpreq.addHeader("Authorization", auth.getAuthHeader());
        httpreq.addHeader("Content-Length", "0");
        httpreq.addHeader("Content-Range", "bytes */" + getFileSizeString());
        response = httpclient.execute(URIUtils.extractHost(uri), httpreq);
        BufferedHttpEntity entity = new BufferedHttpEntity(response.getEntity());
        EntityUtils.consume(response.getEntity());
        String retSrc = EntityUtils.toString(entity);
        String driveMd5 = null;
        if (useOldApi) {
            // Old API will return XML!
            JSONObject result = XML.toJSONObject(retSrc);
            logger.info("id          : "
                    + result.getJSONObject("entry").getString("gd:resourceId").replace("file:", ""));
            logger.info("title       : " + result.getJSONObject("entry").getString("title"));
            logger.info("link        : "
                    + result.getJSONObject("entry").getJSONArray("link").getJSONObject(0).getString("href"));
            logger.info("md5Checksum : " + result.getJSONObject("entry").getString("docs:md5Checksum"));
            driveMd5 = result.getJSONObject("entry").getString("docs:md5Checksum");
        } else {
            JSONObject result = new JSONObject(retSrc);
            logger.info("id          : " + result.getString("id"));
            logger.info("title       : " + result.getString("title"));
            logger.info("link        : " + result.getString("webContentLink"));
            logger.info("md5Checksum : " + result.getString("md5Checksum"));
            driveMd5 = result.getString("md5Checksum");
        }
        // verify the consistency of the md5 values
        return md5.equals(driveMd5);
    } finally {
        if (response != null) {
            response.close();
        }
        if (httpclient != null) {
            httpclient.close();
        }
    }
}

From source file:com.okta.sdk.impl.http.httpclient.HttpClientRequestFactory.java

/**
 * Utility function for creating a new BufferedEntity and wrapping any errors
 * as a RestException.//from   w  w w .  j a  v  a  2 s .c o m
 *
 * @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 RestException("Unable to create HTTP entity: " + e.getMessage(), e);
    }
}

From source file:mobi.dlys.android.core.net.http.handler.BinaryHttpResponseHandler.java

@Override
public void sendResponseMessage(HttpResponse response, HttpUriRequest request) {
    StatusLine status = response.getStatusLine();
    Header[] contentTypeHeaders = response.getHeaders("Content-Type");
    byte[] responseBody = null;
    if (contentTypeHeaders.length != 1) {
        // malformed/ambiguous HTTP Header, ABORT!
        sendFailureMessage(new HttpResponseException(status.getStatusCode(),
                "None, or more than one, Content-Type Header found!"), responseBody);
        return;/*from  w  ww .  j ava 2 s  . c om*/
    }
    // Header contentTypeHeader = contentTypeHeaders[0];
    // boolean foundAllowedContentType = false;
    // for (String anAllowedContentType : mAllowedContentTypes)
    // {
    // String value = contentTypeHeader.getValue();
    // if (Pattern.matches(anAllowedContentType, value))
    // {
    // foundAllowedContentType = true;
    // }
    // }
    // foundAllowedContentType = true;
    // if (!foundAllowedContentType)
    // {
    // //Content-Type not in allowed list, ABORT!
    // sendFailureMessage(new HttpResponseException(status.getStatusCode(),
    // "Content-Type not allowed!"),
    // responseBody);
    // return;
    // }
    try {
        HttpEntity entity = null;
        HttpEntity temp = response.getEntity();
        if (temp != null) {
            entity = new BufferedHttpEntity(temp);
        }
        responseBody = EntityUtils.toByteArray(entity);
    } catch (IOException e) {
        sendFailureMessage(e, (byte[]) null);
    }

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

From source file:com.cndatacom.ordersystem.manager.AsyncHttpResponseHandler.java

void sendResponseMessage(HttpResponse response) {
    StatusLine status = response.getStatusLine();
    String responseBody = null;//from  w ww .j ava2 s.c  o  m
    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(), responseBody);
    }
}