Search in sources :

Example 1 with SessionListener

use of io.undertow.server.session.SessionListener 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()));
    }
    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();
                listeners.start();
                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();
                    }
                }
                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());
                if (!deploymentInfo.isSecurityDisabled()) {
                    HttpHandler securityHandler = setupSecurityHandlers(wrappedHandlers);
                    wrappedHandlers = new PredicateHandler(DispatcherTypePredicate.REQUEST, securityHandler, wrappedHandlers);
                }
                HttpHandler outerHandlers = wrapHandlers(wrappedHandlers, deploymentInfo.getOuterHandlerChainWrappers());
                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();
    state = State.DEPLOYED;
}
Also used : 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) ArrayList(java.util.ArrayList) PredicateHandler(io.undertow.server.handlers.PredicateHandler) ServletException(javax.servlet.ServletException) HttpServerExchange(io.undertow.server.HttpServerExchange) ThreadSetupHandler(io.undertow.servlet.api.ThreadSetupHandler) HttpContinueReadHandler(io.undertow.server.handlers.HttpContinueReadHandler) CrawlerSessionManagerHandler(io.undertow.servlet.handlers.CrawlerSessionManagerHandler) SessionListener(io.undertow.server.session.SessionListener) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo)

Example 2 with SessionListener

use of io.undertow.server.session.SessionListener in project wildfly by wildfly.

the class DistributableSessionTestCase method setNullAttribute.

@Test
public void setNullAttribute() {
    String name = "name";
    Object value = null;
    this.validate(session -> session.setAttribute(name, value));
    SessionManager<LocalSessionContext, Batch> manager = mock(SessionManager.class);
    Batcher<Batch> batcher = mock(Batcher.class);
    BatchContext context = mock(BatchContext.class);
    SessionAttributes attributes = mock(SessionAttributes.class);
    SessionListener listener = mock(SessionListener.class);
    SessionListeners listeners = new SessionListeners();
    listeners.addSessionListener(listener);
    Object expected = new Object();
    when(this.session.getAttributes()).thenReturn(attributes);
    when(attributes.removeAttribute(name)).thenReturn(expected);
    when(this.manager.getSessionListeners()).thenReturn(listeners);
    when(this.manager.getSessionManager()).thenReturn(manager);
    when(manager.getBatcher()).thenReturn(batcher);
    when(batcher.resumeBatch(this.batch)).thenReturn(context);
    Object result = this.adapter.setAttribute(name, value);
    assertSame(expected, result);
    verify(listener, never()).attributeAdded(this.adapter, name, value);
    verify(listener, never()).attributeUpdated(same(this.adapter), same(name), same(value), any());
    verify(listener).attributeRemoved(this.adapter, name, expected);
    verify(context).close();
}
Also used : Batch(org.wildfly.clustering.ee.Batch) SessionListeners(io.undertow.server.session.SessionListeners) SessionAttributes(org.wildfly.clustering.web.session.SessionAttributes) BatchContext(org.wildfly.clustering.ee.BatchContext) SessionListener(io.undertow.server.session.SessionListener) Test(org.junit.Test)

Example 3 with SessionListener

use of io.undertow.server.session.SessionListener in project wildfly by wildfly.

the class DistributableSessionTestCase method setNewAttribute.

@Test
public void setNewAttribute() {
    String name = "name";
    Integer value = Integer.valueOf(1);
    this.validate(session -> session.setAttribute(name, value));
    SessionManager<LocalSessionContext, Batch> manager = mock(SessionManager.class);
    Batcher<Batch> batcher = mock(Batcher.class);
    BatchContext context = mock(BatchContext.class);
    SessionAttributes attributes = mock(SessionAttributes.class);
    SessionListener listener = mock(SessionListener.class);
    SessionListeners listeners = new SessionListeners();
    listeners.addSessionListener(listener);
    Object expected = null;
    when(this.session.getAttributes()).thenReturn(attributes);
    when(attributes.setAttribute(name, value)).thenReturn(expected);
    when(this.manager.getSessionListeners()).thenReturn(listeners);
    when(this.manager.getSessionManager()).thenReturn(manager);
    when(manager.getBatcher()).thenReturn(batcher);
    when(batcher.resumeBatch(this.batch)).thenReturn(context);
    Object result = this.adapter.setAttribute(name, value);
    assertSame(expected, result);
    verify(listener).attributeAdded(this.adapter, name, value);
    verify(listener, never()).attributeUpdated(same(this.adapter), same(name), same(value), any());
    verify(listener, never()).attributeRemoved(same(this.adapter), same(name), any());
    verify(context).close();
}
Also used : Batch(org.wildfly.clustering.ee.Batch) SessionListeners(io.undertow.server.session.SessionListeners) SessionAttributes(org.wildfly.clustering.web.session.SessionAttributes) BatchContext(org.wildfly.clustering.ee.BatchContext) SessionListener(io.undertow.server.session.SessionListener) Test(org.junit.Test)

Example 4 with SessionListener

