From 5551c7d7c2608b5fb3af30996439e380afad8b2d Mon Sep 17 00:00:00 2001 From: anhefti Date: Thu, 4 Jul 2019 15:33:09 +0200 Subject: [PATCH] active client connection handling and find-bugs --- findbugs-excludes.xml | 6 ++-- .../session/ExamSessionService.java | 11 ++++-- .../session/SebClientConnectionService.java | 7 ---- .../session/impl/ExamSessionServiceImpl.java | 36 ++++++++++--------- .../impl/SebClientConnectionServiceImpl.java | 14 -------- .../weblayer/api/ExamAPI_V1_Controller.java | 4 +-- 6 files changed, 35 insertions(+), 43 deletions(-) diff --git a/findbugs-excludes.xml b/findbugs-excludes.xml index 5170e613..95da4dcd 100644 --- a/findbugs-excludes.xml +++ b/findbugs-excludes.xml @@ -12,9 +12,11 @@ - - + + + + \ No newline at end of file diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/ExamSessionService.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/ExamSessionService.java index 61103db9..8c59b82f 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/ExamSessionService.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/ExamSessionService.java @@ -47,14 +47,21 @@ public interface ExamSessionService { void streamDefaultExamConfig(String connectionToken, OutputStream out); /** Get current ClientConnectionData for a specified active SEB client connection. - * + * + * active SEB client connections are connections that were initialized by a SEB client + * on the particular server instance. + * * @param connectionToken the connection token of the active SEB client connection * @return */ Result getConnectionData(String connectionToken); /** Get the collection of ClientConnectionData of all active SEB client connections * of a running exam. - * + * + * active SEB client connections are connections that were initialized by a SEB client + * on the particular server instance. This may not be the all connections of an exam but + * a subset of them. + * * @param examId The exam identifier * @return collection of ClientConnectionData of all active SEB client connections * of a running exam */ diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/SebClientConnectionService.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/SebClientConnectionService.java index 24b96588..cf3d346c 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/SebClientConnectionService.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/SebClientConnectionService.java @@ -9,7 +9,6 @@ package ch.ethz.seb.sebserver.webservice.servicelayer.session; import ch.ethz.seb.sebserver.gbl.model.session.ClientConnection; -import ch.ethz.seb.sebserver.gbl.model.session.ClientConnectionData; import ch.ethz.seb.sebserver.gbl.model.session.ClientEvent; import ch.ethz.seb.sebserver.gbl.util.Result; @@ -100,12 +99,6 @@ public interface SebClientConnectionService { Long institutionId, String clientAddress); - /** Get ClientConnectionData for an active connection (connection on running exam) - * - * @param connectionToken The connection token of the connection to get the ClientConnectionData from - * @return ClientConnectionData for an active connection (connection on running exam) */ - Result getActiveConnectionData(String connectionToken); - /** Notify a ping for a certain client connection. * * @param connectionToken the connection token diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/ExamSessionServiceImpl.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/ExamSessionServiceImpl.java index f28e55ff..4bae21ae 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/ExamSessionServiceImpl.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/ExamSessionServiceImpl.java @@ -17,6 +17,8 @@ import java.util.stream.Collectors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.cache.Cache; +import org.springframework.cache.CacheManager; import org.springframework.context.annotation.Lazy; import org.springframework.security.access.AccessDeniedException; import org.springframework.stereotype.Service; @@ -40,15 +42,18 @@ public class ExamSessionServiceImpl implements ExamSessionService { private final ClientConnectionDAO clientConnectionDAO; private final ExamSessionCacheService examSessionCacheService; private final ExamDAO examDAO; + private final CacheManager cacheManager; protected ExamSessionServiceImpl( final ExamSessionCacheService examSessionCacheService, final ExamDAO examDAO, - final ClientConnectionDAO clientConnectionDAO) { + final ClientConnectionDAO clientConnectionDAO, + final CacheManager cacheManager) { this.examSessionCacheService = examSessionCacheService; this.examDAO = examDAO; this.clientConnectionDAO = clientConnectionDAO; + this.cacheManager = cacheManager; } @Override @@ -147,25 +152,24 @@ public class ExamSessionServiceImpl implements ExamSessionService { @Override public Result getConnectionData(final String connectionToken) { - final ClientConnectionDataInternal activeClientConnection = this.examSessionCacheService - .getActiveClientConnection(connectionToken); - - if (activeClientConnection == null) { - log.error("No active ClientConnection found for token: {}", connectionToken); - return Result.ofError(new IllegalArgumentException("No active ClientConnection found for token")); - } else { - return Result.of(activeClientConnection); - } + return Result.tryCatch(() -> { + final Cache cache = this.cacheManager.getCache(ExamSessionCacheService.CACHE_NAME_ACTIVE_CLIENT_CONNECTION); + return cache.get(connectionToken, ClientConnectionData.class); + }); } @Override public Result> getConnectionData(final Long examId) { - return this.clientConnectionDAO - .getConnectionTokens(examId) - .map(all -> all - .stream() - .map(this.examSessionCacheService::getActiveClientConnection) - .collect(Collectors.toList())); + return Result.tryCatch(() -> { + final Cache cache = this.cacheManager.getCache(ExamSessionCacheService.CACHE_NAME_ACTIVE_CLIENT_CONNECTION); + return this.clientConnectionDAO + .getConnectionTokens(examId) + .getOrThrow() + .stream() + .map(token -> cache.get(token, ClientConnectionData.class)) + .filter(data -> data != null) + .collect(Collectors.toList()); + }); } private void flushCache(final Exam exam) { diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/SebClientConnectionServiceImpl.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/SebClientConnectionServiceImpl.java index 41b2c53e..28ff2bbf 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/SebClientConnectionServiceImpl.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/SebClientConnectionServiceImpl.java @@ -20,7 +20,6 @@ import org.springframework.stereotype.Service; import ch.ethz.seb.sebserver.gbl.model.exam.Exam.ExamType; import ch.ethz.seb.sebserver.gbl.model.session.ClientConnection; import ch.ethz.seb.sebserver.gbl.model.session.ClientConnection.ConnectionStatus; -import ch.ethz.seb.sebserver.gbl.model.session.ClientConnectionData; import ch.ethz.seb.sebserver.gbl.model.session.ClientEvent; import ch.ethz.seb.sebserver.gbl.profile.WebServiceProfile; import ch.ethz.seb.sebserver.gbl.util.Result; @@ -339,19 +338,6 @@ public class SebClientConnectionServiceImpl implements SebClientConnectionServic }); } - @Override - public Result getActiveConnectionData(final String connectionToken) { - final ClientConnectionDataInternal activeClientConnection = this.examSessionCacheService - .getActiveClientConnection(connectionToken); - - if (activeClientConnection == null) { - return Result - .ofError(new IllegalArgumentException("No active client connection found for connectionToken")); - } else { - return Result.of(activeClientConnection); - } - } - @Override public void notifyPing( final String connectionToken, diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ExamAPI_V1_Controller.java b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ExamAPI_V1_Controller.java index 0ee88e01..b30f2bf4 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ExamAPI_V1_Controller.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ExamAPI_V1_Controller.java @@ -259,8 +259,8 @@ public class ExamAPI_V1_Controller { handshakeUpdate(connectionToken, Long.valueOf(examId), null, principal, request); } - final ClientConnectionData connection = this.sebClientConnectionService - .getActiveConnectionData(connectionToken) + final ClientConnectionData connection = this.examSessionService + .getConnectionData(connectionToken) .getOrThrow(); // exam integrity check