added external address alias support for LMSMockup

This commit is contained in:
anhefti 2019-09-09 09:47:08 +02:00
parent 9b105ab476
commit 83b552a36c
2 changed files with 44 additions and 4 deletions

View file

@ -199,7 +199,8 @@ public class LmsAPIServiceImpl implements LmsAPIService {
case MOCKUP: case MOCKUP:
return new MockupLmsAPITemplate( return new MockupLmsAPITemplate(
lmsSetup, lmsSetup,
credentials); credentials,
this.webserviceInfo);
case OPEN_EDX: case OPEN_EDX:
return new OpenEdxLmsAPITemplate( return new OpenEdxLmsAPITemplate(
this.asyncService, this.asyncService,

View file

@ -8,12 +8,14 @@
package ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl; package ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl;
import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -23,6 +25,7 @@ import ch.ethz.seb.sebserver.gbl.model.institution.LmsSetup;
import ch.ethz.seb.sebserver.gbl.model.institution.LmsSetup.LmsType; import ch.ethz.seb.sebserver.gbl.model.institution.LmsSetup.LmsType;
import ch.ethz.seb.sebserver.gbl.model.institution.LmsSetupTestResult; import ch.ethz.seb.sebserver.gbl.model.institution.LmsSetupTestResult;
import ch.ethz.seb.sebserver.gbl.util.Result; import ch.ethz.seb.sebserver.gbl.util.Result;
import ch.ethz.seb.sebserver.webservice.WebserviceInfo;
import ch.ethz.seb.sebserver.webservice.servicelayer.client.ClientCredentials; import ch.ethz.seb.sebserver.webservice.servicelayer.client.ClientCredentials;
import ch.ethz.seb.sebserver.webservice.servicelayer.dao.FilterMap; import ch.ethz.seb.sebserver.webservice.servicelayer.dao.FilterMap;
import ch.ethz.seb.sebserver.webservice.servicelayer.lms.LmsAPIService; import ch.ethz.seb.sebserver.webservice.servicelayer.lms.LmsAPIService;
@ -35,13 +38,16 @@ final class MockupLmsAPITemplate implements LmsAPITemplate {
private final LmsSetup lmsSetup; private final LmsSetup lmsSetup;
private final ClientCredentials credentials; private final ClientCredentials credentials;
private final Collection<QuizData> mockups; private final Collection<QuizData> mockups;
private final WebserviceInfo webserviceInfo;
MockupLmsAPITemplate( MockupLmsAPITemplate(
final LmsSetup lmsSetup, final LmsSetup lmsSetup,
final ClientCredentials credentials) { final ClientCredentials credentials,
final WebserviceInfo webserviceInfo) {
this.lmsSetup = lmsSetup; this.lmsSetup = lmsSetup;
this.credentials = credentials; this.credentials = credentials;
this.webserviceInfo = webserviceInfo;
final Long lmsSetupId = lmsSetup.id; final Long lmsSetupId = lmsSetup.id;
final Long institutionId = lmsSetup.getInstitutionId(); final Long institutionId = lmsSetup.getInstitutionId();
@ -100,7 +106,9 @@ final class MockupLmsAPITemplate implements LmsAPITemplate {
throw new IllegalArgumentException("Wrong clientId or secret"); throw new IllegalArgumentException("Wrong clientId or secret");
} }
final List<QuizData> quizzes = this.mockups.stream() final List<QuizData> quizzes = this.mockups
.stream()
.map(this::getExternalAddressAlias)
.filter(LmsAPIService.quizFilterFunction(filterMap)) .filter(LmsAPIService.quizFilterFunction(filterMap))
.collect(Collectors.toList()); .collect(Collectors.toList());
@ -114,12 +122,43 @@ final class MockupLmsAPITemplate implements LmsAPITemplate {
throw new IllegalArgumentException("Wrong clientId or secret"); throw new IllegalArgumentException("Wrong clientId or secret");
} }
return this.mockups.stream() return this.mockups
.stream()
.map(this::getExternalAddressAlias)
.filter(mockup -> ids.contains(mockup.id)) .filter(mockup -> ids.contains(mockup.id))
.map(mockup -> Result.of(mockup)) .map(mockup -> Result.of(mockup))
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
private QuizData getExternalAddressAlias(final QuizData quizData) {
final String externalAddressAlias = this.webserviceInfo.getExternalAddressAlias(this.lmsSetup.lmsApiUrl);
if (StringUtils.isNoneBlank(externalAddressAlias)) {
try {
if (log.isDebugEnabled()) {
log.debug("Found external address alias: {}", externalAddressAlias);
}
final URL url = new URL(this.lmsSetup.lmsApiUrl);
final int port = url.getPort();
final String _externalStartURI =
this.webserviceInfo.getHttpScheme() + "://" + externalAddressAlias + ":" + port + "/api/";
log.info("Use external address for course access: {}", _externalStartURI);
return new QuizData(
quizData.id, quizData.institutionId, quizData.lmsSetupId, quizData.lmsType,
quizData.name, quizData.description, quizData.startTime,
quizData.endTime, _externalStartURI, quizData.additionalAttributes);
} catch (final Exception e) {
log.error("Failed to create external address from alias: ", e);
return quizData;
}
} else {
return quizData;
}
}
private boolean authenticate() { private boolean authenticate() {
try { try {