Search in sources :

Example 11 with ErrorPage

use of org.springframework.boot.web.server.ErrorPage in project spring-boot by spring-projects.

the class TomcatServletWebServerFactory method configureContext.

/**
 * Configure the Tomcat {@link Context}.
 * @param context the Tomcat context
 * @param initializers initializers to apply
 */
protected void configureContext(Context context, ServletContextInitializer[] initializers) {
    TomcatStarter starter = new TomcatStarter(initializers);
    if (context instanceof TomcatEmbeddedContext) {
        TomcatEmbeddedContext embeddedContext = (TomcatEmbeddedContext) context;
        embeddedContext.setStarter(starter);
        embeddedContext.setFailCtxIfServletStartFails(true);
    }
    context.addServletContainerInitializer(starter, NO_CLASSES);
    for (LifecycleListener lifecycleListener : this.contextLifecycleListeners) {
        context.addLifecycleListener(lifecycleListener);
    }
    for (Valve valve : this.contextValves) {
        context.getPipeline().addValve(valve);
    }
    for (ErrorPage errorPage : getErrorPages()) {
        org.apache.tomcat.util.descriptor.web.ErrorPage tomcatErrorPage = new org.apache.tomcat.util.descriptor.web.ErrorPage();
        tomcatErrorPage.setLocation(errorPage.getPath());
        tomcatErrorPage.setErrorCode(errorPage.getStatusCode());
        tomcatErrorPage.setExceptionType(errorPage.getExceptionName());
        context.addErrorPage(tomcatErrorPage);
    }
    for (MimeMappings.Mapping mapping : getMimeMappings()) {
        context.addMimeMapping(mapping.getExtension(), mapping.getMimeType());
    }
    configureSession(context);
    configureCookieProcessor(context);
    new DisableReferenceClearingContextCustomizer().customize(context);
    for (String webListenerClassName : getWebListenerClassNames()) {
        context.addApplicationListener(webListenerClassName);
    }
    for (TomcatContextCustomizer customizer : this.tomcatContextCustomizers) {
        customizer.customize(context);
    }
}
Also used : ErrorPage(org.springframework.boot.web.server.ErrorPage) LifecycleListener(org.apache.catalina.LifecycleListener) AprLifecycleListener(org.apache.catalina.core.AprLifecycleListener) MimeMappings(org.springframework.boot.web.server.MimeMappings) Valve(org.apache.catalina.Valve)

Example 12 with ErrorPage

use of org.springframework.boot.web.server.ErrorPage in project scoold by Erudika.

the class ScooldServer method errorPageRegistrar.

/**
 * @return Error page registry bean
 */
@Bean
public ErrorPageRegistrar errorPageRegistrar() {
    return new ErrorPageRegistrar() {

        @Override
        public void registerErrorPages(ErrorPageRegistry epr) {
            epr.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/not-found"));
            epr.addErrorPages(new ErrorPage(HttpStatus.FORBIDDEN, "/error/403"));
            epr.addErrorPages(new ErrorPage(HttpStatus.UNAUTHORIZED, "/error/401"));
            epr.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500"));
            epr.addErrorPages(new ErrorPage(HttpStatus.SERVICE_UNAVAILABLE, "/error/503"));
            epr.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/error/400"));
            epr.addErrorPages(new ErrorPage(HttpStatus.METHOD_NOT_ALLOWED, "/error/405"));
            epr.addErrorPages(new ErrorPage(Exception.class, "/error/500"));
        }
    };
}
Also used : ErrorPageRegistry(org.springframework.boot.web.server.ErrorPageRegistry) ErrorPage(org.springframework.boot.web.server.ErrorPage) ErrorPageRegistrar(org.springframework.boot.web.server.ErrorPageRegistrar) Bean(org.springframework.context.annotation.Bean)

Example 13 with ErrorPage

use of org.springframework.boot.web.server.ErrorPage in project spring-boot by spring-projects.

the class ErrorPageFilterTests method errorMessageForRequestWithoutPathInfo.

@Test
public void errorMessageForRequestWithoutPathInfo() throws IOException, ServletException {
    this.request.setServletPath("/test");
    this.filter.addErrorPages(new ErrorPage("/error"));
    this.chain = new MockFilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            super.doFilter(request, response);
            throw new RuntimeException();
        }
    };
    this.filter.doFilter(this.request, this.response, this.chain);
    assertThat(this.output.toString()).contains("request [/test]");
}
Also used : ServletException(javax.servlet.ServletException) NestedServletException(org.springframework.web.util.NestedServletException) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) ErrorPage(org.springframework.boot.web.server.ErrorPage) IOException(java.io.IOException) MockFilterChain(org.springframework.mock.web.MockFilterChain) Test(org.junit.Test)

