diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java index 9251a9dc6cf4..748b7f7f3a9a 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java @@ -19,6 +19,7 @@ import java.io.File; import java.net.InetAddress; import java.nio.charset.Charset; +import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.Map; @@ -30,6 +31,8 @@ import javax.servlet.SessionTrackingMode; import javax.validation.constraints.NotNull; +import io.undertow.Undertow; +import io.undertow.UndertowOptions; import org.apache.catalina.Context; import org.apache.catalina.connector.Connector; import org.apache.catalina.valves.AccessLogValve; @@ -50,12 +53,14 @@ import org.springframework.boot.context.embedded.tomcat.TomcatConnectorCustomizer; import org.springframework.boot.context.embedded.tomcat.TomcatContextCustomizer; import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; +import org.springframework.boot.context.embedded.undertow.UndertowBuilderCustomizer; import org.springframework.boot.context.embedded.undertow.UndertowEmbeddedServletContainerFactory; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.DeprecatedConfigurationProperty; import org.springframework.boot.context.properties.NestedConfigurationProperty; import org.springframework.core.Ordered; import org.springframework.util.StringUtils; +import org.xnio.Option; /** * {@link ConfigurationProperties} for a web server (e.g. port and path settings). Will be @@ -847,6 +852,11 @@ public static class Undertow { private final Accesslog accesslog = new Accesslog(); + /** + * + */ + private ServerOption serverOption; + public Integer getBufferSize() { return this.bufferSize; } @@ -954,6 +964,14 @@ public void setAccessLogDir(File accessLogDir) { getAccesslog().setDir(accessLogDir); } + public ServerOption getServerOption() { + return this.serverOption; + } + + public void setServerOption(ServerOption serverOption) { + this.serverOption = serverOption; + } + void customizeUndertow(UndertowEmbeddedServletContainerFactory factory) { factory.setBufferSize(this.bufferSize); factory.setBuffersPerRegion(this.buffersPerRegion); @@ -963,6 +981,34 @@ void customizeUndertow(UndertowEmbeddedServletContainerFactory factory) { factory.setAccessLogDirectory(this.accesslog.dir); factory.setAccessLogPattern(this.accesslog.pattern); factory.setAccessLogEnabled(this.accesslog.enabled); + + if (this.serverOption != null) { + UndertowBuilderCustomizer customizer = new UndertowBuilderCustomizer() { + @Override + public void customize(io.undertow.Undertow.Builder builder) { + builder.setServerOption(serverOption.getOption(), false); + } + }; + + factory.setBuilderCustomizers(Arrays.asList(customizer)); + } + } + + public enum ServerOption { + + SPDY(UndertowOptions.ENABLE_SPDY), + HTTP2(UndertowOptions.ENABLE_HTTP2); + + private Option option; + + ServerOption(Option option) { + this.option = option; + } + + public Option getOption() { + return this.option; + } + } public static class Accesslog { diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java index 3592e24e3241..7c5b4fac26fa 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java @@ -40,6 +40,8 @@ import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer; import org.springframework.boot.context.embedded.ServletContextInitializer; import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; +import org.springframework.boot.context.embedded.undertow.UndertowBuilderCustomizer; +import org.springframework.boot.context.embedded.undertow.UndertowEmbeddedServletContainerFactory; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.core.IsInstanceOf.instanceOf; @@ -313,6 +315,18 @@ public void customTomcatRemoteIpValve() throws Exception { assertEquals("192.168.0.1", remoteIpValve.getInternalProxies()); } + @Test + public void enableUndertowHttp2() { + Map map = new HashMap(); + map.put("server.undertow.server-option", "HTTP2"); + bindProperties(map); + + UndertowEmbeddedServletContainerFactory container = new UndertowEmbeddedServletContainerFactory(); + this.properties.customize(container); + + assertEquals(1, container.getBuilderCustomizers().size()); + } + private void bindProperties(Map map) { new RelaxedDataBinder(this.properties, "server").bind(new MutablePropertyValues( map));