br.eti.danielcamargo.backend.common.config.WebAppInitializer.java Source code

Java tutorial

Introduction

Here is the source code for br.eti.danielcamargo.backend.common.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 br.eti.danielcamargo.backend.common.config;

import br.eti.danielcamargo.backend.common.config.context.CoreConfig;
import br.eti.danielcamargo.backend.common.config.context.SecurityConfig;
import br.eti.danielcamargo.backend.common.config.context.WebConfig;
import java.util.EnumSet;
import javax.servlet.DispatcherType;
import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
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.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.servlet.DispatcherServlet;

public class WebAppInitializer implements WebApplicationInitializer {

    static Logger LOG = LoggerFactory.getLogger(WebAppInitializer.class);

    @Override
    public void onStartup(ServletContext servletContext) {
        WebApplicationContext rootContext = createRootContext(servletContext);
        configureWebServlets(servletContext, rootContext);
        configureSpringSecurity(servletContext, rootContext);
    }

    private WebApplicationContext createRootContext(ServletContext servletContext) {
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(CoreConfig.class, SecurityConfig.class);
        rootContext.refresh();
        servletContext.addListener(new ContextLoaderListener(rootContext));
        servletContext.setInitParameter("defaultHtmlEscape", "true");
        return rootContext;
    }

    private void configureWebServlets(ServletContext servletContext, WebApplicationContext rootContext) {
        AnnotationConfigWebApplicationContext mvcContext = new AnnotationConfigWebApplicationContext();
        mvcContext.register(WebConfig.class);
        mvcContext.setParent(rootContext);
        DispatcherServlet restDispatcherServlet = new DispatcherServlet(mvcContext);
        String restDispatcherName = "Rest Servlet";
        ServletRegistration.Dynamic restServletRegistration = servletContext.addServlet(restDispatcherName,
                restDispatcherServlet);
        restServletRegistration.setLoadOnStartup(1);
        restServletRegistration.addMapping("/rest/*");
    }

    private void configureSpringSecurity(ServletContext servletContext, WebApplicationContext rootContext) {
        EnumSet<DispatcherType> dispacherTypes = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD);
        FilterRegistration.Dynamic springSecurity = servletContext.addFilter("springSecurityFilterChain",
                new DelegatingFilterProxy("springSecurityFilterChain", rootContext));
        springSecurity.addMappingForUrlPatterns(dispacherTypes, true, "/*");
    }
}