Search in sources :

Example 6 with PathResourceResolver

use of org.springframework.web.servlet.resource.PathResourceResolver in project jphp by jphp-compiler.

the class PWebServer method _makeHandlers.

protected WebServerConfig.Handlers _makeHandlers() {
    return new WebServerConfig.Handlers() {

        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            for (ArrayMemory handler : staticHandlers) {
                Memory path = handler.valueOfIndex("path");
                ResourceHandlerRegistration registration;
                if (path.isArray()) {
                    registration = registry.addResourceHandler(path.toValue(ArrayMemory.class).toStringArray());
                } else {
                    registration = registry.addResourceHandler(path.toString());
                }
                Memory cachePeriod = handler.valueOfIndex("cachePeriod");
                registration.setCachePeriod(cachePeriod.isNull() ? null : cachePeriod.toInteger());
                Memory location = handler.valueOfIndex("location");
                if (location.isArray()) {
                    registration.addResourceLocations(location.toValue(ArrayMemory.class).toStringArray());
                } else {
                    registration.addResourceLocations(location.toString());
                }
                ResourceChainRegistration chain = registration.resourceChain(handler.valueOfIndex("cache").toBoolean());
                if (handler.valueOfIndex("gzip").toBoolean()) {
                    chain.addResolver(new GzipResourceResolver());
                }
                chain.addResolver(new PathResourceResolver());
            }
        }
    };
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) PathResourceResolver(org.springframework.web.servlet.resource.PathResourceResolver) ResourceHandlerRegistration(org.springframework.web.servlet.config.annotation.ResourceHandlerRegistration) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) GzipResourceResolver(org.springframework.web.servlet.resource.GzipResourceResolver) ResourceHandlerRegistry(org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry) ResourceChainRegistration(org.springframework.web.servlet.config.annotation.ResourceChainRegistration)

Example 7 with PathResourceResolver

use of org.springframework.web.servlet.resource.PathResourceResolver in project tutorials by eugenp.

the class MvcConfig method addResourceHandlers.

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    // For examples using Spring 4.1.0
    if ((env.getProperty("resource.handler.conf")).equals("4.1.0")) {
        registry.addResourceHandler("/js/**").addResourceLocations("/js/").setCachePeriod(3600).resourceChain(true).addResolver(new GzipResourceResolver()).addResolver(new PathResourceResolver());
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/", "classpath:/other-resources/").setCachePeriod(3600).resourceChain(true).addResolver(new PathResourceResolver());
        registry.addResourceHandler("/files/**").addResourceLocations("file:/Users/Elena/").setCachePeriod(3600).resourceChain(true).addResolver(new PathResourceResolver());
        registry.addResourceHandler("/other-files/**").addResourceLocations("file:/Users/Elena/").setCachePeriod(3600).resourceChain(true).addResolver(new GzipResourceResolver());
    } else // For examples using Spring 4.0.7
    if ((env.getProperty("resource.handler.conf")).equals("4.0.7")) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/", "/resources/", "classpath:/other-resources/");
        registry.addResourceHandler("/files/**").addResourceLocations("file:/Users/Elena/");
    }
}
Also used : PathResourceResolver(org.springframework.web.servlet.resource.PathResourceResolver) GzipResourceResolver(org.springframework.web.servlet.resource.GzipResourceResolver)

Example 8 with PathResourceResolver

use of org.springframework.web.servlet.resource.PathResourceResolver 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)

Example 9 with PathResourceResolver

use of org.springframework.web.servlet.resource.PathResourceResolver 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);
}
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) 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) 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

PathResourceResolver (org.springframework.web.servlet.resource.PathResourceResolver)9 WebJarsResourceResolver (org.springframework.web.servlet.resource.WebJarsResourceResolver)5 CachingResourceResolver (org.springframework.web.servlet.resource.CachingResourceResolver)4 ResourceResolver (org.springframework.web.servlet.resource.ResourceResolver)4 VersionResourceResolver (org.springframework.web.servlet.resource.VersionResourceResolver)4 Test (org.junit.jupiter.api.Test)3 Resource (org.springframework.core.io.Resource)3 ResourceHttpRequestHandler (org.springframework.web.servlet.resource.ResourceHttpRequestHandler)3 Charset (java.nio.charset.Charset)2 ResourceHandlerRegistry (org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry)2 CachingResourceTransformer (org.springframework.web.servlet.resource.CachingResourceTransformer)2 CssLinkResourceTransformer (org.springframework.web.servlet.resource.CssLinkResourceTransformer)2 GzipResourceResolver (org.springframework.web.servlet.resource.GzipResourceResolver)2 ResourceTransformer (org.springframework.web.servlet.resource.ResourceTransformer)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 lombok.val (lombok.val)1 ResourceUtils (org.apereo.cas.util.ResourceUtils)1 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1 Bean (org.springframework.context.annotation.Bean)1