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:com.jkoolcloud.jesl.net.http.apache.HttpMessageUtils.java

/**
 * Obtain content from HTTP message//  w  w w  .j a  v  a 2s. co m
 *
 * @param message HTTP message
 * @return bytes associated with HTTP content
 * @throws IOException if error reading message content
 */
public static byte[] getContentBytes(HttpMessage message) throws IOException {
    HttpEntity entity = getEntity(message);
    if (entity == null)
        return null;
    return EntityUtils.toByteArray(entity);
}

From source file:com.github.horrorho.inflatabledonkey.responsehandler.ByteArrayResponseHandler.java

@Override
public byte[] handleEntity(HttpEntity entity) throws IOException {
    return EntityUtils.toByteArray(entity);
}

From source file:de.catma.document.source.contenthandler.HttpResponseHandler.java

public byte[] handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
    Header[] headers = response.getHeaders("Content-Type");
    if (headers.length > 0) {
        contentType = headers[0].getValue();
    }/*from w  ww. j  a v a  2s  . co m*/
    // TODO: Content-Encoding, hanle compressed content

    StatusLine statusLine = response.getStatusLine();
    HttpEntity entity = response.getEntity();
    if (statusLine.getStatusCode() >= 300) {
        try {
            EntityUtils.toByteArray(entity);
        } catch (IOException notOfInterest) {
        }
        throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
    }
    return entity == null ? null : EntityUtils.toByteArray(entity);
}

From source file:org.mobicents.xcap.client.impl.XcapEntityImpl.java

/**
 * @param httpEntity//from   w  w w.ja va2s.c o m
 * @throws IOException
 */
@SuppressWarnings("deprecation")
public XcapEntityImpl(HttpEntity httpEntity) throws IOException {
    this.httpEntity = httpEntity;
    rawContent = EntityUtils.toByteArray(httpEntity);
    httpEntity.consumeContent();
}

From source file:org.jwifisd.httpclient.ByteResponseHandler.java

@Override
public byte[] handleResponse(HttpResponse response) throws IOException {
    int status = response.getStatusLine().getStatusCode();
    if (status >= START_HTTP_OK_RESPONSE_RANGE && status < END_HTTP_OK_RESPONSE_RANGE) {
        HttpEntity entity = response.getEntity();
        return entity != null ? EntityUtils.toByteArray(entity) : null;
    } else {/*from   w ww .  j a v  a  2s .c o  m*/
        throw new ClientProtocolException("Unexpected response status: " + status);
    }
}

From source file:com.flipkart.foxtrot.client.handlers.DummyEventHandler.java

@Override
public void handle(HttpRequest request, HttpResponse response, HttpContext context)
        throws HttpException, IOException {
    try {//from w ww . j  ava2  s .  c  o  m
        BasicHttpEntityEnclosingRequest post = (BasicHttpEntityEnclosingRequest) request;
        List<Document> documents = mapper.readValue(EntityUtils.toByteArray(post.getEntity()),
                new TypeReference<List<Document>>() {
                });
        counter.addAndGet(documents.size());
        logger.info("Received {} documents.", documents.size());
    } catch (Exception e) {
        logger.error("Error: ", e);
    }
    response.setStatusCode(201);
}

From source file:org.esigate.HttpErrorPage.java

private static HttpEntity toMemoryEntity(HttpEntity httpEntity) {
    if (httpEntity == null) {
        return null;
    }//from w w w .j  a v  a  2  s  .  c  o m
    HttpEntity memoryEntity;
    try {
        byte[] content = EntityUtils.toByteArray(httpEntity);
        ByteArrayEntity byteArrayEntity = new ByteArrayEntity(content, ContentType.get(httpEntity));
        Header contentEncoding = httpEntity.getContentEncoding();
        if (contentEncoding != null) {
            byteArrayEntity.setContentEncoding(contentEncoding);
        }
        memoryEntity = byteArrayEntity;
    } catch (IOException e) {
        StringBuilderWriter out = new StringBuilderWriter(Parameters.DEFAULT_BUFFER_SIZE);
        PrintWriter pw = new PrintWriter(out);
        e.printStackTrace(pw);
        pw.close();
        memoryEntity = new StringEntity(out.toString(), ContentType.getOrDefault(httpEntity));
    }
    return memoryEntity;
}

From source file:com.k42b3.aletheia.protocol.http.Response.java

public Response(HttpResponse response) throws Exception {
    super(response.getEntity() != null ? EntityUtils.toByteArray(response.getEntity()) : null);

    this.response = response;

    // get response line
    this.setLine(response.getStatusLine().toString());

    // set headers
    LinkedList<Header> header = new LinkedList<Header>();
    Header[] headers = response.getAllHeaders();

    for (int i = 0; i < headers.length; i++) {
        header.add(headers[i]);//from ww w.j  av  a2 s  . com
    }

    this.setHeaders(header);

    // read body
    if (this.content != null) {
        Charset charset = this.detectCharset();
        String body = "";

        if (charset != null) {
            body = new String(this.getContent(), charset);
        } else {
            if (this.isBinary()) {
                body = this.toHexdump();
            } else {
                body = new String(this.getContent(), Charset.forName("UTF-8"));
            }
        }

        this.setBody(body);
    } else {
        this.setBody("");
    }
}

From source file:com.github.horrorho.liquiddonkey.http.ResponseHandlerFactory.java

/**
 * Returns an entity to byte array response handler.
 *
 * @return an entity to byte array response handler, not null
 *///w ww  . ja va2s .c om
public static ResponseHandler<byte[]> toByteArray() {
    return new AbstractResponseHandler<byte[]>() {

        @Override
        public byte[] handleEntity(HttpEntity entity) throws IOException {
            return EntityUtils.toByteArray(entity);
        }
    };
}

From source file:neembuu.httpserver.VFSHandler.java

@Override
public void handle(final HttpRequest request, final HttpResponse response, final HttpContext context)
        throws HttpException, IOException {

    String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH);
    if (!method.equals("GET") && !method.equals("HEAD") && !method.equals("POST")) {
        throw new MethodNotSupportedException(method + " method not supported");
    }//ww w  .j a va 2  s . c om
    String target = request.getRequestLine().getUri();

    if (request instanceof HttpEntityEnclosingRequest) {
        HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
        byte[] entityContent = EntityUtils.toByteArray(entity);
        System.out.println("Incoming entity content (bytes): " + entityContent.length);
    }

    String filePath = URLDecoder.decode(target, "UTF-8");
    FileAttributesProvider fap = fs.open(filePath.split("/"));
    if (fap == null) {
        response.setStatusCode(HttpStatus.SC_NOT_FOUND);
        StringEntity entity = new StringEntity(
                "<html><body><h1>File" + filePath + " not found</h1></body></html>",
                ContentType.create("text/html", "UTF-8"));
        response.setEntity(entity);
        System.out.println("File " + filePath + " not found");

    } else if (fap.getFileType() != FileType.FILE || !(fap instanceof AbstractFile)) {

        response.setStatusCode(HttpStatus.SC_FORBIDDEN);
        StringEntity entity = new StringEntity("<html><body><h1>Access denied</h1></body></html>",
                ContentType.create("text/html", "UTF-8"));
        response.setEntity(entity);
        System.out.println("Cannot read file " + filePath + " fap=" + fap);

    } else {
        long offset = Utils.standardOffsetResponse(request, response, fap.getFileSize());
        response.setStatusCode(HttpStatus.SC_OK);
        VFileEntity body = new VFileEntity((AbstractFile) fap, offset);
        response.setEntity(body);
        System.out.println("Serving file " + filePath);
    }
}