From 6b552b93f9a4eb5ca995eeec76de92142eb66d49 Mon Sep 17 00:00:00 2001 From: anhefti Date: Thu, 31 Oct 2024 11:02:29 +0100 Subject: [PATCH] SEBSERV-601 improved http client handling --- .../ClientHttpRequestFactoryService.java | 2 +- .../ScreenProctoringAPIBinding.java | 29 +++++++++++++------ .../ScreenProctoringServiceImpl.java | 5 +++- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/main/java/ch/ethz/seb/sebserver/ClientHttpRequestFactoryService.java b/src/main/java/ch/ethz/seb/sebserver/ClientHttpRequestFactoryService.java index 9757b6e8b..4fa70fe98 100644 --- a/src/main/java/ch/ethz/seb/sebserver/ClientHttpRequestFactoryService.java +++ b/src/main/java/ch/ethz/seb/sebserver/ClientHttpRequestFactoryService.java @@ -147,7 +147,7 @@ private ClientHttpRequestFactory clientHttpRequestFactory(final ProxyData proxy) .build(); final HttpComponentsClientHttpRequestFactory devClientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(client); - devClientHttpRequestFactory.setBufferRequestBody(false); + //devClientHttpRequestFactory.setBufferRequestBody(false); devClientHttpRequestFactory.setConnectionRequestTimeout(this.connectionRequestTimeout); devClientHttpRequestFactory.setConnectTimeout(this.connectTimeout); devClientHttpRequestFactory.setReadTimeout(this.readTimeout); diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/proctoring/ScreenProctoringAPIBinding.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/proctoring/ScreenProctoringAPIBinding.java index 4b2d22efe..3ea9dde19 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/proctoring/ScreenProctoringAPIBinding.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/proctoring/ScreenProctoringAPIBinding.java @@ -11,7 +11,6 @@ import java.util.*; import ch.ethz.seb.sebserver.ClientHttpRequestFactoryService; -import ch.ethz.seb.sebserver.gbl.model.Page; import ch.ethz.seb.sebserver.gbl.model.exam.SPSAPIAccessData; import ch.ethz.seb.sebserver.gbl.model.user.UserRole; import ch.ethz.seb.sebserver.webservice.WebserviceInfo; @@ -27,6 +26,7 @@ import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; +import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.security.oauth2.client.OAuth2RestTemplate; import org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordResourceDetails; @@ -41,7 +41,6 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonNode; import ch.ethz.seb.sebserver.gbl.Constants; @@ -220,6 +219,7 @@ public GroupName( private final ProctoringSettingsDAO proctoringSettingsDAO; private final AdditionalAttributesDAO additionalAttributesDAO; private final ScreenProctoringGroupDAO screenProctoringGroupDAO; + private final ClientHttpRequestFactoryService clientHttpRequestFactoryService; private final WebserviceInfo webserviceInfo; ScreenProctoringAPIBinding( @@ -229,7 +229,8 @@ public GroupName( final JSONMapper jsonMapper, final ProctoringSettingsDAO proctoringSettingsDAO, final AdditionalAttributesDAO additionalAttributesDAO, - final ScreenProctoringGroupDAO screenProctoringGroupDAO, + final ScreenProctoringGroupDAO screenProctoringGroupDAO, + final ClientHttpRequestFactoryService clientHttpRequestFactoryService, final WebserviceInfo webserviceInfo) { this.userDAO = userDAO; @@ -239,6 +240,7 @@ public GroupName( this.proctoringSettingsDAO = proctoringSettingsDAO; this.additionalAttributesDAO = additionalAttributesDAO; this.screenProctoringGroupDAO = screenProctoringGroupDAO; + this.clientHttpRequestFactoryService = clientHttpRequestFactoryService; this.webserviceInfo = webserviceInfo; } @@ -1400,12 +1402,7 @@ final static class ScreenProctoringServiceOAuthTemplate { resource.setUsername(userCredentials.clientIdAsString()); resource.setPassword(decryptedSecret.toString()); - // TODO use overall HttpRequestFactory to avoid SSL issues - final SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); - requestFactory.setOutputStreaming(false); - final OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(resource); - oAuth2RestTemplate.setRequestFactory(requestFactory); - this.restTemplate = oAuth2RestTemplate; + this.restTemplate = sebScreenProctoringService.getOAuth2RestTemplate(resource); } ResponseEntity testServiceConnection() { @@ -1534,6 +1531,20 @@ ResponseEntity exchange( } } + private OAuth2RestTemplate getOAuth2RestTemplate(final ResourceOwnerPasswordResourceDetails resource) { + + final Result clientHttpRequestFactoryRequest = this.clientHttpRequestFactoryService + .getClientHttpRequestFactory(); + ClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); + if (!clientHttpRequestFactoryRequest.hasError()) { + requestFactory = clientHttpRequestFactoryRequest.get(); + } + + final OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(resource); + oAuth2RestTemplate.setRequestFactory(requestFactory); + return oAuth2RestTemplate; + } + @JsonIgnoreProperties(ignoreUnknown = true) static final class SPSData { diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/proctoring/ScreenProctoringServiceImpl.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/proctoring/ScreenProctoringServiceImpl.java index 6a75fa913..fa845693f 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/proctoring/ScreenProctoringServiceImpl.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/proctoring/ScreenProctoringServiceImpl.java @@ -14,6 +14,7 @@ import java.util.function.Function; import java.util.stream.Collectors; +import ch.ethz.seb.sebserver.ClientHttpRequestFactoryService; import ch.ethz.seb.sebserver.gbl.async.AsyncServiceSpringConfig; import ch.ethz.seb.sebserver.gbl.model.Activatable; import ch.ethz.seb.sebserver.gbl.model.EntityKey; @@ -83,7 +84,8 @@ public ScreenProctoringServiceImpl( final ScreenProctoringGroupDAO screenProctoringGroupDAO, final SEBClientInstructionService sebInstructionService, final ExamSessionCacheService examSessionCacheService, - final WebserviceInfo webserviceInfo) { + final WebserviceInfo webserviceInfo, + final ClientHttpRequestFactoryService clientHttpRequestFactoryService) { this.cryptor = cryptor; this.examDAO = examDAO; @@ -103,6 +105,7 @@ public ScreenProctoringServiceImpl( proctoringSettingsDAO, additionalAttributesDAO, screenProctoringGroupDAO, + clientHttpRequestFactoryService, webserviceInfo); }