diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/olat/OlatLmsData.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/olat/OlatLmsData.java index 9f7dff08..7aab3e28 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/olat/OlatLmsData.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/olat/OlatLmsData.java @@ -11,22 +11,22 @@ package ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.olat; import java.util.List; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonProperty; public final class OlatLmsData { @JsonIgnoreProperties(ignoreUnknown = true) static public final class AssessmentData { - /* OLAT API example: - { - "courseName": "course 1", - "dateFrom": 1624420800000, - "dateTo": 1624658400000, - "description": "", - "key": 6356992, - “repositoryEntryKey”: 462324, - "name": "SEB test" - } + /* + * OLAT API example: + * { + * "courseName": "course 1", + * "dateFrom": 1624420800000, + * "dateTo": 1624658400000, + * "description": "", + * "key": 6356992, + * “repositoryEntryKey”: 462324, + * "name": "SEB test" + * } */ public long key; public long repositoryEntryKey; @@ -39,13 +39,14 @@ public final class OlatLmsData { @JsonIgnoreProperties(ignoreUnknown = true) static final class UserData { - /* OLAT API example: - { - "firstName": "OpenOLAT", - "key": 360448, - "lastName": "Administrator", - "username": "administrator" - } + /* + * OLAT API example: + * { + * "firstName": "OpenOLAT", + * "key": 360448, + * "lastName": "Administrator", + * "username": "administrator" + * } */ public long key; public String firstName; @@ -55,12 +56,13 @@ public final class OlatLmsData { @JsonIgnoreProperties(ignoreUnknown = true) static final class RestrictionData { - /* OLAT API example: - { - "browserExamKeys": [ "1" ], - "configKeys": null, - "key": 8028160 - } + /* + * OLAT API example: + * { + * "browserExamKeys": [ "1" ], + * "configKeys": null, + * "key": 8028160 + * } */ public long key; public List browserExamKeys; @@ -69,16 +71,15 @@ public final class OlatLmsData { @JsonIgnoreProperties(ignoreUnknown = true) static final class RestrictionDataPost { - /* OLAT API example: - { - "configKeys": ["a", "b"], - "browserExamKeys": ["1", "2"] - } + /* + * OLAT API example: + * { + * "configKeys": ["a", "b"], + * "browserExamKeys": ["1", "2"] + * } */ public List browserExamKeys; public List configKeys; } } - - diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/olat/OlatLmsRestTemplate.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/olat/OlatLmsRestTemplate.java index f270a648..6c326124 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/olat/OlatLmsRestTemplate.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/olat/OlatLmsRestTemplate.java @@ -12,17 +12,15 @@ import java.io.IOException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - -import org.springframework.web.client.RestTemplate; -import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpStatus; -import org.springframework.http.HttpRequest; import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpRequest; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.http.client.ClientHttpRequestExecution; import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpResponse; +import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails; +import org.springframework.web.client.RestTemplate; public class OlatLmsRestTemplate extends RestTemplate { @@ -31,27 +29,32 @@ public class OlatLmsRestTemplate extends RestTemplate { private String token; private ClientCredentialsResourceDetails details; - public OlatLmsRestTemplate(ClientCredentialsResourceDetails details) { + public OlatLmsRestTemplate(final ClientCredentialsResourceDetails details) { super(); this.details = details; // Add X-OLAT-TOKEN request header to every request done using this RestTemplate - this.getInterceptors().add(new ClientHttpRequestInterceptor(){ + this.getInterceptors().add(new ClientHttpRequestInterceptor() { @Override - public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { + public ClientHttpResponse intercept(final HttpRequest request, final byte[] body, + final ClientHttpRequestExecution execution) throws IOException { // if there's no token, authenticate first - if (token == null) { authenticate(); } + if (OlatLmsRestTemplate.this.token == null) { + authenticate(); + } // when authenticating, just do a normal call - else if (token.equals("authenticating")) { return execution.execute(request, body); } + else if (OlatLmsRestTemplate.this.token.equals("authenticating")) { + return execution.execute(request, body); + } // otherwise, add the X-OLAT-TOKEN request.getHeaders().set("accept", "application/json"); - request.getHeaders().set("X-OLAT-TOKEN", token); + request.getHeaders().set("X-OLAT-TOKEN", OlatLmsRestTemplate.this.token); ClientHttpResponse response = execution.execute(request, body); log.debug("OLAT [regular API call] {} Headers: {}", response.getStatusCode(), response.getHeaders()); // If we get a 401, re-authenticate and try once more if (response.getStatusCode() == HttpStatus.UNAUTHORIZED) { authenticate(); - request.getHeaders().set("X-OLAT-TOKEN", token); + request.getHeaders().set("X-OLAT-TOKEN", OlatLmsRestTemplate.this.token); response = execution.execute(request, body); log.debug("OLAT [retry API call] {} Headers: {}", response.getStatusCode(), response.getHeaders()); } @@ -62,23 +65,20 @@ public class OlatLmsRestTemplate extends RestTemplate { private void authenticate() { // Authenticate with OLAT and store the received X-OLAT-TOKEN - token = "authenticating"; + this.token = "authenticating"; final String authUrl = String.format("%s%s?password=%s", - details.getAccessTokenUri(), - details.getClientId(), - details.getClientSecret()); + this.details.getAccessTokenUri(), + this.details.getClientId(), + this.details.getClientSecret()); try { final ResponseEntity response = this.getForEntity(authUrl, String.class); final HttpHeaders responseHeaders = response.getHeaders(); log.debug("OLAT [authenticate] {} Headers: {}", response.getStatusCode(), responseHeaders); - token = responseHeaders.getFirst("X-OLAT-TOKEN"); - } - catch (Exception e) { - token = null; + this.token = responseHeaders.getFirst("X-OLAT-TOKEN"); + } catch (final Exception e) { + this.token = null; throw e; } } - } -