Example usage for org.apache.http.client.cache HeaderConstants AGE

List of usage examples for org.apache.http.client.cache HeaderConstants AGE

Introduction

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

Prototype

String AGE

To view the source code for org.apache.http.client.cache HeaderConstants AGE.

Click Source Link

Usage

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

/**
 * Determines if an HttpResponse can be cached.
 *
 * @param httpMethod What type of request was this, a GET, PUT, other?
 * @param response The origin response//from w ww. ja  v  a 2  s . c  o m
 * @return <code>true</code> if response is cacheable
 */
public boolean isResponseCacheable(String httpMethod, HttpResponse response) {
    boolean cacheable = false;

    if (!HeaderConstants.GET_METHOD.equals(httpMethod)) {
        log.debug("Response was not cacheable.");
        return false;
    }

    switch (response.getStatusLine().getStatusCode()) {
    case HttpStatus.SC_OK:
    case HttpStatus.SC_NON_AUTHORITATIVE_INFORMATION:
    case HttpStatus.SC_MULTIPLE_CHOICES:
    case HttpStatus.SC_MOVED_PERMANENTLY:
    case HttpStatus.SC_GONE:
        // these response codes MAY be cached
        cacheable = true;
        log.debug("Response was cacheable");
        break;
    case HttpStatus.SC_PARTIAL_CONTENT:
        // we don't implement Range requests and hence are not
        // allowed to cache partial content
        log.debug("Response was not cacheable (Partial Content)");
        return cacheable;

    default:
        // If the status code is not one of the recognized
        // available codes in HttpStatus Don't Cache
        log.debug("Response was not cacheable (Unknown Status code)");
        return cacheable;
    }

    Header contentLength = response.getFirstHeader(HTTP.CONTENT_LEN);
    if (contentLength != null) {
        int contentLengthValue = Integer.parseInt(contentLength.getValue());
        if (contentLengthValue > this.maxObjectSizeBytes)
            return false;
    }

    Header[] ageHeaders = response.getHeaders(HeaderConstants.AGE);

    if (ageHeaders.length > 1)
        return false;

    Header[] expiresHeaders = response.getHeaders(HeaderConstants.EXPIRES);

    if (expiresHeaders.length > 1)
        return false;

    Header[] dateHeaders = response.getHeaders(HTTP.DATE_HEADER);

    if (dateHeaders.length != 1)
        return false;

    try {
        DateUtils.parseDate(dateHeaders[0].getValue());
    } catch (DateParseException dpe) {
        return false;
    }

    for (Header varyHdr : response.getHeaders(HeaderConstants.VARY)) {
        for (HeaderElement elem : varyHdr.getElements()) {
            if ("*".equals(elem.getName())) {
                return false;
            }
        }
    }

    if (isExplicitlyNonCacheable(response))
        return false;

    return (cacheable || isExplicitlyCacheable(response));
}