Search in sources :

Example 1 with WebMvcConfigurer

use of org.springframework.web.servlet.config.annotation.WebMvcConfigurer in project cas by apereo.

the class CasThemesConfiguration method themesStaticResourcesWebMvcConfigurer.

@Bean
@ConditionalOnMissingBean(name = "themesStaticResourcesWebMvcConfigurer")
public WebMvcConfigurer themesStaticResourcesWebMvcConfigurer(final CasConfigurationProperties casProperties, final WebProperties webProperties, final ThymeleafProperties thymeleafProperties) {
    return new WebMvcConfigurer() {

        @Override
        public void addResourceHandlers(final ResourceHandlerRegistry registry) {
            val templatePrefixes = casProperties.getView().getTemplatePrefixes();
            if (!templatePrefixes.isEmpty()) {
                val registration = registry.addResourceHandler("/**");
                val resources = templatePrefixes.stream().map(prefix -> StringUtils.appendIfMissing(prefix, "/")).map(Unchecked.function(ResourceUtils::getRawResourceFrom)).toArray(Resource[]::new);
                LOGGER.debug("Adding resource handler for resources [{}]", (Object[]) resources);
                registration.addResourceLocations(templatePrefixes.toArray(ArrayUtils.EMPTY_STRING_ARRAY));
                registration.addResourceLocations(webProperties.getResources().getStaticLocations());
                FunctionUtils.doIfNotNull(webProperties.getResources().getCache().getPeriod(), period -> registration.setCachePeriod((int) period.getSeconds()));
                registration.setCacheControl(webProperties.getResources().getCache().getCachecontrol().toHttpCacheControl());
                registration.setUseLastModified(true);
                val cache = thymeleafProperties != null && thymeleafProperties.isCache();
                val chainRegistration = registration.resourceChain(cache);
                val resolver = new PathResourceResolver();
                resolver.setAllowedLocations(resources);
                chainRegistration.addResolver(resolver);
            }
        }
    };
}
Also used : lombok.val(lombok.val) PathResourceResolver(org.springframework.web.servlet.resource.PathResourceResolver) WebMvcConfigurer(org.springframework.web.servlet.config.annotation.WebMvcConfigurer) ResourceUtils(org.apereo.cas.util.ResourceUtils) ResourceHandlerRegistry(org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

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 ResourceHandlerRegistry (org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry)1 WebMvcConfigurer (org.springframework.web.servlet.config.annotation.WebMvcConfigurer)1 PathResourceResolver (org.springframework.web.servlet.resource.PathResourceResolver)1