Make ErrorPageRegistry first class concern
Create ErrorPageRegistry and ErrorPageRegistrar interfaces that allow error page registration to be a first class concern. Prior to this commit ErrorPageFilter needed to implement ConfigurableEmbeddedServletContainer in order to receive ErrorPage registrations. Closes gh-5789
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -37,12 +37,13 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
|
||||
import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration.EmbeddedServletContainerCustomizerBeanPostProcessorRegistrar;
|
||||
import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration.BeanPostProcessorsRegistrar;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
|
||||
import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory;
|
||||
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
|
||||
import org.springframework.boot.context.embedded.undertow.UndertowEmbeddedServletContainerFactory;
|
||||
import org.springframework.boot.web.servlet.ErrorPageRegistrarBeanPostProcessor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
@@ -61,7 +62,7 @@ import org.springframework.util.ObjectUtils;
|
||||
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
|
||||
@Configuration
|
||||
@ConditionalOnWebApplication
|
||||
@Import(EmbeddedServletContainerCustomizerBeanPostProcessorRegistrar.class)
|
||||
@Import(BeanPostProcessorsRegistrar.class)
|
||||
public class EmbeddedServletContainerAutoConfiguration {
|
||||
|
||||
/**
|
||||
@@ -114,7 +115,7 @@ public class EmbeddedServletContainerAutoConfiguration {
|
||||
* Registers a {@link EmbeddedServletContainerCustomizerBeanPostProcessor}. Registered
|
||||
* via {@link ImportBeanDefinitionRegistrar} for early registration.
|
||||
*/
|
||||
public static class EmbeddedServletContainerCustomizerBeanPostProcessorRegistrar
|
||||
public static class BeanPostProcessorsRegistrar
|
||||
implements ImportBeanDefinitionRegistrar, BeanFactoryAware {
|
||||
|
||||
private ConfigurableListableBeanFactory beanFactory;
|
||||
@@ -141,6 +142,13 @@ public class EmbeddedServletContainerAutoConfiguration {
|
||||
EmbeddedServletContainerCustomizerBeanPostProcessor.class));
|
||||
|
||||
}
|
||||
if (ObjectUtils.isEmpty(this.beanFactory.getBeanNamesForType(
|
||||
ErrorPageRegistrarBeanPostProcessor.class, true, false))) {
|
||||
registry.registerBeanDefinition("errorPageRegistrarBeanPostProcessor",
|
||||
new RootBeanDefinition(
|
||||
ErrorPageRegistrarBeanPostProcessor.class));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -39,9 +39,10 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplicat
|
||||
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
|
||||
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
|
||||
import org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider;
|
||||
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
|
||||
import org.springframework.boot.web.servlet.ErrorPage;
|
||||
import org.springframework.boot.web.servlet.ErrorPageRegistrar;
|
||||
import org.springframework.boot.web.servlet.ErrorPageRegistry;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
@@ -257,8 +258,7 @@ public class ErrorMvcAutoConfiguration {
|
||||
* {@link EmbeddedServletContainerCustomizer} that configures the container's error
|
||||
* pages.
|
||||
*/
|
||||
private static class ErrorPageCustomizer
|
||||
implements EmbeddedServletContainerCustomizer, Ordered {
|
||||
private static class ErrorPageCustomizer implements ErrorPageRegistrar, Ordered {
|
||||
|
||||
private final ServerProperties properties;
|
||||
|
||||
@@ -267,9 +267,10 @@ public class ErrorMvcAutoConfiguration {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void customize(ConfigurableEmbeddedServletContainer container) {
|
||||
container.addErrorPages(new ErrorPage(this.properties.getServletPrefix()
|
||||
+ this.properties.getError().getPath()));
|
||||
public void registerErrorPages(ErrorPageRegistry errorPageRegistry) {
|
||||
ErrorPage errorPage = new ErrorPage(this.properties.getServletPrefix()
|
||||
+ this.properties.getError().getPath());
|
||||
errorPageRegistry.addErrorPages(errorPage);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,13 +16,9 @@
|
||||
|
||||
package org.springframework.boot.autoconfigure.websocket;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
|
||||
import org.springframework.boot.context.web.NonEmbeddedServletContainerFactory;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.ResolvableType;
|
||||
|
||||
@@ -39,8 +35,6 @@ import org.springframework.core.ResolvableType;
|
||||
public abstract class WebSocketContainerCustomizer<T extends EmbeddedServletContainerFactory>
|
||||
implements EmbeddedServletContainerCustomizer, Ordered {
|
||||
|
||||
private Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return 0;
|
||||
@@ -49,12 +43,6 @@ public abstract class WebSocketContainerCustomizer<T extends EmbeddedServletCont
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void customize(ConfigurableEmbeddedServletContainer container) {
|
||||
if (container instanceof NonEmbeddedServletContainerFactory) {
|
||||
this.logger.info("NonEmbeddedServletContainerFactory "
|
||||
+ "detected. Websockets support should be native so this "
|
||||
+ "normally is not a problem.");
|
||||
return;
|
||||
}
|
||||
if (getContainerType().isAssignableFrom(container.getClass())) {
|
||||
doCustomize((T) container);
|
||||
}
|
||||
|
||||
@@ -21,13 +21,13 @@ import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
|
||||
import org.springframework.boot.context.embedded.LocalServerPort;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.boot.web.servlet.ErrorPage;
|
||||
import org.springframework.boot.web.servlet.ErrorPageRegistrar;
|
||||
import org.springframework.boot.web.servlet.ErrorPageRegistry;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.stereotype.Controller;
|
||||
@@ -76,7 +76,7 @@ public class RemappedErrorViewIntegrationTests {
|
||||
EmbeddedServletContainerAutoConfiguration.class,
|
||||
DispatcherServletAutoConfiguration.class, ErrorMvcAutoConfiguration.class })
|
||||
@Controller
|
||||
public static class TestConfiguration implements EmbeddedServletContainerCustomizer {
|
||||
public static class TestConfiguration implements ErrorPageRegistrar {
|
||||
|
||||
@RequestMapping("/")
|
||||
public String home() {
|
||||
@@ -84,8 +84,8 @@ public class RemappedErrorViewIntegrationTests {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void customize(ConfigurableEmbeddedServletContainer container) {
|
||||
container.addErrorPages(new ErrorPage("/spring/error"));
|
||||
public void registerErrorPages(ErrorPageRegistry errorPageRegistry) {
|
||||
errorPageRegistry.addErrorPages(new ErrorPage("/spring/error"));
|
||||
}
|
||||
|
||||
// For manual testing
|
||||
|
||||
Reference in New Issue
Block a user