Search in sources :

Example 1 with ServletContextImpl

use of io.undertow.servlet.spec.ServletContextImpl in project undertow by undertow-io.

the class DeploymentManagerImpl method deploy.

@Override
public void deploy() {
    final DeploymentInfo deploymentInfo = originalDeployment.clone();
    if (deploymentInfo.getServletStackTraces() == ServletStackTraces.ALL) {
        UndertowServletLogger.REQUEST_LOGGER.servletStackTracesAll(deploymentInfo.getDeploymentName());
    }
    deploymentInfo.validate();
    final DeploymentImpl deployment = new DeploymentImpl(this, deploymentInfo, servletContainer);
    this.deployment = deployment;
    final ServletContextImpl servletContext = new ServletContextImpl(servletContainer, deployment);
    deployment.setServletContext(servletContext);
    handleExtensions(deploymentInfo, servletContext);
    final List<ThreadSetupHandler> setup = new ArrayList<>();
    setup.add(ServletRequestContextThreadSetupAction.INSTANCE);
    setup.add(new ContextClassLoaderSetupAction(deploymentInfo.getClassLoader()));
    setup.addAll(deploymentInfo.getThreadSetupActions());
    deployment.setThreadSetupActions(setup);
    deployment.getServletPaths().setWelcomePages(deploymentInfo.getWelcomePages());
    if (deploymentInfo.getDefaultEncoding() != null) {
        deployment.setDefaultCharset(Charset.forName(deploymentInfo.getDefaultEncoding()));
    }
    if (deploymentInfo.getDefaultRequestEncoding() != null) {
        deployment.setDefaultRequestCharset(Charset.forName(deploymentInfo.getDefaultRequestEncoding()));
    } else if (deploymentInfo.getDefaultEncoding() != null) {
        deployment.setDefaultRequestCharset(Charset.forName(deploymentInfo.getDefaultEncoding()));
    }
    if (deploymentInfo.getDefaultResponseEncoding() != null) {
        deployment.setDefaultResponseCharset(Charset.forName(deploymentInfo.getDefaultResponseEncoding()));
    } else if (deploymentInfo.getDefaultEncoding() != null) {
        deployment.setDefaultResponseCharset(Charset.forName(deploymentInfo.getDefaultEncoding()));
    }
    handleDeploymentSessionConfig(deploymentInfo, servletContext);
    deployment.setSessionManager(deploymentInfo.getSessionManagerFactory().createSessionManager(deployment));
    deployment.getSessionManager().setDefaultSessionTimeout(deploymentInfo.getDefaultSessionTimeout());
    try {
        deployment.createThreadSetupAction(new ThreadSetupHandler.Action<Void, Object>() {

            @Override
            public Void call(HttpServerExchange exchange, Object ignore) throws Exception {
                final ApplicationListeners listeners = createListeners();
                deployment.setApplicationListeners(listeners);
                // now create the servlets and filters that we know about. We can still get more later
                createServletsAndFilters(deployment, deploymentInfo);
                // first initialize the temp dir
                initializeTempDir(servletContext, deploymentInfo);
                // then run the SCI's
                for (final ServletContainerInitializerInfo sci : deploymentInfo.getServletContainerInitializers()) {
                    final InstanceHandle<? extends ServletContainerInitializer> instance = sci.getInstanceFactory().createInstance();
                    try {
                        instance.getInstance().onStartup(sci.getHandlesTypes(), servletContext);
                    } finally {
                        instance.release();
                    }
                }
                listeners.start();
                deployment.getSessionManager().registerSessionListener(new SessionListenerBridge(deployment, listeners, servletContext));
                for (SessionListener listener : deploymentInfo.getSessionListeners()) {
                    deployment.getSessionManager().registerSessionListener(listener);
                }
                initializeErrorPages(deployment, deploymentInfo);
                initializeMimeMappings(deployment, deploymentInfo);
                listeners.contextInitialized();
                // run
                HttpHandler wrappedHandlers = ServletDispatchingHandler.INSTANCE;
                wrappedHandlers = wrapHandlers(wrappedHandlers, deploymentInfo.getInnerHandlerChainWrappers());
                wrappedHandlers = new RedirectDirHandler(wrappedHandlers, deployment.getServletPaths());
                if (!deploymentInfo.isSecurityDisabled()) {
                    HttpHandler securityHandler = setupSecurityHandlers(wrappedHandlers);
                    wrappedHandlers = new PredicateHandler(DispatcherTypePredicate.REQUEST, securityHandler, wrappedHandlers);
                }
                HttpHandler outerHandlers = wrapHandlers(wrappedHandlers, deploymentInfo.getOuterHandlerChainWrappers());
                outerHandlers = new SendErrorPageHandler(outerHandlers);
                wrappedHandlers = new PredicateHandler(DispatcherTypePredicate.REQUEST, outerHandlers, wrappedHandlers);
                wrappedHandlers = handleDevelopmentModePersistentSessions(wrappedHandlers, deploymentInfo, deployment.getSessionManager(), servletContext);
                MetricsCollector metrics = deploymentInfo.getMetricsCollector();
                if (metrics != null) {
                    wrappedHandlers = new MetricsChainHandler(wrappedHandlers, metrics, deployment);
                }
                if (deploymentInfo.getCrawlerSessionManagerConfig() != null) {
                    wrappedHandlers = new CrawlerSessionManagerHandler(deploymentInfo.getCrawlerSessionManagerConfig(), wrappedHandlers);
                }
                final ServletInitialHandler servletInitialHandler = SecurityActions.createServletInitialHandler(deployment.getServletPaths(), wrappedHandlers, deployment, servletContext);
                HttpHandler initialHandler = wrapHandlers(servletInitialHandler, deployment.getDeploymentInfo().getInitialHandlerChainWrappers());
                initialHandler = new HttpContinueReadHandler(initialHandler);
                if (deploymentInfo.getUrlEncoding() != null) {
                    initialHandler = Handlers.urlDecodingHandler(deploymentInfo.getUrlEncoding(), initialHandler);
                }
                deployment.setInitialHandler(initialHandler);
                deployment.setServletHandler(servletInitialHandler);
                // make sure we have a fresh set of servlet paths
                deployment.getServletPaths().invalidate();
                servletContext.initDone();
                return null;
            }
        }).call(null, null);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    // any problems with the paths won't get detected until the data is initialize
    // so we force initialization here
    deployment.getServletPaths().initData();
    for (ServletContextListener listener : deploymentInfo.getDeploymentCompleteListeners()) {
        listener.contextInitialized(new ServletContextEvent(servletContext));
    }
    state = State.DEPLOYED;
}
Also used : SendErrorPageHandler(io.undertow.servlet.handlers.SendErrorPageHandler) ServletContextListener(javax.servlet.ServletContextListener) RedirectDirHandler(io.undertow.servlet.handlers.RedirectDirHandler) ArrayList(java.util.ArrayList) PredicateHandler(io.undertow.server.handlers.PredicateHandler) HttpServerExchange(io.undertow.server.HttpServerExchange) ThreadSetupHandler(io.undertow.servlet.api.ThreadSetupHandler) HttpContinueReadHandler(io.undertow.server.handlers.HttpContinueReadHandler) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) MetricsCollector(io.undertow.servlet.api.MetricsCollector) HttpHandler(io.undertow.server.HttpHandler) ServletInitialHandler(io.undertow.servlet.handlers.ServletInitialHandler) ServletContainerInitializerInfo(io.undertow.servlet.api.ServletContainerInitializerInfo) ServletContextImpl(io.undertow.servlet.spec.ServletContextImpl) ServletException(javax.servlet.ServletException) CrawlerSessionManagerHandler(io.undertow.servlet.handlers.CrawlerSessionManagerHandler) SessionListener(io.undertow.server.session.SessionListener) ServletContextEvent(javax.servlet.ServletContextEvent)

