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:de.yaio.services.metaextract.server.controller.MetaExtractFacade.java

/**
 * extract metadata from the url//w  w w.  j a v a2s. co  m
 *
 * @param url                   url to read and extract the metadata from
 * @param lang                  language-key to support extraction
 * @return                      extracted metadata from the different extractors
 * @throws IOException          possible errors while reading and copying tmpFiles
 * @throws ExtractorException   possible errors while running extractor
 */
public ExtractedMetaData extractMetaData(final String url, final String lang)
        throws PermissionException, IOException, IOExceptionWithCause, ExtractorException {
    // check url
    getNetFirewall().throwExceptionIfNotAllowed(url);

    // call url
    HttpResponse response = HttpUtils.callGetUrlPure(url, "anonymous", "anonymous", null);
    HttpEntity entity = response.getEntity();

    // check response
    int retCode = response.getStatusLine().getStatusCode();
    if (retCode < 200 || retCode > 299) {
        throw new IOExceptionWithCause("cant read from url", url, new IOException("illegal reponse:"
                + response.getStatusLine() + " for url:" + url + " response:" + EntityUtils.toString(entity)));
    }

    // generate filename from contenttype
    String mimetype = EntityUtils.getContentMimeType(entity);
    MimeTypes allMimeTypes = MimeTypes.getDefaultMimeTypes();
    String fileName;
    try {
        MimeType mime = allMimeTypes.forName(mimetype);
        fileName = "download." + mime.getExtension();

    } catch (MimeTypeException ex) {
        fileName = "download.unknown";
    }

    InputStream input = new ByteArrayInputStream(EntityUtils.toByteArray(entity));
    LOGGER.info("done download data for url:" + url + " with mime:" + mimetype + " to " + fileName);

    return extractMetaData(input, fileName, lang);
}

From source file:gov.jrj.library.http.BinaryHttpResponseHandler.java

@Override
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 ww.  j  a va2s.  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.rackspacecloud.client.cloudfiles.FilesResponse.java

/**
 * Get the body of the response as a byte array
 *
 * @return The body of the response./* w  w  w  .ja v a2  s . c  om*/
 * @throws IOException if an error occurs reading the input stream
 */
public byte[] getResponseBody() throws IOException {
    return EntityUtils.toByteArray(entity);
}

From source file:jp.tonyu.soytext2.file.Comm.java

private Scriptable response2Scriptable(HttpResponse response, Object responseType) throws IOException {
    HttpEntity resEntity = response.getEntity();
    Scriptable res = new BlankScriptableObject();
    if (RT_TEXT.equals(responseType)) {
        String resStr = EntityUtils.toString(resEntity);
        Scriptables.put(res, "content", resStr);
        Log.d(this, resStr);
    } else {//www. j av  a  2 s  .c o  m
        ByteArrayBinData data = new ByteArrayBinData(EntityUtils.toByteArray(resEntity));
        Scriptables.put(res, "content", data);
    }
    Scriptables.put(res, "statusLine", response.getStatusLine());
    Log.d(this, response.getStatusLine());
    if (resEntity != null) {
        Log.d(this, "Response content length: " + resEntity.getContentLength());
    }
    EntityUtils.consume(resEntity);
    return res;
}

From source file:com.traffic.common.utils.http.HttpClientUtils.java

/**
 * map? post//from w w  w  . j a  v a2 s  . co  m
 * @param url
 * @param mapParam
 * @return
 */
public static String httpPost_JSONObject(String url, JSONObject reqParam) {
    logger.debug("httpPost URL [" + url + "] start ");
    logger.debug("httpPost param :" + reqParam.toJSONString());
    HttpClient httpclient = null;
    HttpPost httpPost = null;
    HttpResponse response = null;
    HttpEntity entity = null;
    String result = "";

    try {
        //         httpclient = HttpConnectionManager.getHttpClient();
        httpclient = new DefaultHttpClient();
        httpclient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");
        httpPost = new HttpPost(url);
        // ???
        for (Entry<String, String> entry : headers.entrySet()) {
            httpPost.setHeader(entry.getKey(), entry.getValue());
        }

        // ?    
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        for (String key : reqParam.keySet()) {
            formparams.add(new BasicNameValuePair(key, (String) reqParam.get(key)));
        }
        UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
        httpPost.setEntity(uefEntity);

        // 
        HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 40000);
        // ?
        HttpConnectionParams.setSoTimeout(httpPost.getParams(), 200000);
        response = httpclient.execute(httpPost);
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            logger.error("HttpStatus ERROR" + "Method failed: " + response.getStatusLine());
            return "";
        } else {
            entity = response.getEntity();
            if (null != entity) {
                byte[] bytes = EntityUtils.toByteArray(entity);
                result = new String(bytes, "UTF-8");
            } else {
                logger.error("httpPost URL [" + url + "],httpEntity is null.");
            }
            logger.debug("httpPost response:" + result);
            return result;
        }
    } catch (Exception e) {
        logger.error("httpPost URL [" + url + "] error, ", e);
        return "";
    }
}

From source file:com.flipkart.bifrost.http.HttpCallCommand.java

