Search in sources :

Example 1 with SessionListeners

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

the class DistributableSessionManagerFactory method createSessionManager.

@Override
public io.undertow.server.session.SessionManager createSessionManager(final Deployment deployment) {
    DeploymentInfo info = deployment.getDeploymentInfo();
    boolean statisticsEnabled = info.getMetricsCollector() != null;
    RecordableInactiveSessionStatistics inactiveSessionStatistics = statisticsEnabled ? new RecordableInactiveSessionStatistics() : null;
    Supplier<String> factory = new IdentifierFactoryAdapter(info.getSessionIdGenerator());
    SessionExpirationListener expirationListener = new UndertowSessionExpirationListener(deployment, this.listeners);
    SessionManagerConfiguration<ServletContext> configuration = new SessionManagerConfiguration<ServletContext>() {

        @Override
        public ServletContext getServletContext() {
            return deployment.getServletContext();
        }

        @Override
        public Supplier<String> getIdentifierFactory() {
            return factory;
        }

        @Override
        public SessionExpirationListener getExpirationListener() {
            return expirationListener;
        }

        @Override
        public Recordable<ImmutableSessionMetaData> getInactiveSessionRecorder() {
            return inactiveSessionStatistics;
        }
    };
    SessionManager<Map<String, Object>, Batch> manager = this.factory.createSessionManager(configuration);
    Batcher<Batch> batcher = manager.getBatcher();
    info.addThreadSetupAction(new ThreadSetupHandler() {

        @Override
        public <T, C> Action<T, C> create(Action<T, C> action) {
            return new Action<T, C>() {

                @Override
                public T call(HttpServerExchange exchange, C context) throws Exception {
                    Batch batch = batcher.suspendBatch();
                    try (BatchContext ctx = batcher.resumeBatch(batch)) {
                        return action.call(exchange, context);
                    }
                }
            };
        }
    });
    SessionListeners listeners = this.listeners;
    RecordableSessionManagerStatistics statistics = (inactiveSessionStatistics != null) ? new DistributableSessionManagerStatistics(manager, inactiveSessionStatistics, this.config.getMaxActiveSessions()) : null;
    io.undertow.server.session.SessionManager result = new DistributableSessionManager(new DistributableSessionManagerConfiguration() {

        @Override
        public String getDeploymentName() {
            return info.getDeploymentName();
        }

        @Override
        public SessionManager<Map<String, Object>, Batch> getSessionManager() {
            return manager;
        }

        @Override
        public SessionListeners getSessionListeners() {
            return listeners;
        }

        @Override
        public RecordableSessionManagerStatistics getStatistics() {
            return statistics;
        }

        @Override
        public boolean isOrphanSessionAllowed() {
            // TODO Configure via DeploymentInfo
            return WildFlySecurityManager.doUnchecked(new PrivilegedAction<Boolean>() {

                @Override
                public Boolean run() {
                    return Boolean.getBoolean(ALLOW_ORPHAN_SESSION_PROPERTY);
                }
            });
        }
    });
    result.setDefaultSessionTimeout((int) this.config.getDefaultSessionTimeout().getSeconds());
    return result;
}
Also used : PrivilegedAction(java.security.PrivilegedAction) SessionManagerConfiguration(org.wildfly.clustering.web.session.SessionManagerConfiguration) HttpServerExchange(io.undertow.server.HttpServerExchange) ThreadSetupHandler(io.undertow.servlet.api.ThreadSetupHandler) SessionExpirationListener(org.wildfly.clustering.web.session.SessionExpirationListener) Batch(org.wildfly.clustering.ee.Batch) SessionListeners(io.undertow.server.session.SessionListeners) PrivilegedAction(java.security.PrivilegedAction) IdentifierFactoryAdapter(org.wildfly.clustering.web.undertow.IdentifierFactoryAdapter) ServletContext(javax.servlet.ServletContext) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) SessionManager(org.wildfly.clustering.web.session.SessionManager) BatchContext(org.wildfly.clustering.ee.BatchContext) ImmutableSessionMetaData(org.wildfly.clustering.web.session.ImmutableSessionMetaData) Map(java.util.Map)

Example 2 with SessionListeners

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

the class DistributableSessionTestCase method setNewAttribute.

@Test
public void setNewAttribute() {
    when(this.session.getMetaData()).thenReturn(this.metaData);
    when(this.metaData.isNew()).thenReturn(false);
    io.undertow.server.session.Session session = new DistributableSession(this.manager, this.session, this.config, this.batch, this.closeTask);
    String name = "name";
    Integer value = 1;
    SessionManager<Map<String, Object>, 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 = session.setAttribute(name, value);
    assertSame(expected, result);
    verify(listener).attributeAdded(session, name, value);
    verify(listener, never()).attributeUpdated(same(session), same(name), same(value), any());
    verify(listener, never()).attributeRemoved(same(session), same(name), any());
    verify(context).close();
}
Also used : BatchContext(org.wildfly.clustering.ee.BatchContext) Batch(org.wildfly.clustering.ee.Batch) SessionListeners(io.undertow.server.session.SessionListeners) SessionAttributes(org.wildfly.clustering.web.session.SessionAttributes) SessionListener(io.undertow.server.session.SessionListener) OptionMap(org.xnio.OptionMap) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 3 with SessionListeners

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

