X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=datafilecollector%2Fsrc%2Fmain%2Fjava%2Forg%2Foran%2Fdatafile%2Fhttp%2FDfcHttpClient.java;h=fd56ee9aa371e9f06bcc7992ecfebe48ffb892ea;hb=a93caaf7243d896161775d590f1ab322dd63ee16;hp=7c1ae929d3a5bd332b98097a9f7bed20f6130fe2;hpb=3a9d09f02a50cf1174a6e9ccf8e7d0af88a7ecf3;p=nonrtric%2Fplt%2Franpm.git diff --git a/datafilecollector/src/main/java/org/oran/datafile/http/DfcHttpClient.java b/datafilecollector/src/main/java/org/oran/datafile/http/DfcHttpClient.java index 7c1ae92..fd56ee9 100644 --- a/datafilecollector/src/main/java/org/oran/datafile/http/DfcHttpClient.java +++ b/datafilecollector/src/main/java/org/oran/datafile/http/DfcHttpClient.java @@ -1,6 +1,7 @@ /*- * ============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 @@ -27,6 +28,7 @@ import org.oran.datafile.commons.FileCollectClient; 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; @@ -52,31 +54,27 @@ public class DfcHttpClient implements FileCollectClient { 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 @@ -94,12 +92,13 @@ public class DfcHttpClient implements FileCollectClient { 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(); }