diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/MultipartAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/MultipartAutoConfiguration.java index 369d79162c0..ae9715a45fa 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/MultipartAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/MultipartAutoConfiguration.java @@ -25,11 +25,9 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext; -import org.springframework.boot.context.embedded.MultipartConfigFactory; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.util.StringUtils; import org.springframework.web.multipart.support.StandardServletMultipartResolver; /** @@ -41,7 +39,7 @@ import org.springframework.web.multipart.support.StandardServletMultipartResolve *
* The {@link javax.servlet.MultipartConfigElement} is a Servlet API that's used to * configure how the container handles file uploads. By default - * + * * @author Greg Turnquist * @author Josh Long */ @@ -57,25 +55,7 @@ public class MultipartAutoConfiguration { @Bean @ConditionalOnMissingBean public MultipartConfigElement multipartConfigElement() { - MultipartConfigFactory factory = new MultipartConfigFactory(); - - if (StringUtils.hasText(this.multipartProperties.getFileSizeThreshold())) { - factory.setFileSizeThreshold(this.multipartProperties.getFileSizeThreshold()); - } - - if (StringUtils.hasText(this.multipartProperties.getLocation())) { - factory.setLocation(this.multipartProperties.getLocation()); - } - - if (StringUtils.hasText(this.multipartProperties.getMaxRequestSize())) { - factory.setMaxRequestSize(this.multipartProperties.getMaxRequestSize()); - } - - if (StringUtils.hasText(this.multipartProperties.getMaxFileSize())) { - factory.setMaxFileSize(this.multipartProperties.getMaxFileSize()); - } - - return factory.createMultipartConfig(); + return this.multipartProperties.createMultipartConfig(); } @Bean @@ -83,4 +63,5 @@ public class MultipartAutoConfiguration { public StandardServletMultipartResolver multipartResolver() { return new StandardServletMultipartResolver(); } + } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/MultipartProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/MultipartProperties.java index 91d8e49bc92..8b0e6a737bf 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/MultipartProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/MultipartProperties.java @@ -16,65 +16,99 @@ package org.springframework.boot.autoconfigure.web; +import javax.servlet.MultipartConfigElement; + +import org.springframework.boot.context.embedded.MultipartConfigFactory; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.util.StringUtils; /** - * Properties to be used in configuring - * a javax.servlet.MultipartConfigElement. - * - * {@literal multipart.fileSizeThreshold} specifies the size threshold after which files will be written to disk. Default is 0, which means that the file will be written to disk immediately. - * {@literal multipart.location} specifies the directory where files will be stored. The default is "". A common value is to use the system's temporary directory, which can be obtained - * {@literal multipart.maxFileSize} specifies the maximum size permitted for uploaded files. The default is unlimited. - * {@literal multipart.maxRequestSize} specifies the maximum size allowed for {@literal multipart/form-data} requests. - * - * These properties are ultimately passed through {@link org.springframework.boot.context.embedded.MultipartConfigFactory} - * which means you may specify the values using {@literal long} values or using more readable {@literal String} - * variants that accept {@literal KB} or {@literal MB} suffixes. - * + * Properties to be used in configuring a {@link MultipartConfigElement}. + *+ * These properties are ultimately passed through + * {@link org.springframework.boot.context.embedded.MultipartConfigFactory} which means + * you may specify the values using {@literal long} values or using more readable + * {@literal String} variants that accept {@literal Kb} or {@literal Mb} suffixes. + * * @author Josh Long + * @since 1.1.0 */ @ConfigurationProperties(prefix = "multipart", ignoreUnknownFields = false) public class MultipartProperties { - private String maxFileSize = "1Mb"; + private String location; - private String maxRequestSize = "10Mb"; + private String maxFileSize = "1Mb"; - private String fileSizeThreshold = null; + private String maxRequestSize = "10Mb"; - private String location = null; + private String fileSizeThreshold = "0"; - public String getMaxFileSize() { - return maxFileSize; - } + public String getMaxFileSize() { + return this.maxFileSize; + } - public String getMaxRequestSize() { - return maxRequestSize; - } + public String getMaxRequestSize() { + return this.maxRequestSize; + } - public String getFileSizeThreshold() { - return fileSizeThreshold; - } + public String getFileSizeThreshold() { + return this.fileSizeThreshold; + } - public String getLocation() { - return location; - } + public String getLocation() { + return this.location; + } - public void setMaxFileSize(String maxFileSize) { - this.maxFileSize = maxFileSize; - } + public void setMaxFileSize(String maxFileSize) { + this.maxFileSize = maxFileSize; + } - public void setMaxRequestSize(String maxRequestSize) { - this.maxRequestSize = maxRequestSize; - } + public void setMaxRequestSize(String maxRequestSize) { + this.maxRequestSize = maxRequestSize; + } - public void setLocation(String location) { - this.location = location; - } + public void setLocation(String location) { + this.location = location; + } - public void setFileSizeThreshold(String fileSizeThreshold) { - this.fileSizeThreshold = fileSizeThreshold; - } + public void setFileSizeThreshold(String fileSizeThreshold) { + this.fileSizeThreshold = fileSizeThreshold; + } + + /** + * Create a new {@link MultipartConfigElement} using the + * @return a new {@link MultipartConfigElement} configured using there properties + */ + public MultipartConfigElement createMultipartConfig() { + MultipartConfigFactory factory = new MultipartConfigFactory(); + if (StringUtils.hasText(this.fileSizeThreshold)) { + factory.setFileSizeThreshold(this.fileSizeThreshold); + } + if (StringUtils.hasText(this.location)) { + factory.setLocation(this.location); + } + if (StringUtils.hasText(this.maxRequestSize)) { + factory.setMaxRequestSize(this.maxRequestSize); + } + if (StringUtils.hasText(this.maxFileSize)) { + factory.setMaxFileSize(this.maxFileSize); + } + return factory.createMultipartConfig(); + } } - diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/MultipartAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/MultipartAutoConfigurationTests.java index 79fd73d4b98..a6fd7b11cb9 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/MultipartAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/MultipartAutoConfigurationTests.java @@ -38,15 +38,17 @@ import org.springframework.web.multipart.support.StandardServletMultipartResolve import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import static org.hamcrest.Matchers.equalTo; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertThat; /** * Tests for {@link MultipartAutoConfiguration}. Tests an empty configuration, no * multipart configuration, and a multipart configuration (with both Jetty and Tomcat). - * + * * @author Greg Turnquist * @author Dave Syer * @author Josh Long @@ -71,10 +73,10 @@ public class MultipartAutoConfigurationTests { ContainerWithNothing.class, BaseConfiguration.class); DispatcherServlet servlet = this.context.getBean(DispatcherServlet.class); assertNotNull(servlet.getMultipartResolver()); - assertEquals(1, - this.context.getBeansOfType(StandardServletMultipartResolver.class) - .size()); - assertEquals(1, this.context.getBeansOfType(MultipartResolver.class).size()); + assertThat(this.context.getBeansOfType(StandardServletMultipartResolver.class) + .size(), equalTo(1)); + assertThat(this.context.getBeansOfType(MultipartResolver.class).size(), + equalTo(1)); } @Configuration @@ -87,10 +89,10 @@ public class MultipartAutoConfigurationTests { ContainerWithNoMultipartJetty.class, BaseConfiguration.class); DispatcherServlet servlet = this.context.getBean(DispatcherServlet.class); assertNotNull(servlet.getMultipartResolver()); - assertEquals(1, - this.context.getBeansOfType(StandardServletMultipartResolver.class) - .size()); - assertEquals(1, this.context.getBeansOfType(MultipartResolver.class).size()); + assertThat(this.context.getBeansOfType(StandardServletMultipartResolver.class) + .size(), equalTo(1)); + assertThat(this.context.getBeansOfType(MultipartResolver.class).size(), + equalTo(1)); verifyServletWorks(); } @@ -113,10 +115,10 @@ public class MultipartAutoConfigurationTests { ContainerWithNoMultipartTomcat.class, BaseConfiguration.class); DispatcherServlet servlet = this.context.getBean(DispatcherServlet.class); assertNull(servlet.getMultipartResolver()); - assertEquals(1, - this.context.getBeansOfType(StandardServletMultipartResolver.class) - .size()); - assertEquals(1, this.context.getBeansOfType(MultipartResolver.class).size()); + assertThat(this.context.getBeansOfType(StandardServletMultipartResolver.class) + .size(), equalTo(1)); + assertThat(this.context.getBeansOfType(MultipartResolver.class).size(), + equalTo(1)); verifyServletWorks(); } @@ -134,7 +136,9 @@ public class MultipartAutoConfigurationTests { public void containerWithAutomatedMultipartTomcatConfiguration() throws Exception { this.context = new AnnotationConfigEmbeddedWebApplicationContext( ContainerWithEverythingTomcat.class, BaseConfiguration.class); - new RestTemplate().getForObject("http://localhost:8080/", String.class); + new RestTemplate().getForObject("http://localhost:" + + this.context.getEmbeddedServletContainer().getPort() + "/", + String.class); this.context.getBean(MultipartConfigElement.class); assertSame(this.context.getBean(DispatcherServlet.class).getMultipartResolver(), this.context.getBean(StandardServletMultipartResolver.class)); @@ -149,8 +153,9 @@ public class MultipartAutoConfigurationTests { .addFirst(new PropertySource