All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.autoconfigure.web.ui.WebUiExtensionAutoConfguration Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
package org.zodiac.autoconfigure.web.ui;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.zodiac.autoconfigure.web.condition.ConditionalOnWebUiExtensionEnabled;
import org.zodiac.sdk.toolkit.constants.CharsetConstants;
import org.zodiac.sdk.toolkit.mime.MimeMappings;

@SpringBootConfiguration
@ConditionalOnWebUiExtensionEnabled
@ConditionalOnClass(value = {org.zodiac.webui.config.WebUiExtensionInfo.class, org.thymeleaf.templatemode.TemplateMode.class, org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver.class})
public class WebUiExtensionAutoConfguration {

    private static final Logger LOG = LoggerFactory.getLogger(WebUiExtensionAutoConfguration.class);

    private static final List DEFAULT_UI_ROUTES = Arrays.asList();

    private static final List DEFAULT_UI_ROUTE_EXCLUDES = Arrays.asList("/extensions/**");

    private final ApplicationContext applicationContext;

    public WebUiExtensionAutoConfguration(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
        LOG.info("{} loaded.", getClass().getCanonicalName());
    }

    @Bean
    @ConditionalOnMissingBean
    @ConfigurationProperties(prefix = org.zodiac.webui.constants.WebUiSystemPropertiesConstants.SPRING_RESOURCE_EXTENSIONS_PREFIX, ignoreInvalidFields = true)
    protected WebUiExtensionProperties webUiConfigProperties() {
        return new WebUiExtensionProperties();
    }

    @Bean
    @ConditionalOnMissingBean
    org.zodiac.webui.extension.UiExtensions uiExtensions(WebUiExtensionProperties webUiConfigProperties) throws IOException {
        org.zodiac.webui.extension.UiExtensionsScanner scanner = new org.zodiac.webui.extension.UiExtensionsScanner(this.applicationContext);
        org.zodiac.webui.extension.UiExtensions uiExtensions = scanner.scan(webUiConfigProperties.getExtensionResourceLocationsArray());
        uiExtensions.forEach((e) -> LOG.info("Loaded Web UI Extension: {}", e));
        return uiExtensions;
    }

    @Bean
    @ConditionalOnMissingBean
    protected org.zodiac.webui.controller.UiController homeUiController(WebUiExtensionProperties webUiConfigProperties, org.zodiac.webui.extension.UiExtensions uiExtensions) throws IOException {
        List extensionRoutes = new org.zodiac.webui.extension.UiRoutesScanner(this.applicationContext)
                .scan(webUiConfigProperties.getExtensionResourceLocationsArray());
        List routes = Stream.concat(DEFAULT_UI_ROUTES.stream(), extensionRoutes.stream())
                .collect(Collectors.toList());

        org.zodiac.webui.controller.UiController.Settings uiSettings = new org.zodiac.webui.controller.UiController.Settings(webUiConfigProperties.getTitle(), webUiConfigProperties.getBrand(), webUiConfigProperties.getLoginIcon(),
            webUiConfigProperties.getFavicon(), webUiConfigProperties.getFaviconDanger(), webUiConfigProperties.isNotificationFilterEnabled(),
            webUiConfigProperties.isRememberMeEnabled(), webUiConfigProperties.getAvailableLanguages(), routes, webUiConfigProperties.getExternalViews());

        String publicUrl = webUiConfigProperties.getPublicUrl() != null ? webUiConfigProperties.getPublicUrl()
                : webUiConfigProperties.getContextPath();
        return new org.zodiac.webui.controller.UiController(publicUrl, uiExtensions, uiSettings);
    }

    @Bean
    protected org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver webUiTemplateResolver(WebUiExtensionProperties webUiConfigProperties) {
        org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver resolver = new org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver();
        resolver.setApplicationContext(this.applicationContext);
        resolver.setPrefix(webUiConfigProperties.getTemplateLocation());
        resolver.setSuffix(MimeMappings.HTML_FILE_SUFFIX);
        resolver.setTemplateMode(org.thymeleaf.templatemode.TemplateMode.HTML);
        resolver.setCharacterEncoding(CharsetConstants.UTF_8.name());
        resolver.setCacheable(webUiConfigProperties.isCacheTemplates());
        resolver.setOrder(10);
        resolver.setCheckExistence(true);
        return resolver;
    }

    @SpringBootConfiguration
    @ConditionalOnWebUiExtensionEnabled
    @ConditionalOnWebApplication(type = Type.REACTIVE)
    @ConditionalOnClass(value = {org.zodiac.webui.config.WebUiExtensionInfo.class, org.thymeleaf.templatemode.TemplateMode.class, org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver.class, reactor.core.CorePublisher.class, org.springframework.web.reactive.HandlerMapping.class})
    public static class WebUiExtensionReactiveConfiguration {

        public WebUiExtensionReactiveConfiguration() {
            LOG.info("{} loaded.", getClass().getCanonicalName());
        }

