use of org.springframework.web.servlet.resource.CachingResourceTransformer in project spring-framework by spring-projects.
the class ResourceChainRegistration method getResourceTransformers.
protected List<ResourceTransformer> getResourceTransformers() {
if (this.hasVersionResolver && !this.hasCssLinkTransformer) {
List<ResourceTransformer> result = new ArrayList<>(this.transformers);
boolean hasTransformers = !this.transformers.isEmpty();
boolean hasCaching = hasTransformers && this.transformers.get(0) instanceof CachingResourceTransformer;
result.add(hasCaching ? 1 : 0, new CssLinkResourceTransformer());
return result;
}
return this.transformers;
}
use of org.springframework.web.servlet.resource.CachingResourceTransformer in project spring-framework by spring-projects.
the class MvcNamespaceTests method testResourcesWithResolversTransformers.
@Test
@SuppressWarnings("deprecation")
public void testResourcesWithResolversTransformers() throws Exception {
loadBeanDefinitions("mvc-config-resources-chain.xml");
SimpleUrlHandlerMapping mapping = appContext.getBean(SimpleUrlHandlerMapping.class);
assertThat(mapping).isNotNull();
assertThat(mapping.getUrlMap().get("/resources/**")).isNotNull();
String beanName = (String) mapping.getUrlMap().get("/resources/**");
ResourceHttpRequestHandler handler = appContext.getBean(beanName, ResourceHttpRequestHandler.class);
assertThat(handler).isNotNull();
assertThat(handler.getUrlPathHelper()).isNotNull();
List<ResourceResolver> resolvers = handler.getResourceResolvers();
assertThat(resolvers).hasSize(4);
assertThat(resolvers.get(0)).isInstanceOf(CachingResourceResolver.class);
assertThat(resolvers.get(1)).isInstanceOf(VersionResourceResolver.class);
assertThat(resolvers.get(2)).isInstanceOf(WebJarsResourceResolver.class);
assertThat(resolvers.get(3)).isInstanceOf(PathResourceResolver.class);
CachingResourceResolver cachingResolver = (CachingResourceResolver) resolvers.get(0);
assertThat(cachingResolver.getCache()).isInstanceOf(ConcurrentMapCache.class);
assertThat(cachingResolver.getCache().getName()).isEqualTo("test-resource-cache");
VersionResourceResolver versionResolver = (VersionResourceResolver) resolvers.get(1);
assertThat(versionResolver.getStrategyMap().get("/**/*.js")).isInstanceOf(FixedVersionStrategy.class);
assertThat(versionResolver.getStrategyMap().get("/**")).isInstanceOf(ContentVersionStrategy.class);
PathResourceResolver pathResolver = (PathResourceResolver) resolvers.get(3);
Map<Resource, Charset> locationCharsets = pathResolver.getLocationCharsets();
assertThat(locationCharsets.size()).isEqualTo(1);
assertThat(locationCharsets.values().iterator().next()).isEqualTo(StandardCharsets.ISO_8859_1);
List<ResourceTransformer> transformers = handler.getResourceTransformers();
assertThat(transformers).hasSize(2);
assertThat(transformers.get(0)).isInstanceOf(CachingResourceTransformer.class);
assertThat(transformers.get(1)).isInstanceOf(CssLinkResourceTransformer.class);
CachingResourceTransformer cachingTransformer = (CachingResourceTransformer) transformers.get(0);
assertThat(cachingTransformer.getCache()).isInstanceOf(ConcurrentMapCache.class);
assertThat(cachingTransformer.getCache().getName()).isEqualTo("test-resource-cache");
}
use of org.springframework.web.servlet.resource.CachingResourceTransformer in project spring-framework by spring-projects.
the class ResourceHandlerRegistryTests method resourceChainWithOverrides.
@Test
public void resourceChainWithOverrides() {
CachingResourceResolver cachingResolver = Mockito.mock(CachingResourceResolver.class);
VersionResourceResolver versionResolver = Mockito.mock(VersionResourceResolver.class);
WebJarsResourceResolver webjarsResolver = Mockito.mock(WebJarsResourceResolver.class);
PathResourceResolver pathResourceResolver = new PathResourceResolver();
CachingResourceTransformer cachingTransformer = Mockito.mock(CachingResourceTransformer.class);
CssLinkResourceTransformer cssLinkTransformer = new CssLinkResourceTransformer();
this.registration.setCachePeriod(3600).resourceChain(false).addResolver(cachingResolver).addResolver(versionResolver).addResolver(webjarsResolver).addResolver(pathResourceResolver).addTransformer(cachingTransformer).addTransformer(cssLinkTransformer);
ResourceHttpRequestHandler handler = getHandler("/resources/**");
List<ResourceResolver> resolvers = handler.getResourceResolvers();
assertThat(resolvers).hasSize(4);
assertThat(resolvers.get(0)).isSameAs(cachingResolver);
assertThat(resolvers.get(1)).isSameAs(versionResolver);
assertThat(resolvers.get(2)).isSameAs(webjarsResolver);
assertThat(resolvers.get(3)).isSameAs(pathResourceResolver);
List<ResourceTransformer> transformers = handler.getResourceTransformers();
assertThat(transformers).hasSize(2);
assertThat(transformers.get(0)).isSameAs(cachingTransformer);
assertThat(transformers.get(1)).isSameAs(cssLinkTransformer);
}
Aggregations