private T makeHttpCall() throws Exception {
    HttpUriRequest httpRequest = generateRequestObject();
    if (null != headers) {
        for (Map.Entry<String, String> header : headers.entrySet()) {
            httpRequest.setHeader(header.getKey(), header.getValue());
        }//from  www  .j  a  va  2 s  .com
    }
    CloseableHttpResponse response = null;
    try {
        response = client.execute(httpRequest);
        int statusCode = response.getStatusLine().getStatusCode();
        HttpEntity entity = response.getEntity();
        byte[] responseBytes = (null != entity) ? EntityUtils.toByteArray(entity) : null;
        if (statusCode != successStatus) {
            throw new BifrostException(BifrostException.ErrorCode.SUCCESS_STATUS_MISMATCH,
                    String.format("Call status mismatch. Expected %d Got %d", successStatus, statusCode));
        }
        return mapper.readValue(responseBytes, new TypeReference<T>() {
        });
    } finally {
        if (null != response) {
            try {
                response.close();
            } catch (IOException e) {
                logger.error("Error closing HTTP response: ", e);
            }
        }
    }
}

From source file:zz.pseas.ghost.client.GhostClient.java

public byte[] getBytes(String url) {

    HttpGet get = new HttpGet(url);
    DownloadUtil.setUserAgent(get);//from   w w  w  .j  a v a2  s  .  com
    CloseableHttpResponse resp;
    try {
        resp = client.execute(get, context);
        byte[] bs = EntityUtils.toByteArray(resp.getEntity());
        EntityUtils.consume(resp.getEntity());
        get.releaseConnection();
        return bs;
    } catch (IOException e) {
        return null;
    }

}

From source file:com.digitalpebble.storm.crawler.protocol.httpclient.HttpProtocol.java

@Override
public ProtocolResponse handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
    int status = response.getStatusLine().getStatusCode();
    Metadata metadata = new Metadata();
    HeaderIterator iter = response.headerIterator();
    while (iter.hasNext()) {
        Header header = iter.nextHeader();
        metadata.addValue(header.getName().toLowerCase(Locale.ROOT), header.getValue());
    }/*www .j  a v  a  2  s. c  om*/
    // TODO find a way of limiting by maxContent
    byte[] bytes = EntityUtils.toByteArray(response.getEntity());
    return new ProtocolResponse(bytes, status, metadata);
}

From source file:org.apache.calcite.avatica.remote.AvaticaCommonsHttpClientSpnegoImpl.java

@Override
public byte[] send(byte[] request) {
    HttpClientContext context = HttpClientContext.create();

    context.setTargetHost(host);//w ww .  j a  v a 2  s . c  om
    context.setCredentialsProvider(credentialsProvider);
    context.setAuthSchemeRegistry(authRegistry);
    context.setAuthCache(authCache);

    ByteArrayEntity entity = new ByteArrayEntity(request, ContentType.APPLICATION_OCTET_STREAM);

    // Create the client with the AuthSchemeRegistry and manager
    HttpPost post = new HttpPost(toURI(url));
    post.setEntity(entity);

    try (CloseableHttpResponse response = client.execute(post, context)) {
        final int statusCode = response.getStatusLine().getStatusCode();
        if (HttpURLConnection.HTTP_OK == statusCode || HttpURLConnection.HTTP_INTERNAL_ERROR == statusCode) {
            return EntityUtils.toByteArray(response.getEntity());
        }

        throw new RuntimeException("Failed to execute HTTP Request, got HTTP/" + statusCode);
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        LOG.debug("Failed to execute HTTP request", e);
        throw new RuntimeException(e);
    }
}

From source file:ro.teodorbaciu.commons.client.ws.BaseWsMethods.java

/**
 * Executes an Http get and returns the contents of the call as a byte array.
 *//*from   w  w  w  .ja  va  2  s .  com*/
public byte[] executeHttpGet(String uri) throws Exception {

    String targetUrl = webserviceHost + uri;
    HttpGet get = new HttpGet(targetUrl);

    HttpResponse response = wsHttpClient.execute(get);
    int statusCode = response.getStatusLine().getStatusCode();
    String reasonPhrase = response.getStatusLine().getReasonPhrase();

    if (statusCode == 401) {

        get.abort();// release resources
        throw new AuthorizationRequiredException("You need to authenticate first !");

    } else if (statusCode == 412) {

        get.abort();
        throw new InvalidWsParamsException(
                "Error calling http get because of invalid parameters ! Server returned: " + reasonPhrase);

    } else if (statusCode == 403) {

        get.abort();
        throw new OperationForbiddenException("The server refused to execute the specified operation ! "
                + "Server returned: " + reasonPhrase);

    } else if (statusCode != 200) {

        get.abort();// release resources
        throw new RuntimeException(
                "Could not get http response ! Status:" + statusCode + " Server returned: " + reasonPhrase);

    }

    // get the response content
    HttpEntity entity = response.getEntity();
    if (entity == null) {
        throw new RuntimeException("Could not get http response ! Server returned: " + reasonPhrase);
    }

    return EntityUtils.toByteArray(entity);

}