Search in sources :

Example 1 with WebServletInitializer

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

the class AbstractServletWebServer method addDefaultServlet.

/**
 * Add default servlet
 */
protected void addDefaultServlet() {
    DefaultServletConfiguration servletConfiguration = this.defaultServletConfiguration;
    if (servletConfiguration != null) {
        // config default servlet
        getWebApplicationConfiguration().configureDefaultServlet(servletConfiguration);
        if (servletConfiguration.isEnable()) {
            Servlet defaultServlet = getDefaultServlet(servletConfiguration);
            if (defaultServlet != null) {
                log.info("Default servlet is enabled, use servlet: [{}]", defaultServlet.getServletInfo());
                WebServletInitializer<Servlet> initializer = new WebServletInitializer<>(defaultServlet);
                initializer.setName(Constant.DEFAULT);
                initializer.setOrder(Ordered.HIGHEST_PRECEDENCE);
                initializer.addUrlMappings(servletConfiguration.getUrlMappings());
                initializer.setInitParameters(servletConfiguration.getInitParameters());
                getContextInitializers().add(initializer);
            }
        }
    }
}
Also used : DefaultServletConfiguration(cn.taketoday.web.framework.config.DefaultServletConfiguration) Servlet(jakarta.servlet.Servlet) WebServletInitializer(cn.taketoday.web.servlet.initializer.WebServletInitializer)

Example 2 with WebServletInitializer

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

the class AbstractServletWebServer method addJspServlet.

/**
 * Add jsp to context
 */
protected void addJspServlet() {
    JspServletConfiguration jspServletConfiguration = this.jspServletConfiguration;
    if (jspServletConfiguration != null) {
        // config jsp servlet
        getWebApplicationConfiguration().configureJspServlet(jspServletConfiguration);
        if (jspServletConfiguration.isEnabled()) {
            try {
                Servlet jspServlet = BeanUtils.newInstance(jspServletConfiguration.getClassName());
                log.info("Jsp is enabled, use jsp servlet: [{}]", jspServlet.getServletInfo());
                WebServletInitializer<Servlet> initializer = new WebServletInitializer<>(jspServlet);
                initializer.setName(jspServletConfiguration.getName());
                initializer.setOrder(Ordered.HIGHEST_PRECEDENCE);
                initializer.addUrlMappings(jspServletConfiguration.getUrlMappings());
                initializer.setInitParameters(jspServletConfiguration.getInitParameters());
                getContextInitializers().add(initializer);
            } catch (ClassNotFoundException e) {
                throw new ConfigurationException("jsp servlet class not found", e);
            }
        }
    }
}
Also used : ConfigurationException(cn.taketoday.core.ConfigurationException) Servlet(jakarta.servlet.Servlet) JspServletConfiguration(cn.taketoday.web.framework.config.JspServletConfiguration) WebServletInitializer(cn.taketoday.web.servlet.initializer.WebServletInitializer)

Example 3 with WebServletInitializer

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

the class WebServletApplicationLoader method configureServletInitializers.

/**
 * Configure {@link Servlet}
 *
 * @param applicationContext {@link ApplicationContext}
 * @param contextInitializers {@link WebApplicationInitializer}s
 */
protected void configureServletInitializers(WebApplicationContext applicationContext, List<WebApplicationInitializer> contextInitializers) {
    Collection<Servlet> servlets = applicationContext.getAnnotatedBeans(WebServlet.class);
    for (Servlet servlet : servlets) {
        Class<?> beanClass = servlet.getClass();
        WebServletInitializer<Servlet> webServletInitializer = new WebServletInitializer<>(servlet);
        WebServlet webServlet = beanClass.getAnnotation(WebServlet.class);
        String[] urlPatterns = webServlet.urlPatterns();
        if (ObjectUtils.isEmpty(urlPatterns)) {
            urlPatterns = new String[] { BeanDefinitionBuilder.defaultBeanName(beanClass) };
        }
        webServletInitializer.addUrlMappings(urlPatterns);
        webServletInitializer.setLoadOnStartup(webServlet.loadOnStartup());
        webServletInitializer.setAsyncSupported(webServlet.asyncSupported());
        for (WebInitParam initParam : webServlet.initParams()) {
            webServletInitializer.addInitParameter(initParam.name(), initParam.value());
        }
        MultipartConfig multipartConfig = beanClass.getAnnotation(MultipartConfig.class);
        if (multipartConfig != null) {
            webServletInitializer.setMultipartConfig(new MultipartConfigElement(multipartConfig));
        }
        ServletSecurity servletSecurity = beanClass.getAnnotation(ServletSecurity.class);
        if (servletSecurity != null) {
            webServletInitializer.setServletSecurity(new ServletSecurityElement(servletSecurity));
        }
        String name = webServlet.name();
        if (StringUtils.isEmpty(name)) {
            String displayName = webServlet.displayName();
            if (StringUtils.isEmpty(displayName)) {
                name = BeanDefinitionBuilder.defaultBeanName(beanClass);
            } else {
                name = displayName;
            }
        }
        webServletInitializer.setName(name);
        contextInitializers.add(webServletInitializer);
    }
}
Also used : ServletSecurity(jakarta.servlet.annotation.ServletSecurity) ServletSecurityElement(jakarta.servlet.ServletSecurityElement) MultipartConfigElement(jakarta.servlet.MultipartConfigElement) WebServlet(jakarta.servlet.annotation.WebServlet) MultipartConfig(jakarta.servlet.annotation.MultipartConfig) WebInitParam(jakarta.servlet.annotation.WebInitParam) Servlet(jakarta.servlet.Servlet) WebServlet(jakarta.servlet.annotation.WebServlet) WebServletInitializer(cn.taketoday.web.servlet.initializer.WebServletInitializer)

Aggregations

WebServletInitializer (cn.taketoday.web.servlet.initializer.WebServletInitializer)3 Servlet (jakarta.servlet.Servlet)3 ConfigurationException (cn.taketoday.core.ConfigurationException)1 DefaultServletConfiguration (cn.taketoday.web.framework.config.DefaultServletConfiguration)1 JspServletConfiguration (cn.taketoday.web.framework.config.JspServletConfiguration)1 MultipartConfigElement (jakarta.servlet.MultipartConfigElement)1 ServletSecurityElement (jakarta.servlet.ServletSecurityElement)1 MultipartConfig (jakarta.servlet.annotation.MultipartConfig)1 ServletSecurity (jakarta.servlet.annotation.ServletSecurity)1 WebInitParam (jakarta.servlet.annotation.WebInitParam)1 WebServlet (jakarta.servlet.annotation.WebServlet)1