be.wolkmaan.klimtoren.web.config.WebAppInitializer.java Source code

Java tutorial

Introduction

Here is the source code for be.wolkmaan.klimtoren.web.config.WebAppInitializer.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package be.wolkmaan.klimtoren.web.config;

import java.util.Set;
import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.DispatcherServlet;
import org.zkoss.zk.au.http.DHtmlUpdateServlet;
import org.zkoss.zk.ui.http.DHtmlLayoutServlet;
import org.zkoss.zk.ui.http.HttpSessionListener;

/**
 *
 * @author karl
 */
public class WebAppInitializer implements WebApplicationInitializer {

    private static final Logger logger = LoggerFactory.getLogger(WebAppInitializer.class);

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        // Create the root appcontext
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(RootConfig.class);
        rootContext.register(PersistenceConfig.class);

        // Manage the lifecycle of the root appcontext
        servletContext.addListener(new ContextLoaderListener(rootContext));
        servletContext.setInitParameter("defaultHtmlEscape", "true");

        this.zkLoaderServlet(servletContext);

        // now the config for the Dispatcher servlet
        AnnotationConfigWebApplicationContext mvcContext = new AnnotationConfigWebApplicationContext();
        mvcContext.register(WebMvcConfig.class);

        // Filters
        // http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/filter/package-summary.html
        // Enables support for DELETE and PUT request methods with web browser
        // clients
        // http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/filter/HiddenHttpMethodFilter.html
        FilterRegistration.Dynamic fr = servletContext.addFilter("hiddenHttpMethodFilter",
                new HiddenHttpMethodFilter());
        fr.addMappingForUrlPatterns(null, true, "/*");

        fr = servletContext.addFilter("encodingFilter", new CharacterEncodingFilter());
        fr.setInitParameter("encoding", "UTF-8");
        fr.setInitParameter("forceEncoding", "true");
        fr.addMappingForUrlPatterns(null, true, "/*");

        // The main Spring MVC servlet.
        ServletRegistration.Dynamic appServlet = servletContext.addServlet("appServlet",
                new DispatcherServlet(mvcContext));
        appServlet.setLoadOnStartup(2);
        Set<String> mappingConflicts = appServlet.addMapping("/");

        if (!mappingConflicts.isEmpty()) {
            for (String s : mappingConflicts) {
                logger.error("Mapping conflict: " + s);
            }
            throw new IllegalStateException("'appServlet' cannot be mapped to '/' under Tomcat versions <= 7.0.14");
        }

        HttpSessionListener zkCleanUp = new HttpSessionListener();
        servletContext.addListener(zkCleanUp);

        this.logbackServlet(servletContext);

        this.zkUpdateServlet(servletContext);
    }

    public void logbackServlet(ServletContext servletContext) {
        ServletRegistration.Dynamic servlet = servletContext.addServlet("logbackStatus",
                new ch.qos.logback.classic.ViewStatusMessagesServlet());
        servlet.setLoadOnStartup(3);
        Set<String> mappingConflicts = servlet.addMapping("/admin/logbackStatus/*");

        if (!mappingConflicts.isEmpty()) {
            for (String s : mappingConflicts) {
                logger.error("Mapping conflict: " + s);
            }
            throw new IllegalStateException("servlet cannot be mapped");
        }
    }

    public void zkLoaderServlet(ServletContext servletContext) {
        ServletRegistration.Dynamic servlet = servletContext.addServlet("zkLoader", new DHtmlLayoutServlet());
        servlet.setInitParameter("update-uri", "/zkau");
        servlet.setLoadOnStartup(1);
        servlet.addMapping("*.zul");
        servlet.addMapping("*.zhtml");
        servlet.addMapping("/zk/*");
    }

    public void zkUpdateServlet(ServletContext servletContext) {
        ServletRegistration.Dynamic servlet = servletContext.addServlet("auEngine", new DHtmlUpdateServlet());
        servlet.addMapping("/zkau/*");
    }
}