Prepare LMS Setups
This commit is contained in:
parent
ad2f93e6cd
commit
dade1b2da3
5 changed files with 40 additions and 35 deletions
|
@ -8,7 +8,11 @@
|
||||||
|
|
||||||
package ch.ethz.seb.sebserver.gbl.util;
|
package ch.ethz.seb.sebserver.gbl.util;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.net.URL;
|
||||||
import java.net.URLDecoder;
|
import java.net.URLDecoder;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
@ -686,4 +690,20 @@ public final class Utils {
|
||||||
public static String valueOrEmptyNote(final String value) {
|
public static String valueOrEmptyNote(final String value) {
|
||||||
return StringUtils.isBlank(value) ? Constants.EMPTY_NOTE : value;
|
return StringUtils.isBlank(value) ? Constants.EMPTY_NOTE : value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** This is used to verify if a given URL is available (valid)
|
||||||
|
* Uses java.net.Socket to try to connect to the given URL
|
||||||
|
*
|
||||||
|
* @param urlString the URL string
|
||||||
|
* @return true if SEB Server was able to ping the address. */
|
||||||
|
public static boolean pingHost(final String urlString) {
|
||||||
|
try (Socket socket = new Socket()) {
|
||||||
|
final URL url = new URL(urlString);
|
||||||
|
final int port = (url.getPort() >= 0) ? url.getPort() : 80;
|
||||||
|
socket.connect(new InetSocketAddress(url.getHost(), port), (int) Constants.SECOND_IN_MILLIS * 5);
|
||||||
|
return true;
|
||||||
|
} catch (final IOException e) {
|
||||||
|
return false; // Either timeout or unreachable or failed DNS lookup.
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,10 +8,6 @@
|
||||||
|
|
||||||
package ch.ethz.seb.sebserver.webservice.servicelayer.lms;
|
package ch.ethz.seb.sebserver.webservice.servicelayer.lms;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.InetSocketAddress;
|
|
||||||
import java.net.Socket;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
@ -20,7 +16,6 @@ import java.util.Set;
|
||||||
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
import ch.ethz.seb.sebserver.gbl.Constants;
|
|
||||||
import ch.ethz.seb.sebserver.gbl.api.EntityType;
|
import ch.ethz.seb.sebserver.gbl.api.EntityType;
|
||||||
import ch.ethz.seb.sebserver.gbl.model.exam.Chapters;
|
import ch.ethz.seb.sebserver.gbl.model.exam.Chapters;
|
||||||
import ch.ethz.seb.sebserver.gbl.model.exam.Exam;
|
import ch.ethz.seb.sebserver.gbl.model.exam.Exam;
|
||||||
|
@ -77,18 +72,12 @@ public interface LmsAPITemplate {
|
||||||
* @return Collection of all QuizData from the given id set */
|
* @return Collection of all QuizData from the given id set */
|
||||||
Collection<Result<QuizData>> getQuizzes(Set<String> ids);
|
Collection<Result<QuizData>> getQuizzes(Set<String> ids);
|
||||||
|
|
||||||
/** Get all QuizData for the set of QuizData identifiers from LMS API in a collection
|
/** Get the quiz data with specified identifier.
|
||||||
* of Result. If particular Quiz cannot be loaded because of errors or deletion,
|
|
||||||
* the Result will have an error reference.
|
|
||||||
*
|
*
|
||||||
* NOTE: This method looks first in the cache for all given ids.
|
* Default implementation: Uses getQuizzes(Set<String> ids) and returns the first matching or an error.
|
||||||
* If all quizzes are cached, returns all from cache.
|
|
||||||
* If one quiz is not in the cache, requests all quizzes from the API and refreshes the cache
|
|
||||||
*
|
*
|
||||||
* @param ids the Set of Quiz identifiers to get the QuizData for
|
* @param id the quiz data identifier
|
||||||
* @return Collection of all QuizData from the given id set */
|
* @return Result refer to the quiz data or to an error when happened */
|
||||||
Collection<Result<QuizData>> getQuizzesFromCache(Set<String> ids);
|
|
||||||
|
|
||||||
default Result<QuizData> getQuiz(final String id) {
|
default Result<QuizData> getQuiz(final String id) {
|
||||||
if (StringUtils.isBlank(id)) {
|
if (StringUtils.isBlank(id)) {
|
||||||
return Result.ofError(new RuntimeException("missing model id"));
|
return Result.ofError(new RuntimeException("missing model id"));
|
||||||
|
@ -100,6 +89,18 @@ public interface LmsAPITemplate {
|
||||||
.orElse(Result.ofError(new ResourceNotFoundException(EntityType.EXAM, id)));
|
.orElse(Result.ofError(new ResourceNotFoundException(EntityType.EXAM, id)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Get all QuizData for the set of QuizData identifiers from LMS API in a collection
|
||||||
|
* of Result. If particular Quiz cannot be loaded because of errors or deletion,
|
||||||
|
* the Result will have an error reference.
|
||||||
|
*
|
||||||
|
* NOTE: This method looks first in the cache if existing for all given ids.
|
||||||
|
* If all quizzes are cached, returns all from cache.
|
||||||
|
* If one quiz is not in the cache, requests all quizzes from the API and refreshes the cache
|
||||||
|
*
|
||||||
|
* @param ids the Set of Quiz identifiers to get the QuizData for
|
||||||
|
* @return Collection of all QuizData from the given id set */
|
||||||
|
Collection<Result<QuizData>> getQuizzesFromCache(Set<String> ids);
|
||||||
|
|
||||||
/** Convert a an anonymous or temporary user session identifier from SEB Client into a user
|
/** Convert a an anonymous or temporary user session identifier from SEB Client into a user
|
||||||
* account details.
|
* account details.
|
||||||
*
|
*
|
||||||
|
@ -152,19 +153,4 @@ public interface LmsAPITemplate {
|
||||||
* @return Result refer to the given Exam if successful or to an error if not */
|
* @return Result refer to the given Exam if successful or to an error if not */
|
||||||
Result<Exam> releaseSEBClientRestriction(Exam exam);
|
Result<Exam> releaseSEBClientRestriction(Exam exam);
|
||||||
|
|
||||||
/** This is used to verify if a given LMS Setup URL is available (valid)
|
|
||||||
*
|
|
||||||
* @param urlString the URL string given by the LMS Setup attribute
|
|
||||||
* @return true if SEB Server was able to ping the address. */
|
|
||||||
static boolean pingHost(final String urlString) {
|
|
||||||
try (Socket socket = new Socket()) {
|
|
||||||
final URL url = new URL(urlString);
|
|
||||||
final int port = (url.getPort() >= 0) ? url.getPort() : 80;
|
|
||||||
socket.connect(new InetSocketAddress(url.getHost(), port), (int) Constants.SECOND_IN_MILLIS * 5);
|
|
||||||
return true;
|
|
||||||
} catch (final IOException e) {
|
|
||||||
return false; // Either timeout or unreachable or failed DNS lookup.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -97,7 +97,7 @@ public abstract class CourseAccess {
|
||||||
final List<QuizData> cached = allQuizzesSupplier().getAllCached();
|
final List<QuizData> cached = allQuizzesSupplier().getAllCached();
|
||||||
final List<QuizData> available = (cached != null)
|
final List<QuizData> available = (cached != null)
|
||||||
? cached
|
? cached
|
||||||
: quizzesSupplier(ids).get();
|
: Collections.emptyList();
|
||||||
|
|
||||||
final Map<String, QuizData> quizMapping = available
|
final Map<String, QuizData> quizMapping = available
|
||||||
.stream()
|
.stream()
|
||||||
|
|
|
@ -42,7 +42,7 @@ import ch.ethz.seb.sebserver.gbl.model.Domain.LMS_SETUP;
|
||||||
import ch.ethz.seb.sebserver.gbl.model.institution.LmsSetup;
|
import ch.ethz.seb.sebserver.gbl.model.institution.LmsSetup;
|
||||||
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.servicelayer.lms.LmsAPITemplate;
|
import ch.ethz.seb.sebserver.gbl.util.Utils;
|
||||||
|
|
||||||
final class OpenEdxRestTemplateFactory {
|
final class OpenEdxRestTemplateFactory {
|
||||||
|
|
||||||
|
@ -84,7 +84,7 @@ final class OpenEdxRestTemplateFactory {
|
||||||
"lmsSetup:lmsUrl:notNull"));
|
"lmsSetup:lmsUrl:notNull"));
|
||||||
} else {
|
} else {
|
||||||
// try to connect to the url
|
// try to connect to the url
|
||||||
if (!LmsAPITemplate.pingHost(this.lmsSetup.lmsApiUrl)) {
|
if (!Utils.pingHost(this.lmsSetup.lmsApiUrl)) {
|
||||||
missingAttrs.add(APIMessage.fieldValidationError(
|
missingAttrs.add(APIMessage.fieldValidationError(
|
||||||
LMS_SETUP.ATTR_LMS_URL,
|
LMS_SETUP.ATTR_LMS_URL,
|
||||||
"lmsSetup:lmsUrl:url.invalid"));
|
"lmsSetup:lmsUrl:url.invalid"));
|
||||||
|
|
|
@ -50,7 +50,6 @@ import ch.ethz.seb.sebserver.gbl.model.institution.LmsSetup;
|
||||||
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.gbl.util.Utils;
|
import ch.ethz.seb.sebserver.gbl.util.Utils;
|
||||||
import ch.ethz.seb.sebserver.webservice.servicelayer.lms.LmsAPITemplate;
|
|
||||||
|
|
||||||
class MoodleRestTemplateFactory {
|
class MoodleRestTemplateFactory {
|
||||||
|
|
||||||
|
@ -95,7 +94,7 @@ class MoodleRestTemplateFactory {
|
||||||
"lmsSetup:lmsUrl:notNull"));
|
"lmsSetup:lmsUrl:notNull"));
|
||||||
} else {
|
} else {
|
||||||
// try to connect to the url
|
// try to connect to the url
|
||||||
if (!LmsAPITemplate.pingHost(this.lmsSetup.lmsApiUrl)) {
|
if (!Utils.pingHost(this.lmsSetup.lmsApiUrl)) {
|
||||||
missingAttrs.add(APIMessage.fieldValidationError(
|
missingAttrs.add(APIMessage.fieldValidationError(
|
||||||
LMS_SETUP.ATTR_LMS_URL,
|
LMS_SETUP.ATTR_LMS_URL,
|
||||||
"lmsSetup:lmsUrl:url.invalid"));
|
"lmsSetup:lmsUrl:url.invalid"));
|
||||||
|
|
Loading…
Reference in a new issue