more code cleaup

This commit is contained in:
anhefti 2021-08-12 11:10:58 +02:00
parent d1685f4675
commit dea65b70d2
2 changed files with 55 additions and 54 deletions

View file

@ -11,22 +11,22 @@ package ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.olat;
import java.util.List; import java.util.List;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
public final class OlatLmsData { public final class OlatLmsData {
@JsonIgnoreProperties(ignoreUnknown = true) @JsonIgnoreProperties(ignoreUnknown = true)
static public final class AssessmentData { static public final class AssessmentData {
/* OLAT API example: /*
{ * OLAT API example:
"courseName": "course 1", * {
"dateFrom": 1624420800000, * "courseName": "course 1",
"dateTo": 1624658400000, * "dateFrom": 1624420800000,
"description": "", * "dateTo": 1624658400000,
"key": 6356992, * "description": "",
repositoryEntryKey: 462324, * "key": 6356992,
"name": "SEB test" * repositoryEntryKey: 462324,
} * "name": "SEB test"
* }
*/ */
public long key; public long key;
public long repositoryEntryKey; public long repositoryEntryKey;
@ -39,13 +39,14 @@ public final class OlatLmsData {
@JsonIgnoreProperties(ignoreUnknown = true) @JsonIgnoreProperties(ignoreUnknown = true)
static final class UserData { static final class UserData {
/* OLAT API example: /*
{ * OLAT API example:
"firstName": "OpenOLAT", * {
"key": 360448, * "firstName": "OpenOLAT",
"lastName": "Administrator", * "key": 360448,
"username": "administrator" * "lastName": "Administrator",
} * "username": "administrator"
* }
*/ */
public long key; public long key;
public String firstName; public String firstName;
@ -55,12 +56,13 @@ public final class OlatLmsData {
@JsonIgnoreProperties(ignoreUnknown = true) @JsonIgnoreProperties(ignoreUnknown = true)
static final class RestrictionData { static final class RestrictionData {
/* OLAT API example: /*
{ * OLAT API example:
"browserExamKeys": [ "1" ], * {
"configKeys": null, * "browserExamKeys": [ "1" ],
"key": 8028160 * "configKeys": null,
} * "key": 8028160
* }
*/ */
public long key; public long key;
public List<String> browserExamKeys; public List<String> browserExamKeys;
@ -69,16 +71,15 @@ public final class OlatLmsData {
@JsonIgnoreProperties(ignoreUnknown = true) @JsonIgnoreProperties(ignoreUnknown = true)
static final class RestrictionDataPost { static final class RestrictionDataPost {
/* OLAT API example: /*
{ * OLAT API example:
"configKeys": ["a", "b"], * {
"browserExamKeys": ["1", "2"] * "configKeys": ["a", "b"],
} * "browserExamKeys": ["1", "2"]
* }
*/ */
public List<String> browserExamKeys; public List<String> browserExamKeys;
public List<String> configKeys; public List<String> configKeys;
} }
} }

View file

@ -12,17 +12,15 @@ import java.io.IOException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 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.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpRequestExecution; import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse; 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 { public class OlatLmsRestTemplate extends RestTemplate {
@ -31,27 +29,32 @@ public class OlatLmsRestTemplate extends RestTemplate {
private String token; private String token;
private ClientCredentialsResourceDetails details; private ClientCredentialsResourceDetails details;
public OlatLmsRestTemplate(ClientCredentialsResourceDetails details) { public OlatLmsRestTemplate(final ClientCredentialsResourceDetails details) {
super(); super();
this.details = details; this.details = details;
// Add X-OLAT-TOKEN request header to every request done using this RestTemplate // Add X-OLAT-TOKEN request header to every request done using this RestTemplate
this.getInterceptors().add(new ClientHttpRequestInterceptor(){ this.getInterceptors().add(new ClientHttpRequestInterceptor() {
@Override @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 there's no token, authenticate first
if (token == null) { authenticate(); } if (OlatLmsRestTemplate.this.token == null) {
authenticate();
}
// when authenticating, just do a normal call // 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 // otherwise, add the X-OLAT-TOKEN
request.getHeaders().set("accept", "application/json"); 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); ClientHttpResponse response = execution.execute(request, body);
log.debug("OLAT [regular API call] {} Headers: {}", response.getStatusCode(), response.getHeaders()); log.debug("OLAT [regular API call] {} Headers: {}", response.getStatusCode(), response.getHeaders());
// If we get a 401, re-authenticate and try once more // If we get a 401, re-authenticate and try once more
if (response.getStatusCode() == HttpStatus.UNAUTHORIZED) { if (response.getStatusCode() == HttpStatus.UNAUTHORIZED) {
authenticate(); authenticate();
request.getHeaders().set("X-OLAT-TOKEN", token); request.getHeaders().set("X-OLAT-TOKEN", OlatLmsRestTemplate.this.token);
response = execution.execute(request, body); response = execution.execute(request, body);
log.debug("OLAT [retry API call] {} Headers: {}", response.getStatusCode(), response.getHeaders()); log.debug("OLAT [retry API call] {} Headers: {}", response.getStatusCode(), response.getHeaders());
} }
@ -62,23 +65,20 @@ public class OlatLmsRestTemplate extends RestTemplate {
private void authenticate() { private void authenticate() {
// Authenticate with OLAT and store the received X-OLAT-TOKEN // Authenticate with OLAT and store the received X-OLAT-TOKEN
token = "authenticating"; this.token = "authenticating";
final String authUrl = String.format("%s%s?password=%s", final String authUrl = String.format("%s%s?password=%s",
details.getAccessTokenUri(), this.details.getAccessTokenUri(),
details.getClientId(), this.details.getClientId(),
details.getClientSecret()); this.details.getClientSecret());
try { try {
final ResponseEntity<String> response = this.getForEntity(authUrl, String.class); final ResponseEntity<String> response = this.getForEntity(authUrl, String.class);
final HttpHeaders responseHeaders = response.getHeaders(); final HttpHeaders responseHeaders = response.getHeaders();
log.debug("OLAT [authenticate] {} Headers: {}", response.getStatusCode(), responseHeaders); log.debug("OLAT [authenticate] {} Headers: {}", response.getStatusCode(), responseHeaders);
token = responseHeaders.getFirst("X-OLAT-TOKEN"); this.token = responseHeaders.getFirst("X-OLAT-TOKEN");
} } catch (final Exception e) {
catch (Exception e) { this.token = null;
token = null;
throw e; throw e;
} }
} }
} }