Search in sources :

Example 1 with VersionResourceResolver

use of org.springframework.web.servlet.resource.VersionResourceResolver in project spring-boot by spring-projects.

the class WebMvcAutoConfigurationTests method resourceHandlerFixedStrategyEnabled.

@Test
public void resourceHandlerFixedStrategyEnabled() throws Exception {
    load("spring.resources.chain.strategy.fixed.enabled:true", "spring.resources.chain.strategy.fixed.version:test", "spring.resources.chain.strategy.fixed.paths:/**/*.js");
    assertThat(getResourceResolvers("/webjars/**")).hasSize(3);
    assertThat(getResourceTransformers("/webjars/**")).hasSize(2);
    assertThat(getResourceResolvers("/**")).extractingResultOf("getClass").containsOnly(CachingResourceResolver.class, VersionResourceResolver.class, PathResourceResolver.class);
    assertThat(getResourceTransformers("/**")).extractingResultOf("getClass").containsOnly(CachingResourceTransformer.class, CssLinkResourceTransformer.class);
    VersionResourceResolver resolver = (VersionResourceResolver) getResourceResolvers("/**").get(1);
    assertThat(resolver.getStrategyMap().get("/**/*.js")).isInstanceOf(FixedVersionStrategy.class);
}
Also used : VersionResourceResolver(org.springframework.web.servlet.resource.VersionResourceResolver) Test(org.junit.Test)

Example 2 with VersionResourceResolver

use of org.springframework.web.servlet.resource.VersionResourceResolver in project spring-framework by spring-projects.

the class ResourceHandlerRegistryTests method resourceChainWithVersionResolver.

@Test
public void resourceChainWithVersionResolver() throws Exception {
    VersionResourceResolver versionResolver = new VersionResourceResolver().addFixedVersionStrategy("fixed", "/**/*.js").addContentVersionStrategy("/**");
    this.registration.resourceChain(true).addResolver(versionResolver).addTransformer(new AppCacheManifestTransformer());
    ResourceHttpRequestHandler handler = getHandler("/resources/**");
    List<ResourceResolver> resolvers = handler.getResourceResolvers();
    assertThat(resolvers.toString(), resolvers, Matchers.hasSize(4));
    assertThat(resolvers.get(0), Matchers.instanceOf(CachingResourceResolver.class));
    assertThat(resolvers.get(1), Matchers.sameInstance(versionResolver));
    assertThat(resolvers.get(2), Matchers.instanceOf(WebJarsResourceResolver.class));
    assertThat(resolvers.get(3), Matchers.instanceOf(PathResourceResolver.class));
    List<ResourceTransformer> transformers = handler.getResourceTransformers();
    assertThat(transformers, Matchers.hasSize(3));
    assertThat(transformers.get(0), Matchers.instanceOf(CachingResourceTransformer.class));
    assertThat(transformers.get(1), Matchers.instanceOf(CssLinkResourceTransformer.class));
    assertThat(transformers.get(2), Matchers.instanceOf(AppCacheManifestTransformer.class));
}
Also used : ResourceHttpRequestHandler(org.springframework.web.servlet.resource.ResourceHttpRequestHandler) CachingResourceResolver(org.springframework.web.servlet.resource.CachingResourceResolver) CssLinkResourceTransformer(org.springframework.web.servlet.resource.CssLinkResourceTransformer) PathResourceResolver(org.springframework.web.servlet.resource.PathResourceResolver) AppCacheManifestTransformer(org.springframework.web.servlet.resource.AppCacheManifestTransformer) WebJarsResourceResolver(org.springframework.web.servlet.resource.WebJarsResourceResolver) VersionResourceResolver(org.springframework.web.servlet.resource.VersionResourceResolver) VersionResourceResolver(org.springframework.web.servlet.resource.VersionResourceResolver) PathResourceResolver(org.springframework.web.servlet.resource.PathResourceResolver) WebJarsResourceResolver(org.springframework.web.servlet.resource.WebJarsResourceResolver) ResourceResolver(org.springframework.web.servlet.resource.ResourceResolver) CachingResourceResolver(org.springframework.web.servlet.resource.CachingResourceResolver) CachingResourceTransformer(org.springframework.web.servlet.resource.CachingResourceTransformer) ResourceTransformer(org.springframework.web.servlet.resource.ResourceTransformer) CssLinkResourceTransformer(org.springframework.web.servlet.resource.CssLinkResourceTransformer) CachingResourceTransformer(org.springframework.web.servlet.resource.CachingResourceTransformer) Test(org.junit.Test)

Example 3 with VersionResourceResolver

use of org.springframework.web.servlet.resource.VersionResourceResolver in project spring-framework by spring-projects.

the class MvcNamespaceTests method testResourcesWithResolversTransformersCustom.

