Search in sources :

Example 11 with ServletRequestEvent

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());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpSessionBindingEvent(javax.servlet.http.HttpSessionBindingEvent) ServletsMockitoUtil.createHttpSessionBindingEvent(jodd.petite.ServletsMockitoUtil.createHttpSessionBindingEvent) Ses(jodd.petite.tst.Ses) RequestContextListener(jodd.servlet.RequestContextListener) HttpSession(javax.servlet.http.HttpSession) ServletsMockitoUtil.createServletRequestEvent(jodd.petite.ServletsMockitoUtil.createServletRequestEvent) ServletRequestEvent(javax.servlet.ServletRequestEvent) Test(org.junit.Test)

Example 12 with ServletRequestEvent

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());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpSessionBindingEvent(javax.servlet.http.HttpSessionBindingEvent) ServletsMockitoUtil.createHttpSessionBindingEvent(jodd.petite.ServletsMockitoUtil.createHttpSessionBindingEvent) Ses(jodd.petite.tst.Ses) RequestContextListener(jodd.servlet.RequestContextListener) HttpSession(javax.servlet.http.HttpSession) ServletsMockitoUtil.createServletRequestEvent(jodd.petite.ServletsMockitoUtil.createServletRequestEvent) ServletRequestEvent(javax.servlet.ServletRequestEvent) SessionScope(jodd.petite.scope.SessionScope) Test(org.junit.Test)

Example 13 with ServletRequestEvent

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;
}
Also used : ServletRequestEvent(javax.servlet.ServletRequestEvent)

Example 14 with ServletRequestEvent

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

Example 15 with ServletRequestEvent

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

Aggregations

ServletRequestEvent (javax.servlet.ServletRequestEvent)16 Test (org.junit.Test)9 ServletRequestListener (javax.servlet.ServletRequestListener)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 MockRunnable (org.springframework.core.task.MockRunnable)3 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)3 MockServletContext (org.springframework.mock.web.test.MockServletContext)3 File (java.io.File)2 HttpSession (javax.servlet.http.HttpSession)2 HttpSessionBindingEvent (javax.servlet.http.HttpSessionBindingEvent)2 ServletsMockitoUtil.createHttpSessionBindingEvent (jodd.petite.ServletsMockitoUtil.createHttpSessionBindingEvent)2 ServletsMockitoUtil.createServletRequestEvent (jodd.petite.ServletsMockitoUtil.createServletRequestEvent)2 Ses (jodd.petite.tst.Ses)2 RequestContextListener (jodd.servlet.RequestContextListener)2 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)2 MultiPartInputStreamParser (org.eclipse.jetty.util.MultiPartInputStreamParser)2 ApplicationListeners (io.undertow.servlet.core.ApplicationListeners)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 ServletContext (javax.servlet.ServletContext)1