use of io.undertow.server.session.SessionListener in project wildfly by wildfly.

the class DistributableSessionTestCase method setSameAttribute.

@Test
public void setSameAttribute() {
    String name = "name";
    Integer value = Integer.valueOf(1);
    this.validate(session -> session.setAttribute(name, value));
    SessionManager<LocalSessionContext, Batch> manager = mock(SessionManager.class);
    Batcher<Batch> batcher = mock(Batcher.class);
    BatchContext context = mock(BatchContext.class);
    SessionAttributes attributes = mock(SessionAttributes.class);
    SessionListener listener = mock(SessionListener.class);
    SessionListeners listeners = new SessionListeners();
    listeners.addSessionListener(listener);
    Object expected = value;
    when(this.manager.getSessionManager()).thenReturn(manager);
    when(manager.getBatcher()).thenReturn(batcher);
    when(batcher.resumeBatch(this.batch)).thenReturn(context);
    when(this.session.getAttributes()).thenReturn(attributes);
    when(attributes.setAttribute(name, value)).thenReturn(expected);
    when(this.manager.getSessionListeners()).thenReturn(listeners);
    Object result = this.adapter.setAttribute(name, value);
    assertSame(expected, result);
    verify(listener, never()).attributeAdded(this.adapter, name, value);
    verify(listener, never()).attributeUpdated(same(this.adapter), same(name), same(value), any());
    verify(listener, never()).attributeRemoved(same(this.adapter), same(name), any());
    verify(context).close();
}
Also used : Batch(org.wildfly.clustering.ee.Batch) SessionListeners(io.undertow.server.session.SessionListeners) SessionAttributes(org.wildfly.clustering.web.session.SessionAttributes) BatchContext(org.wildfly.clustering.ee.BatchContext) SessionListener(io.undertow.server.session.SessionListener) Test(org.junit.Test)

Example 5 with SessionListener

use of io.undertow.server.session.SessionListener in project wildfly by wildfly.

the class DistributableSessionTestCase method removeNonExistingAttribute.

@Test
public void removeNonExistingAttribute() {
    String name = "name";
    this.validate(session -> session.removeAttribute(name));
    SessionManager<LocalSessionContext, Batch> manager = mock(SessionManager.class);
    Batcher<Batch> batcher = mock(Batcher.class);
    BatchContext context = mock(BatchContext.class);
    SessionAttributes attributes = mock(SessionAttributes.class);
    SessionListener listener = mock(SessionListener.class);
    SessionListeners listeners = new SessionListeners();
    listeners.addSessionListener(listener);
    when(this.session.getAttributes()).thenReturn(attributes);
    when(attributes.removeAttribute(name)).thenReturn(null);
    when(this.manager.getSessionListeners()).thenReturn(listeners);
    when(this.manager.getSessionManager()).thenReturn(manager);
    when(manager.getBatcher()).thenReturn(batcher);
    when(batcher.resumeBatch(this.batch)).thenReturn(context);
    Object result = this.adapter.removeAttribute(name);
    assertNull(result);
    verify(listener, never()).attributeRemoved(same(this.adapter), same(name), any());
    verify(context).close();
}
Also used : Batch(org.wildfly.clustering.ee.Batch) SessionListeners(io.undertow.server.session.SessionListeners) SessionAttributes(org.wildfly.clustering.web.session.SessionAttributes) BatchContext(org.wildfly.clustering.ee.BatchContext) SessionListener(io.undertow.server.session.SessionListener) Test(org.junit.Test)

Aggregations

SessionListener (io.undertow.server.session.SessionListener)12 SessionListeners (io.undertow.server.session.SessionListeners)9 Test (org.junit.Test)9 Batch (org.wildfly.clustering.ee.Batch)9 BatchContext (org.wildfly.clustering.ee.BatchContext)8 SessionAttributes (org.wildfly.clustering.web.session.SessionAttributes)7 HttpServerExchange (io.undertow.server.HttpServerExchange)4 ServletContainerInitializerInfo (io.undertow.servlet.api.ServletContainerInitializerInfo)2 AuthenticatedSession (io.undertow.security.api.AuthenticatedSessionManager.AuthenticatedSession)1 AuthenticationMechanismFactory (io.undertow.security.api.AuthenticationMechanismFactory)1 NotificationReceiver (io.undertow.security.api.NotificationReceiver)1 HandlerWrapper (io.undertow.server.HandlerWrapper)1 HttpHandler (io.undertow.server.HttpHandler)1 HttpContinueReadHandler (io.undertow.server.handlers.HttpContinueReadHandler)1 PredicateHandler (io.undertow.server.handlers.PredicateHandler)1 Session (io.undertow.server.session.Session)1 SessionConfig (io.undertow.server.session.SessionConfig)1 SessionIdGenerator (io.undertow.server.session.SessionIdGenerator)1 ServletExtension (io.undertow.servlet.ServletExtension)1 Deployment (io.undertow.servlet.api.Deployment)1