Example usage for org.apache.http.client.cache Resource length

List of usage examples for org.apache.http.client.cache Resource length

Introduction

In this page you can find the example usage for org.apache.http.client.cache Resource length.

Prototype

long length();

Source Link

Document

Returns the length in bytes of the response body.

Usage

From source file:org.apache.http.impl.client.cache.BasicHttpCache.java

boolean isIncompleteResponse(HttpResponse resp, Resource resource) {
    int status = resp.getStatusLine().getStatusCode();
    if (status != HttpStatus.SC_OK && status != HttpStatus.SC_PARTIAL_CONTENT) {
        return false;
    }//from w  w w  .j  av  a2 s  .  c om
    Header hdr = resp.getFirstHeader("Content-Length");
    if (hdr == null)
        return false;
    int contentLength;
    try {
        contentLength = Integer.parseInt(hdr.getValue());
    } catch (NumberFormatException nfe) {
        return false;
    }
    return (resource.length() < contentLength);
}

From source file:org.apache.http.impl.client.cache.BasicHttpCache.java

HttpResponse generateIncompleteResponseError(HttpResponse response, Resource resource) {
    int contentLength = Integer.parseInt(response.getFirstHeader("Content-Length").getValue());
    HttpResponse error = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_BAD_GATEWAY, "Bad Gateway");
    error.setHeader("Content-Type", "text/plain;charset=UTF-8");
    String msg = String.format(
            "Received incomplete response " + "with Content-Length %d but actual body length %d", contentLength,
            resource.length());
    byte[] msgBytes = msg.getBytes();
    error.setHeader("Content-Length", Integer.toString(msgBytes.length));
    error.setEntity(new ByteArrayEntity(msgBytes));
    return error;
}