Merge remote-tracking branch 'origin/dev-1.2' into development

Conflicts:
	pom.xml
	src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/ExamSessionControlTask.java
	src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ExamMonitoringController.java
This commit is contained in:
anhefti 2021-12-06 13:41:25 +01:00
commit 3349d67d5a
22 changed files with 193 additions and 92 deletions

View file

@ -686,7 +686,11 @@ public final class Utils {
if (StringUtils.isBlank(text)) {
return StringUtils.EMPTY;
}
return Constants.DOUBLE_QUOTE + text.replace("\"", "\"\"") + Constants.DOUBLE_QUOTE;
return Constants.DOUBLE_QUOTE +
text
.replace("\r\n", "\n")
.replace("\"", "\"\"")
+ Constants.DOUBLE_QUOTE;
}
}

View file

@ -55,26 +55,32 @@ public class SEBClientLogExport extends AbstractDownloadServiceHandler {
queryParams.add(param, String.valueOf(request.getParameter(param)));
}
final InputStream input = this.restService
this.restService
.getBuilder(ExportSEBClientLogs.class)
.withResponseExtractor(response -> {
try {
final InputStream input = response.getBody();
IOUtils.copyLarge(input, downloadOut);
} catch (final IOException e) {
log.error(
"Unexpected error while streaming incoming config data from web-service to output-stream of download response: ",
e);
} finally {
try {
downloadOut.flush();
downloadOut.close();
} catch (final IOException e) {
log.error("Unexpected error while trying to close download output-stream");
}
}
return true;
})
.withQueryParams(queryParams)
.call()
.getOrThrow();
.onError(error -> log.error("Download failed: ", error));
try {
IOUtils.copyLarge(input, downloadOut);
} catch (final IOException e) {
log.error(
"Unexpected error while streaming incoming config data from web-service to output-stream of download response: ",
e);
} finally {
try {
downloadOut.flush();
downloadOut.close();
} catch (final IOException e) {
log.error("Unexpected error while trying to close download output-stream");
}
}
}
}

View file

