From eb9f635004798cae9ac71166ac3bb04dff93c322 Mon Sep 17 00:00:00 2001 From: qct Date: Thu, 20 Sep 2018 12:01:16 +0000 Subject: [PATCH] Migrate size properties to DataSize See gh-14549 --- .../autoconfigure/web/ServerProperties.java | 42 +++++++++---------- .../JettyWebServerFactoryCustomizer.java | 3 +- .../TomcatWebServerFactoryCustomizer.java | 9 ++-- .../UndertowWebServerFactoryCustomizer.java | 6 ++- .../web/ServerPropertiesTests.java | 6 +-- .../appendix-application-properties.adoc | 4 +- 6 files changed, 36 insertions(+), 34 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java index 953f0328e7..82844cd272 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java @@ -325,14 +325,14 @@ public class ServerProperties { private int minSpareThreads = 10; /** - * Maximum size, in bytes, of the HTTP post content. + * Maximum size of the HTTP post content. */ - private int maxHttpPostSize = 2097152; + private DataSize maxHttpPostSize = DataSize.ofMegabytes(2); /** * Maximum size, in bytes, of the HTTP message header. */ - private int maxHttpHeaderSize = 0; + private DataSize maxHttpHeaderSize = DataSize.ofBytes(0); /** * Maximum amount of request body to swallow. @@ -397,11 +397,11 @@ public class ServerProperties { this.minSpareThreads = minSpareThreads; } - public int getMaxHttpPostSize() { + public DataSize getMaxHttpPostSize() { return this.maxHttpPostSize; } - public void setMaxHttpPostSize(int maxHttpPostSize) { + public void setMaxHttpPostSize(DataSize maxHttpPostSize) { this.maxHttpPostSize = maxHttpPostSize; } @@ -499,12 +499,12 @@ public class ServerProperties { @Deprecated @DeprecatedConfigurationProperty(replacement = "server.max-http-header-size") - public int getMaxHttpHeaderSize() { + public DataSize getMaxHttpHeaderSize() { return this.maxHttpHeaderSize; } @Deprecated - public void setMaxHttpHeaderSize(int maxHttpHeaderSize) { + public void setMaxHttpHeaderSize(DataSize maxHttpHeaderSize) { this.maxHttpHeaderSize = maxHttpHeaderSize; } @@ -722,9 +722,9 @@ public class ServerProperties { private final Accesslog accesslog = new Accesslog(); /** - * Maximum size, in bytes, of the HTTP post or put content. + * Maximum size of the HTTP post or put content. */ - private int maxHttpPostSize = 200000; // bytes + private DataSize maxHttpPostSize = DataSize.ofBytes(200000); /** * Number of acceptor threads to use. When the value is -1, the default, the @@ -742,11 +742,11 @@ public class ServerProperties { return this.accesslog; } - public int getMaxHttpPostSize() { + public DataSize getMaxHttpPostSize() { return this.maxHttpPostSize; } - public void setMaxHttpPostSize(int maxHttpPostSize) { + public void setMaxHttpPostSize(DataSize maxHttpPostSize) { this.maxHttpPostSize = maxHttpPostSize; } @@ -937,16 +937,16 @@ public class ServerProperties { public static class Undertow { /** - * Maximum size, in bytes, of the HTTP post content. When the value is -1, the - * default, the size is unlimited. + * Maximum size of the HTTP post content. When the value is -1, the default, the + * size is unlimited. */ - private long maxHttpPostSize = -1; // bytes + private DataSize maxHttpPostSize = DataSize.ofBytes(-1); /** - * Size of each buffer, in bytes. The default is derived from the maximum amount - * of memory that is available to the JVM. + * Size of each buffer. The default is derived from the maximum amount of memory + * that is available to the JVM. */ - private Integer bufferSize; + private DataSize bufferSize = DataSize.ofBytes(0); /** * Number of I/O threads to create for the worker. The default is derived from the @@ -972,19 +972,19 @@ public class ServerProperties { private final Accesslog accesslog = new Accesslog(); - public long getMaxHttpPostSize() { + public DataSize getMaxHttpPostSize() { return this.maxHttpPostSize; } - public void setMaxHttpPostSize(long maxHttpPostSize) { + public void setMaxHttpPostSize(DataSize maxHttpPostSize) { this.maxHttpPostSize = maxHttpPostSize; } - public Integer getBufferSize() { + public DataSize getBufferSize() { return this.bufferSize; } - public void setBufferSize(Integer bufferSize) { + public void setBufferSize(DataSize bufferSize) { this.bufferSize = bufferSize; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/JettyWebServerFactoryCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/JettyWebServerFactoryCustomizer.java index 5dab73bd83..b6b8129a88 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/JettyWebServerFactoryCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/JettyWebServerFactoryCustomizer.java @@ -78,7 +78,8 @@ public class JettyWebServerFactoryCustomizer implements .asInt(DataSize::toBytes) .to((maxHttpHeaderSize) -> customizeMaxHttpHeaderSize(factory, maxHttpHeaderSize)); - propertyMapper.from(jettyProperties::getMaxHttpPostSize).when(this::isPositive) + propertyMapper.from(jettyProperties::getMaxHttpPostSize).whenNonNull() + .asInt(DataSize::toBytes) .to((maxHttpPostSize) -> customizeMaxHttpPostSize(factory, maxHttpPostSize)); propertyMapper.from(properties::getConnectionTimeout).whenNonNull() diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java index 3bf75348d2..cd8e8d6e84 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java @@ -92,8 +92,8 @@ public class TomcatWebServerFactoryCustomizer implements propertyMapper.from(tomcatProperties::getMaxSwallowSize).whenNonNull() .asInt(DataSize::toBytes) .to((maxSwallowSize) -> customizeMaxSwallowSize(factory, maxSwallowSize)); - propertyMapper.from(tomcatProperties::getMaxHttpPostSize) - .when((maxHttpPostSize) -> maxHttpPostSize != 0) + propertyMapper.from(tomcatProperties::getMaxHttpPostSize).whenNonNull() + .asInt(DataSize::toBytes) .to((maxHttpPostSize) -> customizeMaxHttpPostSize(factory, maxHttpPostSize)); propertyMapper.from(tomcatProperties::getAccesslog) @@ -118,9 +118,8 @@ public class TomcatWebServerFactoryCustomizer implements @SuppressWarnings("deprecation") private DataSize determineMaxHttpHeaderSize() { - return isPositive(this.serverProperties.getTomcat().getMaxHttpHeaderSize()) - ? DataSize - .ofBytes(this.serverProperties.getTomcat().getMaxHttpHeaderSize()) + return (this.serverProperties.getTomcat().getMaxHttpHeaderSize().toBytes() > 0) + ? this.serverProperties.getTomcat().getMaxHttpHeaderSize() : this.serverProperties.getMaxHttpHeaderSize(); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/UndertowWebServerFactoryCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/UndertowWebServerFactoryCustomizer.java index c08ef547db..2ba3e644ab 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/UndertowWebServerFactoryCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/UndertowWebServerFactoryCustomizer.java @@ -64,7 +64,8 @@ public class UndertowWebServerFactoryCustomizer implements ServerProperties.Undertow.Accesslog accesslogProperties = undertowProperties .getAccesslog(); PropertyMapper propertyMapper = PropertyMapper.get().alwaysApplyingWhenNonNull(); - propertyMapper.from(undertowProperties::getBufferSize).to(factory::setBufferSize); + propertyMapper.from(undertowProperties::getBufferSize).whenNonNull() + .asInt(DataSize::toBytes).to(factory::setBufferSize); propertyMapper.from(undertowProperties::getIoThreads).to(factory::setIoThreads); propertyMapper.from(undertowProperties::getWorkerThreads) .to(factory::setWorkerThreads); @@ -88,7 +89,8 @@ public class UndertowWebServerFactoryCustomizer implements .asInt(DataSize::toBytes) .to((maxHttpHeaderSize) -> customizeMaxHttpHeaderSize(factory, maxHttpHeaderSize)); - propertyMapper.from(undertowProperties::getMaxHttpPostSize).when(this::isPositive) + propertyMapper.from(undertowProperties::getMaxHttpPostSize).whenNonNull() + .asInt(DataSize::toBytes) .to((maxHttpPostSize) -> customizeMaxHttpPostSize(factory, maxHttpPostSize)); propertyMapper.from(properties::getConnectionTimeout) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java index b909677ea3..6a9a849838 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java @@ -226,7 +226,7 @@ public class ServerPropertiesTests { @Test public void tomcatMaxHttpPostSizeMatchesConnectorDefault() throws Exception { - assertThat(this.properties.getTomcat().getMaxHttpPostSize()) + assertThat(this.properties.getTomcat().getMaxHttpPostSize().toBytes()) .isEqualTo(getDefaultConnector().getMaxPostSize()); } @@ -333,7 +333,7 @@ public class ServerPropertiesTests { String message = failure.get().getCause().getMessage(); int defaultMaxPostSize = Integer .valueOf(message.substring(message.lastIndexOf(' ')).trim()); - assertThat(this.properties.getJetty().getMaxHttpPostSize()) + assertThat(this.properties.getJetty().getMaxHttpPostSize().toBytes()) .isEqualTo(defaultMaxPostSize); } finally { @@ -343,7 +343,7 @@ public class ServerPropertiesTests { @Test public void undertowMaxHttpPostSizeMatchesDefault() { - assertThat(this.properties.getUndertow().getMaxHttpPostSize()) + assertThat(this.properties.getUndertow().getMaxHttpPostSize().toBytes()) .isEqualTo(UndertowOptions.DEFAULT_MAX_ENTITY_SIZE); } diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index e14a368bf3..06c2a19b28 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -263,7 +263,7 @@ content into your application. Rather, pick only the properties that you need. 172\\.2[0-9]{1}\\.\\d{1,3}\\.\\d{1,3}|\\ 172\\.3[0-1]{1}\\.\\d{1,3}\\.\\d{1,3} # Regular expression matching trusted IP addresses. server.tomcat.max-connections=10000 # Maximum number of connections that the server will accept and process at any given time. - server.tomcat.max-http-post-size=2097152 # Maximum size in bytes of the HTTP post content. + server.tomcat.max-http-post-size=2MB # Maximum size of the HTTP post content. server.tomcat.max-swallow-size=2MB # Maximum amount of request body to swallow. server.tomcat.max-threads=200 # Maximum amount of worker threads. server.tomcat.min-spare-threads=10 # Minimum amount of worker threads. @@ -282,7 +282,7 @@ content into your application. Rather, pick only the properties that you need. server.undertow.accesslog.prefix=access_log. # Log file name prefix. server.undertow.accesslog.rotate=true # Whether to enable access log rotation. server.undertow.accesslog.suffix=log # Log file name suffix. - server.undertow.buffer-size= # Size of each buffer, in bytes. + server.undertow.buffer-size=0 # Size of each buffer, in bytes. server.undertow.direct-buffers= # Allocate buffers outside the Java heap. The default is derived from the maximum amount of memory that is available to the JVM. server.undertow.eager-filter-init=true # Whether servlet filters should be initialized on startup. server.undertow.io-threads= # Number of I/O threads to create for the worker. The default is derived from the number of available processors.