Search in sources :

Example 21 with ServletContextListener

use of javax.servlet.ServletContextListener in project undertow by undertow-io.

the class ServerSentEventSCI method onStartup.

@Override
public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException {
    if (c == null || c.isEmpty()) {
        return;
    }
    try {
        final Map<String, ServerSentEventConnectionCallback> callbacks = new HashMap<>();
        ServletContextImpl servletContext = (ServletContextImpl) ctx;
        final List<InstanceHandle<?>> handles = new ArrayList<>();
        for (Class<?> clazz : c) {
            final ServerSentEvent annotation = clazz.getAnnotation(ServerSentEvent.class);
            if (annotation == null) {
                continue;
            }
            String path = annotation.value();
            final InstanceHandle<?> instance = servletContext.getDeployment().getDeploymentInfo().getClassIntrospecter().createInstanceFactory(clazz).createInstance();
            handles.add(instance);
            callbacks.put(path, (ServerSentEventConnectionCallback) instance.getInstance());
        }
        if (callbacks.isEmpty()) {
            return;
        }
        servletContext.getDeployment().getDeploymentInfo().addInnerHandlerChainWrapper(new HandlerWrapper() {

            @Override
            public HttpHandler wrap(HttpHandler handler) {
                PathTemplateHandler pathTemplateHandler = new PathTemplateHandler(handler, false);
                for (Map.Entry<String, ServerSentEventConnectionCallback> e : callbacks.entrySet()) {
                    pathTemplateHandler.add(e.getKey(), new ServerSentEventHandler(e.getValue()));
                }
                return pathTemplateHandler;
            }
        });
        servletContext.addListener(new ServletContextListener() {

            @Override
            public void contextInitialized(ServletContextEvent sce) {
            }

            @Override
            public void contextDestroyed(ServletContextEvent sce) {
                for (InstanceHandle<?> h : handles) {
                    h.release();
                }
            }
        });
    } catch (Exception e) {
        throw new ServletException(e);
    }
}
Also used : HttpHandler(io.undertow.server.HttpHandler) HashMap(java.util.HashMap) ServletContextListener(javax.servlet.ServletContextListener) ServerSentEventConnectionCallback(io.undertow.server.handlers.sse.ServerSentEventConnectionCallback) PathTemplateHandler(io.undertow.server.handlers.PathTemplateHandler) ServletContextImpl(io.undertow.servlet.spec.ServletContextImpl) ArrayList(java.util.ArrayList) ServerSentEventHandler(io.undertow.server.handlers.sse.ServerSentEventHandler) HandlerWrapper(io.undertow.server.HandlerWrapper) ServletException(javax.servlet.ServletException) ServletException(javax.servlet.ServletException) InstanceHandle(io.undertow.servlet.api.InstanceHandle) ServletContextEvent(javax.servlet.ServletContextEvent)

Aggregations

ServletContextListener (javax.servlet.ServletContextListener)21 ServletContextEvent (javax.servlet.ServletContextEvent)19 Test (org.junit.Test)11 MockServletContext (org.springframework.mock.web.test.MockServletContext)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)4 ServletContext (javax.servlet.ServletContext)3 HandlerWrapper (io.undertow.server.HandlerWrapper)2 HttpHandler (io.undertow.server.HttpHandler)2 Servlet (javax.servlet.Servlet)2 ServletException (javax.servlet.ServletException)2 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)2 BeanDefinitionStoreException (org.springframework.beans.factory.BeanDefinitionStoreException)2 PathTemplateHandler (io.undertow.server.handlers.PathTemplateHandler)1 PredicatedHandler (io.undertow.server.handlers.builder.PredicatedHandler)1 CachingResourceManager (io.undertow.server.handlers.resource.CachingResourceManager)1 FileResourceManager (io.undertow.server.handlers.resource.FileResourceManager)1 ResourceManager (io.undertow.server.handlers.resource.ResourceManager)1 ServerSentEventConnectionCallback (io.undertow.server.handlers.sse.ServerSentEventConnectionCallback)1 ServerSentEventHandler (io.undertow.server.handlers.sse.ServerSentEventHandler)1