|
|
@ -18,6 +18,7 @@ package org.springframework.boot.web.embedded.tomcat;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import java.net.URISyntaxException;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.nio.charset.Charset;
|
|
|
|
import java.nio.charset.Charset;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
@ -26,10 +27,17 @@ import java.util.Arrays;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.naming.InitialContext;
|
|
|
|
import javax.naming.InitialContext;
|
|
|
|
import javax.naming.NamingException;
|
|
|
|
import javax.naming.NamingException;
|
|
|
|
|
|
|
|
import javax.servlet.MultipartConfigElement;
|
|
|
|
|
|
|
|
import javax.servlet.ServletContext;
|
|
|
|
import javax.servlet.ServletException;
|
|
|
|
import javax.servlet.ServletException;
|
|
|
|
|
|
|
|
import javax.servlet.ServletRegistration.Dynamic;
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
|
|
|
|
|
|
import org.apache.catalina.Container;
|
|
|
|
import org.apache.catalina.Container;
|
|
|
|
import org.apache.catalina.Context;
|
|
|
|
import org.apache.catalina.Context;
|
|
|
@ -58,9 +66,20 @@ import org.mockito.InOrder;
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.boot.testsupport.rule.OutputCapture;
|
|
|
|
import org.springframework.boot.testsupport.rule.OutputCapture;
|
|
|
|
import org.springframework.boot.web.server.WebServerException;
|
|
|
|
import org.springframework.boot.web.server.WebServerException;
|
|
|
|
|
|
|
|
import org.springframework.boot.web.servlet.ServletContextInitializer;
|
|
|
|
import org.springframework.boot.web.servlet.server.AbstractServletWebServerFactory;
|
|
|
|
import org.springframework.boot.web.servlet.server.AbstractServletWebServerFactory;
|
|
|
|
import org.springframework.boot.web.servlet.server.AbstractServletWebServerFactoryTests;
|
|
|
|
import org.springframework.boot.web.servlet.server.AbstractServletWebServerFactoryTests;
|
|
|
|
|
|
|
|
import org.springframework.core.io.ByteArrayResource;
|
|
|
|
|
|
|
|
import org.springframework.http.HttpEntity;
|
|
|
|
|
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
|
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
|
|
|
|
import org.springframework.http.MediaType;
|
|
|
|
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
import org.springframework.test.util.ReflectionTestUtils;
|
|
|
|
import org.springframework.test.util.ReflectionTestUtils;
|
|
|
|
|
|
|
|
import org.springframework.util.FileSystemUtils;
|
|
|
|
|
|
|
|
import org.springframework.util.LinkedMultiValueMap;
|
|
|
|
|
|
|
|
import org.springframework.util.MultiValueMap;
|
|
|
|
|
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
|
|
@ -457,6 +476,48 @@ public class TomcatServletWebServerFactoryTests
|
|
|
|
assertThat(context.getClearReferencesThreadLocals()).isFalse();
|
|
|
|
assertThat(context.getClearReferencesThreadLocals()).isFalse();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
|
|
public void nonExistentUploadDirectoryIsCreatedUponMultipartUpload()
|
|
|
|
|
|
|
|
throws IOException, URISyntaxException {
|
|
|
|
|
|
|
|
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(0);
|
|
|
|
|
|
|
|
AtomicReference<ServletContext> servletContextReference = new AtomicReference<>();
|
|
|
|
|
|
|
|
factory.addInitializers(new ServletContextInitializer() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void onStartup(ServletContext servletContext) throws ServletException {
|
|
|
|
|
|
|
|
servletContextReference.set(servletContext);
|
|
|
|
|
|
|
|
Dynamic servlet = servletContext.addServlet("upload", new HttpServlet() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
protected void doPost(HttpServletRequest req,
|
|
|
|
|
|
|
|
HttpServletResponse resp)
|
|
|
|
|
|
|
|
throws ServletException, IOException {
|
|
|
|
|
|
|
|
req.getParts();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
servlet.addMapping("/upload");
|
|
|
|
|
|
|
|
servlet.setMultipartConfig(new MultipartConfigElement((String) null));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
this.webServer = factory.getWebServer();
|
|
|
|
|
|
|
|
this.webServer.start();
|
|
|
|
|
|
|
|
File temp = (File) servletContextReference.get()
|
|
|
|
|
|
|
|
.getAttribute(ServletContext.TEMPDIR);
|
|
|
|
|
|
|
|
FileSystemUtils.deleteRecursively(temp);
|
|
|
|
|
|
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
|
|
|
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
|
|
|
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
|
|
|
|
|
|
|
|
body.add("file", new ByteArrayResource(new byte[1024 * 1024]));
|
|
|
|
|
|
|
|
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
|
|
|
|
|
|
|
|
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body,
|
|
|
|
|
|
|
|
headers);
|
|
|
|
|
|
|
|
ResponseEntity<String> response = restTemplate
|
|
|
|
|
|
|
|
.postForEntity(getLocalUrl("/upload"), requestEntity, String.class);
|
|
|
|
|
|
|
|
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
protected JspServlet getJspServlet() throws ServletException {
|
|
|
|
protected JspServlet getJspServlet() throws ServletException {
|
|
|
|
Tomcat tomcat = ((TomcatWebServer) this.webServer).getTomcat();
|
|
|
|
Tomcat tomcat = ((TomcatWebServer) this.webServer).getTomcat();
|
|
|
|