From 62a6db12c6b9e1e55a89ea251514f1c2ea404201 Mon Sep 17 00:00:00 2001 From: anhefti Date: Wed, 31 May 2023 12:57:06 +0200 Subject: [PATCH] revert DeferredResult --- .../java/ch/ethz/seb/sebserver/SEBServer.java | 1 + .../weblayer/api/ExamAPI_V1_Controller.java | 328 ++++++------------ 2 files changed, 103 insertions(+), 226 deletions(-) diff --git a/src/main/java/ch/ethz/seb/sebserver/SEBServer.java b/src/main/java/ch/ethz/seb/sebserver/SEBServer.java index 6e630bd0..d3018c4d 100644 --- a/src/main/java/ch/ethz/seb/sebserver/SEBServer.java +++ b/src/main/java/ch/ethz/seb/sebserver/SEBServer.java @@ -101,6 +101,7 @@ public class SEBServer { protocolHandler.setProcessorCache(-1); protocolHandler.setTcpNoDelay(true); protocolHandler.setThreadPriority(Thread.NORM_PRIORITY + 1); + protocolHandler.setMaxConnections(2000); if (protocolHandler instanceof Http11NioProtocol) { System.out.println("*************** Http11NioProtocol"); ((Http11NioProtocol) protocolHandler).setPollerThreadPriority(Thread.MAX_PRIORITY); diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ExamAPI_V1_Controller.java b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ExamAPI_V1_Controller.java index 210ecddf..04a16689 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ExamAPI_V1_Controller.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ExamAPI_V1_Controller.java @@ -36,7 +36,6 @@ import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.context.request.async.DeferredResult; import ch.ethz.seb.sebserver.gbl.Constants; import ch.ethz.seb.sebserver.gbl.api.API; @@ -94,7 +93,7 @@ public class ExamAPI_V1_Controller { method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) - public DeferredResult> handshakeCreate( + public CompletableFuture> handshakeCreate( @RequestParam(name = API.PARAM_INSTITUTION_ID, required = false) final Long instIdRequestParam, @RequestParam(name = API.EXAM_API_PARAM_EXAM_ID, required = false) final Long examIdRequestParam, @RequestParam(name = API.EXAM_API_PARAM_CLIENT_ID, required = false) final String clientIdRequestParam, @@ -103,133 +102,69 @@ public class ExamAPI_V1_Controller { final HttpServletRequest request, final HttpServletResponse response) { - final DeferredResult> deferredResult = new DeferredResult<>(); - this.executor.execute(() -> { - final POSTMapper mapper = new POSTMapper(formParams, request.getQueryString()); + return CompletableFuture.supplyAsync( + () -> { - final String remoteAddr = this.getClientAddress(request); - final Long institutionId = (instIdRequestParam != null) - ? instIdRequestParam - : mapper.getLong(API.PARAM_INSTITUTION_ID); - final Long examId = (examIdRequestParam != null) - ? examIdRequestParam - : mapper.getLong(API.EXAM_API_PARAM_EXAM_ID); - final String clientId = (clientIdRequestParam != null) - ? clientIdRequestParam - : mapper.getString(API.EXAM_API_PARAM_CLIENT_ID); + final POSTMapper mapper = new POSTMapper(formParams, request.getQueryString()); - // Create and get new ClientConnection if all integrity checks passes - final ClientConnection clientConnection = this.sebClientConnectionService - .createClientConnection( - principal, - institutionId, - remoteAddr, - mapper.getString(API.EXAM_API_PARAM_SEB_VERSION), - mapper.getString(API.EXAM_API_PARAM_SEB_OS_NAME), - mapper.getString(API.EXAM_API_PARAM_SEB_MACHINE_NAME), - examId, - clientId) - .getOrThrow(); + final String remoteAddr = this.getClientAddress(request); + final Long institutionId = (instIdRequestParam != null) + ? instIdRequestParam + : mapper.getLong(API.PARAM_INSTITUTION_ID); + final Long examId = (examIdRequestParam != null) + ? examIdRequestParam + : mapper.getLong(API.EXAM_API_PARAM_EXAM_ID); + final String clientId = (clientIdRequestParam != null) + ? clientIdRequestParam + : mapper.getString(API.EXAM_API_PARAM_CLIENT_ID); - response.setHeader( - API.EXAM_API_SEB_CONNECTION_TOKEN, - clientConnection.connectionToken); + // Create and get new ClientConnection if all integrity checks passes + final ClientConnection clientConnection = this.sebClientConnectionService + .createClientConnection( + principal, + institutionId, + remoteAddr, + mapper.getString(API.EXAM_API_PARAM_SEB_VERSION), + mapper.getString(API.EXAM_API_PARAM_SEB_OS_NAME), + mapper.getString(API.EXAM_API_PARAM_SEB_MACHINE_NAME), + examId, + clientId) + .getOrThrow(); - // Crate list of running exams - List result; - if (examId == null) { - result = this.examSessionService.getRunningExamsForInstitution(institutionId) - .getOrThrow() - .stream() - .map(this::createRunningExamInfo) - .filter(this::checkConsistency) - .collect(Collectors.toList()); - } else { + response.setHeader( + API.EXAM_API_SEB_CONNECTION_TOKEN, + clientConnection.connectionToken); - final Exam exam = this.examSessionService - .getExamDAO() - .byPK(examId) - .getOrThrow(); + // Crate list of running exams + List result; + if (examId == null) { + result = this.examSessionService.getRunningExamsForInstitution(institutionId) + .getOrThrow() + .stream() + .map(this::createRunningExamInfo) + .filter(this::checkConsistency) + .collect(Collectors.toList()); + } else { - result = Arrays.asList(createRunningExamInfo(exam)); - processASKSalt(response, clientConnection); - processAlternativeBEK(response, clientConnection.examId); - } + final Exam exam = this.examSessionService + .getExamDAO() + .byPK(examId) + .getOrThrow(); - if (result.isEmpty()) { - log.warn( - "There are no currently running exams for institution: {}. SEB connection creation denied", - institutionId); - } + result = Arrays.asList(createRunningExamInfo(exam)); + processASKSalt(response, clientConnection); + processAlternativeBEK(response, clientConnection.examId); + } - deferredResult.setResult(result); - }); + if (result.isEmpty()) { + log.warn( + "There are no currently running exams for institution: {}. SEB connection creation denied", + institutionId); + } - return deferredResult; - -// return CompletableFuture.supplyAsync( -// () -> { -// -// final POSTMapper mapper = new POSTMapper(formParams, request.getQueryString()); -// -// final String remoteAddr = this.getClientAddress(request); -// final Long institutionId = (instIdRequestParam != null) -// ? instIdRequestParam -// : mapper.getLong(API.PARAM_INSTITUTION_ID); -// final Long examId = (examIdRequestParam != null) -// ? examIdRequestParam -// : mapper.getLong(API.EXAM_API_PARAM_EXAM_ID); -// final String clientId = (clientIdRequestParam != null) -// ? clientIdRequestParam -// : mapper.getString(API.EXAM_API_PARAM_CLIENT_ID); -// -// // Create and get new ClientConnection if all integrity checks passes -// final ClientConnection clientConnection = this.sebClientConnectionService -// .createClientConnection( -// principal, -// institutionId, -// remoteAddr, -// mapper.getString(API.EXAM_API_PARAM_SEB_VERSION), -// mapper.getString(API.EXAM_API_PARAM_SEB_OS_NAME), -// mapper.getString(API.EXAM_API_PARAM_SEB_MACHINE_NAME), -// examId, -// clientId) -// .getOrThrow(); -// -// response.setHeader( -// API.EXAM_API_SEB_CONNECTION_TOKEN, -// clientConnection.connectionToken); -// -// // Crate list of running exams -// List result; -// if (examId == null) { -// result = this.examSessionService.getRunningExamsForInstitution(institutionId) -// .getOrThrow() -// .stream() -// .map(this::createRunningExamInfo) -// .filter(this::checkConsistency) -// .collect(Collectors.toList()); -// } else { -// -// final Exam exam = this.examSessionService -// .getExamDAO() -// .byPK(examId) -// .getOrThrow(); -// -// result = Arrays.asList(createRunningExamInfo(exam)); -// processASKSalt(response, clientConnection); -// processAlternativeBEK(response, clientConnection.examId); -// } -// -// if (result.isEmpty()) { -// log.warn( -// "There are no currently running exams for institution: {}. SEB connection creation denied", -// institutionId); -// } -// -// return result; -// }, -// this.executor); + return result; + }, + this.executor); } private boolean checkConsistency(final RunningExamInfo info) { @@ -248,7 +183,7 @@ public class ExamAPI_V1_Controller { path = API.EXAM_API_HANDSHAKE_ENDPOINT, method = RequestMethod.PATCH, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) - public DeferredResult handshakeUpdate( + public CompletableFuture handshakeUpdate( @RequestHeader(name = API.EXAM_API_SEB_CONNECTION_TOKEN, required = true) final String connectionToken, @RequestParam(name = API.EXAM_API_PARAM_EXAM_ID, required = false) final Long examId, @RequestParam(name = API.EXAM_API_USER_SESSION_ID, required = false) final String userSessionId, @@ -263,71 +198,39 @@ public class ExamAPI_V1_Controller { final HttpServletRequest request, final HttpServletResponse response) { - final DeferredResult deferredResult = new DeferredResult<>(); - this.executor.execute(() -> { - { + return CompletableFuture.runAsync( + () -> { - final String remoteAddr = this.getClientAddress(request); - final Long institutionId = getInstitutionId(principal); + final String remoteAddr = this.getClientAddress(request); + final Long institutionId = getInstitutionId(principal); - final ClientConnection clientConnection = this.sebClientConnectionService - .updateClientConnection( - connectionToken, - institutionId, - examId, - remoteAddr, - sebVersion, - sebOSName, - sebMachinName, - userSessionId, - clientId, - browserSignatureKey) - .getOrThrow(); + final ClientConnection clientConnection = this.sebClientConnectionService + .updateClientConnection( + connectionToken, + institutionId, + examId, + remoteAddr, + sebVersion, + sebOSName, + sebMachinName, + userSessionId, + clientId, + browserSignatureKey) + .getOrThrow(); - if (clientConnection.examId != null) { - processASKSalt(response, clientConnection); - processAlternativeBEK(response, clientConnection.examId); - } - - deferredResult.setResult(null); - } - }); - - return deferredResult; - -// return CompletableFuture.runAsync( -// () -> { -// -// final String remoteAddr = this.getClientAddress(request); -// final Long institutionId = getInstitutionId(principal); -// -// final ClientConnection clientConnection = this.sebClientConnectionService -// .updateClientConnection( -// connectionToken, -// institutionId, -// examId, -// remoteAddr, -// sebVersion, -// sebOSName, -// sebMachinName, -// userSessionId, -// clientId, -// browserSignatureKey) -// .getOrThrow(); -// -// if (clientConnection.examId != null) { -// processASKSalt(response, clientConnection); -// processAlternativeBEK(response, clientConnection.examId); -// } -// }, -// this.executor); + if (clientConnection.examId != null) { + processASKSalt(response, clientConnection); + processAlternativeBEK(response, clientConnection.examId); + } + }, + this.executor); } @RequestMapping( path = API.EXAM_API_HANDSHAKE_ENDPOINT, method = RequestMethod.PUT, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) - public DeferredResult handshakeEstablish( + public CompletableFuture handshakeEstablish( @RequestHeader(name = API.EXAM_API_SEB_CONNECTION_TOKEN, required = true) final String connectionToken, @RequestParam(name = API.EXAM_API_PARAM_EXAM_ID, required = false) final Long examId, @RequestParam(name = API.EXAM_API_USER_SESSION_ID, required = false) final String userSessionId, @@ -342,58 +245,31 @@ public class ExamAPI_V1_Controller { final HttpServletRequest request, final HttpServletResponse response) { - final DeferredResult deferredResult = new DeferredResult<>(); - this.executor.execute(() -> { + return CompletableFuture.runAsync( + () -> { - final String remoteAddr = this.getClientAddress(request); - final Long institutionId = getInstitutionId(principal); + final String remoteAddr = this.getClientAddress(request); + final Long institutionId = getInstitutionId(principal); - final ClientConnection clientConnection = this.sebClientConnectionService - .establishClientConnection( - connectionToken, - institutionId, - examId, - remoteAddr, - sebVersion, - sebOSName, - sebMachinName, - userSessionId, - clientId, - browserSignatureKey) - .getOrThrow(); + final ClientConnection clientConnection = this.sebClientConnectionService + .establishClientConnection( + connectionToken, + institutionId, + examId, + remoteAddr, + sebVersion, + sebOSName, + sebMachinName, + userSessionId, + clientId, + browserSignatureKey) + .getOrThrow(); - if (clientConnection.examId != null) { - processAlternativeBEK(response, clientConnection.examId); - } - - deferredResult.setResult(null); - }); - return deferredResult; -// return CompletableFuture.runAsync( -// () -> { -// -// final String remoteAddr = this.getClientAddress(request); -// final Long institutionId = getInstitutionId(principal); -// -// final ClientConnection clientConnection = this.sebClientConnectionService -// .establishClientConnection( -// connectionToken, -// institutionId, -// examId, -// remoteAddr, -// sebVersion, -// sebOSName, -// sebMachinName, -// userSessionId, -// clientId, -// browserSignatureKey) -// .getOrThrow(); -// -// if (clientConnection.examId != null) { -// processAlternativeBEK(response, clientConnection.examId); -// } -// }, -// this.executor); + if (clientConnection.examId != null) { + processAlternativeBEK(response, clientConnection.examId); + } + }, + this.executor); } @RequestMapping(