com.gosmarter.it.eis.config.ExcelERPWebApplicationInitializer.java Source code

Java tutorial

Introduction

Here is the source code for com.gosmarter.it.eis.config.ExcelERPWebApplicationInitializer.java

Source

/*
 * Copyright 2002-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gosmarter.it.eis.config;

import java.util.EnumSet;

import javax.servlet.DispatcherType;
import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.zkoss.web.servlet.dsp.InterpreterServlet;
import org.zkoss.zk.au.http.DHtmlUpdateServlet;
import org.zkoss.zk.ui.http.DHtmlLayoutServlet;
import org.zkoss.zk.ui.http.HttpSessionListener;

import com.gosmarter.it.eis.servlet.WebDbInitializer;

/**
 * No customizations of {@link AbstractSecurityWebApplicationInitializer} are
 * necessary.
 * 
 * @author Rob Winch
 */

public class ExcelERPWebApplicationInitializer implements WebApplicationInitializer {

    static Log logger = LogFactory.getLog(ExcelERPWebApplicationInitializer.class);

    private static final Class<?>[] configurationClasses = new Class<?>[] { RootConfig.class, BeanConfig.class };

    public void onStartup(ServletContext servletContext) throws ServletException {
        registerListener(servletContext);
        registerDispatcherServlet(servletContext);

        registerSpringSecurityFilterChain(servletContext);
    }

    private void registerDispatcherServlet(ServletContext servletContext) {

        AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
        dispatcherContext.register(RootConfig.class);

        logger.debug("after register(DispatcherConfig function");

        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("zkLoader", new DHtmlLayoutServlet());

        dispatcher.setInitParameter("update-uri", "/zkau");

        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("*.zul");

        dispatcher = servletContext.addServlet("auEngine", new DHtmlUpdateServlet());
        dispatcher.addMapping("/zkau/*");

        dispatcher = servletContext.addServlet("dspLoader", new InterpreterServlet());
        dispatcher.setInitParameter("class-resource", "true");
        dispatcher.addMapping("*.dsp");

    }

    private void registerListener(ServletContext servletContext) {
        AnnotationConfigWebApplicationContext rootContext = createContext(configurationClasses);

        servletContext.addListener(new RequestContextListener());
        servletContext.addListener(new ContextLoaderListener(rootContext));
        servletContext.addListener(new HttpSessionListener());
        servletContext.addListener(new WebDbInitializer());
    }

    private void registerSpringSecurityFilterChain(ServletContext servletContext) {
        DelegatingFilterProxy delegatingFilterProxy = new DelegatingFilterProxy("springSecurityFilterChain");
        FilterRegistration fr = servletContext.addFilter("securityFilter", delegatingFilterProxy);
        fr.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), true, "/*");
    }

    private AnnotationConfigWebApplicationContext createContext(final Class<?>... annotatedClasses) {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();

        context.register(annotatedClasses);
        return context;
    }

}