From 1d332fc5798fc60ecf2572689a4298abc244a818 Mon Sep 17 00:00:00 2001 From: anhefti Date: Wed, 29 May 2024 12:05:20 +0200 Subject: [PATCH] SEBSP-129 and SEBSERV-418 --- .../impl/FullLmsIntegrationServiceImpl.java | 63 +++++++++++++------ .../api/LmsIntegrationController.java | 2 - .../api/SEBClientConfigController.java | 4 +- 3 files changed, 45 insertions(+), 24 deletions(-) diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/FullLmsIntegrationServiceImpl.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/FullLmsIntegrationServiceImpl.java index 4f4b1b18..eec10228 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/FullLmsIntegrationServiceImpl.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/FullLmsIntegrationServiceImpl.java @@ -10,6 +10,8 @@ package ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl; import java.io.ByteArrayOutputStream; import java.io.OutputStream; +import java.io.PipedInputStream; +import java.io.PipedOutputStream; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Collection; @@ -49,6 +51,7 @@ import ch.ethz.seb.sebserver.webservice.servicelayer.sebconfig.ConnectionConfigu import ch.ethz.seb.sebserver.webservice.servicelayer.sebconfig.ConnectionConfigurationService; import ch.ethz.seb.sebserver.webservice.servicelayer.session.ExamSessionService; import ch.ethz.seb.sebserver.webservice.servicelayer.session.ScreenProctoringService; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.joda.time.DateTime; import org.slf4j.Logger; @@ -326,24 +329,28 @@ public class FullLmsIntegrationServiceImpl implements FullLmsIntegrationService try { - final Result examResult = lmsSetupDAO - .getLmsSetupIdByConnectionId(lmsUUID) - .flatMap(lmsAPIService::getLmsAPITemplate) - .map(findQuizData(courseId, quizId)) - .flatMap(this::findExam); + // TODO this is hardcoded for Testing, below out-commented code is real business - if (examResult.hasError()) { - throw new APIMessage.APIMessageException(APIMessage.ErrorMessage.ILLEGAL_API_ARGUMENT.of("Exam not found")); - } + this.connectionConfigurationService.exportSEBClientConfiguration(out, "1", null); - final Exam exam = examResult.get(); - - final String connectionConfigId = getConnectionConfigurationId(exam); - if (StringUtils.isBlank(connectionConfigId)) { - throw new APIMessage.APIMessageException(APIMessage.ErrorMessage.ILLEGAL_API_ARGUMENT.of("No active Connection Configuration found")); - } - - this.connectionConfigurationService.exportSEBClientConfiguration(out, connectionConfigId, exam.id); +// final Result examResult = lmsSetupDAO +// .getLmsSetupIdByConnectionId(lmsUUID) +// .flatMap(lmsAPIService::getLmsAPITemplate) +// .map(findQuizData(courseId, quizId)) +// .flatMap(this::findExam); +// +// if (examResult.hasError()) { +// throw new APIMessage.APIMessageException(APIMessage.ErrorMessage.ILLEGAL_API_ARGUMENT.of("Exam not found")); +// } +// +// final Exam exam = examResult.get(); +// +// final String connectionConfigId = getConnectionConfigurationId(exam); +// if (StringUtils.isBlank(connectionConfigId)) { +// throw new APIMessage.APIMessageException(APIMessage.ErrorMessage.ILLEGAL_API_ARGUMENT.of("No active Connection Configuration found")); +// } +// +// this.connectionConfigurationService.exportSEBClientConfiguration(out, connectionConfigId, exam.id); return Result.EMPTY; } catch (final Exception e) { @@ -523,11 +530,27 @@ public class FullLmsIntegrationServiceImpl implements FullLmsIntegrationService final String connectionConfigId = getConnectionConfigurationId(exam); final ByteArrayOutputStream out = new ByteArrayOutputStream(); - this.connectionConfigurationService - .exportSEBClientConfiguration(out, connectionConfigId, exam.id); + final PipedOutputStream pout; + final PipedInputStream pin; + try { + pout = new PipedOutputStream(); + pin = new PipedInputStream(pout); - // TODO check if this works as expected - return template.applyConnectionConfiguration(exam, out.toByteArray()); + this.connectionConfigurationService + .exportSEBClientConfiguration(pout, connectionConfigId, exam.id); + + out.flush(); + + IOUtils.copyLarge(pin, out); + + // TODO check if this works as expected + return template.applyConnectionConfiguration(exam, out.toByteArray()); + + } catch (final Exception e) { + throw new RuntimeException("Failed to stream output", e); + } finally { + IOUtils.closeQuietly(out); + } }) .onError(error -> log.error("Failed to apply ConnectionConfiguration for exam: {} error: {}", exam, error.getMessage())) .getOr(exam); diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/LmsIntegrationController.java b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/LmsIntegrationController.java index d37ce358..e32fc78b 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/LmsIntegrationController.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/LmsIntegrationController.java @@ -108,8 +108,6 @@ public class LmsIntegrationController { @RequestParam(name = API.LMS_FULL_INTEGRATION_QUIZ_ID) final String quizId, final HttpServletResponse response) throws IOException { - // TODO change this according to the outcome of discussion about Moodle Connection Configuration handling - final ServletOutputStream outputStream = response.getOutputStream(); final PipedOutputStream pout; final PipedInputStream pin; diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/SEBClientConfigController.java b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/SEBClientConfigController.java index 16966ee4..d4566347 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/SEBClientConfigController.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/SEBClientConfigController.java @@ -121,8 +121,8 @@ public class SEBClientConfigController extends ActivatableEntityController