@Test
public void testResourcesWithResolversTransformersCustom() throws Exception {
    loadBeanDefinitions("mvc-config-resources-chain-no-auto.xml", 12);
    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(GzipResourceResolver.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));
}
Also used : FixedVersionStrategy(org.springframework.web.servlet.resource.FixedVersionStrategy) ResourceHttpRequestHandler(org.springframework.web.servlet.resource.ResourceHttpRequestHandler) AppCacheManifestTransformer(org.springframework.web.servlet.resource.AppCacheManifestTransformer) VersionResourceResolver(org.springframework.web.servlet.resource.VersionResourceResolver) GzipResourceResolver(org.springframework.web.servlet.resource.GzipResourceResolver) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) PathResourceResolver(org.springframework.web.servlet.resource.PathResourceResolver) PathResourceResolver(org.springframework.web.servlet.resource.PathResourceResolver) ResourceResolver(org.springframework.web.servlet.resource.ResourceResolver) CachingResourceResolver(org.springframework.web.servlet.resource.CachingResourceResolver) WebJarsResourceResolver(org.springframework.web.servlet.resource.WebJarsResourceResolver) VersionResourceResolver(org.springframework.web.servlet.resource.VersionResourceResolver) GzipResourceResolver(org.springframework.web.servlet.resource.GzipResourceResolver) ContentVersionStrategy(org.springframework.web.servlet.resource.ContentVersionStrategy) CachingResourceTransformer(org.springframework.web.servlet.resource.CachingResourceTransformer) ResourceTransformer(org.springframework.web.servlet.resource.ResourceTransformer) CssLinkResourceTransformer(org.springframework.web.servlet.resource.CssLinkResourceTransformer) CachingResourceTransformer(org.springframework.web.servlet.resource.CachingResourceTransformer) Test(org.junit.Test)

Example 4 with VersionResourceResolver

use of org.springframework.web.servlet.resource.VersionResourceResolver in project spring-framework by spring-projects.

the class ResourceHandlerRegistryTests method resourceChainWithOverrides.

@Test
public void resourceChainWithOverrides() throws Exception {
    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);
    AppCacheManifestTransformer appCacheTransformer = Mockito.mock(AppCacheManifestTransformer.class);
    CssLinkResourceTransformer cssLinkTransformer = new CssLinkResourceTransformer();
    this.registration.setCachePeriod(3600).resourceChain(false).addResolver(cachingResolver).addResolver(versionResolver).addResolver(webjarsResolver).addResolver(pathResourceResolver).addTransformer(cachingTransformer).addTransformer(appCacheTransformer).addTransformer(cssLinkTransformer);
    ResourceHttpRequestHandler handler = getHandler("/resources/**");
    List<ResourceResolver> resolvers = handler.getResourceResolvers();
    assertThat(resolvers.toString(), resolvers, Matchers.hasSize(4));
    assertThat(resolvers.get(0), Matchers.sameInstance(cachingResolver));
    assertThat(resolvers.get(1), Matchers.sameInstance(versionResolver));
    assertThat(resolvers.get(2), Matchers.sameInstance(webjarsResolver));
    assertThat(resolvers.get(3), Matchers.sameInstance(pathResourceResolver));
    List<ResourceTransformer> transformers = handler.getResourceTransformers();
    assertThat(transformers, Matchers.hasSize(3));
    assertThat(transformers.get(0), Matchers.sameInstance(cachingTransformer));
    assertThat(transformers.get(1), Matchers.sameInstance(appCacheTransformer));
    assertThat(transformers.get(2), Matchers.sameInstance(cssLinkTransformer));
}
Also used : CachingResourceResolver(org.springframework.web.servlet.resource.CachingResourceResolver) CssLinkResourceTransformer(org.springframework.web.servlet.resource.CssLinkResourceTransformer) ResourceHttpRequestHandler(org.springframework.web.servlet.resource.ResourceHttpRequestHandler) PathResourceResolver(org.springframework.web.servlet.resource.PathResourceResolver) WebJarsResourceResolver(org.springframework.web.servlet.resource.WebJarsResourceResolver) AppCacheManifestTransformer(org.springframework.web.servlet.resource.AppCacheManifestTransformer) VersionResourceResolver(org.springframework.web.servlet.resource.VersionResourceResolver) VersionResourceResolver(org.springframework.web.servlet.resource.VersionResourceResolver) PathResourceResolver(org.springframework.web.servlet.resource.PathResourceResolver) WebJarsResourceResolver(org.springframework.web.servlet.resource.WebJarsResourceResolver) ResourceResolver(org.springframework.web.servlet.resource.ResourceResolver) CachingResourceResolver(org.springframework.web.servlet.resource.CachingResourceResolver) CachingResourceTransformer(org.springframework.web.servlet.resource.CachingResourceTransformer) ResourceTransformer(org.springframework.web.servlet.resource.ResourceTransformer) CssLinkResourceTransformer(org.springframework.web.servlet.resource.CssLinkResourceTransformer) CachingResourceTransformer(org.springframework.web.servlet.resource.CachingResourceTransformer) Test(org.junit.Test)

Example 5 with VersionResourceResolver

use of org.springframework.web.servlet.resource.VersionResourceResolver in project spring-framework by spring-projects.

