Skip to content

Commit

Permalink
backport: Set clusterKey cookie at first request (#143) (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
tamasmak authored Oct 30, 2024
1 parent 4805978 commit 58e743d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,17 @@ private SessionTrackerCookie() {
public static void setIfNeeded(HttpSession session,
HttpServletRequest request, HttpServletResponse response) {
Optional<Cookie> 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);
}

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 58e743d

Please sign in to comment.