Search in sources :

Example 1 with ServletRequestEvent

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);
        }
    }
}
Also used : ServletRequestListener(jakarta.servlet.ServletRequestListener) JSR356AsyncSupport(org.atmosphere.container.JSR356AsyncSupport) JSR356AsyncSupport(org.atmosphere.container.JSR356AsyncSupport) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletRequestEvent(jakarta.servlet.ServletRequestEvent) Map(java.util.Map)

Example 2 with ServletRequestEvent

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();
}
Also used : MockRunnable(org.springframework.core.task.MockRunnable) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ServletRequestEvent(jakarta.servlet.ServletRequestEvent) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 3 with ServletRequestEvent

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();
}
Also used : MockRunnable(org.springframework.core.task.MockRunnable) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ServletRequestEvent(jakarta.servlet.ServletRequestEvent) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 4 with ServletRequestEvent

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"));
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) ServletRequestListener(jakarta.servlet.ServletRequestListener) ArrayList(java.util.ArrayList) ServletRequestEvent(jakarta.servlet.ServletRequestEvent) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 5 with ServletRequestEvent

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);
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletContext(jakarta.servlet.ServletContext) ServletRequestEvent(jakarta.servlet.ServletRequestEvent) Test(org.junit.jupiter.api.Test)

Aggregations

ServletRequestEvent (jakarta.servlet.ServletRequestEvent)7 Test (org.junit.jupiter.api.Test)4 ServletRequestListener (jakarta.servlet.ServletRequestListener)3 MockRunnable (org.springframework.core.task.MockRunnable)3 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)3 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)3 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)2 ServletContext (jakarta.servlet.ServletContext)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Context (org.apache.catalina.Context)1 Tomcat (org.apache.catalina.startup.Tomcat)1 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)1 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)1 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)1 JSR356AsyncSupport (org.atmosphere.container.JSR356AsyncSupport)1 Test (org.junit.Test)1