the class MvcNamespaceTests method testResourcesWithResolversTransformers.

@Test
public void testResourcesWithResolversTransformers() throws Exception {
    loadBeanDefinitions("mvc-config-resources-chain.xml", 11);
    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);
    List<ResourceResolver> resolvers = handler.getResourceResolvers();
    assertThat(resolvers, Matchers.hasSize(4));
    assertThat(resolvers.get(0), Matchers.instanceOf(CachingResourceResolver.class));
    assertThat(resolvers.get(1), Matchers.instanceOf(VersionResourceResolver.class));
    assertThat(resolvers.get(2), Matchers.instanceOf(WebJarsResourceResolver.class));
    assertThat(resolvers.get(3), Matchers.instanceOf(PathResourceResolver.class));
    CachingResourceResolver cachingResolver = (CachingResourceResolver) resolvers.get(0);
    assertThat(cachingResolver.getCache(), Matchers.instanceOf(ConcurrentMapCache.class));
    assertEquals("test-resource-cache", cachingResolver.getCache().getName());
    VersionResourceResolver versionResolver = (VersionResourceResolver) resolvers.get(1);
    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(3));
    assertThat(transformers.get(0), Matchers.instanceOf(CachingResourceTransformer.class));
    assertThat(transformers.get(1), Matchers.instanceOf(CssLinkResourceTransformer.class));
    assertThat(transformers.get(2), Matchers.instanceOf(AppCacheManifestTransformer.class));
    CachingResourceTransformer cachingTransformer = (CachingResourceTransformer) transformers.get(0);
    assertThat(cachingTransformer.getCache(), Matchers.instanceOf(ConcurrentMapCache.class));
    assertEquals("test-resource-cache", cachingTransformer.getCache().getName());
}
Also used : FixedVersionStrategy(org.springframework.web.servlet.resource.FixedVersionStrategy) ResourceHttpRequestHandler(org.springframework.web.servlet.resource.ResourceHttpRequestHandler) CssLinkResourceTransformer(org.springframework.web.servlet.resource.CssLinkResourceTransformer) AppCacheManifestTransformer(org.springframework.web.servlet.resource.AppCacheManifestTransformer) VersionResourceResolver(org.springframework.web.servlet.resource.VersionResourceResolver) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) ConcurrentMapCache(org.springframework.cache.concurrent.ConcurrentMapCache) CachingResourceResolver(org.springframework.web.servlet.resource.CachingResourceResolver) PathResourceResolver(org.springframework.web.servlet.resource.PathResourceResolver) WebJarsResourceResolver(org.springframework.web.servlet.resource.WebJarsResourceResolver) PathResourceResolver(org.springframework.web.servlet.resource.PathResourceResolver) ResourceResolver(org.springframework.web.servlet.resource.ResourceResolver) CachingResourceResolver(org.springframework.web.servlet.resource.CachingResourceResolver) WebJarsResourceResolver(org.springframework.web.servlet.resource.WebJarsResourceResolver) VersionResourceResolver(org.springframework.web.servlet.resource.VersionResourceResolver) GzipResourceResolver(org.springframework.web.servlet.resource.GzipResourceResolver) ContentVersionStrategy(org.springframework.web.servlet.resource.ContentVersionStrategy) CachingResourceTransformer(org.springframework.web.servlet.resource.CachingResourceTransformer) ResourceTransformer(org.springframework.web.servlet.resource.ResourceTransformer) CssLinkResourceTransformer(org.springframework.web.servlet.resource.CssLinkResourceTransformer) CachingResourceTransformer(org.springframework.web.servlet.resource.CachingResourceTransformer) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)7 VersionResourceResolver (org.springframework.web.servlet.resource.VersionResourceResolver)7 AppCacheManifestTransformer (org.springframework.web.servlet.resource.AppCacheManifestTransformer)4 CachingResourceResolver (org.springframework.web.servlet.resource.CachingResourceResolver)4 CachingResourceTransformer (org.springframework.web.servlet.resource.CachingResourceTransformer)4 CssLinkResourceTransformer (org.springframework.web.servlet.resource.CssLinkResourceTransformer)4 PathResourceResolver (org.springframework.web.servlet.resource.PathResourceResolver)4 ResourceHttpRequestHandler (org.springframework.web.servlet.resource.ResourceHttpRequestHandler)4 ResourceResolver (org.springframework.web.servlet.resource.ResourceResolver)4 ResourceTransformer (org.springframework.web.servlet.resource.ResourceTransformer)4 WebJarsResourceResolver (org.springframework.web.servlet.resource.WebJarsResourceResolver)4 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)2 ContentVersionStrategy (org.springframework.web.servlet.resource.ContentVersionStrategy)2 FixedVersionStrategy (org.springframework.web.servlet.resource.FixedVersionStrategy)2 GzipResourceResolver (org.springframework.web.servlet.resource.GzipResourceResolver)2 ConcurrentMapCache (org.springframework.cache.concurrent.ConcurrentMapCache)1