Example usage for org.springframework.http HttpHeaders getCacheControl

List of usage examples for org.springframework.http HttpHeaders getCacheControl

Introduction

In this page you can find the example usage for org.springframework.http HttpHeaders getCacheControl.

Prototype

@Nullable
public String getCacheControl() 

Source Link

Document

Return the value of the Cache-Control header.

Usage

From source file:cz.jirutka.spring.http.client.cache.internal.CacheControl.java

public static CacheControl parseCacheControl(HttpHeaders headers) {
    Assert.notNull(headers, "headers must not be null");

    return valueOf(headers.getCacheControl());
}

From source file:cz.jirutka.spring.http.client.cache.DefaultResponseExpirationResolver.java

private int parseMaxAgeHeader(HttpHeaders headers) {
    CacheControl cc = CacheControl.valueOf(headers.getCacheControl());
    return cc.getMaxAge(sharedCache);
}

From source file:net.eusashead.hateoas.hal.response.impl.HalResponseBuilderImplTest.java

private void assertHeaders(ResponseEntity<Representation> response) {
    HttpHeaders headers = response.getHeaders();
    Assert.assertNotNull(headers.getETag());
    Assert.assertEquals("W/\"123456789\"", headers.getETag());
    Assert.assertEquals(123456000, headers.getLastModified());
    Assert.assertNotNull(headers.getDate());
    Assert.assertNotNull(headers.getExpires());
    Assert.assertEquals(headers.getDate() + 1000000, headers.getExpires());
    Assert.assertNotNull(headers.getCacheControl());
    Assert.assertEquals("public, must-revalidate, proxy-revalidate, max-age=1000", headers.getCacheControl());

}

From source file:cz.jirutka.spring.http.client.cache.DefaultCachingPolicy.java

protected boolean isResponseCacheable(ClientHttpResponse response) {

    boolean cacheable = false;
    HttpHeaders headers = response.getHeaders();

    try {//from www  .ja  v  a 2  s .  co  m
        int status = response.getRawStatusCode();
        if (isImplicitlyCacheableStatus(status)) {
            cacheable = true; //MAY be cached

        } else if (isUncacheableStatus(status)) {
            log.trace("Response with status code {} is not cacheable", status);
            return false;
        }
    } catch (IOException ex) {
        throw new IllegalStateException(ex);
    }

    if (isExplicitlyNonCacheable(response)) {
        log.trace("Response with Cache-Control: '{}' is not cacheable", headers.getCacheControl());
        return false;
    }

    if (headers.getContentLength() > maxBodySizeBytes) {
        log.debug("Response with Content-Lenght {} > {} is not cacheable", headers.getContentLength(),
                maxBodySizeBytes);
        return false;
    }

    try {
        if (response.getHeaders().getDate() < 0) {
            log.debug("Response without a valid Date header is not cacheable");
            return false;
        }
    } catch (IllegalArgumentException ex) {
        return false;
    }

    // dunno how to properly handle Vary
    if (headers.containsKey("Vary")) {
        log.trace("Response with Vary header is not cacheable");
        return false;
    }

    return (cacheable || isExplicitlyCacheable(response));
}

From source file:org.springframework.cloud.stream.app.http.source.DefaultMixedCaseContentTypeHttpHeaderMapper.java

