pull/5/merge
Phillip Webb 12 years ago
parent 7bf3e35249
commit 018231d20a

@ -13,31 +13,31 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.bootstrap.autoconfigure.web; package org.springframework.bootstrap.autoconfigure.web;
import javax.servlet.MultipartConfigElement; import javax.servlet.MultipartConfigElement;
import org.springframework.bootstrap.context.annotation.ConditionalOnBean; import org.springframework.bootstrap.context.annotation.ConditionalOnBean;
import org.springframework.bootstrap.context.annotation.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.support.StandardServletMultipartResolver; import org.springframework.web.multipart.support.StandardServletMultipartResolver;
/** /**
* Autoconfiguration for multipart uploads. It detects the existence of a * {@link EnableAutoConfiguration Auto-configuration} for multi-part uploads. It detects
* {@link MultipartConfigElement} in the app context and then adds critical beans * the existence of a {@link MultipartConfigElement} in the app context and then adds
* while also autowiring it into the Jetty/Tomcat embedded containers. * critical beans while also autowiring it into the Jetty/Tomcat embedded containers.
* *
* @author Greg Turnquist * @author Greg Turnquist
*
*/ */
@Configuration @Configuration
public class MultipartAutoConfiguration { public class MultipartAutoConfiguration {
@ConditionalOnBean(MultipartConfigElement.class)
@Bean @Bean
@ConditionalOnBean(MultipartConfigElement.class)
public StandardServletMultipartResolver multipartResolver() { public StandardServletMultipartResolver multipartResolver() {
System.out.println("Loading up a MultipartResolver!!!"); return new StandardServletMultipartResolver();
return new StandardServletMultipartResolver();
} }
} }

