Commit 8a5c026a authored by making's avatar making Committed by Brian Clozel

Add spring.http.multipart.resolve-lazily

This commit adds the `spring.http.multipart.resolve-lazily`
property, in order to configure whether the multipart request
should be resolved lazily, as the parameters are accessed.
parent f0259c82
...@@ -43,6 +43,7 @@ import org.springframework.web.servlet.DispatcherServlet; ...@@ -43,6 +43,7 @@ import org.springframework.web.servlet.DispatcherServlet;
* *
* @author Greg Turnquist * @author Greg Turnquist
* @author Josh Long * @author Josh Long
* @author Toshiaki Maki
*/ */
@Configuration @Configuration
@ConditionalOnClass({ Servlet.class, StandardServletMultipartResolver.class, @ConditionalOnClass({ Servlet.class, StandardServletMultipartResolver.class,
...@@ -66,7 +67,9 @@ public class MultipartAutoConfiguration { ...@@ -66,7 +67,9 @@ public class MultipartAutoConfiguration {
@Bean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME) @Bean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME)
@ConditionalOnMissingBean(MultipartResolver.class) @ConditionalOnMissingBean(MultipartResolver.class)
public StandardServletMultipartResolver multipartResolver() { public StandardServletMultipartResolver multipartResolver() {
return new StandardServletMultipartResolver(); StandardServletMultipartResolver multipartResolver = new StandardServletMultipartResolver();
multipartResolver.setResolveLazily(multipartProperties.isResolveLazily());
return multipartResolver;
} }
} }
...@@ -42,6 +42,7 @@ import org.springframework.util.StringUtils; ...@@ -42,6 +42,7 @@ import org.springframework.util.StringUtils;
* {@literal String} variants that accept {@literal Kb} or {@literal Mb} suffixes. * {@literal String} variants that accept {@literal Kb} or {@literal Mb} suffixes.
* *
* @author Josh Long * @author Josh Long
* @author Toshiaki Maki
* @since 1.1.0 * @since 1.1.0
*/ */
@ConfigurationProperties(prefix = "spring.http.multipart", ignoreUnknownFields = false) @ConfigurationProperties(prefix = "spring.http.multipart", ignoreUnknownFields = false)
...@@ -75,6 +76,12 @@ public class MultipartProperties { ...@@ -75,6 +76,12 @@ public class MultipartProperties {
*/ */
private String fileSizeThreshold = "0"; private String fileSizeThreshold = "0";
/**
* Whether to resolve the multipart request lazily at the time of file or parameter
* access.
*/
private boolean resolveLazily = false;
public boolean getEnabled() { public boolean getEnabled() {
return this.enabled; return this.enabled;
} }
...@@ -115,6 +122,14 @@ public class MultipartProperties { ...@@ -115,6 +122,14 @@ public class MultipartProperties {
this.fileSizeThreshold = fileSizeThreshold; this.fileSizeThreshold = fileSizeThreshold;
} }
public boolean isResolveLazily() {
return resolveLazily;
}
public void setResolveLazily(boolean resolveLazily) {
this.resolveLazily = resolveLazily;
}
/** /**
* Create a new {@link MultipartConfigElement} using the properties. * Create a new {@link MultipartConfigElement} using the properties.
* @return a new {@link MultipartConfigElement} configured using there properties * @return a new {@link MultipartConfigElement} configured using there properties
......
...@@ -40,6 +40,7 @@ import org.springframework.http.client.ClientHttpRequest; ...@@ -40,6 +40,7 @@ import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpResponse; import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
...@@ -59,6 +60,7 @@ import static org.mockito.Mockito.mock; ...@@ -59,6 +60,7 @@ import static org.mockito.Mockito.mock;
* @author Dave Syer * @author Dave Syer
* @author Josh Long * @author Josh Long
* @author Ivan Sopov * @author Ivan Sopov
* @author Toshiaki Maki
*/ */
public class MultipartAutoConfigurationTests { public class MultipartAutoConfigurationTests {
...@@ -188,6 +190,20 @@ public class MultipartAutoConfigurationTests { ...@@ -188,6 +190,20 @@ public class MultipartAutoConfigurationTests {
.isNotInstanceOf(StandardServletMultipartResolver.class); .isNotInstanceOf(StandardServletMultipartResolver.class);
} }
@Test
public void configureResolveLazily() {
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context,
"spring.http.multipart.resolve-lazily=true");
this.context.register(ContainerWithNothing.class, BaseConfiguration.class);
this.context.refresh();
StandardServletMultipartResolver multipartResolver = this.context
.getBean(StandardServletMultipartResolver.class);
boolean resolveLazily = (Boolean) ReflectionTestUtils.getField(multipartResolver,
"resolveLazily");
assertThat(resolveLazily).isTrue();
}
private void verify404() throws Exception { private void verify404() throws Exception {
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
ClientHttpRequest request = requestFactory.createRequest( ClientHttpRequest request = requestFactory.createRequest(
......
...@@ -283,6 +283,7 @@ content into your application; rather pick only the properties that you need. ...@@ -283,6 +283,7 @@ content into your application; rather pick only the properties that you need.
spring.http.multipart.location= # Intermediate location of uploaded files. spring.http.multipart.location= # Intermediate location of uploaded files.
spring.http.multipart.max-file-size=1Mb # Max file size. Values can use the suffixed "MB" or "KB" to indicate a Megabyte or Kilobyte size. spring.http.multipart.max-file-size=1Mb # Max file size. Values can use the suffixed "MB" or "KB" to indicate a Megabyte or Kilobyte size.
spring.http.multipart.max-request-size=10Mb # Max request size. Values can use the suffixed "MB" or "KB" to indicate a Megabyte or Kilobyte size. spring.http.multipart.max-request-size=10Mb # Max request size. Values can use the suffixed "MB" or "KB" to indicate a Megabyte or Kilobyte size.
spring.http.multipart.resolve-lazily=false # Whether to resolve the multipart request lazily at the time of file or parameter access.
# JACKSON ({sc-spring-boot-autoconfigure}/jackson/JacksonProperties.{sc-ext}[JacksonProperties]) # JACKSON ({sc-spring-boot-autoconfigure}/jackson/JacksonProperties.{sc-ext}[JacksonProperties])
spring.jackson.date-format= # Date format string or a fully-qualified date format class name. For instance `yyyy-MM-dd HH:mm:ss`. spring.jackson.date-format= # Date format string or a fully-qualified date format class name. For instance `yyyy-MM-dd HH:mm:ss`.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment