diff --git a/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/sessiontracker/SessionTrackerCookie.java b/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/sessiontracker/SessionTrackerCookie.java index 23f1dfb..baf4576 100644 --- a/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/sessiontracker/SessionTrackerCookie.java +++ b/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/sessiontracker/SessionTrackerCookie.java @@ -13,6 +13,7 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpSession; + import java.util.Optional; import java.util.UUID; import java.util.function.Consumer; @@ -45,11 +46,14 @@ public static void setIfNeeded(HttpSession session, Optional clusterKeyCookie = getCookie(request); if (clusterKeyCookie.isEmpty()) { String clusterKey = UUID.randomUUID().toString(); - session.setAttribute(CurrentKey.COOKIE_NAME, clusterKey); + if (session != null) { + session.setAttribute(CurrentKey.COOKIE_NAME, clusterKey); + } Cookie cookie = new Cookie(CurrentKey.COOKIE_NAME, clusterKey); cookieConsumer.accept(cookie); response.addCookie(cookie); - } else if (session.getAttribute(CurrentKey.COOKIE_NAME) == null) { + } else if (session != null + && session.getAttribute(CurrentKey.COOKIE_NAME) == null) { String clusterKey = clusterKeyCookie.get().getValue(); session.setAttribute(CurrentKey.COOKIE_NAME, clusterKey); } diff --git a/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/sessiontracker/SessionTrackerFilter.java b/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/sessiontracker/SessionTrackerFilter.java index f921e6e..193adcd 100644 --- a/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/sessiontracker/SessionTrackerFilter.java +++ b/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/sessiontracker/SessionTrackerFilter.java @@ -16,6 +16,7 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpSession; + import java.io.IOException; import java.util.function.Consumer; @@ -72,10 +73,8 @@ protected void doFilter(HttpServletRequest request, try { HttpSession session = request.getSession(false); - if (session != null) { - SessionTrackerCookie.setIfNeeded(session, request, response, - cookieConsumer(request)); - } + SessionTrackerCookie.setIfNeeded(session, request, response, + cookieConsumer(request)); super.doFilter(request, response, chain); if (session != null && request.isRequestedSessionIdValid() diff --git a/kubernetes-kit-starter/src/test/java/com/vaadin/kubernetes/starter/sessiontracker/SessionTrackerCookieTest.java b/kubernetes-kit-starter/src/test/java/com/vaadin/kubernetes/starter/sessiontracker/SessionTrackerCookieTest.java index d5752b1..d595c3e 100644 --- a/kubernetes-kit-starter/src/test/java/com/vaadin/kubernetes/starter/sessiontracker/SessionTrackerCookieTest.java +++ b/kubernetes-kit-starter/src/test/java/com/vaadin/kubernetes/starter/sessiontracker/SessionTrackerCookieTest.java @@ -144,4 +144,23 @@ void getValue_valueIsReturned() { assertTrue(value.isPresent()); assertEquals(clusterKey, value.get()); } + + + @Test + void setIfNeeded_nullCookiesAndSession_cookieIsConfigured() { + HttpServletRequest request = mock(HttpServletRequest.class); + when(request.getCookies()).thenReturn(null); + HttpServletResponse response = mock(HttpServletResponse.class); + @SuppressWarnings("unchecked") + Consumer cookieConsumer = (Consumer) mock( + Consumer.class); + + SessionTrackerCookie.setIfNeeded(null, request, response, + cookieConsumer); + + verify(cookieConsumer).accept(any()); + verify(response).addCookie(any()); + } + + }