Search in sources :

Example 1 with NestedServletException

use of cn.taketoday.web.servlet.NestedServletException in project today-infrastructure by TAKETODAY.

the class ErrorPageFilterTests method nestedServletExceptionIsUnwrapped.

@Test
void nestedServletExceptionIsUnwrapped() throws Exception {
    this.filter.addErrorPages(new ErrorPage(RuntimeException.class, "/500"));
    this.chain = new TestFilterChain((request, response, chain) -> {
        chain.call();
        throw new NestedServletException("Wrapper", new RuntimeException("BAD"));
    });
    this.filter.doFilter(this.request, this.response, this.chain);
    assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getStatus()).isEqualTo(500);
    assertThat(this.request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE)).isEqualTo(500);
    assertThat(this.request.getAttribute(RequestDispatcher.ERROR_MESSAGE)).isEqualTo("BAD");
    Map<String, Object> requestAttributes = getAttributesForDispatch("/500");
    assertThat(requestAttributes.get(RequestDispatcher.ERROR_EXCEPTION_TYPE)).isEqualTo(RuntimeException.class);
    assertThat(requestAttributes.get(RequestDispatcher.ERROR_EXCEPTION)).isInstanceOf(RuntimeException.class);
    assertThat(this.request.getAttribute(RequestDispatcher.ERROR_EXCEPTION_TYPE)).isNull();
    assertThat(this.request.getAttribute(RequestDispatcher.ERROR_EXCEPTION)).isNull();
    assertThat(this.request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI)).isEqualTo("/test/path");
    assertThat(this.response.isCommitted()).isTrue();
    assertThat(this.response.getForwardedUrl()).isEqualTo("/500");
}
Also used : MockFilterConfig(cn.taketoday.mock.web.MockFilterConfig) RequestDispatcher(jakarta.servlet.RequestDispatcher) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) Enumeration(java.util.Enumeration) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) MockRequestDispatcher(cn.taketoday.mock.web.MockRequestDispatcher) ClientAbortException(org.apache.catalina.connector.ClientAbortException) DeferredResult(cn.taketoday.web.context.async.DeferredResult) ServletException(jakarta.servlet.ServletException) MockFilterChain(cn.taketoday.mock.web.MockFilterChain) OutputCaptureExtension(cn.taketoday.framework.test.system.OutputCaptureExtension) MockHttpServletRequest(cn.taketoday.mock.web.MockHttpServletRequest) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) ErrorPageFilter(cn.taketoday.framework.web.servlet.support.ErrorPageFilter) NestedServletException(cn.taketoday.web.servlet.NestedServletException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BDDMockito.given(org.mockito.BDDMockito.given) MissingRequestParameterException(cn.taketoday.web.bind.MissingRequestParameterException) Map(java.util.Map) WebAsyncManager(cn.taketoday.web.context.async.WebAsyncManager) WebAsyncUtils(cn.taketoday.web.context.async.WebAsyncUtils) ServletRequest(jakarta.servlet.ServletRequest) BDDMockito.then(org.mockito.BDDMockito.then) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test) ErrorPage(cn.taketoday.framework.web.server.ErrorPage) HttpServletResponseWrapper(jakarta.servlet.http.HttpServletResponseWrapper) Mockito.never(org.mockito.Mockito.never) StandardServletAsyncWebRequest(cn.taketoday.web.context.async.StandardServletAsyncWebRequest) ServletResponse(jakarta.servlet.ServletResponse) CapturedOutput(cn.taketoday.framework.test.system.CapturedOutput) HttpStatus(cn.taketoday.http.HttpStatus) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) MockHttpServletResponse(cn.taketoday.mock.web.MockHttpServletResponse) Mockito.mock(org.mockito.Mockito.mock) ErrorPage(cn.taketoday.framework.web.server.ErrorPage) NestedServletException(cn.taketoday.web.servlet.NestedServletException) HttpServletResponseWrapper(jakarta.servlet.http.HttpServletResponseWrapper) Test(org.junit.jupiter.api.Test)

Example 2 with NestedServletException

use of cn.taketoday.web.servlet.NestedServletException in project today-framework by TAKETODAY.

the class ErrorPageFilterTests method nestedServletExceptionIsUnwrapped.

