use of javax.servlet.ServletRequestEvent in project jodd by oblac.
the class ShutdownTest method testSessionShutdown.
@Test
public void testSessionShutdown() {
// http session
HttpSession session = createSession("S2");
HttpServletRequest request = createRequest(session);
ServletRequestEvent requestEvent = createServletRequestEvent(request);
HttpSessionBindingEvent event = createHttpSessionBindingEvent(session);
// jodd
RequestContextListener requestContextListener = new RequestContextListener();
// start session, init request
requestContextListener.requestInitialized(requestEvent);
// petite
PetiteContainer pc = new PetiteContainer();
pc.registerPetiteBean(Ses.class, null, null, null, false);
Ses ses = (Ses) pc.getBean("ses");
assertNotNull(ses);
ses.setValue("jodd");
// session not expired
assertEquals("jodd", ses.getValue());
// shutdown
pc.shutdown();
assertEquals("-jodd", ses.getValue());
}
use of javax.servlet.ServletRequestEvent in project jodd by oblac.
the class ShutdownTest method testSessionExpired.
@Test
public void testSessionExpired() {
// http session
HttpSession session = createSession("S1");
HttpServletRequest request = createRequest(session);
ServletRequestEvent requestEvent = createServletRequestEvent(request);
HttpSessionBindingEvent event = createHttpSessionBindingEvent(session);
// jodd
RequestContextListener requestContextListener = new RequestContextListener();
// start session, init request
requestContextListener.requestInitialized(requestEvent);
// petite
PetiteContainer pc = new PetiteContainer();
pc.registerPetiteBean(Ses.class, null, null, null, false);
// callback not yet added
SessionScope.SessionBeans sessionBeans = (SessionScope.SessionBeans) session.getAttribute(ATTR_NAME);
assertNull(sessionBeans);
Ses ses = (Ses) pc.getBean("ses");
assertNotNull(ses);
// callback added
sessionBeans = (SessionScope.SessionBeans) session.getAttribute(ATTR_NAME);
assertNotNull(sessionBeans);
ses.setValue("jodd");
// session expired
sessionBeans.valueUnbound(event);
assertEquals("-jodd", ses.getValue());
pc.shutdown();
assertEquals("-jodd", ses.getValue());
}
use of javax.servlet.ServletRequestEvent in project jodd by oblac.
the class ServletsMockitoUtil method createServletRequestEvent.
public static ServletRequestEvent createServletRequestEvent(HttpServletRequest request) {
ServletRequestEvent event = mock(ServletRequestEvent.class);
when(event.getServletRequest()).thenReturn(request);
return event;
}
use of javax.servlet.ServletRequestEvent in project spring-framework by spring-projects.
the class RequestContextListenerTests method requestContextListenerWithSameThreadAndAttributesGone.
@Test
public void requestContextListenerWithSameThreadAndAttributesGone() {
RequestContextListener listener = new RequestContextListener();
MockServletContext context = new MockServletContext();
MockHttpServletRequest request = new MockHttpServletRequest(context);
request.setAttribute("test", "value");
assertNull(RequestContextHolder.getRequestAttributes());
listener.requestInitialized(new ServletRequestEvent(context, request));
assertNotNull(RequestContextHolder.getRequestAttributes());
assertEquals("value", RequestContextHolder.getRequestAttributes().getAttribute("test", RequestAttributes.SCOPE_REQUEST));
MockRunnable runnable = new MockRunnable();
RequestContextHolder.getRequestAttributes().registerDestructionCallback("test", runnable, RequestAttributes.SCOPE_REQUEST);
request.clearAttributes();
listener.requestDestroyed(new ServletRequestEvent(context, request));
assertNull(RequestContextHolder.getRequestAttributes());
assertTrue(runnable.wasExecuted());
}
use of javax.servlet.ServletRequestEvent in project atmosphere by Atmosphere.
the class ContainerInitializer method onStartup.
@Override
public void onStartup(Set<Class<?>> classes, final ServletContext c) throws ServletException {
c.log("Initializing AtmosphereFramework");
for (Map.Entry<String, ? extends ServletRegistration> reg : c.getServletRegistrations().entrySet()) {
String disableSwitchValue = reg.getValue().getInitParameter(ApplicationConfig.DISABLE_ATMOSPHERE_INITIALIZER);
// check if AtmosphereInitializer is disabled via web.xml see: https://github.com/Atmosphere/atmosphere/issues/1695
if (Boolean.parseBoolean(disableSwitchValue)) {
c.log("Container managed initialization disabled for servlet: " + reg.getValue().getName());
continue;
}
if (c.getAttribute(reg.getKey()) == null && IOUtils.isAtmosphere(reg.getValue().getClassName())) {
final AtmosphereFramework framework = AtmosphereFrameworkInitializer.newAtmosphereFramework(c, false, true);
// Hack to make jsr356 works. Pretty ugly.
DefaultAsyncSupportResolver resolver = new DefaultAsyncSupportResolver(framework.getAtmosphereConfig());
List<Class<? extends AsyncSupport>> l = resolver.detectWebSocketPresent(false, true);
if (resolver.testClassExists(DefaultAsyncSupportResolver.JSR356_WEBSOCKET)) {
try {
framework.setAsyncSupport(new JSR356AsyncSupport(framework.getAtmosphereConfig(), c));
} catch (IllegalStateException ex) {
framework.initializationError(ex);
}
}
try {
c.addListener(new ServletRequestListener() {
@Override
public void requestDestroyed(ServletRequestEvent sre) {
}
@Override
public void requestInitialized(ServletRequestEvent sre) {
HttpServletRequest r = HttpServletRequest.class.cast(sre.getServletRequest());
AtmosphereConfig config = framework.getAtmosphereConfig();
if (config.isSupportSession() && Utils.webSocketEnabled(r)) {
r.getSession(config.getInitParameter(ApplicationConfig.PROPERTY_SESSION_CREATE, true));
}
}
});
} catch (Throwable t) {
c.log("AtmosphereFramework : Unable to install WebSocket Session Creator", t);
}
try {
String s = c.getInitParameter(PROPERTY_SESSION_SUPPORT);
if (s != null) {
boolean sessionSupport = Boolean.valueOf(s);
if (sessionSupport && c.getMajorVersion() > 2) {
c.addListener(SessionSupport.class);
c.log("AtmosphereFramework : Installed " + SessionSupport.class);
}
}
} catch (Throwable t) {
c.log("AtmosphereFramework : SessionSupport error. Make sure you also define {} as a listener in web.xml, see https://github.com/Atmosphere/atmosphere/wiki/Enabling-HttpSession-Support", t);
}
c.setAttribute(reg.getKey(), framework);
}
}
}
Aggregations