io.springagora.store.AppInitializer.java Source code

Java tutorial

Introduction

Here is the source code for io.springagora.store.AppInitializer.java

Source

/*
 * Spring Agora
 * https://github.com/ThiagoUriel/spring-agora-web
 * Copyright (C) 2014 - Thiago Uriel M. Garcia
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package io.springagora.store;

import io.springagora.store.rest.RestConfig;
import io.springagora.store.web.WebConfig;

import java.util.EnumSet;

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

import org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.DispatcherServlet;

/**
 * Application initializer for Servlet 3.0 environment.
 * <p>
 * This is a very valid sample for any Spring WEB/MVC application, using a REST design,
 * since it enables the Dispatcher Servlet and also the filter needed to handle HTTP
 * methods informed via HTML hidden parameter "_method".
 *  
 * @author Thiago Uriel M. Garcia
 */
public class AppInitializer implements WebApplicationInitializer {
    private static final String URL_PATTERN_WEB = "/pages/*";
    private static final String URL_PATTERN_REST = "/api/*";

    private static final String dispatcherWebName = "Spring MVC Dispatcher Servlet (WEB)";
    private static final String dispatcherRestName = "Spring MVC Dispatcher Servlet (REST)";

    /** {@inheritDoc} */
    @Override
    public void onStartup(ServletContext container) {
        initializeSpringContext(container);
        initializeWebApplication(container);
        initializeRESTfulAPI(container);
        initializeOpenEMInViewFilter(container);
    }

    /**
     * Creates the Spring Root Context, which is the context providing core features
     * to the application, like the database layer configuration, aspect configuration
     * ando so on. This is loaded by the Spring's context listener.
     * 
     * @param container
     *      Container where all web objects are going to be registered. 
     */
    private void initializeSpringContext(ServletContext container) {
        //
        // The root context is created upon all definition contained in the Java class,
        // (SpringAppConfig) containing the Spring initialization directives.
        //
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(ApplicationConfig.class);
        container.addListener(new ContextLoaderListener(rootContext));
    }

    /**
     * Web Application.
     * 
     * @param container
     *      {@code ServletContext}. Representation of the context that is serving
     *      the JEE application. Servlets, filters and listeners are registered
     *      via this interface.
     */
    private void initializeWebApplication(ServletContext container) {
        AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
        dispatcherContext.register(WebConfig.class);

        DispatcherServlet webDispatcher = new DispatcherServlet(dispatcherContext);
        ServletRegistration.Dynamic servletReg = container.addServlet(dispatcherWebName, webDispatcher);
        servletReg.setLoadOnStartup(1);
        servletReg.addMapping(URL_PATTERN_WEB);

        HiddenHttpMethodFilter filter = new HiddenHttpMethodFilter();
        FilterRegistration.Dynamic filterReg = container.addFilter("Hidden HTTP Method Filter", filter);
        filterReg.addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST), true, dispatcherWebName);
    }

    /**
     * RESTful API.
     * 
     * @param container
     *      {@code ServletContext}. Representation of the context that is serving
     *      the JEE application. Servlets, filters and listeners are registered
     *      via this interface.
     */
    private void initializeRESTfulAPI(ServletContext container) {
        AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
        dispatcherContext.register(RestConfig.class);

        DispatcherServlet restDispatcher = new DispatcherServlet(dispatcherContext);
        ServletRegistration.Dynamic servletReg = container.addServlet(dispatcherRestName, restDispatcher);
        servletReg.setLoadOnStartup(2);
        servletReg.addMapping(URL_PATTERN_REST);
    }

    private void initializeOpenEMInViewFilter(ServletContext container) {
        OpenEntityManagerInViewFilter sessionFilter = new OpenEntityManagerInViewFilter();
        FilterRegistration.Dynamic filterReg = container.addFilter("Open EM In View Filter", sessionFilter);
        filterReg.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, URL_PATTERN_WEB);
        filterReg.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, URL_PATTERN_REST);
    }
}