/*-
* ============LICENSE_START======================================================================
* Copyright (C) 2020-2021 Nokia. All rights reserved.
+ * Copyright (C) 2023 Nordix Foundation.
* ===============================================================================================
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
import org.oran.datafile.exceptions.DatafileTaskException;
import org.oran.datafile.exceptions.NonRetryableDatafileTaskException;
import org.oran.datafile.model.FileServerData;
+import org.oran.datafile.oauth2.SecurityContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private Disposable disposableClient;
protected HttpClient client;
+ private final SecurityContext securityContext;
- public DfcHttpClient(FileServerData fileServerData) {
+ public DfcHttpClient(SecurityContext securityContext, FileServerData fileServerData) {
this.fileServerData = fileServerData;
+ this.securityContext = securityContext;
}
@Override
public void open() throws DatafileTaskException {
logger.trace("Setting httpClient for file download.");
- String authorizationContent = getAuthorizationContent();
- this.client =
- HttpClient.create(pool).keepAlive(true).headers(h -> h.add("Authorization", authorizationContent));
-
- logger.trace("httpClient, auth header was set.");
- }
-
- protected String getAuthorizationContent() throws DatafileTaskException {
- String jwtToken = HttpUtils.getJWTToken(fileServerData);
- if (!jwtToken.isEmpty()) {
- return HttpUtils.jwtAuthContent(jwtToken);
- }
- if (!HttpUtils.isBasicAuthDataFilled(fileServerData)) {
- throw new DatafileTaskException("Not sufficient basic auth data for file.");
+ final String authorizationContent = this.securityContext.getBearerAuthToken();
+ this.client = HttpClient.create(pool).keepAlive(true);
+ if (!authorizationContent.isEmpty()) {
+ this.client = this.client.headers(h -> h.add("Authorization", "Bearer " + authorizationContent));
+ logger.trace("httpClient, auth header was set.");
+ } else if (!this.fileServerData.password.isEmpty()) {
+ String basicAuthContent =
+ HttpUtils.basicAuthContent(this.fileServerData.userId, this.fileServerData.password);
+ this.client = this.client.headers(h -> h.add("Authorization", basicAuthContent));
}
- return HttpUtils.basicAuthContent(this.fileServerData.userId, this.fileServerData.password);
}
@Override
try {
latch.await();
} catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
throw new DatafileTaskException("Interrupted exception after datafile download - ", e);
}
if (isDownloadFailed(errorMessage)) {
- if (errorMessage.get() instanceof NonRetryableDatafileTaskException) {
- throw (NonRetryableDatafileTaskException) errorMessage.get();
+ if (errorMessage.get() instanceof NonRetryableDatafileTaskException nonRetryableException) {
+ throw nonRetryableException;
}
throw (DatafileTaskException) errorMessage.get();
}