Rework ErrorWrapperEmbeddedServletContainerFactory

Rename ErrorWrapperEmbeddedServletContainerFactory to ErrorPageFilter
and extend AbstractConfigurableEmbeddedServletContainer rather
than AbstractEmbeddedServletContainerFactory.

Fixes gh-416
This commit is contained in:
Phillip Webb 2014-03-03 21:24:13 -08:00
parent 99350a2216
commit 6c8e81f870
3 changed files with 14 additions and 39 deletions

View File

@ -30,33 +30,30 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import org.springframework.boot.context.embedded.AbstractEmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.EmbeddedServletContainer;
import org.springframework.boot.context.embedded.AbstractConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerException;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.ErrorPage;
import org.springframework.boot.context.embedded.ServletContextInitializer;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
/**
* A special {@link EmbeddedServletContainerFactory} for non-embedded applications (i.e.
* deployed WAR files). It registers error pages and handles application errors by
* filtering requests and forwarding to the error pages instead of letting the container
* handle them. Error pages are a feature of the servlet spec but there is no Java API for
* registering them in the spec. This filter works around that by accepting error page
* registrations from Spring Boot's {@link EmbeddedServletContainerCustomizer} (any beans
* of that type in the context will be applied to this container).
* A special {@link AbstractConfigurableEmbeddedServletContainer} for non-embedded
* applications (i.e. deployed WAR files). It registers error pages and handles
* application errors by filtering requests and forwarding to the error pages instead of
* letting the container handle them. Error pages are a feature of the servlet spec but
* there is no Java API for registering them in the spec. This filter works around that by
* accepting error page registrations from Spring Boot's
* {@link EmbeddedServletContainerCustomizer} (any beans of that type in the context will
* be applied to this container).
*
* @author Dave Syer
* @author Phillip Webb
*/
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ErrorWrapperEmbeddedServletContainerFactory extends
AbstractEmbeddedServletContainerFactory implements Filter {
class ErrorPageFilter extends AbstractConfigurableEmbeddedServletContainer implements
Filter {
// From RequestDispatcher but not referenced to remain compatible with Servlet 2.5
@ -161,28 +158,6 @@ public class ErrorWrapperEmbeddedServletContainerFactory extends
throw new IllegalStateException(ex);
}
@Override
public EmbeddedServletContainer getEmbeddedServletContainer(
ServletContextInitializer... initializers) {
return new EmbeddedServletContainer() {
@Override
public void start() throws EmbeddedServletContainerException {
}
@Override
public void stop() throws EmbeddedServletContainerException {
}
@Override
public int getPort() {
return -1;
}
};
}
@Override
public void addErrorPages(ErrorPage... errorPages) {
for (ErrorPage errorPage : errorPages) {

View File

@ -85,7 +85,7 @@ public abstract class SpringBootServletInitializer implements WebApplicationInit
application.contextClass(AnnotationConfigEmbeddedWebApplicationContext.class);
application = configure(application);
// Ensure error pages are registered
application.sources(ErrorWrapperEmbeddedServletContainerFactory.class);
application.sources(ErrorPageFilter.class);
return (WebApplicationContext) application.run();
}

View File

@ -36,13 +36,13 @@ import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
/**
* Tests for {@link ErrorWrapperEmbeddedServletContainerFactory}.
* Tests for {@link ErrorPageFilter}.
*
* @author Dave Syer
*/
public class ErrorWrapperEmbeddedServletContainerFactoryTests {
private ErrorWrapperEmbeddedServletContainerFactory filter = new ErrorWrapperEmbeddedServletContainerFactory();
private ErrorPageFilter filter = new ErrorPageFilter();
private MockHttpServletRequest request = new MockHttpServletRequest();