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:
@@ -23,6 +23,7 @@ import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.springframework.boot.web.servlet.ErrorPage;
|
||||
import org.springframework.boot.web.servlet.ErrorPageRegistry;
|
||||
import org.springframework.boot.web.servlet.ServletContextInitializer;
|
||||
|
||||
/**
|
||||
@@ -36,7 +37,7 @@ import org.springframework.boot.web.servlet.ServletContextInitializer;
|
||||
* @see EmbeddedServletContainerFactory
|
||||
* @see EmbeddedServletContainerCustomizer
|
||||
*/
|
||||
public interface ConfigurableEmbeddedServletContainer {
|
||||
public interface ConfigurableEmbeddedServletContainer extends ErrorPageRegistry {
|
||||
|
||||
/**
|
||||
* Sets the context path for the embedded servlet container. The context should start
|
||||
@@ -102,12 +103,6 @@ public interface ConfigurableEmbeddedServletContainer {
|
||||
*/
|
||||
void setRegisterDefaultServlet(boolean registerDefaultServlet);
|
||||
|
||||
/**
|
||||
* Adds error pages that will be used when handling exceptions.
|
||||
* @param errorPages the error pages
|
||||
*/
|
||||
void addErrorPages(ErrorPage... errorPages);
|
||||
|
||||
/**
|
||||
* Sets the error pages that will be used when handling exceptions.
|
||||
* @param errorPages the error pages
|
||||
|
||||
@@ -18,8 +18,8 @@ package org.springframework.boot.context.web;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
|
||||
import org.springframework.boot.context.embedded.ErrorPageRegistrar;
|
||||
import org.springframework.boot.context.embedded.ErrorPageRegistry;
|
||||
import org.springframework.boot.web.servlet.ErrorPageRegistrar;
|
||||
import org.springframework.boot.web.servlet.ErrorPageRegistry;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2013 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.
|
||||
@@ -17,13 +17,16 @@
|
||||
package org.springframework.boot.context.web;
|
||||
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
|
||||
import org.springframework.boot.web.servlet.ErrorPageRegistry;
|
||||
|
||||
/**
|
||||
* Marker interface for {@link EmbeddedServletContainerFactory} types that are actually
|
||||
* safe to run in a non-embedded container.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @deprecated as of 1.4 in favor of {@link ErrorPageRegistry}
|
||||
*/
|
||||
@Deprecated
|
||||
public interface NonEmbeddedServletContainerFactory {
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.web.servlet;
|
||||
|
||||
/**
|
||||
* Interface to be implemented by types that register {@link ErrorPage ErrorPages}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @since 1.4.0
|
||||
*/
|
||||
public interface ErrorPageRegistrar {
|
||||
|
||||
/**
|
||||
* Register pages as required with the given registry.
|
||||
* @param registry the error page registry
|
||||
*/
|
||||
void registerErrorPages(ErrorPageRegistry registry);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.web.servlet;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
||||
|
||||
/**
|
||||
* {@link BeanPostProcessor} that apply all {@link ErrorPageRegistrar}s from the bean
|
||||
* factory to {@link ErrorPageRegistry} beans.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @since 1.4.0
|
||||
*/
|
||||
public class ErrorPageRegistrarBeanPostProcessor
|
||||
implements BeanPostProcessor, ApplicationContextAware {
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
private List<ErrorPageRegistrar> registrars;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext)
|
||||
throws BeansException {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName)
|
||||
throws BeansException {
|
||||
if (bean instanceof ErrorPageRegistry) {
|
||||
postProcessBeforeInitialization((ErrorPageRegistry) bean);
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object postProcessAfterInitialization(Object bean, String beanName)
|
||||
throws BeansException {
|
||||
return bean;
|
||||
}
|
||||
|
||||
private void postProcessBeforeInitialization(ErrorPageRegistry registry) {
|
||||
for (ErrorPageRegistrar registrar : getRegistrars()) {
|
||||
registrar.registerErrorPages(registry);
|
||||
}
|
||||
}
|
||||
|
||||
private Collection<ErrorPageRegistrar> getRegistrars() {
|
||||
if (this.registrars == null) {
|
||||
// Look up does not include the parent context
|
||||
this.registrars = new ArrayList<ErrorPageRegistrar>(this.applicationContext
|
||||
.getBeansOfType(ErrorPageRegistrar.class, false, false).values());
|
||||
Collections.sort(this.registrars, AnnotationAwareOrderComparator.INSTANCE);
|
||||
this.registrars = Collections.unmodifiableList(this.registrars);
|
||||
}
|
||||
return this.registrars;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.web.servlet;
|
||||
|
||||
/**
|
||||
* Interface for a registry that holds {@link ErrorPage ErrorPages}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @since 1.4.0
|
||||
*/
|
||||
public interface ErrorPageRegistry {
|
||||
|
||||
/**
|
||||
* Adds error pages that will be used when handling exceptions.
|
||||
* @param errorPages the error pages
|
||||
*/
|
||||
void addErrorPages(ErrorPage... errorPages);
|
||||
|
||||
}
|
||||
@@ -33,10 +33,9 @@ import javax.servlet.http.HttpServletResponseWrapper;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.boot.context.embedded.AbstractConfigurableEmbeddedServletContainer;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
|
||||
import org.springframework.boot.context.web.NonEmbeddedServletContainerFactory;
|
||||
import org.springframework.boot.web.servlet.ErrorPage;
|
||||
import org.springframework.boot.web.servlet.ErrorPageRegistrar;
|
||||
import org.springframework.boot.web.servlet.ErrorPageRegistry;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -44,14 +43,13 @@ import org.springframework.web.filter.OncePerRequestFilter;
|
||||
import org.springframework.web.util.NestedServletException;
|
||||
|
||||
/**
|
||||
* A special {@link AbstractConfigurableEmbeddedServletContainer} for non-embedded
|
||||
* A Servlet {@link Filter} that provides an {@link ErrorPageRegistry} 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).
|
||||
* accepting error page registrations from Spring Boot's {@link ErrorPageRegistrar} (any
|
||||
* beans of that type in the context will be applied to this container).
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Phillip Webb
|
||||
@@ -60,8 +58,7 @@ import org.springframework.web.util.NestedServletException;
|
||||
*/
|
||||
@Component
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
public class ErrorPageFilter extends AbstractConfigurableEmbeddedServletContainer
|
||||
implements Filter, NonEmbeddedServletContainerFactory {
|
||||
public class ErrorPageFilter implements Filter, ErrorPageRegistry {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(ErrorPageFilter.class);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user