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:impalapayapis.ApiRequestBank.java

public String doPost() {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    StringEntity entity;/*from  w w  w.j a v a 2  s.  c  o m*/
    String out = "";

    try {
        entity = new StringEntity(params);
        HttpPost httppost = new HttpPost(url);
        httppost.setEntity(entity);
        HttpResponse response = httpclient.execute(httppost);

        // for debugging
        System.out.println(entity.getContentType());
        System.out.println(entity.getContentLength());
        System.out.println(EntityUtils.toString(entity));
        System.out.println(EntityUtils.toByteArray(entity).length);

        //System.out.println(           "----------------------------------------");

        System.out.println(response.getStatusLine());
        System.out.println(url);

        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        out = rd.readLine();
        JsonElement root = new JsonParser().parse(out);

        String specificvalue = root.getAsJsonObject().get("replace with key of the value to retrieve")
                .getAsString();
        System.out.println(specificvalue);

        /**
         * String line = ""; while ((line = rd.readLine()) != null) {
         * //System.out.println(line); }
        *
         */

    } catch (UnsupportedEncodingException e) {
        logger.error("UnsupportedEncodingException for URL: '" + url + "'");

        logger.error(ExceptionUtils.getStackTrace(e));
    } catch (ClientProtocolException e) {
        logger.error("ClientProtocolException for URL: '" + url + "'");
        logger.error(ExceptionUtils.getStackTrace(e));
    } catch (IOException e) {
        logger.error("IOException for URL: '" + url + "'");
        logger.error(ExceptionUtils.getStackTrace(e));
    }
    return out;
}

From source file:com.kixeye.relax.HttpResponse.java

/**
 * @param result//from w w  w  . j a va  2s  .co m
 * @param serDe
 * @param objectType
 * @throws IOException
 */
protected HttpResponse(org.apache.http.HttpResponse result, RestClientSerDe serDe, Class<T> objectType)
        throws IOException {
    this.statusCode = result.getStatusLine().getStatusCode();

    Header[] headers = result.getAllHeaders();
    if (headers != null) {
        for (Header header : headers) {
            List<String> headerValues = this.headers.get(header.getName());
            if (headerValues == null) {
                headerValues = new ArrayList<>();
                this.headers.put(header.getName(), headerValues);
            }
            headerValues.add(header.getValue());
        }
    }

    HttpEntity entity = result.getEntity();
    byte[] data = null;

    if (entity != null) {
        data = EntityUtils.toByteArray(entity);
        EntityUtils.consumeQuietly(entity);
    }

    Header contentTypeHeader = result.getFirstHeader("Content-Type");

    if (!Void.class.equals(objectType)) {
        this.body = new SerializedObject<>(serDe,
                contentTypeHeader != null ? contentTypeHeader.getValue() : null, data, objectType);
    } else {
        this.body = null;
    }
}

From source file:com.strato.hidrive.api.bll.file.GetThumbnailGateway.java

@Override
protected ResponseHandler<String> createResponseHandler() {
    super.createResponseHandler();
    return new ResponseHandler<String>() {
        @Override/*from  w  ww .j a  v  a  2s  .  c  om*/
        public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == HttpStatus.SC_OK) {
                HttpEntity entity = response.getEntity();
                byte[] bytes = EntityUtils.toByteArray(entity);
                bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
            }
            return DONE_JSON_RESULT;
        }
    };
}

From source file:cn.jachohx.crawler.Page.java

/**
 * Loads the content of this page from a fetched
 * HttpEntity.//w w  w .j a  v a2 s .c o m
 */
public void load(HttpEntity entity) throws Exception {

    contentType = null;
    Header type = entity.getContentType();
    if (type != null) {
        contentType = type.getValue();
    }

    contentEncoding = null;
    Header encoding = entity.getContentEncoding();
    if (encoding != null) {
        contentEncoding = encoding.getValue();
    }

    contentCharset = EntityUtils.getContentCharSet(entity);

    contentData = EntityUtils.toByteArray(entity);

}

From source file:org.jasig.portal.security.provider.saml.HttpRequestPostprocessor.java

/**
 * This method triggers delegated SAML authentication when it is requested
 * by the WSP.  After a successful authentication, this method attempts
 * to redirect the request to the location identified by the WSP at the end
 * of the delegated SAML authentication.  To do that, this method changes the
 * HTTP status to a 302 and sets the Location header accordingly.
 * /*w  w  w .  j  av a  2s  . c  o m*/
 * @see org.apache.http.HttpResponseInterceptor#process(org.apache.http.HttpResponse, org.apache.http.protocol.HttpContext)
 */
