Subversion Repositories SmartDukaan

Rev

Rev 35385 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.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.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
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.servlet.DispatcherServlet;

import com.spice.profitmandi.common.web.filter.CorsFilter;

public class SpringWebAppInitializer implements WebApplicationInitializer{
        
        private static final Logger LOGGER=LogManager.getLogger(SpringWebAppInitializer.class);
        
        private static final String DISPATCHER_SERVLET_NAME="DispatcherServlet";
        private static final String SPRING_CONFIG_PACKAGE_LOCATION="com.spice.profitmandi.web.config";
        
        public void onStartup(ServletContext servletContext) throws ServletException {
                WebApplicationContext context = getContext();
        servletContext.addListener(new ContextLoaderListener(context));
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet(DISPATCHER_SERVLET_NAME, new DispatcherServlet(context));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");

        // Register CORS filter to handle CORS for all requests including error responses
        FilterRegistration.Dynamic corsFilter = servletContext.addFilter("corsFilter", new CorsFilter());
        corsFilter.addMappingForUrlPatterns(
                EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE, DispatcherType.ERROR),
                false,
                "/*"
        );

        LOGGER.info("Context is loaded");
        }
        
        private AnnotationConfigWebApplicationContext getContext() {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.setConfigLocation(SPRING_CONFIG_PACKAGE_LOCATION);
        return context;
    }
        
        
}