refactor: centralise session key format in SessionKeys utility

This commit is contained in:
2026-05-21 20:57:32 +02:00
parent 1a86c23565
commit 5776676eeb
3 changed files with 20 additions and 8 deletions
@@ -23,8 +23,6 @@ import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public class ConnectionController {
private static final String SUFFIX = "_CONNECTION_ID";
private final ConnectionService connectionService;
@PostMapping("/login")
@@ -34,7 +32,7 @@ public class ConnectionController {
LoginResult result = connectionService.login(request);
HttpSession session = httpReq.getSession(true);
session.setAttribute(request.serviceType() + SUFFIX, result.connectionId());
session.setAttribute(SessionKeys.connectionId(request.serviceType()), result.connectionId());
if (result.expiresAt() != null) {
long secs = Instant.now().until(result.expiresAt(), ChronoUnit.SECONDS);
@@ -52,9 +50,10 @@ public class ConnectionController {
}
List<ConnectionStatusResponse> statuses = new ArrayList<>();
Collections.list(session.getAttributeNames()).stream().filter(k -> k.endsWith(SUFFIX))
Collections.list(session.getAttributeNames()).stream()
.filter(k -> k.endsWith(SessionKeys.CONNECTION_ID_SUFFIX))
.forEach(k -> {
String serviceType = k.replace(SUFFIX, "");
String serviceType = k.replace(SessionKeys.CONNECTION_ID_SUFFIX, "");
Long id = (Long) session.getAttribute(k);
ConnectionStatusResponse status =
connectionService.getConnectionStatus(serviceType, id);
@@ -71,9 +70,9 @@ public class ConnectionController {
HttpSession session = httpReq.getSession(false);
if (session != null) {
session.removeAttribute(serviceType + SUFFIX);
session.removeAttribute(SessionKeys.connectionId(serviceType));
boolean hasMore = Collections.list(session.getAttributeNames()).stream()
.anyMatch(k -> k.endsWith(SUFFIX));
.anyMatch(k -> k.endsWith(SessionKeys.CONNECTION_ID_SUFFIX));
if (!hasMore) {
session.invalidate();
}
@@ -0,0 +1,12 @@
package com.vaessl.app.connection;
public final class SessionKeys {
static final String CONNECTION_ID_SUFFIX = "_CONNECTION_ID";
private SessionKeys() {}
public static String connectionId(String serviceType) {
return serviceType + CONNECTION_ID_SUFFIX;
}
}