get OLAT token lazily when first needed, not upon instantiation
This commit is contained in:
parent
59da4bcf4e
commit
0029cd4ec3
1 changed files with 18 additions and 12 deletions
|
@ -34,25 +34,26 @@ public class OlatLmsRestTemplate extends RestTemplate {
|
||||||
public OlatLmsRestTemplate(ClientCredentialsResourceDetails details) {
|
public OlatLmsRestTemplate(ClientCredentialsResourceDetails details) {
|
||||||
super();
|
super();
|
||||||
this.details = details;
|
this.details = details;
|
||||||
authenticate();
|
|
||||||
|
|
||||||
// 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(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
|
||||||
request.getHeaders().set("accept", "application/json");
|
// if there's no token, authenticate first
|
||||||
// if we don't have a token (this is normal during authentication), just do the call
|
if (token == null) { authenticate(); }
|
||||||
if (token == null) { return execution.execute(request, body); }
|
// when authenticating, just do a normal call
|
||||||
|
else if (token == "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("X-OLAT-TOKEN", token);
|
request.getHeaders().set("X-OLAT-TOKEN", token);
|
||||||
ClientHttpResponse response = execution.execute(request, body);
|
ClientHttpResponse response = execution.execute(request, body);
|
||||||
log.debug("OLAT [regular API call] Response Headers: {}", 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", token);
|
||||||
response = execution.execute(request, body);
|
response = execution.execute(request, body);
|
||||||
log.debug("OLAT [retry API call] Response Headers: {}", response.getHeaders());
|
log.debug("OLAT [retry API call] {} Headers: {}", response.getStatusCode(), response.getHeaders());
|
||||||
}
|
}
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
@ -61,17 +62,22 @@ 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 = null;
|
token = "authenticating";
|
||||||
final String authUrl = String.format("%s%s?password=%s",
|
final String authUrl = String.format("%s%s?password=%s",
|
||||||
details.getAccessTokenUri(),
|
details.getAccessTokenUri(),
|
||||||
details.getClientId(),
|
details.getClientId(),
|
||||||
details.getClientSecret());
|
details.getClientSecret());
|
||||||
final HttpHeaders httpHeaders = new HttpHeaders();
|
try {
|
||||||
ResponseEntity<String> response = this.getForEntity(authUrl, String.class);
|
ResponseEntity<String> response = this.getForEntity(authUrl, String.class);
|
||||||
HttpHeaders responseHeaders = response.getHeaders();
|
HttpHeaders responseHeaders = response.getHeaders();
|
||||||
log.debug("OLAT [authenticate] Response Headers: {}", responseHeaders);
|
log.debug("OLAT [authenticate] {} Headers: {}", response.getStatusCode(), responseHeaders);
|
||||||
token = responseHeaders.getFirst("X-OLAT-TOKEN");
|
token = responseHeaders.getFirst("X-OLAT-TOKEN");
|
||||||
}
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
token = null;
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue