X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=datafilecollector%2Fsrc%2Fmain%2Fjava%2Forg%2Foran%2Fdatafile%2Fhttp%2FDfcHttpsClient.java;h=5cd0a3185576194988c32b833c74c5d70dce042e;hb=69316aa635c8f28557a38aab90e3362295d0725f;hp=fde36d21147ccd1cd7ba10b3d27c9c1d98b790f9;hpb=f0af18429aec79a590835103fedd753ee5ea93a9;p=nonrtric%2Fplt%2Franpm.git diff --git a/datafilecollector/src/main/java/org/oran/datafile/http/DfcHttpsClient.java b/datafilecollector/src/main/java/org/oran/datafile/http/DfcHttpsClient.java index fde36d2..5cd0a31 100644 --- a/datafilecollector/src/main/java/org/oran/datafile/http/DfcHttpsClient.java +++ b/datafilecollector/src/main/java/org/oran/datafile/http/DfcHttpsClient.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START====================================================================== * Copyright (C) 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 @@ -38,10 +39,10 @@ import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.util.EntityUtils; import org.oran.datafile.commons.FileCollectClient; -import org.oran.datafile.commons.FileServerData; import org.oran.datafile.exceptions.DatafileTaskException; import org.oran.datafile.exceptions.NonRetryableDatafileTaskException; -import org.oran.datafile.service.HttpUtils; +import org.oran.datafile.model.FileServerData; +import org.oran.datafile.oauth2.SecurityContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -58,10 +59,13 @@ public class DfcHttpsClient implements FileCollectClient { private final FileServerData fileServerData; private final PoolingHttpClientConnectionManager connectionManager; + private final SecurityContext securityContext; - public DfcHttpsClient(FileServerData fileServerData, PoolingHttpClientConnectionManager connectionManager) { + public DfcHttpsClient(SecurityContext securityContext, FileServerData fileServerData, + PoolingHttpClientConnectionManager connectionManager) { this.fileServerData = fileServerData; this.connectionManager = connectionManager; + this.securityContext = securityContext; } @Override @@ -82,8 +86,11 @@ public class DfcHttpsClient implements FileCollectClient { logger.trace("Prepare to collectFile {}", localFile); HttpGet httpGet = new HttpGet(HttpUtils.prepareHttpsUri(fileServerData, remoteFile)); - String authorizationContent = getAuthorizationContent(); + String authorizationContent = this.securityContext.getBearerAuthToken(); if (!authorizationContent.isEmpty()) { + httpGet.addHeader("Authorization", "Bearer " + authorizationContent); + } else if (!this.fileServerData.password.isEmpty()) { + authorizationContent = HttpUtils.basicAuthContent(this.fileServerData.userId, this.fileServerData.password); httpGet.addHeader("Authorization", authorizationContent); } try { @@ -96,33 +103,7 @@ public class DfcHttpsClient implements FileCollectClient { logger.trace("HTTPS collectFile OK"); } - private String getAuthorizationContent() throws DatafileTaskException { - String jwtToken = HttpUtils.getJWTToken(fileServerData); - if (shouldUseBasicAuth(jwtToken)) { - return HttpUtils.basicAuthContent(this.fileServerData.userId, this.fileServerData.password); - } - return HttpUtils.jwtAuthContent(jwtToken); - } - - private boolean shouldUseBasicAuth(String jwtToken) throws DatafileTaskException { - return basicAuthValidNotPresentOrThrow() && jwtToken.isEmpty(); - } - - protected boolean basicAuthValidNotPresentOrThrow() throws DatafileTaskException { - if (isAuthDataEmpty()) { - return false; - } - if (HttpUtils.isBasicAuthDataFilled(fileServerData)) { - return true; - } - throw new DatafileTaskException("Not sufficient basic auth data for file."); - } - - private boolean isAuthDataEmpty() { - return this.fileServerData.userId.isEmpty() && this.fileServerData.password.isEmpty(); - } - - protected HttpResponse makeCall(HttpGet httpGet) throws IOException, DatafileTaskException { + HttpResponse makeCall(HttpGet httpGet) throws IOException, DatafileTaskException { try { HttpResponse httpResponse = executeHttpClient(httpGet); if (isResponseOk(httpResponse)) { @@ -144,11 +125,11 @@ public class DfcHttpsClient implements FileCollectClient { } } - protected CloseableHttpResponse executeHttpClient(HttpGet httpGet) throws IOException { + CloseableHttpResponse executeHttpClient(HttpGet httpGet) throws IOException { return httpsClient.execute(httpGet); } - protected boolean isResponseOk(HttpResponse httpResponse) { + boolean isResponseOk(HttpResponse httpResponse) { return getResponseCode(httpResponse) == 200; } @@ -156,11 +137,11 @@ public class DfcHttpsClient implements FileCollectClient { return httpResponse.getStatusLine().getStatusCode(); } - protected boolean isErrorInConnection(HttpResponse httpResponse) { + boolean isErrorInConnection(HttpResponse httpResponse) { return getResponseCode(httpResponse) >= 400; } - protected void processResponse(HttpResponse response, Path localFile) throws IOException { + void processResponse(HttpResponse response, Path localFile) throws IOException { logger.trace("Starting to process response."); HttpEntity entity = response.getEntity(); InputStream stream = entity.getContent(); @@ -170,7 +151,7 @@ public class DfcHttpsClient implements FileCollectClient { logger.trace("Transmission was successful - {} bytes downloaded.", numBytes); } - protected long writeFile(Path localFile, InputStream stream) throws IOException { + long writeFile(Path localFile, InputStream stream) throws IOException { return Files.copy(stream, localFile, StandardCopyOption.REPLACE_EXISTING); }