Example 2 with ServletContextImpl

use of io.undertow.servlet.spec.ServletContextImpl in project undertow by undertow-io.

the class ServletContextListenerTestCase method testServletContextAttributeListener.

@Test
public void testServletContextAttributeListener() throws IOException {
    ServletContextImpl sc = manager.getDeployment().getServletContext();
    sc.setAttribute("test", "1");
    Assert.assertNotNull(ServletContextTestListener.servletContextAttributeEvent);
    Assert.assertEquals(ServletContextTestListener.servletContextAttributeEvent.getName(), "test");
    Assert.assertEquals(ServletContextTestListener.servletContextAttributeEvent.getValue(), "1");
    sc.setAttribute("test", "2");
    Assert.assertEquals(ServletContextTestListener.servletContextAttributeEvent.getName(), "test");
    Assert.assertEquals(ServletContextTestListener.servletContextAttributeEvent.getValue(), "1");
    sc.setAttribute("test", "3");
    Assert.assertEquals(ServletContextTestListener.servletContextAttributeEvent.getName(), "test");
    Assert.assertEquals(ServletContextTestListener.servletContextAttributeEvent.getValue(), "2");
    sc.removeAttribute("test");
    Assert.assertEquals(ServletContextTestListener.servletContextAttributeEvent.getName(), "test");
    Assert.assertEquals(ServletContextTestListener.servletContextAttributeEvent.getValue(), "3");
}
Also used : ServletContextImpl(io.undertow.servlet.spec.ServletContextImpl) Test(org.junit.Test)

Example 3 with ServletContextImpl

use of io.undertow.servlet.spec.ServletContextImpl in project undertow by undertow-io.

