package cn.web.config;

import org.apache.catalina.servlets.DefaultServlet;
import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.ErrorProperties.IncludeAttribute;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.context.request.RequestContextListener;

@Configuration
public class ServletConfig 
{
	/**
	 * 让static下的静态资源走DefaultServlet, 不走SpringMVC DispatchServlet
	 * @return ServletRegistrationBean
	 */
	@Bean
	public ServletRegistrationBean<DefaultServlet> servletRegistrationBean() {
		return new ServletRegistrationBean<DefaultServlet>(new DefaultServlet(), "/static/*");
	}
     /**定义系统产生异常时，*/
	@Bean
	public ConfigurableServletWebServerFactory containerCustomizer() 
	{
		TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
		factory.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/common/error/500"));
		// factory.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/customError"));

		factory.addErrorPages(new ErrorPage(HttpStatus.UNAUTHORIZED, "/common/error/500"));
		factory.addErrorPages(new ErrorPage(HttpStatus.FORBIDDEN, "/common/error/500"));
		factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/common/error/404"));
		factory.addErrorPages(new ErrorPage(HttpStatus.METHOD_NOT_ALLOWED, "/common/error/500"));

		factory.addErrorPages(new ErrorPage(HttpStatus.NOT_ACCEPTABLE, "/common/error/500"));
		return factory;
	}

	@Bean
	public ErrorProperties errorProperties() {
		final ErrorProperties properties = new ErrorProperties();
		//properties.setIncludeStacktrace(IncludeStacktrace.ALWAYS);
		properties.setIncludeMessage(IncludeAttribute.ALWAYS);
		return properties;
	}

}
