Skip to content

Commit 6507d17

Browse files
committed
Merge branch '1.5.x' into 2.0.x
2 parents be32421 + 70eee61 commit 6507d17

File tree

3 files changed

+67
-8
lines changed

3 files changed

+67
-8
lines changed

spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2927,13 +2927,6 @@ https://github.com/undertow-io/undertow[Undertow] servers. Most developers use t
29272927
appropriate "`Starter`" to obtain a fully configured instance. By default, the embedded
29282928
server listens for HTTP requests on port `8080`.
29292929

2930-
WARNING: If you choose to use Tomcat on https://www.centos.org/[CentOS], be aware that, by
2931-
default, a temporary directory is used to store compiled JSPs, file uploads, and so on.
2932-
This directory may be deleted by `tmpwatch` while your application is running, leading to
2933-
failures. To avoid this behavior, you may want to customize your `tmpwatch` configuration
2934-
such that `tomcat.*` directories are not deleted or configure `server.tomcat.basedir` such
2935-
that embedded Tomcat uses a different location.
2936-
29372930

29382931

29392932
[[boot-features-embedded-container-servlets-filters-listeners]]

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactory.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -200,6 +200,12 @@ protected void prepareContext(Host host, ServletContextInitializer[] initializer
200200
resetDefaultLocaleMapping(context);
201201
addLocaleMappings(context);
202202
context.setUseRelativeRedirects(false);
203+
try {
204+
context.setCreateUploadTargets(true);
205+
}
206+
catch (NoSuchMethodError ex) {
207+
// Tomcat is < 8.5.39. Continue.
208+
}
203209
configureTldSkipPatterns(context);
204210
WebappLoader loader = new WebappLoader(context.getParentClassLoader());
205211
loader.setLoaderClass(TomcatEmbeddedWebappClassLoader.class.getName());

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactoryTests.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.File;
2020
import java.io.IOException;
21+
import java.net.URISyntaxException;
2122
import java.net.URL;
2223
import java.nio.charset.Charset;
2324
import java.nio.charset.StandardCharsets;
@@ -26,11 +27,17 @@
2627
import java.util.Locale;
2728
import java.util.Map;
2829
import java.util.Set;
30+
import java.util.concurrent.atomic.AtomicReference;
2931

3032
import javax.naming.InitialContext;
3133
import javax.naming.NamingException;
34+
import javax.servlet.MultipartConfigElement;
35+
import javax.servlet.ServletContext;
3236
import javax.servlet.ServletException;
37+
import javax.servlet.ServletRegistration.Dynamic;
3338
import javax.servlet.http.HttpServlet;
39+
import javax.servlet.http.HttpServletRequest;
40+
import javax.servlet.http.HttpServletResponse;
3441

3542
import org.apache.catalina.Container;
3643
import org.apache.catalina.Context;
@@ -58,9 +65,20 @@
5865

5966
import org.springframework.boot.testsupport.rule.OutputCapture;
6067
import org.springframework.boot.web.server.WebServerException;
68+
import org.springframework.boot.web.servlet.ServletContextInitializer;
6169
import org.springframework.boot.web.servlet.server.AbstractServletWebServerFactory;
6270
import org.springframework.boot.web.servlet.server.AbstractServletWebServerFactoryTests;
71+
import org.springframework.core.io.ByteArrayResource;
72+
import org.springframework.http.HttpEntity;
73+
import org.springframework.http.HttpHeaders;
74+
import org.springframework.http.HttpStatus;
75+
import org.springframework.http.MediaType;
76+
import org.springframework.http.ResponseEntity;
6377
import org.springframework.test.util.ReflectionTestUtils;
78+
import org.springframework.util.FileSystemUtils;
79+
import org.springframework.util.LinkedMultiValueMap;
80+
import org.springframework.util.MultiValueMap;
81+
import org.springframework.web.client.RestTemplate;
6482

6583
import static org.assertj.core.api.Assertions.assertThat;
6684
import static org.junit.Assert.fail;
@@ -443,6 +461,48 @@ public void exceptionThrownOnLoadFailureWhenFailCtxIfServletStartFailsIsTrue() {
443461
this.webServer.start();
444462
}
445463

464+
@Test
465+
public void nonExistentUploadDirectoryIsCreatedUponMultipartUpload()
466+
throws IOException, URISyntaxException {
467+
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(0);
468+
AtomicReference<ServletContext> servletContextReference = new AtomicReference<>();
469+
factory.addInitializers(new ServletContextInitializer() {
470+
471+
@Override
472+
public void onStartup(ServletContext servletContext) throws ServletException {
473+
servletContextReference.set(servletContext);
474+
Dynamic servlet = servletContext.addServlet("upload", new HttpServlet() {
475+
476+
@Override
477+
protected void doPost(HttpServletRequest req,
478+
HttpServletResponse resp)
479+
throws ServletException, IOException {
480+
req.getParts();
481+
}
482+
483+
});
484+
servlet.addMapping("/upload");
485+
servlet.setMultipartConfig(new MultipartConfigElement((String) null));
486+
}
487+
488+
});
489+
this.webServer = factory.getWebServer();
490+
this.webServer.start();
491+
File temp = (File) servletContextReference.get()
492+
.getAttribute(ServletContext.TEMPDIR);
493+
FileSystemUtils.deleteRecursively(temp);
494+
RestTemplate restTemplate = new RestTemplate();
495+
HttpHeaders headers = new HttpHeaders();
496+
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
497+
body.add("file", new ByteArrayResource(new byte[1024 * 1024]));
498+
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
499+
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body,
500+
headers);
501+
ResponseEntity<String> response = restTemplate
502+
.postForEntity(getLocalUrl("/upload"), requestEntity, String.class);
503+
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
504+
}
505+
446506
@Override
447507
protected JspServlet getJspServlet() throws ServletException {
448508
Tomcat tomcat = ((TomcatWebServer) this.webServer).getTomcat();

0 commit comments

Comments
 (0)