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

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

Introduction

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

Prototype

String CACHE_CONTROL

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

Click Source Link

Usage

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

/**
 * Determines if an HttpRequest can be served from the cache.
 *
 * @param request//from w ww. j  a  v  a2s .  c o m
 *            an HttpRequest
 * @return boolean Is it possible to serve this request from cache
 */
public boolean isServableFromCache(HttpRequest request) {
    String method = request.getRequestLine().getMethod();

    ProtocolVersion pv = request.getRequestLine().getProtocolVersion();
    if (HttpVersion.HTTP_1_1.compareToVersion(pv) != 0) {
        log.debug("Request was not serveable from cache");
        return false;
    }

    if (!method.equals(HeaderConstants.GET_METHOD)) {
        log.debug("Request was not serveable from cache");
        return false;
    }

    if (request.getHeaders(HeaderConstants.PRAGMA).length > 0) {
        log.debug("Request was not serveable from cache");
        return false;
    }

    Header[] cacheControlHeaders = request.getHeaders(HeaderConstants.CACHE_CONTROL);
    for (Header cacheControl : cacheControlHeaders) {
        for (HeaderElement cacheControlElement : cacheControl.getElements()) {
            if (HeaderConstants.CACHE_CONTROL_NO_STORE.equalsIgnoreCase(cacheControlElement.getName())) {
                log.debug("Request was not serveable from Cache");
                return false;
            }

            if (HeaderConstants.CACHE_CONTROL_NO_CACHE.equalsIgnoreCase(cacheControlElement.getName())) {
                log.debug("Request was not serveable from cache");
                return false;
            }
        }
    }

    log.debug("Request was serveable from cache");
    return true;
}

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}/* ww  w. ja va2  s .  c  o m*/
 *
 * @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 mayCallBackend(final HttpRequestWrapper request) {
    for (final Header h : request.getHeaders(HeaderConstants.CACHE_CONTROL)) {
        for (final HeaderElement elt : h.getElements()) {
            if ("only-if-cached".equals(elt.getName())) {
                log.trace("Request marked only-if-cached");
                return false;
            }/*from  www.j av a 2s  . c  om*/
        }
    }
    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 w w.  ja va2 s.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 mayCallBackend(final HttpRequest request) {
    for (final Header h : request.getHeaders(HeaderConstants.CACHE_CONTROL)) {
        for (final HeaderElement elt : h.getElements()) {
            if ("only-if-cached".equals(elt.getName())) {
                this.log.debug("Request marked only-if-cached");
                return false;
            }//  w w w.  j  a v  a 2 s.  co  m
        }
    }
    return true;
}

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;
                    }/*from w ww  .j av a  2  s.c  om*/
                } 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.ConditionalRequestBuilder.java

/**
 * When a {@link HttpCacheEntry} is stale but 'might' be used as a response
 * to an {@link HttpRequest} we will attempt to revalidate the entry with
 * the origin.  Build the origin {@link HttpRequest} here and return it.
 *
 * @param request the original request from the caller
 * @param cacheEntry the entry that needs to be revalidated
 * @return the wrapped request//from w  ww. j  av  a 2  s  .  c o  m
 * @throws ProtocolException when I am unable to build a new origin request.
 */
public HttpRequest buildConditionalRequest(HttpRequest request, HttpCacheEntry cacheEntry)
        throws ProtocolException {
    RequestWrapper wrapperRequest = new RequestWrapper(request);
    wrapperRequest.resetHeaders();
    Header eTag = cacheEntry.getFirstHeader(HeaderConstants.ETAG);
    if (eTag != null) {
        wrapperRequest.setHeader(HeaderConstants.IF_NONE_MATCH, eTag.getValue());
    }
    Header lastModified = cacheEntry.getFirstHeader(HeaderConstants.LAST_MODIFIED);
    if (lastModified != null) {
        wrapperRequest.setHeader(HeaderConstants.IF_MODIFIED_SINCE, lastModified.getValue());
    }
    boolean mustRevalidate = false;
    for (Header h : cacheEntry.getHeaders(HeaderConstants.CACHE_CONTROL)) {
        for (HeaderElement elt : h.getElements()) {
            if (HeaderConstants.CACHE_CONTROL_MUST_REVALIDATE.equalsIgnoreCase(elt.getName())
                    || HeaderConstants.CACHE_CONTROL_PROXY_REVALIDATE.equalsIgnoreCase(elt.getName())) {
                mustRevalidate = true;
                break;
            }
        }
    }
    if (mustRevalidate) {
        wrapperRequest.addHeader("Cache-Control", "max-age=0");
    }
    return wrapperRequest;

}

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

protected boolean isExplicitlyNonCacheable(HttpResponse response) {
    Header[] cacheControlHeaders = response.getHeaders(HeaderConstants.CACHE_CONTROL);
    for (Header header : cacheControlHeaders) {
        for (HeaderElement elem : header.getElements()) {
            if (HeaderConstants.CACHE_CONTROL_NO_STORE.equals(elem.getName())
                    || HeaderConstants.CACHE_CONTROL_NO_CACHE.equals(elem.getName())
                    || (sharedCache && "private".equals(elem.getName()))) {
                return true;
            }// w  w w.j  a  v  a 2 s.co m
        }
    }
    return false;
}

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

protected boolean hasCacheControlParameterFrom(HttpMessage msg, String[] params) {
    Header[] cacheControlHeaders = msg.getHeaders(HeaderConstants.CACHE_CONTROL);
    for (Header header : cacheControlHeaders) {
        for (HeaderElement elem : header.getElements()) {
            for (String param : params) {
                if (param.equalsIgnoreCase(elem.getName())) {
                    return true;
                }/*from  w w w. j a va  2 s.  c  om*/
            }
        }
    }
    return false;
}