From 58e743de72b203831db790f4175cfd1c0fdb3252 Mon Sep 17 00:00:00 2001 From: Tamas Mak Date: Wed, 30 Oct 2024 10:15:33 +0100 Subject: [PATCH] backport: Set clusterKey cookie at first request (#143) (#150) --- .../starter/sessiontracker/SessionTrackerCookie.java | 10 ++++++---- .../starter/sessiontracker/SessionTrackerFilter.java | 4 +--- .../sessiontracker/SessionTrackerCookieTest.java | 11 +++++++++++ 3 files changed, 18 insertions(+), 7 deletions(-) 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 b6f20d0..8c3db32 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 @@ -41,15 +41,17 @@ private SessionTrackerCookie() { public static void setIfNeeded(HttpSession session, HttpServletRequest request, HttpServletResponse response) { Optional clusterKeyCookie = getCookie(request); - if (!clusterKeyCookie.isPresent()) { + if (clusterKeyCookie.isEmpty()) { String clusterKey = UUID.randomUUID().toString(); - session.setAttribute(CurrentKey.COOKIE_NAME, clusterKey); + if (session != null) { + session.setAttribute(CurrentKey.COOKIE_NAME, clusterKey); + } response.addCookie(new Cookie(CurrentKey.COOKIE_NAME, clusterKey)); - } 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 ebc3e53..138b8cb 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 @@ -66,9 +66,7 @@ protected void doFilter(HttpServletRequest request, try { HttpSession session = request.getSession(false); - if (session != null) { - SessionTrackerCookie.setIfNeeded(session, request, response); - } + SessionTrackerCookie.setIfNeeded(session, request, response); 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 4c92bb4..365f58b 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 @@ -126,4 +126,15 @@ 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); + + SessionTrackerCookie.setIfNeeded(null, request, response); + + verify(response).addCookie(any()); + } }