@ -23,7 +23,6 @@ import java.util.Comparator;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
@ -210,13 +209,6 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
} }
initializers.add(initializer); initializers.add(initializer);
} }
Map<String, MultipartConfigElement> multipartConfigBeans;
MultipartConfigElement multipartConfigElement = null;
multipartConfigBeans = getBeanFactory().getBeansOfType(MultipartConfigElement.class);
for (MultipartConfigElement bean : multipartConfigBeans.values()) {
multipartConfigElement = bean;
}
List<Entry<String, Servlet>> servletBeans = getOrderedBeansOfType(Servlet.class); List<Entry<String, Servlet>> servletBeans = getOrderedBeansOfType(Servlet.class);
for (Entry<String, Servlet> servletBean : servletBeans) { for (Entry<String, Servlet> servletBean : servletBeans) {
@ -229,15 +221,11 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
if (name.equals(DISPATCHER_SERVLET_NAME)) { if (name.equals(DISPATCHER_SERVLET_NAME)) {
url = "/"; // always map the main dispatcherServlet to "/" url = "/"; // always map the main dispatcherServlet to "/"
} }
if (multipartConfigElement != null) { ServletRegistrationBean registration = new ServletRegistrationBean(servlet,
initializers.add(new ServletRegistrationBean(servlet, multipartConfigElement, url) {{ url);
setName(name); registration.setName(name);
}}); registration.setMultipartConfig(getMultipartConfig());
} else { initializers.add(registration);
initializers.add(new ServletRegistrationBean(servlet, url) {{
setName(name);
}});
}
} }
for (Entry<String, Filter> filterBean : getOrderedBeansOfType(Filter.class)) { for (Entry<String, Filter> filterBean : getOrderedBeansOfType(Filter.class)) {
@ -254,6 +242,14 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
return initializers; return initializers;
} }
private MultipartConfigElement getMultipartConfig() {
List<Entry<String, MultipartConfigElement>> beans = getOrderedBeansOfType(MultipartConfigElement.class);
if (beans.isEmpty()) {
return null;
}
return beans.get(0).getValue();
}
/** /**
* Prepare the {@link WebApplicationContext} with the given fully loaded * Prepare the {@link WebApplicationContext} with the given fully loaded
* {@link ServletContext}. This method is usually called from * {@link ServletContext}. This method is usually called from

@ -56,8 +56,8 @@ public class ServletRegistrationBean extends RegistrationBean {
private int loadOnStartup = 1; private int loadOnStartup = 1;
private Set<Filter> filters = new LinkedHashSet<Filter>(); private Set<Filter> filters = new LinkedHashSet<Filter>();
private MultipartConfigElement multipartConfigElement = null; private MultipartConfigElement multipartConfig;
/** /**
* Create a new {@link ServletRegistrationBean} instance. * Create a new {@link ServletRegistrationBean} instance.
@ -67,7 +67,7 @@ public class ServletRegistrationBean extends RegistrationBean {
/** /**
* Create a new {@link ServletRegistrationBean} instance with the specified * Create a new {@link ServletRegistrationBean} instance with the specified
* {@link Servlet} and URL mapping. * {@link Servlet} and URL mappings.
* @param servlet the servlet being mapped * @param servlet the servlet being mapped
* @param urlMappings the URLs being mapped * @param urlMappings the URLs being mapped
*/ */
@ -75,11 +75,6 @@ public class ServletRegistrationBean extends RegistrationBean {
setServlet(servlet); setServlet(servlet);
addUrlMappings(urlMappings); addUrlMappings(urlMappings);
} }
public ServletRegistrationBean(Servlet servlet, MultipartConfigElement multipartConfigElement, String... urlMappings) {
this(servlet, urlMappings);
this.multipartConfigElement = multipartConfigElement;
}
/** /**
* Sets the servlet to be registered. * Sets the servlet to be registered.
@ -152,6 +147,22 @@ public class ServletRegistrationBean extends RegistrationBean {
this.filters.addAll(Arrays.asList(filters)); this.filters.addAll(Arrays.asList(filters));
} }
/**
* Set the the {@link MultipartConfigElement multi-part configuration}.
* @param multipartConfig the muti-part configuration to set or {@code null}
*/
public void setMultipartConfig(MultipartConfigElement multipartConfig) {
this.multipartConfig = multipartConfig;
}
/**
* Returns the {@link MultipartConfigElement multi-part configuration} to be applied
* or {@code null}.
*/
public MultipartConfigElement getMultipartConfig() {
return this.multipartConfig;
}
/** /**
* Returns the servlet name that will be registered. * Returns the servlet name that will be registered.
*/ */
@ -189,8 +200,8 @@ public class ServletRegistrationBean extends RegistrationBean {
} }
registration.addMapping(urlMapping); registration.addMapping(urlMapping);
registration.setLoadOnStartup(this.loadOnStartup); registration.setLoadOnStartup(this.loadOnStartup);
if (multipartConfigElement != null) { if (this.multipartConfig != null) {
registration.setMultipartConfig(multipartConfigElement); registration.setMultipartConfig(this.multipartConfig);
} }
} }
} }

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.bootstrap.autoconfigure.web; package org.springframework.bootstrap.autoconfigure.web;
import javax.servlet.MultipartConfigElement; import javax.servlet.MultipartConfigElement;
@ -40,8 +41,8 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame; import static org.junit.Assert.assertSame;
/** /**
* A series of embedded unit tests, based on an empty configuration, no multipart * Tests for {@link MultipartAutoConfiguration}. Tests an empty configuration, no
* configuration, and a multipart configuration, with both Jetty and Tomcat. * multipart configuration, and a multipart configuration (with both Jetty and Tomcat).
* *
* @author Greg Turnquist * @author Greg Turnquist
* @author Dave Syer * @author Dave Syer
@ -51,7 +52,7 @@ public class MultipartAutoConfigurationTests {
private AnnotationConfigEmbeddedWebApplicationContext context; private AnnotationConfigEmbeddedWebApplicationContext context;
@Rule @Rule
public ExpectedException exception = ExpectedException.none(); public ExpectedException thrown = ExpectedException.none();
@After @After
public void close() { public void close() {
@ -121,8 +122,39 @@ public class MultipartAutoConfigurationTests {
verifyServletWorks(); verifyServletWorks();
} }
@Test
public void containerWithAutomatedMultipartJettyConfiguration() {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
ContainerWithEverythingJetty.class,
EmbeddedServletContainerAutoConfiguration.class,
MultipartAutoConfiguration.class);
this.context.getBean(MultipartConfigElement.class);
assertSame(this.context.getBean(DispatcherServlet.class).getMultipartResolver(),
this.context.getBean(StandardServletMultipartResolver.class));
verifyServletWorks();
}
@Test
public void containerWithAutomatedMultipartTomcatConfiguration() {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
ContainerWithEverythingTomcat.class,
EmbeddedServletContainerAutoConfiguration.class,
MultipartAutoConfiguration.class);
this.context.getBean(MultipartConfigElement.class);
assertSame(this.context.getBean(DispatcherServlet.class).getMultipartResolver(),
this.context.getBean(StandardServletMultipartResolver.class));
verifyServletWorks();
}
private void verifyServletWorks() {
RestTemplate restTemplate = new RestTemplate();
assertEquals(restTemplate.getForObject("http://localhost:8080/", String.class),
"Hello");
}
@Configuration @Configuration
public static class ContainerWithNoMultipartTomcat { public static class ContainerWithNoMultipartTomcat {
@Bean @Bean
TomcatEmbeddedServletContainerFactory containerFactory() { TomcatEmbeddedServletContainerFactory containerFactory() {
return new TomcatEmbeddedServletContainerFactory(); return new TomcatEmbeddedServletContainerFactory();
@ -134,18 +166,6 @@ public class MultipartAutoConfigurationTests {
} }
} }
@Test
public void containerWithAutomatedMultipartJettyConfiguration() {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
ContainerWithEverythingJetty.class,
EmbeddedServletContainerAutoConfiguration.class,
MultipartAutoConfiguration.class);
this.context.getBean(MultipartConfigElement.class);
assertSame(this.context.getBean(DispatcherServlet.class).getMultipartResolver(),
this.context.getBean(StandardServletMultipartResolver.class));
verifyServletWorks();
}
@Configuration @Configuration
public static class ContainerWithEverythingJetty { public static class ContainerWithEverythingJetty {
@Bean @Bean
@ -164,18 +184,6 @@ public class MultipartAutoConfigurationTests {
} }
} }
@Test
public void containerWithAutomatedMultipartTomcatConfiguration() {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
ContainerWithEverythingTomcat.class,
EmbeddedServletContainerAutoConfiguration.class,
MultipartAutoConfiguration.class);
this.context.getBean(MultipartConfigElement.class);
assertSame(this.context.getBean(DispatcherServlet.class).getMultipartResolver(),
this.context.getBean(StandardServletMultipartResolver.class));
verifyServletWorks();
}
@Configuration @Configuration
@EnableWebMvc @EnableWebMvc
public static class ContainerWithEverythingTomcat { public static class ContainerWithEverythingTomcat {
@ -204,10 +212,4 @@ public class MultipartAutoConfigurationTests {
} }
} }
private void verifyServletWorks() {
RestTemplate restTemplate = new RestTemplate();
assertEquals(restTemplate.getForObject("http://localhost:8080/", String.class),
"Hello");
}
} }

Loading…
Cancel
Save