@Test
void nestedServletExceptionIsUnwrapped() throws Exception {
    this.filter.addErrorPages(new ErrorPage(RuntimeException.class, "/500"));
    this.chain = new TestFilterChain((request, response, chain) -> {
        chain.call();
        throw new NestedServletException("Wrapper", new RuntimeException("BAD"));
    });
    this.filter.doFilter(this.request, this.response, this.chain);
    assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getStatus()).isEqualTo(500);
    assertThat(this.request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE)).isEqualTo(500);
    assertThat(this.request.getAttribute(RequestDispatcher.ERROR_MESSAGE)).isEqualTo("BAD");
    Map<String, Object> requestAttributes = getAttributesForDispatch("/500");
    assertThat(requestAttributes.get(RequestDispatcher.ERROR_EXCEPTION_TYPE)).isEqualTo(RuntimeException.class);
    assertThat(requestAttributes.get(RequestDispatcher.ERROR_EXCEPTION)).isInstanceOf(RuntimeException.class);
    assertThat(this.request.getAttribute(RequestDispatcher.ERROR_EXCEPTION_TYPE)).isNull();
    assertThat(this.request.getAttribute(RequestDispatcher.ERROR_EXCEPTION)).isNull();
    assertThat(this.request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI)).isEqualTo("/test/path");
    assertThat(this.response.isCommitted()).isTrue();
    assertThat(this.response.getForwardedUrl()).isEqualTo("/500");
}
Also used : MockFilterConfig(cn.taketoday.mock.web.MockFilterConfig) RequestDispatcher(jakarta.servlet.RequestDispatcher) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) Enumeration(java.util.Enumeration) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) MockRequestDispatcher(cn.taketoday.mock.web.MockRequestDispatcher) ClientAbortException(org.apache.catalina.connector.ClientAbortException) DeferredResult(cn.taketoday.web.context.async.DeferredResult) ServletException(jakarta.servlet.ServletException) MockFilterChain(cn.taketoday.mock.web.MockFilterChain) OutputCaptureExtension(cn.taketoday.framework.test.system.OutputCaptureExtension) MockHttpServletRequest(cn.taketoday.mock.web.MockHttpServletRequest) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) ErrorPageFilter(cn.taketoday.framework.web.servlet.support.ErrorPageFilter) NestedServletException(cn.taketoday.web.servlet.NestedServletException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BDDMockito.given(org.mockito.BDDMockito.given) MissingRequestParameterException(cn.taketoday.web.bind.MissingRequestParameterException) Map(java.util.Map) WebAsyncManager(cn.taketoday.web.context.async.WebAsyncManager) WebAsyncUtils(cn.taketoday.web.context.async.WebAsyncUtils) ServletRequest(jakarta.servlet.ServletRequest) BDDMockito.then(org.mockito.BDDMockito.then) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test) ErrorPage(cn.taketoday.framework.web.server.ErrorPage) HttpServletResponseWrapper(jakarta.servlet.http.HttpServletResponseWrapper) Mockito.never(org.mockito.Mockito.never) StandardServletAsyncWebRequest(cn.taketoday.web.context.async.StandardServletAsyncWebRequest) ServletResponse(jakarta.servlet.ServletResponse) CapturedOutput(cn.taketoday.framework.test.system.CapturedOutput) HttpStatus(cn.taketoday.http.HttpStatus) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) MockHttpServletResponse(cn.taketoday.mock.web.MockHttpServletResponse) Mockito.mock(org.mockito.Mockito.mock) ErrorPage(cn.taketoday.framework.web.server.ErrorPage) NestedServletException(cn.taketoday.web.servlet.NestedServletException) HttpServletResponseWrapper(jakarta.servlet.http.HttpServletResponseWrapper) Test(org.junit.jupiter.api.Test)

Example 3 with NestedServletException

use of cn.taketoday.web.servlet.NestedServletException in project today-framework by TAKETODAY.

the class GenericFilterBean method init.

/**
 * Standard way of initializing this filter.
 * Map config parameters onto bean properties of this filter, and
 * invoke subclass initialization.
 *
 * @param filterConfig the configuration for this filter
 * @throws ServletException if bean properties are invalid (or required
 * properties are missing), or if subclass initialization fails.
 * @see #initFilterBean
 */
@Override
public final void init(FilterConfig filterConfig) throws ServletException {
    Assert.notNull(filterConfig, "FilterConfig must not be null");
    this.filterConfig = filterConfig;
    // Set bean properties from init parameters.
    PropertyValues pvs = getFilterConfigPropertyValues(filterConfig, requiredProperties);
    if (!pvs.isEmpty()) {
        try {
            BeanWrapper bw = BeanWrapper.forBeanPropertyAccess(this);
            ResourceLoader resourceLoader = new ServletContextResourceLoader(filterConfig.getServletContext());
            Environment env = this.environment;
            if (env == null) {
                env = new StandardServletEnvironment();
            }
            bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, env));
            initBeanWrapper(bw);
            bw.setPropertyValues(pvs, true);
        } catch (BeansException ex) {
            String msg = "Failed to set bean properties on filter '" + filterConfig.getFilterName() + "': " + ex.getMessage();
            logger.error(msg, ex);
            throw new NestedServletException(msg, ex);
        }
    }
    // Let subclasses do whatever initialization they like.
    initFilterBean();
    if (logger.isDebugEnabled()) {
        logger.debug("Filter '{}' configured for use", filterConfig.getFilterName());
    }
}
Also used : ResourceLoader(cn.taketoday.core.io.ResourceLoader) ServletContextResourceLoader(cn.taketoday.web.context.support.ServletContextResourceLoader) BeanWrapper(cn.taketoday.beans.BeanWrapper) PropertyValues(cn.taketoday.beans.PropertyValues) NestedServletException(cn.taketoday.web.servlet.NestedServletException) StandardServletEnvironment(cn.taketoday.web.context.support.StandardServletEnvironment) Environment(cn.taketoday.core.env.Environment) ResourceEditor(cn.taketoday.core.io.ResourceEditor) StandardServletEnvironment(cn.taketoday.web.context.support.StandardServletEnvironment) ServletContextResourceLoader(cn.taketoday.web.context.support.ServletContextResourceLoader) BeansException(cn.taketoday.beans.BeansException)

