Search in sources :

Example 1 with ServletContextResourceLoader

use of cn.taketoday.web.context.support.ServletContextResourceLoader in project today-infrastructure by TAKETODAY.

the class ServletContextSupportTests method testServletContextResourceLoader.

@Test
public void testServletContextResourceLoader() {
    MockServletContext sc = new MockServletContext("classpath:cn/taketoday/web/context");
    ServletContextResourceLoader rl = new ServletContextResourceLoader(sc);
    assertThat(rl.getResource("/WEB-INF/web.xml").exists()).isTrue();
    assertThat(rl.getResource("WEB-INF/web.xml").exists()).isTrue();
    assertThat(rl.getResource("../context/WEB-INF/web.xml").exists()).isTrue();
    assertThat(rl.getResource("/../context/WEB-INF/web.xml").exists()).isTrue();
}
Also used : MockServletContext(cn.taketoday.web.mock.MockServletContext) ServletContextResourceLoader(cn.taketoday.web.context.support.ServletContextResourceLoader) Test(org.junit.jupiter.api.Test)

Example 2 with ServletContextResourceLoader

use of cn.taketoday.web.context.support.ServletContextResourceLoader in project today-framework by TAKETODAY.

the class ServletContextSupportTests method testServletContextResourceLoader.

@Test
public void testServletContextResourceLoader() {
    MockServletContext sc = new MockServletContext("classpath:cn/taketoday/web/context");
    ServletContextResourceLoader rl = new ServletContextResourceLoader(sc);
    assertThat(rl.getResource("/WEB-INF/web.xml").exists()).isTrue();
    assertThat(rl.getResource("WEB-INF/web.xml").exists()).isTrue();
    assertThat(rl.getResource("../context/WEB-INF/web.xml").exists()).isTrue();
    assertThat(rl.getResource("/../context/WEB-INF/web.xml").exists()).isTrue();
}
Also used : MockServletContext(cn.taketoday.web.testfixture.servlet.MockServletContext) ServletContextResourceLoader(cn.taketoday.web.context.support.ServletContextResourceLoader) Test(org.junit.jupiter.api.Test)

Example 3 with ServletContextResourceLoader

use of cn.taketoday.web.context.support.ServletContextResourceLoader 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 ServletContextResourceLoader

use of cn.taketoday.web.context.support.ServletContextResourceLoader 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

ServletContextResourceLoader (cn.taketoday.web.context.support.ServletContextResourceLoader)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 StandardServletEnvironment (cn.taketoday.web.context.support.StandardServletEnvironment)2 NestedServletException (cn.taketoday.web.servlet.NestedServletException)2 Test (org.junit.jupiter.api.Test)2 MockServletContext (cn.taketoday.web.mock.MockServletContext)1 MockServletContext (cn.taketoday.web.testfixture.servlet.MockServletContext)1