Example usage for org.apache.http.util EntityUtils toByteArray

List of usage examples for org.apache.http.util EntityUtils toByteArray

Introduction

In this page you can find the example usage for org.apache.http.util EntityUtils toByteArray.

Prototype

public static byte[] toByteArray(HttpEntity httpEntity) throws IOException 

Source Link

Usage

From source file:ch.iterate.openstack.swift.Response.java

/**
 * Get the body of the response as a byte array
 *
 * @return The body of the response.//from   w  w  w . ja va  2 s . co  m
 * @throws IOException If an error occurs reading the input stream
 */
public byte[] getResponseBody() throws IOException {
    return EntityUtils.toByteArray(response.getEntity());
}

From source file:com.duanlei.simplenet.base.Response.java

/** Reads the contents of HttpEntity into a byte[]. */
private byte[] entityToBytes(HttpEntity entity) {
    try {// www .  j av  a2  s .c om
        return EntityUtils.toByteArray(entity);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return new byte[0];
}

From source file:com.ajah.http.Http.java

/**
 * Return the body of the response as a byte array (such as an image).
 * //from   w ww.  j a  v a2s  .com
 * @see #getBytes(URI)
 * @param uri
 *            The URI to fetch.
 * @return The response body as a String.
 * @throws IOException
 *             If the response could not be completed.
 * @throws UnexpectedResponseCode
 *             If an unexpected/illegal response status is issued.
 * @throws NotFoundException
 *             If the resource could not be found at the URI (404).
 * @throws URISyntaxException
 */
public static byte[] getBytes(final String uri)
        throws IOException, NotFoundException, UnexpectedResponseCode, URISyntaxException {
    return EntityUtils.toByteArray(internalGet(new URI(uri)));
}

From source file:org.elasticsearch.test.rest.yaml.ClientYamlTestResponse.java

ClientYamlTestResponse(Response response) throws IOException {
    this.response = response;
    if (response.getEntity() != null) {
        String contentType = response.getHeader("Content-Type");
        this.bodyContentType = XContentType.fromMediaTypeOrFormat(contentType);
        try {//from  w  ww  .  j a va  2  s . c o  m
            byte[] bytes = EntityUtils.toByteArray(response.getEntity());
            //skip parsing if we got text back (e.g. if we called _cat apis)
            if (bodyContentType != null) {
                this.parsedResponse = ObjectPath.createFromXContent(bodyContentType.xContent(),
                        new BytesArray(bytes));
            }
            this.body = bytes;
        } catch (IOException e) {
            EntityUtils.consumeQuietly(response.getEntity());
            throw e;
        }
    } else {
        this.body = null;
        this.bodyContentType = null;
    }
}

From source file:nl.salp.warcraft4j.io.CachedHttpDataReader.java

/**
 * Read the data from a file (direct blocking http read).
 *
 * @param url The URL of the file to read.
 *
 * @return The file contents.//from  w ww . jav a  2 s .  c o m
 *
 * @throws DataReadingException When reading the file failed.
 */
private static byte[] getData(String url) throws DataReadingException {
    try (CloseableHttpClient httpClient = HttpClients.createDefault();
            CloseableHttpResponse response = httpClient.execute(new HttpGet(URI.create(url)))) {
        StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() > 300) {
            throw new DataReadingException(String.format("Error opening HTTP data reader for %s: error %d: %s",
                    url, statusLine.getStatusCode(), statusLine.getReasonPhrase()));
        }
        HttpEntity entity = response.getEntity();
        if (entity == null) {
            throw new DataReadingException(format("HTTP data reader received no response from for %s", url));
        }
        byte[] data = EntityUtils.toByteArray(entity);
        EntityUtils.consume(entity);
        return data;
    } catch (IOException e) {
        throw new DataReadingException(e);
    }
}

From source file:com.example.util.SecurityUtil.java

/**
 * //from ww  w  .j  av  a2  s. c o  m
 * @param entity
 * @return
 */
public static byte[] decryptHttpEntity(HttpEntity entity) {
    byte[] buffer = null;
    try {
        buffer = EntityUtils.toByteArray(entity);
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (buffer != null) {
        return new Crypter().decrypt(buffer, SECRET_KEY_HTTP);
    }
    return buffer;
}

From source file:com.francisli.processing.http.HttpResponse.java

HttpResponse(org.apache.http.HttpResponse response) throws IOException {
    this.response = response;

    StatusLine status = response.getStatusLine();
    this.statusCode = status.getStatusCode();
    this.statusMessage = status.getReasonPhrase();

    HttpEntity entity = response.getEntity();
    if (entity.getContentType() != null) {
        contentType = entity.getContentType().getValue();
    }/* w w  w  .ja  va2  s . c  om*/
    content = EntityUtils.toByteArray(entity);
    contentLength = content.length;
    contentCharSet = EntityUtils.getContentCharSet(entity);
    if (contentCharSet == null) {
        contentCharSet = HTTP.UTF_8;
    }
}

From source file:com.subgraph.vega.internal.http.requests.RepeatableStreamingEntity.java

RepeatableStreamingEntity(HttpEntity originalEntity) throws IOException {
    copyEntityProperties(originalEntity);
    if (originalEntity.isStreaming())
        setActiveInputStream(originalEntity.getContent(), originalEntity.getContentLength());
    else//from  w  w w  . ja v  a 2 s  . c o  m
        setActiveByteArrayEntity(EntityUtils.toByteArray(originalEntity));
}

From source file:org.jssec.android.https.privatecertificate.PrivateCertificateHttpsGet.java

@Override
protected Object doInBackground(String... params) {

    DefaultHttpClient client = new DefaultHttpClient();

    try {//from   w ww .j  a  v  a  2s .  co m
        // ?1 ???
        // assets??????????KeyStoreclient?
        KeyStore ks = KeyStoreUtil.getEmptyKeyStore();
        KeyStoreUtil.loadX509Certificate(ks, mContext.getResources().getAssets().open("cacert.crt"));
        Scheme sch = new Scheme("https", new SSLSocketFactory(ks), 443);
        client.getConnectionManager().getSchemeRegistry().register(sch);

        // ?2 URI?https://??
        // ?3 ???????
        HttpGet request = new HttpGet(params[0]);
        HttpResponse response = client.execute(request);
        checkResponse(response);

        // ?4 ?????????
        return EntityUtils.toByteArray(response.getEntity());
    } catch (SSLException e) {
        // ?5 SSLException?????????
        // ??????
        return e;
    } catch (Exception e) {
        return e;
    } finally {
        // ?HttpClientshutdown?
        client.getConnectionManager().shutdown();
    }
}

From source file:ru.algorithmist.jquant.infr.GAEHTTPClient.java

public String getContent(URI url) throws IOException {
    HttpResponse resp = client.execute(new HttpGet(url));
    HttpEntity entity = resp.getEntity();
    String encoding = EntityUtils.getContentCharSet(entity);

    logger.info("Character encoding  " + encoding);
    if (encoding == null) {
        encoding = "UTF-8";
    }//from  w  w  w .ja  v  a  2  s  .com
    return new String(EntityUtils.toByteArray(entity), encoding);
}