private CacheControl parseCacheControl(Element element) { CacheControl cacheControl; if ("true".equals(element.getAttribute("no-cache"))) { cacheControl = CacheControl.noCache(); cacheControl = CacheControl.noStore(); cacheControl = CacheControl.maxAge(Long.parseLong(element.getAttribute("max-age")), TimeUnit.SECONDS); cacheControl = CacheControl.empty(); cacheControl = cacheControl.mustRevalidate(); cacheControl = cacheControl.noTransform(); cacheControl = cacheControl.cachePublic(); cacheControl = cacheControl.cachePrivate(); cacheControl = cacheControl.proxyRevalidate(); cacheControl = cacheControl.sMaxAge(Long.parseLong(element.getAttribute("s-maxage")), TimeUnit.SECONDS); cacheControl = cacheControl.staleWhileRevalidate( Long.parseLong(element.getAttribute("stale-while-revalidate")), TimeUnit.SECONDS); cacheControl = cacheControl.staleIfError( Long.parseLong(element.getAttribute("stale-if-error")), TimeUnit.SECONDS);
@Test public void maxAgeAndDirectives() throws Exception { CacheControl cc = CacheControl.maxAge(3600, TimeUnit.SECONDS).cachePublic().noTransform(); assertThat(cc.getHeaderValue(), Matchers.equalTo("max-age=3600, no-transform, public")); }
CacheControl cControl; if (cacheSeconds > 0) { cControl = CacheControl.maxAge(cacheSeconds, TimeUnit.SECONDS); if (this.alwaysMustRevalidate) { cControl = cControl.mustRevalidate(); cControl = (this.useCacheControlNoStore ? CacheControl.noStore() : CacheControl.noCache()); cControl = CacheControl.empty();
public ResponseEntity<String> handleHttpEntityWithCacheControl(HttpEntity<byte[]> httpEntity) throws Exception { String responseBody = "Handled requestBody=[" + new String(httpEntity.getBody(), "UTF-8") + "]"; return ResponseEntity.ok().cacheControl(CacheControl.maxAge(1, TimeUnit.HOURS)).body(responseBody); }
response.setHeader( "cache-control", CacheControl.maxAge(cachingTime, TimeUnit.SECONDS) .cachePublic() .getHeaderValue());
@Test public void cacheControl() { CacheControl control = CacheControl.maxAge(1, TimeUnit.HOURS).noTransform(); HttpHeaders headers = new HttpHeaders(); headers.setCacheControl(control.getHeaderValue()); HeaderAssertions assertions = headerAssertions(headers); // Success assertions.cacheControl(control); try { assertions.cacheControl(CacheControl.noStore()); fail("Wrong value expected"); } catch (AssertionError error) { // Expected } }
/** * Set a configured {@link CacheControl} instance as the * new value of the {@code Cache-Control} header. * @since 5.0.5 */ public void setCacheControl(CacheControl cacheControl) { setOrRemove(CACHE_CONTROL, cacheControl.getHeaderValue()); }
@Test public void maxAgeAndSMaxAge() throws Exception { CacheControl cc = CacheControl.maxAge(1, TimeUnit.HOURS).sMaxAge(30, TimeUnit.MINUTES); assertThat(cc.getHeaderValue(), Matchers.equalTo("max-age=3600, s-maxage=1800")); }
@Test public void testResourcesWithResolversTransformersCustom() throws Exception { loadBeanDefinitions("mvc-config-resources-chain-no-auto.xml"); SimpleUrlHandlerMapping mapping = appContext.getBean(SimpleUrlHandlerMapping.class); assertNotNull(mapping); assertNotNull(mapping.getUrlMap().get("/resources/**")); ResourceHttpRequestHandler handler = appContext.getBean((String) mapping.getUrlMap().get("/resources/**"), ResourceHttpRequestHandler.class); assertNotNull(handler); assertThat(handler.getCacheControl().getHeaderValue(), Matchers.equalTo(CacheControl.maxAge(1, TimeUnit.HOURS) .sMaxAge(30, TimeUnit.MINUTES).cachePublic().getHeaderValue())); List<ResourceResolver> resolvers = handler.getResourceResolvers(); assertThat(resolvers, Matchers.hasSize(3)); assertThat(resolvers.get(0), Matchers.instanceOf(VersionResourceResolver.class)); assertThat(resolvers.get(1), Matchers.instanceOf(EncodedResourceResolver.class)); assertThat(resolvers.get(2), Matchers.instanceOf(PathResourceResolver.class)); VersionResourceResolver versionResolver = (VersionResourceResolver) resolvers.get(0); assertThat(versionResolver.getStrategyMap().get("/**/*.js"), Matchers.instanceOf(FixedVersionStrategy.class)); assertThat(versionResolver.getStrategyMap().get("/**"), Matchers.instanceOf(ContentVersionStrategy.class)); List<ResourceTransformer> transformers = handler.getResourceTransformers(); assertThat(transformers, Matchers.hasSize(2)); assertThat(transformers.get(0), Matchers.instanceOf(CachingResourceTransformer.class)); assertThat(transformers.get(1), Matchers.instanceOf(AppCacheManifestTransformer.class)); }
@Test public void staleIfError() throws Exception { CacheControl cc = CacheControl.maxAge(1, TimeUnit.HOURS).staleIfError(2, TimeUnit.HOURS); assertThat(cc.getHeaderValue(), Matchers.equalTo("max-age=3600, stale-if-error=7200")); }
@Test public void staleWhileRevalidate() throws Exception { CacheControl cc = CacheControl.maxAge(1, TimeUnit.HOURS).staleWhileRevalidate(2, TimeUnit.HOURS); assertThat(cc.getHeaderValue(), Matchers.equalTo("max-age=3600, stale-while-revalidate=7200")); }
@Test public void noCachePrivate() throws Exception { CacheControl cc = CacheControl.noCache().cachePrivate(); assertThat(cc.getHeaderValue(), Matchers.equalTo("no-cache, private")); }
@Test public void cacheControl() { Integer entity = 42; ResponseEntity<Integer> responseEntity = ResponseEntity.status(HttpStatus.OK) .cacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePrivate(). mustRevalidate().proxyRevalidate().sMaxAge(30, TimeUnit.MINUTES)) .body(entity); assertNotNull(responseEntity); assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); assertTrue(responseEntity.getHeaders().containsKey(HttpHeaders.CACHE_CONTROL)); assertEquals(entity, responseEntity.getBody()); String cacheControlHeader = responseEntity.getHeaders().getCacheControl(); assertThat(cacheControlHeader, Matchers.equalTo("max-age=3600, must-revalidate, private, proxy-revalidate, s-maxage=1800")); }
@Test public void maxAge() throws Exception { CacheControl cc = CacheControl.maxAge(1, TimeUnit.HOURS); assertThat(cc.getHeaderValue(), Matchers.equalTo("max-age=3600")); }
@GetMapping(value = "/autocompleteKeys", produces = APPLICATION_JSON_VALUE) public ResponseEntity<List<String>> getAutocompleteKeys() { return ResponseEntity.ok() .cacheControl(CacheControl.maxAge(namesMaxAge, TimeUnit.SECONDS).mustRevalidate()) .body(autocompleteKeys); }
@Test public void noStore() throws Exception { CacheControl cc = CacheControl.noStore(); assertThat(cc.getHeaderValue(), Matchers.equalTo("no-store")); }
@Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resource/**") .addResourceLocations("classpath:resource/"); ofNullable(cacheControlMaxAge).ifPresent(value -> { try { registry.addResourceHandler(CHUNK_JS, BUNDLE_JS, BUNDLE_CSS, PNG, JPG, WOFF, EOF, TTF) .addResourceLocations("classpath:resource/") .setCacheControl(CacheControl.maxAge(value, TimeUnit.SECONDS).cachePublic()); } catch (Exception e) { LOGGER.debug("Please check the value of \"polaris.resources.cache.cacheControl.max-age\" in application.yaml. Resource caching is not enabled."); } }); registry.addResourceHandler("/assets/**") .addResourceLocations("classpath:resource/assets/"); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); }
@Test public void emptyCacheControl() throws Exception { CacheControl cc = CacheControl.empty(); assertThat(cc.getHeaderValue(), Matchers.nullValue()); }
public HttpHeadersBuilder withNoCache() { this.headers.set( HttpHeaders.CACHE_CONTROL, CacheControl.noCache().getHeaderValue() ); return this; }
@Test public void cacheControlBuilder() { headers.setCacheControl(CacheControl.noCache()); assertEquals("Invalid Cache-Control header", "no-cache", headers.getCacheControl()); assertEquals("Invalid Cache-Control header", "no-cache", headers.getFirst("cache-control")); }