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;
}
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();
}
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();
}
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();
}
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();
}
Aggregations