refactor: centralise session key format in SessionKeys utility
This commit is contained in:
@@ -23,8 +23,6 @@ import lombok.RequiredArgsConstructor;
|
|||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class ConnectionController {
|
public class ConnectionController {
|
||||||
|
|
||||||
private static final String SUFFIX = "_CONNECTION_ID";
|
|
||||||
|
|
||||||
private final ConnectionService connectionService;
|
private final ConnectionService connectionService;
|
||||||
|
|
||||||
@PostMapping("/login")
|
@PostMapping("/login")
|
||||||
@@ -34,7 +32,7 @@ public class ConnectionController {
|
|||||||
LoginResult result = connectionService.login(request);
|
LoginResult result = connectionService.login(request);
|
||||||
|
|
||||||
HttpSession session = httpReq.getSession(true);
|
HttpSession session = httpReq.getSession(true);
|
||||||
session.setAttribute(request.serviceType() + SUFFIX, result.connectionId());
|
session.setAttribute(SessionKeys.connectionId(request.serviceType()), result.connectionId());
|
||||||
|
|
||||||
if (result.expiresAt() != null) {
|
if (result.expiresAt() != null) {
|
||||||
long secs = Instant.now().until(result.expiresAt(), ChronoUnit.SECONDS);
|
long secs = Instant.now().until(result.expiresAt(), ChronoUnit.SECONDS);
|
||||||
@@ -52,9 +50,10 @@ public class ConnectionController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
List<ConnectionStatusResponse> statuses = new ArrayList<>();
|
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 -> {
|
.forEach(k -> {
|
||||||
String serviceType = k.replace(SUFFIX, "");
|
String serviceType = k.replace(SessionKeys.CONNECTION_ID_SUFFIX, "");
|
||||||
Long id = (Long) session.getAttribute(k);
|
Long id = (Long) session.getAttribute(k);
|
||||||
ConnectionStatusResponse status =
|
ConnectionStatusResponse status =
|
||||||
connectionService.getConnectionStatus(serviceType, id);
|
connectionService.getConnectionStatus(serviceType, id);
|
||||||
@@ -71,9 +70,9 @@ public class ConnectionController {
|
|||||||
|
|
||||||
HttpSession session = httpReq.getSession(false);
|
HttpSession session = httpReq.getSession(false);
|
||||||
if (session != null) {
|
if (session != null) {
|
||||||
session.removeAttribute(serviceType + SUFFIX);
|
session.removeAttribute(SessionKeys.connectionId(serviceType));
|
||||||
boolean hasMore = Collections.list(session.getAttributeNames()).stream()
|
boolean hasMore = Collections.list(session.getAttributeNames()).stream()
|
||||||
.anyMatch(k -> k.endsWith(SUFFIX));
|
.anyMatch(k -> k.endsWith(SessionKeys.CONNECTION_ID_SUFFIX));
|
||||||
if (!hasMore) {
|
if (!hasMore) {
|
||||||
session.invalidate();
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.PostMapping;
|
|||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import com.vaessl.app.connection.SessionKeys;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.servlet.http.HttpSession;
|
import jakarta.servlet.http.HttpSession;
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
@@ -30,7 +31,7 @@ public class SearchController {
|
|||||||
@PageableDefault(size = 20) Pageable pageable, HttpServletRequest httpReq) {
|
@PageableDefault(size = 20) Pageable pageable, HttpServletRequest httpReq) {
|
||||||
HttpSession session = httpReq.getSession(false);
|
HttpSession session = httpReq.getSession(false);
|
||||||
if (session == null
|
if (session == null
|
||||||
|| session.getAttribute(request.serviceType() + "_CONNECTION_ID") == null) {
|
|| session.getAttribute(SessionKeys.connectionId(request.serviceType())) == null) {
|
||||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
|
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user