Example 4 with NestedServletException

use of cn.taketoday.web.servlet.NestedServletException in project today-infrastructure by TAKETODAY.

the class GenericFilterBean method init.

/**
 * Standard way of initializing this filter.
 * Map config parameters onto bean properties of this filter, and
 * invoke subclass initialization.
 *
 * @param filterConfig the configuration for this filter
 * @throws ServletException if bean properties are invalid (or required
 * properties are missing), or if subclass initialization fails.
 * @see #initFilterBean
 */
@Override
public final void init(FilterConfig filterConfig) throws ServletException {
    Assert.notNull(filterConfig, "FilterConfig must not be null");
    this.filterConfig = filterConfig;
    // Set bean properties from init parameters.
    PropertyValues pvs = getFilterConfigPropertyValues(filterConfig, requiredProperties);
    if (!pvs.isEmpty()) {
        try {
            BeanWrapper bw = BeanWrapper.forBeanPropertyAccess(this);
            ResourceLoader resourceLoader = new ServletContextResourceLoader(filterConfig.getServletContext());
            Environment env = this.environment;
            if (env == null) {
                env = new StandardServletEnvironment();
            }
            bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, env));
            initBeanWrapper(bw);
            bw.setPropertyValues(pvs, true);
        } catch (BeansException ex) {
            String msg = "Failed to set bean properties on filter '" + filterConfig.getFilterName() + "': " + ex.getMessage();
            logger.error(msg, ex);
            throw new NestedServletException(msg, ex);
        }
    }
    // Let subclasses do whatever initialization they like.
    initFilterBean();
    if (logger.isDebugEnabled()) {
        logger.debug("Filter '{}' configured for use", filterConfig.getFilterName());
    }
}
Also used : ResourceLoader(cn.taketoday.core.io.ResourceLoader) ServletContextResourceLoader(cn.taketoday.web.context.support.ServletContextResourceLoader) BeanWrapper(cn.taketoday.beans.BeanWrapper) PropertyValues(cn.taketoday.beans.PropertyValues) NestedServletException(cn.taketoday.web.servlet.NestedServletException) StandardServletEnvironment(cn.taketoday.web.context.support.StandardServletEnvironment) Environment(cn.taketoday.core.env.Environment) ResourceEditor(cn.taketoday.core.io.ResourceEditor) StandardServletEnvironment(cn.taketoday.web.context.support.StandardServletEnvironment) ServletContextResourceLoader(cn.taketoday.web.context.support.ServletContextResourceLoader) BeansException(cn.taketoday.beans.BeansException)

Aggregations

NestedServletException (cn.taketoday.web.servlet.NestedServletException)4 BeanWrapper (cn.taketoday.beans.BeanWrapper)2 BeansException (cn.taketoday.beans.BeansException)2 PropertyValues (cn.taketoday.beans.PropertyValues)2 Environment (cn.taketoday.core.env.Environment)2 ResourceEditor (cn.taketoday.core.io.ResourceEditor)2 ResourceLoader (cn.taketoday.core.io.ResourceLoader)2 CapturedOutput (cn.taketoday.framework.test.system.CapturedOutput)2 OutputCaptureExtension (cn.taketoday.framework.test.system.OutputCaptureExtension)2 ErrorPage (cn.taketoday.framework.web.server.ErrorPage)2 ErrorPageFilter (cn.taketoday.framework.web.servlet.support.ErrorPageFilter)2 HttpStatus (cn.taketoday.http.HttpStatus)2 MockFilterChain (cn.taketoday.mock.web.MockFilterChain)2 MockFilterConfig (cn.taketoday.mock.web.MockFilterConfig)2 MockHttpServletRequest (cn.taketoday.mock.web.MockHttpServletRequest)2 MockHttpServletResponse (cn.taketoday.mock.web.MockHttpServletResponse)2 MockRequestDispatcher (cn.taketoday.mock.web.MockRequestDispatcher)2 MissingRequestParameterException (cn.taketoday.web.bind.MissingRequestParameterException)2 DeferredResult (cn.taketoday.web.context.async.DeferredResult)2 StandardServletAsyncWebRequest (cn.taketoday.web.context.async.StandardServletAsyncWebRequest)2