Search in sources :

Example 1 with ResourceTransformer

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

the class ResourceHandlerRegistryTests method resourceChainWithoutCaching.

@Test
public void resourceChainWithoutCaching() {
    this.registration.resourceChain(false);
    ResourceHttpRequestHandler handler = getHandler("/resources/**");
    List<ResourceResolver> resolvers = handler.getResourceResolvers();
    assertThat(resolvers).hasSize(2);
    assertThat(resolvers.get(0)).isInstanceOf(WebJarsResourceResolver.class);
    assertThat(resolvers.get(1)).isInstanceOf(PathResourceResolver.class);
    List<ResourceTransformer> transformers = handler.getResourceTransformers();
    assertThat(transformers).isEmpty();
}
Also used : ResourceHttpRequestHandler(org.springframework.web.servlet.resource.ResourceHttpRequestHandler) VersionResourceResolver(org.springframework.web.servlet.resource.VersionResourceResolver) CachingResourceResolver(org.springframework.web.servlet.resource.CachingResourceResolver) PathResourceResolver(org.springframework.web.servlet.resource.PathResourceResolver) WebJarsResourceResolver(org.springframework.web.servlet.resource.WebJarsResourceResolver) ResourceResolver(org.springframework.web.servlet.resource.ResourceResolver) ResourceTransformer(org.springframework.web.servlet.resource.ResourceTransformer) CssLinkResourceTransformer(org.springframework.web.servlet.resource.CssLinkResourceTransformer) CachingResourceTransformer(org.springframework.web.servlet.resource.CachingResourceTransformer) Test(org.junit.jupiter.api.Test)

Example 2 with ResourceTransformer

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

the class ResourceHandlerRegistryTests method resourceChainWithVersionResolver.

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

Example 3 with ResourceTransformer

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

the class ResourceHandlerRegistryTests method resourceChain.

@Test
public void resourceChain() {
    ResourceResolver mockResolver = Mockito.mock(ResourceResolver.class);
    ResourceTransformer mockTransformer = Mockito.mock(ResourceTransformer.class);
    this.registration.resourceChain(true).addResolver(mockResolver).addTransformer(mockTransformer);
    ResourceHttpRequestHandler handler = getHandler("/resources/**");
    List<ResourceResolver> resolvers = handler.getResourceResolvers();
    assertThat(resolvers).hasSize(4);
    assertThat(resolvers.get(0)).isInstanceOf(CachingResourceResolver.class);
    CachingResourceResolver cachingResolver = (CachingResourceResolver) resolvers.get(0);
    assertThat(cachingResolver.getCache()).isInstanceOf(ConcurrentMapCache.class);
    assertThat(resolvers.get(1)).isEqualTo(mockResolver);
    assertThat(resolvers.get(2)).isInstanceOf(WebJarsResourceResolver.class);
    assertThat(resolvers.get(3)).isInstanceOf(PathResourceResolver.class);
    List<ResourceTransformer> transformers = handler.getResourceTransformers();
    assertThat(transformers).hasSize(2);
    assertThat(transformers.get(0)).isInstanceOf(CachingResourceTransformer.class);
    assertThat(transformers.get(1)).isEqualTo(mockTransformer);
}
Also used : ResourceHttpRequestHandler(org.springframework.web.servlet.resource.ResourceHttpRequestHandler) CachingResourceResolver(org.springframework.web.servlet.resource.CachingResourceResolver) VersionResourceResolver(org.springframework.web.servlet.resource.VersionResourceResolver) CachingResourceResolver(org.springframework.web.servlet.resource.CachingResourceResolver) PathResourceResolver(org.springframework.web.servlet.resource.PathResourceResolver) WebJarsResourceResolver(org.springframework.web.servlet.resource.WebJarsResourceResolver) ResourceResolver(org.springframework.web.servlet.resource.ResourceResolver) ResourceTransformer(org.springframework.web.servlet.resource.ResourceTransformer) CssLinkResourceTransformer(org.springframework.web.servlet.resource.CssLinkResourceTransformer) CachingResourceTransformer(org.springframework.web.servlet.resource.CachingResourceTransformer) Test(org.junit.jupiter.api.Test)

Example 4 with ResourceTransformer

use of org.springframework.web.servlet.resource.ResourceTransformer 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;
}
Also used : CssLinkResourceTransformer(org.springframework.web.servlet.resource.CssLinkResourceTransformer) ArrayList(java.util.ArrayList) 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)

Example 5 with ResourceTransformer

use of org.springframework.web.servlet.resource.ResourceTransformer 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");
}
Also used : ResourceHttpRequestHandler(org.springframework.web.servlet.resource.ResourceHttpRequestHandler) VersionResourceResolver(org.springframework.web.servlet.resource.VersionResourceResolver) Resource(org.springframework.core.io.Resource) Charset(java.nio.charset.Charset) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) CachingResourceResolver(org.springframework.web.servlet.resource.CachingResourceResolver) 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) EncodedResourceResolver(org.springframework.web.servlet.resource.EncodedResourceResolver) 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.jupiter.api.Test)

Aggregations

CachingResourceTransformer (org.springframework.web.servlet.resource.CachingResourceTransformer)7 CssLinkResourceTransformer (org.springframework.web.servlet.resource.CssLinkResourceTransformer)7 ResourceTransformer (org.springframework.web.servlet.resource.ResourceTransformer)7 Test (org.junit.jupiter.api.Test)6 CachingResourceResolver (org.springframework.web.servlet.resource.CachingResourceResolver)6 PathResourceResolver (org.springframework.web.servlet.resource.PathResourceResolver)6 ResourceHttpRequestHandler (org.springframework.web.servlet.resource.ResourceHttpRequestHandler)6 ResourceResolver (org.springframework.web.servlet.resource.ResourceResolver)6 VersionResourceResolver (org.springframework.web.servlet.resource.VersionResourceResolver)6 WebJarsResourceResolver (org.springframework.web.servlet.resource.WebJarsResourceResolver)6 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)2 EncodedResourceResolver (org.springframework.web.servlet.resource.EncodedResourceResolver)2 Charset (java.nio.charset.Charset)1 ArrayList (java.util.ArrayList)1 Resource (org.springframework.core.io.Resource)1