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

org.zodiac.autoconfigure.web.server.ReactiveWebServerCustomizerConfiguration Maven / Gradle / Ivy

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

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.web.embedded.jetty.JettyReactiveWebServerFactory;
import org.springframework.boot.web.embedded.tomcat.TomcatReactiveWebServerFactory;
import org.springframework.boot.web.embedded.undertow.UndertowReactiveWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.zodiac.commons.util.Asserts;
import org.zodiac.commons.util.Classes;

@SpringBootConfiguration
@ConditionalOnWebApplication(type = Type.REACTIVE)
class ReactiveWebServerCustomizerConfiguration {

    public ReactiveWebServerCustomizerConfiguration() {
    }

    @SpringBootConfiguration
    @ConditionalOnWebApplication(type = Type.REACTIVE)
    @ConditionalOnClass(value = {org.springframework.web.server.WebHandler.class, org.apache.catalina.startup.Tomcat.class, org.apache.coyote.UpgradeProtocol.class})
    static class TomcatConfiguration {

        private final ServerProperties serverProperties;

        public TomcatConfiguration(ServerProperties serverProperties) {
            this.serverProperties = serverProperties;
        }

        @Bean
        protected WebServerFactoryCustomizer tomcatServerFactoryCustomizer() {
            return factory -> factory.addConnectorCustomizers(connector -> {
                /*开启HTTP2 H2c 配置*/
                enableHttp2(connector);
            });
        }

        private void enableHttp2(org.apache.catalina.connector.Connector connector) {
            connector.addUpgradeProtocol(new org.apache.coyote.http2.Http2Protocol());
        }

    }

    @SpringBootConfiguration
    @ConditionalOnWebApplication(type = Type.REACTIVE)
    @ConditionalOnClass(value = {org.springframework.web.server.WebHandler.class, org.eclipse.jetty.server.Server.class, org.eclipse.jetty.servlet.ServletHolder.class})
    static class JettyConfiguration {

        private final ServerProperties serverProperties;

        public JettyConfiguration(ServerProperties serverProperties) {
            this.serverProperties = serverProperties;
        }

        @Bean
        protected WebServerFactoryCustomizer jettyServerFactoryCustomizer() {
            return factory -> factory.addServerCustomizers(server -> {
                /*开启HTTP2 H2c 配置*/
                enableHttp2(server);
            });
        }

        private void enableHttp2(org.eclipse.jetty.server.Server server) {
            Asserts.state(isAlpnPresent(),
                () -> "The 'org.eclipse.jetty:jetty-alpn-server' dependency is required for HTTP/2 support.");
            Asserts.state(isConscryptPresent(), () -> "The 'org.eclipse.jetty.http2:http2-server' and Conscrypt "
                + "dependencies are required for HTTP/2 support.");
        }

        private boolean isAlpnPresent() {
            return Classes.isPresent("org.eclipse.jetty.http2.server.HTTP2ServerConnectionFactory", null);
        }

        private boolean isConscryptPresent() {
            return Classes.isPresent("org.conscrypt.Conscrypt", null);
        }

    }

    @SpringBootConfiguration
    @ConditionalOnWebApplication(type = Type.REACTIVE)
    @ConditionalOnClass(value = {org.springframework.web.server.WebHandler.class, io.undertow.Undertow.class, org.xnio.SslClientAuthMode.class})
    static class UndertowConfiguration {

        private final ServerProperties serverProperties;

        public UndertowConfiguration(ServerProperties serverProperties) {
            this.serverProperties = serverProperties;
        }

        @Bean
        protected WebServerFactoryCustomizer undertowServerFactoryCustomizer() {
            return factory -> factory.addBuilderCustomizers(builder -> {
                /*开启HTTP2 H2c 配置*/
                enableHttp2(builder);
            });
        }

        private void enableHttp2(io.undertow.Undertow.Builder builder) {
            builder.setServerOption(io.undertow.UndertowOptions.ENABLE_HTTP2, true);
        }

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy