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

org.zodiac.autoconfigure.server.proxy.ProxyServerConfiguration Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
package org.zodiac.autoconfigure.server.proxy;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.Optional;

import org.springframework.beans.factory.ObjectProvider;
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.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.servlet.MultipartProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;
import org.zodiac.autoconfigure.netty2.NettyServerProperties;
import org.zodiac.commons.constants.SystemPropertiesConstants;
import org.zodiac.commons.util.Colls;

@SpringBootConfiguration
@ConditionalOnProperty(name = org.zodiac.server.proxy.constants.ProxyServerSystemPropertiesConstants.SERVER_PROXY_ENABLED, havingValue = "true")
@ConditionalOnClass(value = {org.zodiac.server.proxy.ProxyManager.class, org.zodiac.netty.core.config.NettyServerInfo.class, org.zodiac.server.base.impl.AbstractBaseNettyServer.class})
class ProxyServerConfiguration {

    @Bean
    @ConditionalOnMissingBean
    @ConfigurationProperties(prefix = SystemPropertiesConstants.Zodiac.SERVER_NETTY_PREFIX, ignoreInvalidFields = true)
    protected NettyServerHttpProxyProperties nettyServerHttpProxyProperties(
        ObjectProvider serverPropertiesProvider,
        ObjectProvider multipartPropertiesProvider,
        NettyServerProperties nettyServerProperties) {
        NettyServerHttpProxyProperties nettyServerHttpProxyProperties =
            new NettyServerHttpProxyProperties(nettyServerProperties);
        Optional.ofNullable(serverPropertiesProvider.getIfAvailable()).ifPresent(p -> {
            nettyServerHttpProxyProperties.getHandler().setEnableSsl(null != p.getSsl() && p.getSsl().isEnabled());
            nettyServerHttpProxyProperties.getHttp().getHttp20()
                .setEnabled(null != p.getHttp2() && p.getHttp2().isEnabled());
        });
        Optional.ofNullable(multipartPropertiesProvider.getIfAvailable()).ifPresent(p -> {
            nettyServerHttpProxyProperties.getHttp().getServlet().getMultipart().setEnabled(p.getEnabled())
                .setFileSizeThreshold(p.getFileSizeThreshold()).setLocation(p.getLocation())
                .setMaxFileSize(p.getMaxFileSize()).setMaxRequestSize(p.getMaxFileSize())
                .setResolveLazily(p.isResolveLazily());
        });
        return nettyServerHttpProxyProperties;
    }

    @Bean
    @ConditionalOnMissingBean
    @ConfigurationProperties(prefix = org.zodiac.server.proxy.constants.ProxyServerSystemPropertiesConstants.SERVER_PROXY_PREFIX, ignoreInvalidFields = true)
    protected NettyServerProxySettingsProperties nettyServerProxySettingsProperties(ObjectProvider> httpRequestFilterPluginOptionsListProvider,
        ObjectProvider> httpResponseFilterPluginOptionsListProvider) {
        Map httpRequestFilterPluginOptionsList =
                httpRequestFilterPluginOptionsListProvider.getIfAvailable();
        Map httpResponseFilterPluginOptionsList =
                httpResponseFilterPluginOptionsListProvider.getIfAvailable();
        NettyServerProxySettingsProperties properties = new NettyServerProxySettingsProperties();
        if (Colls.notEmptyMap(httpRequestFilterPluginOptionsList)) {
            properties.getPlugins().getHttpRequest().putAll(httpRequestFilterPluginOptionsList);
        }
        if (Colls.notEmptyMap(httpResponseFilterPluginOptionsList)) {
            properties.getPlugins().getHttpResponse().putAll(httpResponseFilterPluginOptionsList);
        }
        return properties;
    }