the class DistributableSessionTestCase method removeNonExistingAttribute.

@Test
public void removeNonExistingAttribute() {
    when(this.session.getMetaData()).thenReturn(this.metaData);
    when(this.metaData.isNew()).thenReturn(false);
    io.undertow.server.session.Session session = new DistributableSession(this.manager, this.session, this.config, this.batch, this.closeTask);
    String name = "name";
    SessionManager<Map<String, Object>, 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 = session.removeAttribute(name);
    assertNull(result);
    verify(listener, never()).attributeRemoved(same(session), same(name), any());
    verify(context).close();
}
Also used : BatchContext(org.wildfly.clustering.ee.BatchContext) Batch(org.wildfly.clustering.ee.Batch) SessionListeners(io.undertow.server.session.SessionListeners) SessionAttributes(org.wildfly.clustering.web.session.SessionAttributes) SessionListener(io.undertow.server.session.SessionListener) OptionMap(org.xnio.OptionMap) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 4 with SessionListeners

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

the class DistributableSessionTestCase method setNullAttribute.

@Test
public void setNullAttribute() {
    when(this.session.getMetaData()).thenReturn(this.metaData);
    when(this.metaData.isNew()).thenReturn(false);
    io.undertow.server.session.Session session = new DistributableSession(this.manager, this.session, this.config, this.batch, this.closeTask);
    String name = "name";
    Object value = null;
    SessionManager<Map<String, Object>, 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 = session.setAttribute(name, value);
    assertSame(expected, result);
    verify(listener, never()).attributeAdded(session, name, value);
    verify(listener, never()).attributeUpdated(same(session), same(name), same(value), any());
    verify(listener).attributeRemoved(session, name, expected);
    verify(context).close();
}
Also used : BatchContext(org.wildfly.clustering.ee.BatchContext) Batch(org.wildfly.clustering.ee.Batch) SessionListeners(io.undertow.server.session.SessionListeners) SessionAttributes(org.wildfly.clustering.web.session.SessionAttributes) SessionListener(io.undertow.server.session.SessionListener) OptionMap(org.xnio.OptionMap) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 5 with SessionListeners

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

the class DistributableSessionTestCase method setSameAttribute.

@Test
public void setSameAttribute() {
    when(this.session.getMetaData()).thenReturn(this.metaData);
    when(this.metaData.isNew()).thenReturn(false);
    io.undertow.server.session.Session session = new DistributableSession(this.manager, this.session, this.config, this.batch, this.closeTask);
    String name = "name";
    Integer value = 1;
    SessionManager<Map<String, Object>, 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 = session.setAttribute(name, value);
    assertSame(expected, result);
    verify(listener, never()).attributeAdded(session, name, value);
    verify(listener, never()).attributeUpdated(same(session), same(name), same(value), any());
    verify(listener, never()).attributeRemoved(same(session), same(name), any());
    verify(context).close();
}
Also used : BatchContext(org.wildfly.clustering.ee.BatchContext) Batch(org.wildfly.clustering.ee.Batch) SessionListeners(io.undertow.server.session.SessionListeners) SessionAttributes(org.wildfly.clustering.web.session.SessionAttributes) SessionListener(io.undertow.server.session.SessionListener) OptionMap(org.xnio.OptionMap) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

SessionListeners (io.undertow.server.session.SessionListeners)11 Map (java.util.Map)11 Batch (org.wildfly.clustering.ee.Batch)11 SessionListener (io.undertow.server.session.SessionListener)10 Test (org.junit.Test)10 BatchContext (org.wildfly.clustering.ee.BatchContext)10 HashMap (java.util.HashMap)9 SessionAttributes (org.wildfly.clustering.web.session.SessionAttributes)9 OptionMap (org.xnio.OptionMap)9 HttpServerExchange (io.undertow.server.HttpServerExchange)4 SessionConfig (io.undertow.server.session.SessionConfig)2 Duration (java.time.Duration)2 Instant (java.time.Instant)2 ImmutableSessionMetaData (org.wildfly.clustering.web.session.ImmutableSessionMetaData)2 SessionExpirationListener (org.wildfly.clustering.web.session.SessionExpirationListener)2 SessionMetaData (org.wildfly.clustering.web.session.SessionMetaData)2 Session (io.undertow.server.session.Session)1 Deployment (io.undertow.servlet.api.Deployment)1 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)1 ThreadSetupHandler (io.undertow.servlet.api.ThreadSetupHandler)1