Search in sources :

Example 1 with ResourceResolver

use of org.springframework.web.servlet.resource.ResourceResolver in project spring-boot-admin by codecentric.

the class ResourcePatternResolvingResourceResolverTest method test_resolveResource.

@Test
public void test_resolveResource() {
    ResourceResolver resolver = new ResourcePatternResolvingResourceResolver(new PathMatchingResourcePatternResolver(), "classpath:/t*Resource.txt");
    resolver.resolveResource(null, null, null, new ResourceResolverChain() {

        @Override
        public Resource resolveResource(HttpServletRequest request, String requestPath, List<? extends Resource> locations) {
            assertThat(locations.size(), is(1));
            assertThat(locations.get(0).getFilename(), is("testResource.txt"));
            return null;
        }

        @Override
        public String resolveUrlPath(String resourcePath, List<? extends Resource> locations) {
            return null;
        }
    });
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ResourceResolver(org.springframework.web.servlet.resource.ResourceResolver) Resource(org.springframework.core.io.Resource) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ResourceResolverChain(org.springframework.web.servlet.resource.ResourceResolverChain) Test(org.junit.Test)

Example 2 with ResourceResolver

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

the class ResourceChainRegistration method getResourceResolvers.

protected List<ResourceResolver> getResourceResolvers() {
    if (!this.hasPathResolver) {
        List<ResourceResolver> result = new ArrayList<>(this.resolvers);
        if (isWebJarsAssetLocatorPresent && !this.hasWebjarsResolver) {
            result.add(new WebJarsResourceResolver());
        }
        result.add(new PathResourceResolver());
        return result;
    }
    return this.resolvers;
}
Also used : PathResourceResolver(org.springframework.web.servlet.resource.PathResourceResolver) WebJarsResourceResolver(org.springframework.web.servlet.resource.WebJarsResourceResolver) ArrayList(java.util.ArrayList) 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)

Example 3 with ResourceResolver

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

the class ResourceHandlerRegistryTests method resourceChain.

@Test
public void resourceChain() throws Exception {
    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.toString(), resolvers, Matchers.hasSize(4));
    assertThat(resolvers.get(0), Matchers.instanceOf(CachingResourceResolver.class));
    CachingResourceResolver cachingResolver = (CachingResourceResolver) resolvers.get(0);
    assertThat(cachingResolver.getCache(), Matchers.instanceOf(ConcurrentMapCache.class));
    assertThat(resolvers.get(1), Matchers.equalTo(mockResolver));
    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(2));
    assertThat(transformers.get(0), Matchers.instanceOf(CachingResourceTransformer.class));
    assertThat(transformers.get(1), Matchers.equalTo(mockTransformer));
}
Also used : ConcurrentMapCache(org.springframework.cache.concurrent.ConcurrentMapCache) ResourceHttpRequestHandler(org.springframework.web.servlet.resource.ResourceHttpRequestHandler) CachingResourceResolver(org.springframework.web.servlet.resource.CachingResourceResolver) PathResourceResolver(org.springframework.web.servlet.resource.PathResourceResolver) WebJarsResourceResolver(org.springframework.web.servlet.resource.WebJarsResourceResolver) 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 4 with ResourceResolver

use of org.springframework.web.servlet.resource.ResourceResolver 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 5 with ResourceResolver

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

the class ResourceHandlerRegistryTests method resourceChainWithoutCaching.

@Test
public void resourceChainWithoutCaching() throws Exception {
    this.registration.resourceChain(false);
    ResourceHttpRequestHandler handler = getHandler("/resources/**");
    List<ResourceResolver> resolvers = handler.getResourceResolvers();
    assertThat(resolvers, Matchers.hasSize(2));
    assertThat(resolvers.get(0), Matchers.instanceOf(WebJarsResourceResolver.class));
    assertThat(resolvers.get(1), Matchers.instanceOf(PathResourceResolver.class));
    List<ResourceTransformer> transformers = handler.getResourceTransformers();
    assertThat(transformers, Matchers.hasSize(0));
}
Also used : ResourceHttpRequestHandler(org.springframework.web.servlet.resource.ResourceHttpRequestHandler) PathResourceResolver(org.springframework.web.servlet.resource.PathResourceResolver) WebJarsResourceResolver(org.springframework.web.servlet.resource.WebJarsResourceResolver) 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) 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

ResourceResolver (org.springframework.web.servlet.resource.ResourceResolver)8 Test (org.junit.Test)7 CachingResourceResolver (org.springframework.web.servlet.resource.CachingResourceResolver)7 PathResourceResolver (org.springframework.web.servlet.resource.PathResourceResolver)7 VersionResourceResolver (org.springframework.web.servlet.resource.VersionResourceResolver)7 WebJarsResourceResolver (org.springframework.web.servlet.resource.WebJarsResourceResolver)7 CachingResourceTransformer (org.springframework.web.servlet.resource.CachingResourceTransformer)6 CssLinkResourceTransformer (org.springframework.web.servlet.resource.CssLinkResourceTransformer)6 ResourceHttpRequestHandler (org.springframework.web.servlet.resource.ResourceHttpRequestHandler)6 ResourceTransformer (org.springframework.web.servlet.resource.ResourceTransformer)6 AppCacheManifestTransformer (org.springframework.web.servlet.resource.AppCacheManifestTransformer)4 ConcurrentMapCache (org.springframework.cache.concurrent.ConcurrentMapCache)2 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 ArrayList (java.util.ArrayList)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Resource (org.springframework.core.io.Resource)1 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)1