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

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

Introduction

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

Prototype

String IF_MODIFIED_SINCE

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

Click Source Link

Usage

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

private boolean hasSupportedLastModifiedValidator(HttpRequest request) {
    return hasValidDateField(request, HeaderConstants.IF_MODIFIED_SINCE);
}

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

/**
 * Check entry against If-Modified-Since, if If-Modified-Since is in the future it is invalid as per
 * http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
 * @param request/*from w  w w  .  ja va 2s .  c om*/
 * @param entry
 * @param now
 * @return
 */
private boolean lastModifiedValidatorMatches(HttpRequest request, HttpCacheEntry entry, Date now) {
    Header lastModifiedHeader = entry.getFirstHeader(HeaderConstants.LAST_MODIFIED);
    Date lastModified = null;
    try {
        if (lastModifiedHeader != null) {
            lastModified = DateUtils.parseDate(lastModifiedHeader.getValue());
        }
    } catch (DateParseException dpe) {
        // nop
    }

    if (lastModified == null) {
        return false;
    }

    for (Header h : request.getHeaders(HeaderConstants.IF_MODIFIED_SINCE)) {
        try {
            Date ifModifiedSince = DateUtils.parseDate(h.getValue());
            if (ifModifiedSince.after(now) || lastModified.after(ifModifiedSince)) {
                return false;
            }
        } catch (DateParseException dpe) {
            // nop
        }
    }
    return true;
}

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

private CloseableHttpResponse generateCachedResponse(final HttpRequestWrapper request,
        final HttpContext context, final HttpCacheEntry entry, final Date now) {
    final CloseableHttpResponse cachedResponse;
    if (request.containsHeader(HeaderConstants.IF_NONE_MATCH)
            || request.containsHeader(HeaderConstants.IF_MODIFIED_SINCE)) {
        cachedResponse = responseGenerator.generateNotModifiedResponse(entry);
    } else {/* w w  w .  ja va  2 s.  c  o  m*/
        cachedResponse = responseGenerator.generateResponse(entry);
    }
    setResponseStatus(context, CacheResponseStatus.CACHE_HIT);
    if (validityPolicy.getStalenessSecs(entry, now) > 0L) {
        cachedResponse.addHeader(HeaderConstants.WARNING, "110 localhost \"Response is stale\"");
    }
    return cachedResponse;
}

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

private HttpResponse generateCachedResponse(final HttpRequestWrapper request,
        final HttpCacheContext clientContext, final HttpCacheEntry entry, final Date now) {
    final HttpResponse cachedResponse;
    if (request.containsHeader(HeaderConstants.IF_NONE_MATCH)
            || request.containsHeader(HeaderConstants.IF_MODIFIED_SINCE)) {
        cachedResponse = this.responseGenerator.generateNotModifiedResponse(entry);
    } else {/* w w  w .  j  a  v  a  2s . co m*/
        cachedResponse = this.responseGenerator.generateResponse(request, entry);
    }
    setResponseStatus(clientContext, CacheResponseStatus.CACHE_HIT);
    if (this.validityPolicy.getStalenessSecs(entry, now) > 0L) {
        cachedResponse.addHeader("Warning", "110 localhost \"Response is stale\"");
    }
    return cachedResponse;
}

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

private HttpResponse generateCachedResponse(HttpRequest request, HttpContext context, HttpCacheEntry entry,
        Date now) {/*  w w w.j av a  2s .c  o m*/
    final HttpResponse cachedResponse;
    if (request.containsHeader(HeaderConstants.IF_NONE_MATCH)
            || request.containsHeader(HeaderConstants.IF_MODIFIED_SINCE)) {
        cachedResponse = responseGenerator.generateNotModifiedResponse(entry);
    } else {
        cachedResponse = responseGenerator.generateResponse(entry);
    }
    setResponseStatus(context, CacheResponseStatus.CACHE_HIT);
    if (validityPolicy.getStalenessSecs(entry, now) > 0L) {
        cachedResponse.addHeader("Warning", "110 localhost \"Response is stale\"");
    }
    return cachedResponse;
}

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  www .  jav a 2s .  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;

}