        @SpringBootConfiguration
        @ConditionalOnWebUiExtensionEnabled
        @ConditionalOnClass(value = {org.zodiac.webui.config.WebUiExtensionInfo.class, org.thymeleaf.templatemode.TemplateMode.class, org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver.class, reactor.core.CorePublisher.class, org.springframework.web.reactive.HandlerMapping.class})
        public static class WebUiExtensionWebFluxConfiguration implements org.springframework.web.reactive.config.WebFluxConfigurer {

            private final WebUiExtensionProperties webUiConfigProperties;

            private final ApplicationContext applicationContext;

            public WebUiExtensionWebFluxConfiguration(WebUiExtensionProperties webUiConfigProperties,
                ApplicationContext applicationContext) {
                this.webUiConfigProperties = webUiConfigProperties;
                this.applicationContext = applicationContext;
                LOG.info("{} loaded.", getClass().getCanonicalName());
            }

            @Override
            public void addResourceHandlers(org.springframework.web.reactive.config.ResourceHandlerRegistry registry) {
                registry.addResourceHandler(webUiConfigProperties.path("/**"))
                        .addResourceLocations(webUiConfigProperties.getResourceLocationsArray())
                        .setCacheControl(webUiConfigProperties.getCache().toCacheControl());
                registry.addResourceHandler(webUiConfigProperties.path("/extensions/**"))
                        .addResourceLocations(webUiConfigProperties.getExtensionResourceLocationsArray())
                        .setCacheControl(webUiConfigProperties.getCache().toCacheControl());
            }

            @Bean
            @ConditionalOnMissingBean
            protected org.zodiac.webui.reactive.HomepageForwardingFilter homepageForwardFilter()
                    throws IOException {
                List extensionRoutes = new org.zodiac.webui.extension.UiRoutesScanner(this.applicationContext)
                        .scan(webUiConfigProperties.getExtensionResourceLocationsArray());
                List routes = Stream.concat(DEFAULT_UI_ROUTES.stream(), extensionRoutes.stream())
                        .map(webUiConfigProperties::path).collect(Collectors.toList());
                String homepage = webUiConfigProperties.path("/");
                return new org.zodiac.webui.reactive.HomepageForwardingFilter(homepage, routes,
                        DEFAULT_UI_ROUTE_EXCLUDES);
            }

        }

    }

    @SpringBootConfiguration
    @ConditionalOnWebUiExtensionEnabled
    @ConditionalOnWebApplication(type = Type.SERVLET)
    @ConditionalOnClass(value = {org.zodiac.webui.config.WebUiExtensionInfo.class, org.thymeleaf.templatemode.TemplateMode.class, org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver.class, javax.servlet.http.HttpServlet.class, org.springframework.web.servlet.HandlerMapping.class})
    public static class WebUiExtensionServletConfiguration {

        public WebUiExtensionServletConfiguration() {
            LOG.info("{} loaded.", getClass().getCanonicalName());
        }

        @SpringBootConfiguration
        @ConditionalOnWebUiExtensionEnabled
        @ConditionalOnClass(value = {org.zodiac.webui.config.WebUiExtensionInfo.class, org.thymeleaf.templatemode.TemplateMode.class, org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver.class, javax.servlet.http.HttpServlet.class, org.springframework.web.servlet.HandlerMapping.class})
        public static class WebUiExtensionWebMvcConfiguration implements org.springframework.web.servlet.config.annotation.WebMvcConfigurer {

            private final WebUiExtensionProperties webUiConfigProperties;

            private final ApplicationContext applicationContext;

            public WebUiExtensionWebMvcConfiguration(WebUiExtensionProperties webUiConfigProperties,
                ApplicationContext applicationContext) {
                this.webUiConfigProperties = webUiConfigProperties;
                this.applicationContext = applicationContext;
                LOG.info("{} loaded.", getClass().getCanonicalName());
            }

            @Override
            public void addResourceHandlers(
                    org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry registry) {
                registry.addResourceHandler(webUiConfigProperties.path("/**"))
                        .addResourceLocations(webUiConfigProperties.getResourceLocationsArray())
                        .setCacheControl(webUiConfigProperties.getCache().toCacheControl());
                registry.addResourceHandler(webUiConfigProperties.path("/extensions/**"))
                        .addResourceLocations(webUiConfigProperties.getExtensionResourceLocationsArray())
                        .setCacheControl(webUiConfigProperties.getCache().toCacheControl());
            }

            @Bean
            @ConditionalOnMissingBean
            protected org.zodiac.webui.servlet.HomepageForwardingFilter homepageForwardFilter()
                    throws IOException {
                List extensionRoutes = new org.zodiac.webui.extension.UiRoutesScanner(this.applicationContext)
                        .scan(webUiConfigProperties.getExtensionResourceLocationsArray());
                List routes = Stream.concat(DEFAULT_UI_ROUTES.stream(), extensionRoutes.stream())
                        .map(webUiConfigProperties::path).collect(Collectors.toList());
                String homepage = webUiConfigProperties.path("/");
                return new org.zodiac.webui.servlet.HomepageForwardingFilter(homepage, routes,
                        DEFAULT_UI_ROUTE_EXCLUDES);
            }

        }

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy