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

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

Introduction

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

Prototype

String CACHE_CONTROL_MAX_AGE

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

Click Source Link

Usage

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

/**
 * Determine if I can utilize a {@link HttpCacheEntry} to respond to the given
 * {@link HttpRequest}//from  w ww  . java2s . com
 *
 * @param host
 *            {@link HttpHost}
 * @param request
 *            {@link HttpRequest}
 * @param entry
 *            {@link HttpCacheEntry}
 * @return boolean yes/no answer
 */
public boolean canCachedResponseBeUsed(HttpHost host, HttpRequest request, HttpCacheEntry entry, Date now) {
    if (!isFreshEnough(entry, request, now)) {
        log.debug("Cache entry was not fresh enough");
        return false;
    }

    if (!validityStrategy.contentLengthHeaderMatchesActualLength(entry)) {
        log.debug("Cache entry Content-Length and header information do not match");
        return false;
    }

    if (hasUnsupportedConditionalHeaders(request)) {
        log.debug("Request contained conditional headers we don't handle");
        return false;
    }

    if (isConditional(request) && !allConditionalsMatch(request, entry, now)) {
        return false;
    }

    for (Header ccHdr : request.getHeaders(HeaderConstants.CACHE_CONTROL)) {
        for (HeaderElement elt : ccHdr.getElements()) {
            if (HeaderConstants.CACHE_CONTROL_NO_CACHE.equals(elt.getName())) {
                log.debug("Response contained NO CACHE directive, cache was not suitable");
                return false;
            }

            if (HeaderConstants.CACHE_CONTROL_NO_STORE.equals(elt.getName())) {
                log.debug("Response contained NO STORE directive, cache was not suitable");
                return false;
            }

            if (HeaderConstants.CACHE_CONTROL_MAX_AGE.equals(elt.getName())) {
                try {
                    int maxage = Integer.parseInt(elt.getValue());
                    if (validityStrategy.getCurrentAgeSecs(entry, now) > maxage) {
                        log.debug("Response from cache was NOT suitable due to max age");
                        return false;
                    }
                } catch (NumberFormatException ex) {
                    // err conservatively
                    log.debug("Response from cache was malformed: " + ex.getMessage());
                    return false;
                }
            }

            if (HeaderConstants.CACHE_CONTROL_MAX_STALE.equals(elt.getName())) {
                try {
                    int maxstale = Integer.parseInt(elt.getValue());
                    if (validityStrategy.getFreshnessLifetimeSecs(entry) > maxstale) {
                        log.debug("Response from cache was not suitable due to Max stale freshness");
                        return false;
                    }
                } catch (NumberFormatException ex) {
                    // err conservatively
                    log.debug("Response from cache was malformed: " + ex.getMessage());
                    return false;
                }
            }

            if (HeaderConstants.CACHE_CONTROL_MIN_FRESH.equals(elt.getName())) {
                try {
                    long minfresh = Long.parseLong(elt.getValue());
                    if (minfresh < 0L)
                        return false;
                    long age = validityStrategy.getCurrentAgeSecs(entry, now);
                    long freshness = validityStrategy.getFreshnessLifetimeSecs(entry);
                    if (freshness - age < minfresh) {
                        log.debug("Response from cache was not suitable due to min fresh "
                                + "freshness requirement");
                        return false;
                    }
                } catch (NumberFormatException ex) {
                    // err conservatively
                    log.debug("Response from cache was malformed: " + ex.getMessage());
                    return false;
                }
            }
        }
    }

    log.debug("Response from cache was suitable");
    return true;
}

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

private boolean explicitFreshnessRequest(final HttpRequestWrapper request, final HttpCacheEntry entry,
        final Date now) {
    for (final Header h : request.getHeaders(HeaderConstants.CACHE_CONTROL)) {
        for (final HeaderElement elt : h.getElements()) {
            if (HeaderConstants.CACHE_CONTROL_MAX_STALE.equals(elt.getName())) {
                try {
                    final int maxstale = Integer.parseInt(elt.getValue());
                    final long age = validityPolicy.getCurrentAgeSecs(entry, now);
                    final long lifetime = validityPolicy.getFreshnessLifetimeSecs(entry);
                    if (age - lifetime > maxstale) {
                        return true;
                    }//from   w ww  .j  a  v  a 2s.c o m
                } catch (final NumberFormatException nfe) {
                    return true;
                }
            } else if (HeaderConstants.CACHE_CONTROL_MIN_FRESH.equals(elt.getName())
                    || HeaderConstants.CACHE_CONTROL_MAX_AGE.equals(elt.getName())) {
                return true;
            }
        }
    }
    return false;
}

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

private boolean explicitFreshnessRequest(final HttpRequest request, final HttpCacheEntry entry,
        final Date now) {
    for (final Header h : request.getHeaders(HeaderConstants.CACHE_CONTROL)) {
        for (final HeaderElement elt : h.getElements()) {
            if (HeaderConstants.CACHE_CONTROL_MAX_STALE.equals(elt.getName())) {
                try {
                    final int maxstale = Integer.parseInt(elt.getValue());
                    final long age = this.validityPolicy.getCurrentAgeSecs(entry, now);
                    final long lifetime = this.validityPolicy.getFreshnessLifetimeSecs(entry);
                    if (age - lifetime > maxstale) {
                        return true;
                    }/*  ww  w  .j a  va  2  s.  co  m*/
                } catch (final NumberFormatException nfe) {
                    return true;
                }
            } else if (HeaderConstants.CACHE_CONTROL_MIN_FRESH.equals(elt.getName())
                    || HeaderConstants.CACHE_CONTROL_MAX_AGE.equals(elt.getName())) {
                return true;
            }
        }
    }
    return false;
}