Skip to content

Commit

Permalink
Merge pull request #34525 from manovotn/issue34332
Browse files Browse the repository at this point in the history
Undertow - fire context events for session context
  • Loading branch information
mkouba authored Jul 10, 2023
2 parents f21a955 + ed08d10 commit 7c34500
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.quarkus.undertow.test.sessioncontext;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.context.BeforeDestroyed;
import jakarta.enterprise.context.Destroyed;
import jakarta.enterprise.context.Initialized;
import jakarta.enterprise.context.SessionScoped;
import jakarta.enterprise.event.Observes;

@ApplicationScoped
public class ObservingBean {

int timesInitObserved = 0;
int timesBeforeDestroyedObserved = 0;
int timesDestroyedObserved = 0;

public int getTimesInitObserved() {
return timesInitObserved;
}

public int getTimesBeforeDestroyedObserved() {
return timesBeforeDestroyedObserved;
}

public int getTimesDestroyedObserved() {
return timesDestroyedObserved;
}

public void observeInit(@Observes @Initialized(SessionScoped.class) Object event) {
timesInitObserved++;
}

public void observeBeforeDestroyed(@Observes @BeforeDestroyed(SessionScoped.class) Object event) {
timesBeforeDestroyedObserved++;
}

public void observeDestroyed(@Observes @Destroyed(SessionScoped.class) Object event) {
timesDestroyedObserved++;
}

public void resetState() {
this.timesInitObserved = 0;
this.timesBeforeDestroyedObserved = 0;
this.timesDestroyedObserved = 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.arc.Arc;
import io.quarkus.test.QuarkusUnitTest;
import io.restassured.response.Response;

public class SessionContextTestCase {

@RegisterExtension
static QuarkusUnitTest runner = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar.addClasses(TestServlet.class, Foo.class));
.withApplicationRoot((jar) -> jar.addClasses(TestServlet.class, Foo.class, ObservingBean.class));

@Test
public void testServlet() {
Expand All @@ -30,4 +32,18 @@ public void testServlet() {
response.then().statusCode(200).body(is("count=1"));
}

@Test
public void testContextEvents() {
ObservingBean observingBean = Arc.container().select(ObservingBean.class).get();

// make sure we start with zero events to keep this test method independent
observingBean.resetState();

// following request creates a session and also destroys it by enforcing invalidation
when().get("/foo?destroy=true").then().statusCode(200);
Assertions.assertEquals(1, observingBean.getTimesInitObserved());
Assertions.assertEquals(1, observingBean.getTimesBeforeDestroyedObserved());
Assertions.assertEquals(1, observingBean.getTimesDestroyedObserved());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
import java.util.Objects;
import java.util.stream.Collectors;

import jakarta.enterprise.context.BeforeDestroyed;
import jakarta.enterprise.context.ContextNotActiveException;
import jakarta.enterprise.context.Destroyed;
import jakarta.enterprise.context.Initialized;
import jakarta.enterprise.context.SessionScoped;
import jakarta.enterprise.context.spi.Contextual;
import jakarta.enterprise.context.spi.CreationalContext;
import jakarta.enterprise.event.Event;
import jakarta.servlet.annotation.WebListener;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
Expand Down Expand Up @@ -205,12 +209,20 @@ public boolean equals(Object obj) {
@Override
public void sessionDestroyed(HttpSessionEvent se) {
HttpSession session = se.getSession();
Event<Object> event = Arc.container().beanManager().getEvent();
event.select(HttpSession.class, BeforeDestroyed.Literal.SESSION).fire(session);
try {
DESTRUCT_SESSION.set(session);
destroy(session);
event.select(HttpSession.class, Destroyed.Literal.SESSION).fire(session);
} finally {
DESTRUCT_SESSION.remove();
}
}

@Override
public void sessionCreated(HttpSessionEvent se) {
Arc.container().beanManager().getEvent().select(HttpSession.class, Initialized.Literal.SESSION).fire(se.getSession());
}

}

0 comments on commit 7c34500

Please sign in to comment.