com.gantzgulch.sharing.configuration.WebInitializer.java Source code

Java tutorial

Introduction

Here is the source code for com.gantzgulch.sharing.configuration.WebInitializer.java

Source

/*
 * Copyright 2011 GantzGulch, Inc.
 * 
 * This file is part of GantzFileSharing.
 * 
 * GantzFileSharing is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GantzFileSharing is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with GantzFileSharing.  If not, see <http://www.gnu.org/licenses/>. 
 */
package com.gantzgulch.sharing.configuration;

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

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.servlet.DispatcherServlet;

import com.gantzgulch.sharing.configurationmvc.MvcAppConfig;

public class WebInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {

        AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
        appContext.getEnvironment().setActiveProfiles("production");
        appContext.scan("com.gantzgulch.sharing");

        ContextLoaderListener contextListener = new ContextLoaderListener(appContext);
        servletContext.addListener(contextListener);

        DelegatingFilterProxy filterProxy = new DelegatingFilterProxy("springSecurityFilterChain");
        FilterRegistration.Dynamic filter = servletContext.addFilter("securityFilter", filterProxy);
        filter.addMappingForUrlPatterns(null, false, "/*");

        AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext();
        webContext.register(MvcAppConfig.class);

        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("sharing",
                new DispatcherServlet(webContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("*.htm");
    }

}