From 4cd498d29e76201d7937eced774074a1ea0d28f6 Mon Sep 17 00:00:00 2001 From: anhefti Date: Tue, 16 Jul 2024 16:25:02 +0200 Subject: [PATCH] SEBSERV-566 fixed, update SPS session --- .../ScreenProctoringAPIBinding.java | 48 +++++++++++++++++++ .../ScreenProctoringServiceImpl.java | 33 +++++++++---- 2 files changed, 71 insertions(+), 10 deletions(-) diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/proctoring/ScreenProctoringAPIBinding.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/proctoring/ScreenProctoringAPIBinding.java index 0219a18f..f943c910 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/proctoring/ScreenProctoringAPIBinding.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/proctoring/ScreenProctoringAPIBinding.java @@ -727,6 +727,45 @@ class ScreenProctoringAPIBinding { return token; } + String updateSEBSession( + final Long groupId, + final ClientConnectionRecord clientConnection) { + + final String token = clientConnection.getConnectionToken(); + final ScreenProctoringServiceOAuthTemplate apiTemplate = this.getAPITemplate(clientConnection.getExamId()); + + final String uri = UriComponentsBuilder + .fromUriString(apiTemplate.spsAPIAccessData.getSpsServiceURL()) + .path(SPS_API.SESSION_ENDPOINT) + .pathSegment(token) + .build() + .toUriString(); + + final Map params = new HashMap<>(); + params.put(SPS_API.SESSION.ATTR_UUID, token); + params.put(SPS_API.SESSION.ATTR_GROUP_ID, String.valueOf(groupId)); + params.put(SPS_API.SESSION.ATTR_CLIENT_IP, clientConnection.getClientAddress()); + params.put(SPS_API.SESSION.ATTR_CLIENT_NAME, clientConnection.getExamUserSessionId()); + params.put(SPS_API.SESSION.ATTR_CLIENT_MACHINE_NAME, clientConnection.getClientMachineName()); + params.put(SPS_API.SESSION.ATTR_CLIENT_OS_NAME, clientConnection.getClientOsName()); + params.put(SPS_API.SESSION.ATTR_CLIENT_VERSION, clientConnection.getClientVersion()); + + ResponseEntity exchange = null; + try { + final String jsonSession = jsonMapper.writeValueAsString(params); + exchange = apiTemplate.exchangePUT(uri, jsonSession); + } catch (final JsonProcessingException e) { + throw new RuntimeException(e); + } + + if (exchange.getStatusCode() != HttpStatus.OK) { + throw new RuntimeException( + "Failed to update SPS SEB session for SEB connection: " + token); + } + + return token; + } + void deleteExamOnScreenProctoring(final Exam exam) { try { @@ -1369,6 +1408,15 @@ class ScreenProctoringAPIBinding { return exchange(url, HttpMethod.POST, body, getHeaders()); } + ResponseEntity exchangePUT( + final String url, + final String body) { + + final HttpHeaders httpHeaders = getHeaders(); + httpHeaders.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); + return exchange(url, HttpMethod.PUT, body, httpHeaders); + } + HttpHeaders getHeadersJSONRequest() { final HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/proctoring/ScreenProctoringServiceImpl.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/proctoring/ScreenProctoringServiceImpl.java index 33a73d5b..59df8743 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/proctoring/ScreenProctoringServiceImpl.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/proctoring/ScreenProctoringServiceImpl.java @@ -387,19 +387,32 @@ public class ScreenProctoringServiceImpl implements ScreenProctoringService { try { final Long examId = ccRecord.getExamId(); final Exam runningExam = this.examSessionCacheService.getRunningExam(examId); + final Long existingGroupId = ccRecord.getScreenProctoringGroupId(); - // apply SEB connection to screen proctoring group - final ScreenProctoringGroup group = applySEBConnectionToGroup( - ccRecord, - runningExam); - placeReservedInGroup = group.id; + if (existingGroupId == null) { - // create screen proctoring session for SEB connection on SPS service - final String spsSessionToken = this.screenProctoringAPIBinding - .createSEBSession(examId, group, ccRecord); + // apply SEB connection to screen proctoring group + final ScreenProctoringGroup group = applySEBConnectionToGroup( + ccRecord, + runningExam); + placeReservedInGroup = group.id; - // create instruction for SEB and add it to instruction queue for SEB connection - registerJoinInstruction(ccRecord, spsSessionToken, group, runningExam); + // create screen proctoring session for SEB connection on SPS service + final String spsSessionToken = this.screenProctoringAPIBinding + .createSEBSession(examId, group, ccRecord); + + // create instruction for SEB and add it to instruction queue for SEB connection + registerJoinInstruction(ccRecord, spsSessionToken, group, runningExam); + } else { + // just update session on SPS site + this.screenProctoringGroupDAO + .getScreenProctoringGroup(existingGroupId) + .map(group -> this.screenProctoringAPIBinding.updateSEBSession( + group.id, + ccRecord)) + .onError(error -> log.error("Failed to update SEB Session on SPS: {}", ccRecord, error)); + + } this.clientConnectionDAO .markScreenProctoringApplied(ccRecord.getId(), ccRecord.getConnectionToken())