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

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

Introduction

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

Prototype

String GET_METHOD

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

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/* w ww. ja  v a2  s.co 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.CacheInvalidator.java

private boolean notGetOrHeadRequest(String method) {
    return !(HeaderConstants.GET_METHOD.equals(method) || HeaderConstants.HEAD_METHOD.equals(method));
}

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   ww w.  j  a 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));
}