Skip to content

Commit

Permalink
SEBSERV-601 improved http client handling
Browse files Browse the repository at this point in the history
  • Loading branch information
anhefti committed Oct 31, 2024
1 parent 3e5ff42 commit 6b552b9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -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;
Expand All @@ -239,6 +240,7 @@ public GroupName(
this.proctoringSettingsDAO = proctoringSettingsDAO;
this.additionalAttributesDAO = additionalAttributesDAO;
this.screenProctoringGroupDAO = screenProctoringGroupDAO;
this.clientHttpRequestFactoryService = clientHttpRequestFactoryService;
this.webserviceInfo = webserviceInfo;
}

Expand Down Expand Up @@ -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<String> testServiceConnection() {
Expand Down Expand Up @@ -1534,6 +1531,20 @@ ResponseEntity<String> exchange(
}
}

private OAuth2RestTemplate getOAuth2RestTemplate(final ResourceOwnerPasswordResourceDetails resource) {

final Result<ClientHttpRequestFactory> 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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -103,6 +105,7 @@ public ScreenProctoringServiceImpl(
proctoringSettingsDAO,
additionalAttributesDAO,
screenProctoringGroupDAO,
clientHttpRequestFactoryService,
webserviceInfo);
}

Expand Down

0 comments on commit 6b552b9

Please sign in to comment.