use of jakarta.servlet.ServletRequestEvent in project atmosphere by Atmosphere.
the class ContainerInitializer method onStartup.
@Override
public void onStartup(Set<Class<?>> classes, final ServletContext c) {
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);
// Don't use WebLogic Native WebSocket support if JSR356 is available
int size = c.getServerInfo().toLowerCase().contains("weblogic") ? 1 : 0;
String s = reg.getValue().getInitParameter(ApplicationConfig.PROPERTY_COMET_SUPPORT);
boolean force = c.getServerInfo().toLowerCase().contains("glassfish") || c.getServerInfo().toLowerCase().contains("payara");
if (s != null && s.equals(JSR356AsyncSupport.class.getName())) {
force = true;
} else if (s != null) {
force = false;
}
if (force || l.size() == size && 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 {
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);
}
}
}
use of jakarta.servlet.ServletRequestEvent in project spring-framework by spring-projects.
the class RequestContextListenerTests method requestContextListenerWithSameThread.
@Test
public void requestContextListenerWithSameThread() {
RequestContextListener listener = new RequestContextListener();
MockServletContext context = new MockServletContext();
MockHttpServletRequest request = new MockHttpServletRequest(context);
request.setAttribute("test", "value");
assertThat(RequestContextHolder.getRequestAttributes()).isNull();
listener.requestInitialized(new ServletRequestEvent(context, request));
assertThat(RequestContextHolder.getRequestAttributes()).isNotNull();
assertThat(RequestContextHolder.getRequestAttributes().getAttribute("test", RequestAttributes.SCOPE_REQUEST)).isEqualTo("value");
MockRunnable runnable = new MockRunnable();
RequestContextHolder.getRequestAttributes().registerDestructionCallback("test", runnable, RequestAttributes.SCOPE_REQUEST);
listener.requestDestroyed(new ServletRequestEvent(context, request));
assertThat(RequestContextHolder.getRequestAttributes()).isNull();
assertThat(runnable.wasExecuted()).isTrue();
}
use of jakarta.servlet.ServletRequestEvent in project spring-framework by spring-projects.
the class RequestContextListenerTests method requestContextListenerWithDifferentThread.
@Test
public void requestContextListenerWithDifferentThread() {
final RequestContextListener listener = new RequestContextListener();
final MockServletContext context = new MockServletContext();
final MockHttpServletRequest request = new MockHttpServletRequest(context);
request.setAttribute("test", "value");
assertThat(RequestContextHolder.getRequestAttributes()).isNull();
listener.requestInitialized(new ServletRequestEvent(context, request));
assertThat(RequestContextHolder.getRequestAttributes()).isNotNull();
assertThat(RequestContextHolder.getRequestAttributes().getAttribute("test", RequestAttributes.SCOPE_REQUEST)).isEqualTo("value");
MockRunnable runnable = new MockRunnable();
RequestContextHolder.getRequestAttributes().registerDestructionCallback("test", runnable, RequestAttributes.SCOPE_REQUEST);
// Execute requestDestroyed callback in different thread.
Thread thread = new Thread() {
@Override
public void run() {
listener.requestDestroyed(new ServletRequestEvent(context, request));
}
};
thread.start();
try {
thread.join();
} catch (InterruptedException ex) {
}
// Still bound to original thread, but at least completed.
assertThat(RequestContextHolder.getRequestAttributes()).isNotNull();
assertThat(runnable.wasExecuted()).isTrue();
// Check that a repeated execution in the same thread works and performs cleanup.
listener.requestInitialized(new ServletRequestEvent(context, request));
listener.requestDestroyed(new ServletRequestEvent(context, request));
assertThat(RequestContextHolder.getRequestAttributes()).isNull();
}
use of jakarta.servlet.ServletRequestEvent in project tomcat by apache.
the class TestStandardHostValve method testSRLAfterError.
@Test
public void testSRLAfterError() throws Exception {
// Set up a container
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
// Add the error page
Tomcat.addServlet(ctx, "error", new ErrorServlet());
ctx.addServletMappingDecoded("/error", "error");
final List<String> result = new ArrayList<>();
// Add the request listener
ServletRequestListener servletRequestListener = new ServletRequestListener() {
@Override
public void requestDestroyed(ServletRequestEvent sre) {
result.add("Visit requestDestroyed");
}
@Override
public void requestInitialized(ServletRequestEvent sre) {
result.add("Visit requestInitialized");
}
};
((StandardContext) ctx).addApplicationEventListener(servletRequestListener);
tomcat.start();
// Request a page that triggers an error
ByteChunk bc = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() + "/error?errorCode=400", bc, null);
Assert.assertEquals(400, rc);
Assert.assertTrue(result.contains("Visit requestInitialized"));
Assert.assertTrue(result.contains("Visit requestDestroyed"));
}
use of jakarta.servlet.ServletRequestEvent in project OpenGrok by OpenGrok.
the class WebappListenerTest method testRequest.
/**
* Simple smoke test for WebappListener request handling.
*/
@Test
public void testRequest() {
WebappListener wl = new WebappListener();
final HttpServletRequest req = mock(HttpServletRequest.class);
final ServletContext servletContext = mock(ServletContext.class);
when(req.getServletContext()).thenReturn(servletContext);
ServletRequestEvent event = new ServletRequestEvent(servletContext, req);
wl.requestInitialized(event);
wl.requestDestroyed(event);
}
Aggregations