@ -0,0 +1,43 @@
/*
* Copyright (c) 2021 ETH Zürich, Educational Development and Technology (LET)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package ch.ethz.seb.sebserver.gui.service.remote.webservice.api;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequest;
import com.fasterxml.jackson.core.type.TypeReference;
import ch.ethz.seb.sebserver.gbl.util.Result;
public class AbstractDownloadCall extends RestCall<Boolean> {
protected AbstractDownloadCall(
final MediaType contentType,
final String path) {
super(new RestCall.TypeKey<>(CallType.UNDEFINED, null, new TypeReference<Boolean>() {
}), HttpMethod.GET, contentType, path);
}
@Override
protected Result<Boolean> exchange(final RestCallBuilder builder) {
return Result.tryCatch(() -> builder
.getRestTemplate()
.execute(
builder.buildURI(),
this.httpMethod,
(final ClientHttpRequest requestCallback) -> {
},
builder.getResponseExtractor(),
builder.getURIVariables()));
}
}

View file

@ -17,6 +17,7 @@ import org.springframework.http.client.ClientHttpRequest;
import ch.ethz.seb.sebserver.gbl.util.Result;
@Deprecated // This is not streaming correctly. Use AbstractDownloadCall instead
public abstract class AbstractExportCall extends RestCall<InputStream> {
protected AbstractExportCall(

View file

@ -29,6 +29,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.ResponseExtractor;
import org.springframework.web.client.RestClientResponseException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
@ -224,6 +225,7 @@ public abstract class RestCall<T> {
private final HttpHeaders httpHeaders;
private String body = null;
private InputStream streamingBody = null;
private ResponseExtractor<Boolean> responseExtractor = null;
private final MultiValueMap<String, String> queryParams;
private final Map<String, String> uriVariables;
@ -253,6 +255,15 @@ public abstract class RestCall<T> {
return this.restTemplate;
}
public RestCallBuilder withResponseExtractor(final ResponseExtractor<Boolean> responseExtractor) {
this.responseExtractor = responseExtractor;
return this;
}
public ResponseExtractor<Boolean> getResponseExtractor() {
return this.responseExtractor;
}
public RestCallBuilder withRestTemplate(final RestTemplate restTemplate) {
this.restTemplate = restTemplate;
return this;

View file

@ -8,33 +8,21 @@
package ch.ethz.seb.sebserver.gui.service.remote.webservice.api.exam;
import java.io.InputStream;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.core.type.TypeReference;
import ch.ethz.seb.sebserver.gbl.api.API;
import ch.ethz.seb.sebserver.gbl.api.EntityType;
import ch.ethz.seb.sebserver.gbl.profile.GuiProfile;
import ch.ethz.seb.sebserver.gui.service.remote.webservice.api.AbstractExportCall;
import ch.ethz.seb.sebserver.gui.service.remote.webservice.api.AbstractDownloadCall;
@Lazy
@Component
@GuiProfile
public class ExportSEBClientLogs extends AbstractExportCall {
public class ExportSEBClientLogs extends AbstractDownloadCall {
public ExportSEBClientLogs() {
super(new TypeKey<>(
CallType.UNDEFINED,
EntityType.CLIENT_EVENT,
new TypeReference<InputStream>() {
}),
HttpMethod.GET,
MediaType.APPLICATION_FORM_URLENCODED,
super(MediaType.APPLICATION_FORM_URLENCODED,
API.SEB_CLIENT_EVENT_ENDPOINT
+ API.SEB_CLIENT_EVENT_EXPORT_PATH_SEGMENT);
}

View file

@ -8,6 +8,17 @@
package ch.ethz.seb.sebserver.webservice;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import ch.ethz.seb.sebserver.SEBServerInit;
import ch.ethz.seb.sebserver.WebSecurityConfig;
import ch.ethz.seb.sebserver.gbl.client.ClientCredentialServiceImpl;
@ -20,16 +31,6 @@ import ch.ethz.seb.sebserver.webservice.servicelayer.authorization.impl.SEBServe
import ch.ethz.seb.sebserver.webservice.servicelayer.dao.FilterMap;
import ch.ethz.seb.sebserver.webservice.servicelayer.dao.InstitutionDAO;
import ch.ethz.seb.sebserver.webservice.servicelayer.dao.UserDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
@Component
@WebServiceProfile
@ -69,7 +70,7 @@ class AdminUserInitializer {
try {
log.debug("Create initial admin account is switched on. Check database if exists...");
final Result<SEBServerUser> byUsername = this.userDAO.sebServerUserByUsername(this.adminName);
final Result<SEBServerUser> byUsername = this.userDAO.sebServerAdminByUsername(this.adminName);
if (byUsername.hasValue()) {
log.debug("Initial admin account already exists. Check if the password must be reset...");
@ -130,7 +131,7 @@ class AdminUserInitializer {
return account;
})
.getOrThrow();
}
}
} catch (final Exception e) {
SEBServerInit.INIT_LOGGER.error("---->");
SEBServerInit.INIT_LOGGER.error("----> SEB Server initial admin-account creation failed: ", e);

View file

@ -94,7 +94,7 @@ public class WebserviceInit implements ApplicationListener<ApplicationReadyEvent
SEBServerInit.INIT_LOGGER.info("------> Ping update time: {}",
this.environment.getProperty("sebserver.webservice.distributed.pingUpdate"));
SEBServerInit.INIT_LOGGER.info("------> Connection update time: {}",
this.environment.getProperty("sebserver.webservice.distributed.connectionUpdate"));
this.environment.getProperty("sebserver.webservice.distributed.connectionUpdate", "2000"));
}
try {

View file

@ -49,6 +49,12 @@ public interface UserDAO extends ActivatableEntityDAO<UserInfo, UserMod>, BulkAc
* @return a Result of SEBServerUser for specified username. Or an exception result on error case */
Result<SEBServerUser> sebServerUserByUsername(String username);
/** Use this to get the SEBServerUser admin principal for a given username.
*
* @param username The username of the user to get SEBServerUser from
* @return a Result of SEBServerUser for specified username. Or an exception result on error case */
Result<SEBServerUser> sebServerAdminByUsername(String username);
/** Use this to get a Collection containing EntityKey's of all entities that belongs to a given User.
*
* @param uuid The UUID of the user

View file

@ -127,6 +127,21 @@ public class UserDAOImpl implements UserDAO {
.flatMap(this::sebServerUserFromRecord);
}
@Override
@Transactional(readOnly = true)
public Result<SEBServerUser> sebServerAdminByUsername(final String username) {
return getSingleResource(
username,
this.userRecordMapper
.selectByExample()
.where(UserRecordDynamicSqlSupport.username, isEqualTo(username))
.and(UserRecordDynamicSqlSupport.active,
isEqualTo(BooleanUtils.toInteger(true)))
.build()
.execute())
.flatMap(this::sebServerUserFromRecord);
}
@Override
@Transactional(readOnly = true)
public Result<Collection<UserInfo>> all(final Long institutionId, final Boolean active) {

View file

@ -242,7 +242,9 @@ public class ExamAdminServiceImpl implements ExamAdminService {
examId,
ProctoringServiceSettings.ATTR_ENABLE_PROCTORING)
.map(rec -> BooleanUtils.toBoolean(rec.getValue()))
.onError(error -> log.error("Failed to verify proctoring enabled for exam: {}", examId, error));
.onError(error -> log.warn("Failed to verify proctoring enabled for exam: {}, {}",
examId,
error.getMessage()));
if (result.hasError()) {
return Result.of(false);
}

View file

@ -228,7 +228,7 @@ public class SEBClientEventAdminServiceImpl implements SEBClientEventAdminServic
private final String sort;
private int pageNumber = 1;
private final int pageSize = 1000;
private final int pageSize = 10000;
private Collection<ClientEventRecord> nextRecords;

View file

@ -112,11 +112,13 @@ class ExamSessionControlTask implements DisposableBean {
@Scheduled(fixedRateString = "${sebserver.webservice.api.exam.update-ping:5000}")
public void examSessionUpdateTask() {
this.sebClientConnectionService.updatePingEvents();
if (!this.webserviceInfoDAO.isMaster(this.webserviceInfo.getWebserviceUUID())) {
return;
}
this.sebClientConnectionService.updatePingEvents();
this.sebClientConnectionService.cleanupInstructions();
this.examProcotringRoomService.updateProctoringCollectingRooms();
}

View file

@ -61,6 +61,8 @@ public abstract class AbstractLogLevelCountIndicator extends AbstractLogIndicato
return super.currentValue;
}
// TODO do this within a better reactive way like ping updates
try {
final Long errors = this.clientEventRecordMapper

View file

@ -67,9 +67,9 @@ public abstract class AbstractLogNumberIndicator extends AbstractLogIndicator {
return super.currentValue;
}
try {
// TODO do this within a better reactive way like ping updates
System.out.println("************** loadFromPersistent");
try {
final List<ClientEventRecord> execute = this.clientEventRecordMapper.selectByExample()
.where(ClientEventRecordDynamicSqlSupport.clientConnectionId, isEqualTo(this.connectionId))

View file

@ -19,8 +19,6 @@ import org.springframework.beans.factory.annotation.Qualifier;
import ch.ethz.seb.sebserver.gbl.async.AsyncServiceSpringConfig;
import ch.ethz.seb.sebserver.gbl.model.exam.Indicator;
import ch.ethz.seb.sebserver.gbl.model.exam.Indicator.IndicatorType;
import ch.ethz.seb.sebserver.gbl.model.session.ClientEvent;
import ch.ethz.seb.sebserver.gbl.model.session.ClientEvent.EventType;
import ch.ethz.seb.sebserver.gbl.util.Utils;
import ch.ethz.seb.sebserver.webservice.datalayer.batis.ClientEventLastPingMapper;
@ -35,12 +33,12 @@ public abstract class AbstractPingIndicator extends AbstractClientIndicator {
private final Executor executor;
protected final DistributedPingCache distributedPingCache;
//protected Long pingRecord = null;
protected PingUpdate pingUpdate = null;
protected AbstractPingIndicator(
final DistributedPingCache distributedPingCache,
@Qualifier(AsyncServiceSpringConfig.EXAM_API_PING_SERVICE_EXECUTOR_BEAN_NAME) final Executor executor) {
super();
this.executor = executor;
this.distributedPingCache = distributedPingCache;
@ -81,7 +79,9 @@ public abstract class AbstractPingIndicator extends AbstractClientIndicator {
try {
this.executor.execute(this.pingUpdate);
} catch (final Exception e) {
//log.warn("Failed to schedule ping task: {}" + e.getMessage());
if (log.isDebugEnabled()) {
log.warn("Failed to schedule ping task: {}" + e.getMessage());
}
}
}
}
@ -115,30 +115,6 @@ public abstract class AbstractPingIndicator extends AbstractClientIndicator {
public abstract ClientEventRecord updateLogEvent(final long now);
@Override
public double computeValueAt(final long timestamp) {
// TODO Auto-generated method stub
return 0;
}
@Override
public void notifyValueChange(final ClientEvent event) {
// TODO Auto-generated method stub
}
@Override
public void notifyValueChange(final ClientEventRecord clientEventRecord) {
// TODO Auto-generated method stub
}
@Override
public IndicatorType getType() {
// TODO Auto-generated method stub
return null;
}
static final class PingUpdate implements Runnable {
private final ClientEventLastPingMapper clientEventLastPingMapper;
@ -151,8 +127,12 @@ public abstract class AbstractPingIndicator extends AbstractClientIndicator {
@Override
public void run() {
this.clientEventLastPingMapper
.updatePingTime(this.pingRecord, Utils.getMillisecondsNow());
try {
this.clientEventLastPingMapper
.updatePingTime(this.pingRecord, Utils.getMillisecondsNow());
} catch (final Exception e) {
log.error("Failed to update ping: {}", e.getMessage());
}
}
}

View file

@ -176,6 +176,7 @@ public class DistributedPingCache implements DisposableBean {
public Long getLastPing(final Long pingRecordId, final boolean missing) {
try {
Long ping = this.pingCache.get(pingRecordId);
if (ping == null && !missing) {
@ -199,7 +200,7 @@ public class DistributedPingCache implements DisposableBean {
}
private void updatePings() {
if (this.pingCache.isEmpty()) {
return;
}

View file

@ -83,7 +83,7 @@ public final class PingIntervalClientIndicator extends AbstractPingIndicator {
this.missingPing = this.pingErrorThreshold < value;
} catch (final Exception e) {
log.error("Failed to initialize missingPing: {}", e.getMessage());
this.missingPing = false;
this.missingPing = true;
}
}
@ -111,8 +111,8 @@ public final class PingIntervalClientIndicator extends AbstractPingIndicator {
@Override
public double getValue() {
final long now = DateTimeUtils.currentTimeMillis();
return now - super.getValue();
final double value = super.getValue();
return DateTimeUtils.currentTimeMillis() - value;
}
@Override

View file

@ -177,6 +177,16 @@ public class APIExceptionHandler extends ResponseEntityExceptionHandler {
.createErrorResponse(ex.getMessage());
}
@ExceptionHandler(ExamNotRunningException.class)
public ResponseEntity<Object> handleExamNotRunning(
final ExamNotRunningException ex,
final WebRequest request) {
log.warn("{}", ex.getMessage());
return APIMessage.ErrorMessage.INTEGRITY_VALIDATION
.createErrorResponse(ex.getMessage());
}
@ExceptionHandler(APIConstraintViolationException.class)
public ResponseEntity<Object> handleIllegalAPIArgumentException(
final APIConstraintViolationException ex,

View file

@ -202,6 +202,7 @@ public class ExamMonitoringController {
@PathVariable(name = API.EXAM_API_SEB_CONNECTION_TOKEN, required = true) final String connectionToken) {
checkPrivileges(institutionId, examId);
return this.examSessionService
.getConnectionData(connectionToken)
.getOrThrow();
@ -267,6 +268,7 @@ public class ExamMonitoringController {
@PathVariable(name = API.EXAM_API_SEB_CONNECTION_TOKEN, required = true) final String connectionToken) {
checkPrivileges(institutionId, examId);
this.sebClientNotificationService.confirmPendingNotification(
notificationId,
examId,
@ -313,8 +315,14 @@ public class ExamMonitoringController {
UserRole.EXAM_SUPPORTER,
UserRole.EXAM_ADMIN);
// check exam running
final Exam runningExam = this.examSessionService.getRunningExam(examId).getOr(null);
if (runningExam == null) {
throw new ExamNotRunningException("Exam not currently running: " + examId);
}
// check running exam privilege for specified exam
if (!hasRunningExamPrivilege(examId, institutionId)) {
if (!hasRunningExamPrivilege(runningExam, institutionId)) {
throw new PermissionDeniedException(
EntityType.EXAM,
PrivilegeType.READ,
@ -322,12 +330,6 @@ public class ExamMonitoringController {
}
}
private boolean hasRunningExamPrivilege(final Long examId, final Long institution) {
return hasRunningExamPrivilege(
this.examSessionService.getRunningExam(examId).getOr(null),
institution);
}
private boolean hasRunningExamPrivilege(final Exam exam, final Long institution) {
if (exam == null) {
return false;

View file

@ -0,0 +1,27 @@
/*
* Copyright (c) 2021 ETH Zürich, Educational Development and Technology (LET)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package ch.ethz.seb.sebserver.webservice.weblayer.api;
public class ExamNotRunningException extends RuntimeException {
private static final long serialVersionUID = -2931666431463176875L;
public ExamNotRunningException() {
super();
}
public ExamNotRunningException(final String message, final Throwable cause) {
super(message, cause);
}
public ExamNotRunningException(final String message) {
super(message);
}
}

View file

@ -38,7 +38,7 @@ sebserver.webservice.internalSecret=${sebserver.password}
### webservice networking
sebserver.webservice.forceMaster=false
sebserver.webservice.distributed=false
sebserver.webservice.distributed=true
sebserver.webservice.distributed.pingUpdate=3000
sebserver.webservice.http.external.scheme=https
sebserver.webservice.http.external.servername=