Example 14 with ErrorPage

use of org.springframework.boot.web.server.ErrorPage in project spring-boot by spring-projects.

the class AbstractServletWebServerFactoryTests method errorPageFromPutRequest.

@Test
void errorPageFromPutRequest() throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    factory.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/hello"));
    this.webServer = factory.getWebServer(exampleServletRegistration(), errorServletRegistration());
    this.webServer.start();
    assertThat(getResponse(getLocalUrl("/hello"), HttpMethod.PUT)).isEqualTo("Hello World");
    assertThat(getResponse(getLocalUrl("/bang"), HttpMethod.PUT)).isEqualTo("Hello World");
}
Also used : ErrorPage(org.springframework.boot.web.server.ErrorPage) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 15 with ErrorPage

use of org.springframework.boot.web.server.ErrorPage in project spring-boot by spring-projects.

the class ErrorPageFilterTests method responseIsCommittedWhenRequestIsAsyncAndStatusIs400Plus.

@Test
void responseIsCommittedWhenRequestIsAsyncAndStatusIs400Plus() throws Exception {
    this.filter.addErrorPages(new ErrorPage("/error"));
    this.request.setAsyncStarted(true);
    this.chain = new TestFilterChain((request, response, chain) -> {
        chain.call();
        response.sendError(400, "BAD");
    });
    this.filter.doFilter(this.request, this.response, this.chain);
    assertThat(this.chain.getRequest()).isEqualTo(this.request);
    assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getResponse()).isEqualTo(this.response);
    assertThat(this.response.isCommitted()).isTrue();
}
Also used : RequestDispatcher(jakarta.servlet.RequestDispatcher) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockFilterChain(org.springframework.mock.web.MockFilterChain) Enumeration(java.util.Enumeration) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DeferredResult(org.springframework.web.context.request.async.DeferredResult) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) ClientAbortException(org.apache.catalina.connector.ClientAbortException) ServletException(jakarta.servlet.ServletException) WebAsyncManager(org.springframework.web.context.request.async.WebAsyncManager) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) OutputCaptureExtension(org.springframework.boot.testsupport.system.OutputCaptureExtension) BDDMockito.given(org.mockito.BDDMockito.given) NestedServletException(org.springframework.web.util.NestedServletException) Map(java.util.Map) StandardServletAsyncWebRequest(org.springframework.web.context.request.async.StandardServletAsyncWebRequest) ServletRequest(jakarta.servlet.ServletRequest) MissingServletRequestParameterException(org.springframework.web.bind.MissingServletRequestParameterException) WebAsyncUtils(org.springframework.web.context.request.async.WebAsyncUtils) ErrorPage(org.springframework.boot.web.server.ErrorPage) MockRequestDispatcher(org.springframework.mock.web.MockRequestDispatcher) BDDMockito.then(org.mockito.BDDMockito.then) IOException(java.io.IOException) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Test(org.junit.jupiter.api.Test) HttpServletResponseWrapper(jakarta.servlet.http.HttpServletResponseWrapper) HttpStatus(org.springframework.http.HttpStatus) Mockito.never(org.mockito.Mockito.never) ServletResponse(jakarta.servlet.ServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) MockFilterConfig(org.springframework.mock.web.MockFilterConfig) CapturedOutput(org.springframework.boot.testsupport.system.CapturedOutput) Mockito.mock(org.mockito.Mockito.mock) ErrorPage(org.springframework.boot.web.server.ErrorPage) HttpServletResponseWrapper(jakarta.servlet.http.HttpServletResponseWrapper) Test(org.junit.jupiter.api.Test)

Aggregations

ErrorPage (org.springframework.boot.web.server.ErrorPage)24 Test (org.junit.jupiter.api.Test)20 IOException (java.io.IOException)19 MockFilterChain (org.springframework.mock.web.MockFilterChain)19 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)19 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)19 NestedServletException (org.springframework.web.util.NestedServletException)19 RequestDispatcher (jakarta.servlet.RequestDispatcher)17 ServletException (jakarta.servlet.ServletException)17 ServletRequest (jakarta.servlet.ServletRequest)17 ServletResponse (jakarta.servlet.ServletResponse)17 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)17 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)17 HttpServletResponseWrapper (jakarta.servlet.http.HttpServletResponseWrapper)17 Enumeration (java.util.Enumeration)17 HashMap (java.util.HashMap)17 Map (java.util.Map)17 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)17 ClientAbortException (org.apache.catalina.connector.ClientAbortException)17 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)17