the class DefaultServlet method init.

@Override
public void init(ServletConfig config) throws ServletException {
    super.init(config);
    ServletContextImpl sc = (ServletContextImpl) config.getServletContext();
    this.deployment = sc.getDeployment();
    DefaultServletConfig defaultServletConfig = deployment.getDeploymentInfo().getDefaultServletConfig();
    if (defaultServletConfig != null) {
        defaultAllowed = defaultServletConfig.isDefaultAllowed();
        allowed = new HashSet<>();
        if (defaultServletConfig.getAllowed() != null) {
            allowed.addAll(defaultServletConfig.getAllowed());
        }
        disallowed = new HashSet<>();
        if (defaultServletConfig.getDisallowed() != null) {
            disallowed.addAll(defaultServletConfig.getDisallowed());
        }
    }
    if (config.getInitParameter(DEFAULT_ALLOWED) != null) {
        defaultAllowed = Boolean.parseBoolean(config.getInitParameter(DEFAULT_ALLOWED));
    }
    if (config.getInitParameter(ALLOWED_EXTENSIONS) != null) {
        String extensions = config.getInitParameter(ALLOWED_EXTENSIONS);
        allowed = new HashSet<>(Arrays.asList(extensions.split(",")));
    }
    if (config.getInitParameter(DISALLOWED_EXTENSIONS) != null) {
        String extensions = config.getInitParameter(DISALLOWED_EXTENSIONS);
        disallowed = new HashSet<>(Arrays.asList(extensions.split(",")));
    }
    if (config.getInitParameter(RESOLVE_AGAINST_CONTEXT_ROOT) != null) {
        resolveAgainstContextRoot = Boolean.parseBoolean(config.getInitParameter(RESOLVE_AGAINST_CONTEXT_ROOT));
    }
    if (config.getInitParameter(ALLOW_POST) != null) {
        allowPost = Boolean.parseBoolean(config.getInitParameter(ALLOW_POST));
    }
    if (deployment.getDeploymentInfo().getPreCompressedResources().isEmpty()) {
        this.resourceSupplier = new DefaultResourceSupplier(deployment.getDeploymentInfo().getResourceManager());
    } else {
        PreCompressedResourceSupplier preCompressedResourceSupplier = new PreCompressedResourceSupplier(deployment.getDeploymentInfo().getResourceManager());
        for (Map.Entry<String, String> entry : deployment.getDeploymentInfo().getPreCompressedResources().entrySet()) {
            preCompressedResourceSupplier.addEncoding(entry.getKey(), entry.getValue());
        }
        this.resourceSupplier = preCompressedResourceSupplier;
    }
    String listings = config.getInitParameter(DIRECTORY_LISTING);
    if (Boolean.valueOf(listings)) {
        this.directoryListingEnabled = true;
    }
}
Also used : ServletContextImpl(io.undertow.servlet.spec.ServletContextImpl) DefaultServletConfig(io.undertow.servlet.api.DefaultServletConfig) DefaultResourceSupplier(io.undertow.server.handlers.resource.DefaultResourceSupplier) PreCompressedResourceSupplier(io.undertow.server.handlers.resource.PreCompressedResourceSupplier) Map(java.util.Map)

Example 4 with ServletContextImpl

use of io.undertow.servlet.spec.ServletContextImpl 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

ServletContextImpl (io.undertow.servlet.spec.ServletContextImpl)4 HttpHandler (io.undertow.server.HttpHandler)2 ArrayList (java.util.ArrayList)2 ServletContextEvent (javax.servlet.ServletContextEvent)2 ServletContextListener (javax.servlet.ServletContextListener)2 ServletException (javax.servlet.ServletException)2 HandlerWrapper (io.undertow.server.HandlerWrapper)1 HttpServerExchange (io.undertow.server.HttpServerExchange)1 HttpContinueReadHandler (io.undertow.server.handlers.HttpContinueReadHandler)1 PathTemplateHandler (io.undertow.server.handlers.PathTemplateHandler)1 PredicateHandler (io.undertow.server.handlers.PredicateHandler)1 DefaultResourceSupplier (io.undertow.server.handlers.resource.DefaultResourceSupplier)1 PreCompressedResourceSupplier (io.undertow.server.handlers.resource.PreCompressedResourceSupplier)1 ServerSentEventConnectionCallback (io.undertow.server.handlers.sse.ServerSentEventConnectionCallback)1 ServerSentEventHandler (io.undertow.server.handlers.sse.ServerSentEventHandler)1 SessionListener (io.undertow.server.session.SessionListener)1 DefaultServletConfig (io.undertow.servlet.api.DefaultServletConfig)1 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)1 InstanceHandle (io.undertow.servlet.api.InstanceHandle)1 MetricsCollector (io.undertow.servlet.api.MetricsCollector)1