Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
 
@WebListener
public class SpringLoader implements ServletContextListener {
    protected static final String ANNOTATION_CONFIG_CONTEXT = "ANNOTATION_CONFIG_APPLICATION_CONTEXT";

    /**
     * Initialize standalone spring context. Registers the spring configuration and finally register it at the servlet context
     */
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        AnnotationConfigApplicationContext springContext = new AnnotationConfigApplicationContext();
        springContext.register(SpringContext.class);
        springContext.refresh();
        final ServletContext servletContext = servletContextEvent.getServletContext();
        servletContext.setAttribute(ANNOTATION_CONFIG_CONTEXT, springContext);
        ServletRegistration servletRegistration = servletContext.addServlet("SAMLDispatcherServlet", DispatcherServlet.class);
        servletRegistration.addMapping("/saml/*");
        FilterRegistration.Dynamic filterRegistration = servletContext.addFilter("LoginFilter", SPFilter.class);
        filterRegistration.addMappingForUrlPatterns(null, false, "/api/*");
        filterRegistration.addMappingForUrlPatterns(null, false, "/saml/*");
        filterRegistration.setInitParameter("a", "b");
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        Object ctx = servletContextEvent.getServletContext().getAttribute(ANNOTATION_CONFIG_CONTEXT);
        if(ctx instanceof AnnotationConfigApplicationContext) {
            ((AnnotationConfigApplicationContext)ctx).close();
        }
    }
}

...