    @Bean
    @Order(value = Integer.MIN_VALUE)
    @ConditionalOnMissingBean
    protected NettyServerHttpProxyProperties nettyServerHttpProxyProperties(
        NettyServerProperties nettyServerProperties,
        NettyServerProxySettingsProperties nettyServerProxySettingsProperties) {
        NettyServerHttpProxyProperties nettyServerHttpProxyProperties =
            new NettyServerHttpProxyProperties(nettyServerProperties);
        org.zodiac.server.proxy.config.adaptor.NettyServerProxySettingsInfo proxySettings =
            nettyServerHttpProxyProperties.getProxy();
        proxySettings.setEnabled(nettyServerProxySettingsProperties.isEnabled());
        proxySettings.getProxyRules().putAll(nettyServerProxySettingsProperties.getProxyRules());
        proxySettings.getProxyRules().putAll(nettyServerProxySettingsProperties.getProxyRules());
        proxySettings.getUpstreams().putAll(nettyServerProxySettingsProperties.getUpstreams());
        return nettyServerHttpProxyProperties;
    }

    @Bean
    @ConditionalOnMissingBean
    protected org.zodiac.server.proxy.http.HttpProxyServer httpProxyServer(
        NettyServerHttpProxyProperties nettyServerHttpProxyProperties,
        ObjectProvider> activityTrackersProvider,
        ObjectProvider hostResolverProvider,
        ObjectProvider namingClientProvider) {
        org.zodiac.server.proxy.http.HttpProxyServer httpProxyServer = new org.zodiac.server.proxy.http.HttpProxyServer(
            nettyServerHttpProxyProperties, activityTrackersProvider.getIfAvailable(),
            hostResolverProvider.getIfAvailable(), namingClientProvider.getIfAvailable());

        initPlugin(nettyServerHttpProxyProperties.getProxy().getPlugins());

        return httpProxyServer;
    }

    private void initPlugin(
        org.zodiac.server.proxy.config.adaptor.NettyServerProxyPluginsSettingsInfo proxyPluginsSettingsInfo) {
        initHttpRequestFilterPlugin(proxyPluginsSettingsInfo);
        initHttpResponseFilterPlugin(proxyPluginsSettingsInfo);
    }

    private void initHttpRequestFilterPlugin(
        org.zodiac.server.proxy.config.adaptor.NettyServerProxyPluginsSettingsInfo proxyPluginsSettingsInfo) {
        for (Entry entry : proxyPluginsSettingsInfo
                .getHttpRequest().entrySet()) {
            String pluginKey = entry.getKey();
            org.zodiac.server.proxy.http.config.simple.HttpProxyRequestFilterPluginOption pluginOption =
                entry.getValue();
            org.zodiac.server.proxy.http.plugin.filter.request.AbstractHttpRequestFilter requestFilter =
                org.zodiac.server.proxy.http.plugin.filter.SingletonHttpFilterBeanRegistrar.getInstance()
                    .getHttpRequestFilter(pluginKey);
            if (null != requestFilter) {
                requestFilter.setInputEncoding(pluginOption.getInputEncoding())
                    .setOutputEncoding(pluginOption.getOutputEncoding())
                    .setContentPatterns(pluginOption.getContentPatterns().stream().map(p -> Pattern.compile(p))
                        .collect(Collectors.toSet()))
                    .setFilePatterns(pluginOption.getFilePatterns().stream().map(p -> Pattern.compile(p))
                        .collect(Collectors.toSet()));
            }
        }
    }

    private void initHttpResponseFilterPlugin(
        org.zodiac.server.proxy.config.adaptor.NettyServerProxyPluginsSettingsInfo proxyPluginsSettingsInfo) {
        for (Entry entry : proxyPluginsSettingsInfo
                .getHttpResponse().entrySet()) {
            String pluginKey = entry.getKey();
            org.zodiac.server.proxy.http.config.simple.HttpProxyResponseFilterPluginOption pluginOption =
                entry.getValue();
            org.zodiac.server.proxy.http.plugin.filter.response.AbstractHttpResponseFilter responseFilter =
                org.zodiac.server.proxy.http.plugin.filter.SingletonHttpFilterBeanRegistrar.getInstance()
                    .getHttpResponseFilter(pluginKey);
            if (null != responseFilter) {
                responseFilter.setInputEncoding(pluginOption.getInputEncoding())
                    .setOutputEncoding(pluginOption.getOutputEncoding())
                    .setContentPatterns(pluginOption.getContentPatterns().stream().map(p -> Pattern.compile(p))
                        .collect(Collectors.toSet()))
                    .setFilePatterns(pluginOption.getFilePatterns().stream().map(p -> Pattern.compile(p))
                        .collect(Collectors.toSet()));
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy