Example usage for org.apache.http.client.cache HttpCacheEntry getFirstHeader

List of usage examples for org.apache.http.client.cache HttpCacheEntry getFirstHeader

Introduction

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

Prototype

public Header getFirstHeader(String name) 

Source Link

Document

Returns the first header from the origin response with the given name.

Usage

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

private boolean alreadyHaveNewerCacheEntry(HttpHost target, HttpRequest request, HttpResponse backendResponse) {
    HttpCacheEntry existing = null;
    try {// w w w.ja v  a  2s .  co m
        existing = responseCache.getCacheEntry(target, request);
    } catch (IOException ioe) {
        // nop
    }
    if (existing == null)
        return false;
    Header entryDateHeader = existing.getFirstHeader("Date");
    if (entryDateHeader == null)
        return false;
    Header responseDateHeader = backendResponse.getFirstHeader("Date");
    if (responseDateHeader == null)
        return false;
    try {
        Date entryDate = DateUtils.parseDate(entryDateHeader.getValue());
        Date responseDate = DateUtils.parseDate(responseDateHeader.getValue());
        return responseDate.before(entryDate);
    } catch (DateParseException e) {
    }
    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  w  w .ja  va2s.  c om*/
 * @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;

}