public void process(HttpResponse res, HttpContext ctx) throws HttpException, IOException {
    Header actionHeader = res.getFirstHeader("SoapAction");

    if (actionHeader != null
            && actionHeader.getValue().equalsIgnoreCase("http://www.oasis-open.org/committees/security")) {
        HttpEntity entity = res.getEntity();
        HttpResponse authnResponse = samlService.authenticate(samlSession, EntityUtils.toByteArray(entity));

        /*
         * The following logic may require enhancing in the future.  It attempts to copy the result
         * of the delegated SAML authentication to the original HttpResponse, which triggered
         * the authentication.  This will most often result in a redirection to the originally
         * requested resource.  However, the WSP may be replaying the HTTP POST form data,
         * which may not result in redirection.
         * 
         * Basically, we need to make the original request return what the successful authentication
         * result returned.
         */
        // Remove original headers
        Header[] headers = res.getAllHeaders();
        for (Header header : headers) {
            res.removeHeader(header);
        }
        // Replace with the new headers
        headers = authnResponse.getAllHeaders();
        for (Header header : headers) {
            res.addHeader(header);
        }

        res.setEntity(authnResponse.getEntity());
        res.setStatusLine(authnResponse.getStatusLine());
        res.setLocale(authnResponse.getLocale());
    }

}

From source file:com.seajas.search.contender.http.SizeRestrictedResponseHandler.java

/**
 * {@inheritDoc}//w ww  .j  av a  2s  .  c om
 */
@Override
public SizeRestrictedHttpResponse handleResponse(final HttpResponse response) throws IOException {
    try {
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                if (maximumContentLength > 0 && entity.getContentLength() > maximumContentLength) {
                    logger.error("The given stream is too large to process - " + entity.getContentLength()
                            + " > " + maximumContentLength);

                    return null;
                }

                return new SizeRestrictedHttpResponse(response.getLastHeader("Content-Type"),
                        EntityUtils.toByteArray(entity));
            } else
                return null;
        } else {
            logger.error("Could not retrieve the given stream" + (uri != null ? " for URI '" + uri + "'" : "")
                    + ", status = " + response.getStatusLine());

            EntityUtils.consume(response.getEntity());

            return null;
        }
    } catch (IOException e) {
        EntityUtils.consume(response.getEntity());

        throw e;
    }
}

From source file:org.pentaho.di.core.util.HttpClientUtil.java

public static byte[] responseToByteArray(HttpResponse response) throws IOException {
    return EntityUtils.toByteArray(response.getEntity());
}

From source file:org.apache.hadoop.gateway.shell.BasicResponse.java

public byte[] getBytes() throws IOException {
    if (!consumed && bytes == null) {
        bytes = EntityUtils.toByteArray(response.getEntity());
        consumed = true;/*from  ww w  .jav a2 s . c o  m*/
    }
    return bytes;
}

From source file:com.geekandroid.sdk.pay.utils.Util.java

public static byte[] httpGet(final String url) {
    if (url == null || url.length() == 0) {
        Log.e(TAG, "httpGet, url is null");
        return null;
    }//ww  w . ja v a2  s  . c om
    HttpClient httpClient = getNewHttpClient();
    HttpGet httpGet = new HttpGet(url);

    try {
        HttpResponse resp = httpClient.execute(httpGet);
        if (resp.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            Log.e(TAG, "httpGet fail, status code = " + resp.getStatusLine().getStatusCode());
            return null;
        }

        return EntityUtils.toByteArray(resp.getEntity());

    } catch (Exception e) {
        Log.e(TAG, "httpGet exception, e = " + e.getMessage());
        e.printStackTrace();
        return null;
    }
}

From source file:de.micromata.genome.tpsb.httpClient.HttpClientTestBuilder.java

public HttpClientTestBuilder executeMethod(HttpRequestBase method) {
    initHttpClient();//w w w .j  a  v a 2s.  co  m
    try {
        final CloseableHttpResponse response = httpClient.execute(method);
        lastResponseBody = EntityUtils.toByteArray(response.getEntity());
        lastHttpStatus = response.getStatusLine().getStatusCode();
    } catch (RuntimeException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
    return getBuilder();
}