private Object getHttpHeader(HttpHeaders source, String name) {
    if (ACCEPT.equalsIgnoreCase(name)) {
        return source.getAccept();
    } else if (ACCEPT_CHARSET.equalsIgnoreCase(name)) {
        return source.getAcceptCharset();
    } else if (ALLOW.equalsIgnoreCase(name)) {
        return source.getAllow();
    } else if (CACHE_CONTROL.equalsIgnoreCase(name)) {
        String cacheControl = source.getCacheControl();
        return (StringUtils.hasText(cacheControl)) ? cacheControl : null;
    } else if (CONTENT_LENGTH.equalsIgnoreCase(name)) {
        long contentLength = source.getContentLength();
        return (contentLength > -1) ? contentLength : null;
    } else if (CONTENT_TYPE.equalsIgnoreCase(name)) {
        return source.getContentType();
    } else if (DATE.equalsIgnoreCase(name)) {
        long date = source.getDate();
        return (date > -1) ? date : null;
    } else if (ETAG.equalsIgnoreCase(name)) {
        String eTag = source.getETag();
        return (StringUtils.hasText(eTag)) ? eTag : null;
    } else if (EXPIRES.equalsIgnoreCase(name)) {
        try {/*www.j  a  v  a 2  s.  com*/
            long expires = source.getExpires();
            return (expires > -1) ? expires : null;
        } catch (Exception e) {
            if (logger.isDebugEnabled()) {
                logger.debug(e.getMessage());
            }
            // According to RFC 2616
            return null;
        }
    } else if (IF_NONE_MATCH.equalsIgnoreCase(name)) {
        return source.getIfNoneMatch();
    } else if (IF_MODIFIED_SINCE.equalsIgnoreCase(name)) {
        long modifiedSince = source.getIfModifiedSince();
        return (modifiedSince > -1) ? modifiedSince : null;
    } else if (IF_UNMODIFIED_SINCE.equalsIgnoreCase(name)) {
        String unmodifiedSince = source.getFirst(IF_UNMODIFIED_SINCE);
        return unmodifiedSince != null ? this.getFirstDate(unmodifiedSince, IF_UNMODIFIED_SINCE) : null;
    } else if (LAST_MODIFIED.equalsIgnoreCase(name)) {
        long lastModified = source.getLastModified();
        return (lastModified > -1) ? lastModified : null;
    } else if (LOCATION.equalsIgnoreCase(name)) {
        return source.getLocation();
    } else if (PRAGMA.equalsIgnoreCase(name)) {
        String pragma = source.getPragma();
        return (StringUtils.hasText(pragma)) ? pragma : null;
    }
    return source.get(name);
}

From source file:org.springframework.integration.http.support.DefaultHttpHeaderMapper.java

private Object getHttpHeader(HttpHeaders source, String name) {
    if (ACCEPT.equalsIgnoreCase(name)) {
        return source.getAccept();
    } else if (ACCEPT_CHARSET.equalsIgnoreCase(name)) {
        return source.getAcceptCharset();
    } else if (ALLOW.equalsIgnoreCase(name)) {
        return source.getAllow();
    } else if (CACHE_CONTROL.equalsIgnoreCase(name)) {
        String cacheControl = source.getCacheControl();
        return (StringUtils.hasText(cacheControl)) ? cacheControl : null;
    } else if (CONTENT_LENGTH.equalsIgnoreCase(name)) {
        long contentLength = source.getContentLength();
        return (contentLength > -1) ? contentLength : null;
    } else if (CONTENT_TYPE.equalsIgnoreCase(name)) {
        return source.getContentType();
    } else if (DATE.equalsIgnoreCase(name)) {
        long date = source.getDate();
        return (date > -1) ? date : null;
    } else if (ETAG.equalsIgnoreCase(name)) {
        String eTag = source.getETag();
        return (StringUtils.hasText(eTag)) ? eTag : null;
    } else if (EXPIRES.equalsIgnoreCase(name)) {
        try {/*  ww w. jav a  2  s  . co m*/
            long expires = source.getExpires();
            return (expires > -1) ? expires : null;
        } catch (Exception e) {
            if (logger.isDebugEnabled()) {
                logger.debug(e.getMessage());
            }
            // According to RFC 2616
            return null;
        }
    } else if (IF_NONE_MATCH.equalsIgnoreCase(name)) {
        return source.getIfNoneMatch();
    } else if (IF_UNMODIFIED_SINCE.equalsIgnoreCase(name)) {
        long unmodifiedSince = source.getIfNotModifiedSince();
        return (unmodifiedSince > -1) ? unmodifiedSince : null;
    } else if (LAST_MODIFIED.equalsIgnoreCase(name)) {
        long lastModified = source.getLastModified();
        return (lastModified > -1) ? lastModified : null;
    } else if (LOCATION.equalsIgnoreCase(name)) {
        return source.getLocation();
    } else if (PRAGMA.equalsIgnoreCase(name)) {
        String pragma = source.getPragma();
        return (StringUtils.hasText(pragma)) ? pragma : null;
    }
    return source.get(name);
}