All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sap.cloud.yaas.servicesdk.springboot.ErrorPagesAutoConfiguration Maven / Gradle / Ivy

There is a newer version: 4.17.1
Show newest version
/*
 * © 2017 SAP SE or an SAP affiliate company.
 * All rights reserved.
 * Please see http://www.sap.com/corporate-en/legal/copyright/index.epx for additional trademark information and
 * notices.
 */
package com.sap.cloud.yaas.servicesdk.springboot;


import com.sap.cloud.yaas.servicesdk.servletsupport.servlets.ErrorMessageServlet;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
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.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * Auto Configuration for YaaS specific error servlet pages, provided by {@link ErrorMessageServlet}. Any
 * unhandled error that propagates to the container is ensured to be displayed in JSON format,
 * without exposing internal information about the container.
 *
 * The configuration will be active only if {@link ErrorMessageServlet} is
 * already contained in the {@link org.springframework.beans.factory.BeanFactory}.
 */
@Configuration
@ConditionalOnClass({
		EmbeddedServletContainerCustomizer.class,
		ErrorMessageServlet.class})
@ConditionalOnWebApplication
public class ErrorPagesAutoConfiguration
{
	private static final String SERVLET_MAPPING_PATH = "/WEB-INF/error.message.servlet";

	/**
	 * Factory method that registers an instance of the {@link ErrorMessageServlet} as a Servlet for requests to
	 * {@value #SERVLET_MAPPING_PATH}.
	 *
	 * @return The errorMessageServletRegistration bean.
	 */
	@Bean
	public ServletRegistrationBean errorMessageServletRegistration()
	{
		return new ServletRegistrationBean(new ErrorMessageServlet(), SERVLET_MAPPING_PATH);
	}

	/**
	 * Sub Configuration class that customizes auto-configured embedded servlet container to use the provided
	 * {@link ErrorMessageServlet}.
	 */
	@Configuration
	public static class ServletContainerCustomizer implements ErrorPageRegistrar
	{
		@Override
		public void registerErrorPages(final ErrorPageRegistry registry)
		{
			registry.addErrorPages(new ErrorPage(SERVLET_MAPPING_PATH));
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy