2 * ============LICENSE_START======================================================================
3 * Copyright (C) 2020-2021 Nokia. All rights reserved.
4 * Copyright (C) 2023 Nordix Foundation.
5 * ===============================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7 * in compliance with the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software distributed under the License
12 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13 * or implied. See the License for the specific language governing permissions and limitations under
15 * ============LICENSE_END========================================================================
17 package org.oran.datafile.http;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.nio.file.Files;
22 import java.nio.file.Path;
23 import java.util.concurrent.CountDownLatch;
24 import java.util.concurrent.atomic.AtomicReference;
25 import java.util.function.Consumer;
27 import org.oran.datafile.commons.FileCollectClient;
28 import org.oran.datafile.exceptions.DatafileTaskException;
29 import org.oran.datafile.exceptions.NonRetryableDatafileTaskException;
30 import org.oran.datafile.model.FileServerData;
31 import org.oran.datafile.oauth2.SecurityContext;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import reactor.core.Disposable;
36 import reactor.core.publisher.Flux;
37 import reactor.core.publisher.Mono;
38 import reactor.netty.http.client.HttpClient;
39 import reactor.netty.http.client.HttpClientResponse;
40 import reactor.netty.resources.ConnectionProvider;
43 * Gets file from PNF with HTTP protocol.
46 public class DfcHttpClient implements FileCollectClient {
48 // Be aware to be less than ScheduledTasks.NUMBER_OF_WORKER_THREADS
49 private static final int MAX_NUMBER_OF_CONNECTIONS = 200;
50 private static final Logger logger = LoggerFactory.getLogger(DfcHttpClient.class);
51 private static final ConnectionProvider pool = ConnectionProvider.create("default", MAX_NUMBER_OF_CONNECTIONS);
53 private final FileServerData fileServerData;
54 private Disposable disposableClient;
56 protected HttpClient client;
57 private final SecurityContext securityContext;
59 public DfcHttpClient(SecurityContext securityContext, FileServerData fileServerData) {
60 this.fileServerData = fileServerData;
61 this.securityContext = securityContext;
65 public void open() throws DatafileTaskException {
66 logger.trace("Setting httpClient for file download.");
68 final String authorizationContent = this.securityContext.getBearerAuthToken();
69 this.client = HttpClient.create(pool).keepAlive(true);
70 if (!authorizationContent.isEmpty()) {
71 this.client = this.client.headers(h -> h.add("Authorization", "Bearer " + authorizationContent));
72 logger.trace("httpClient, auth header was set.");
73 } else if (!this.fileServerData.password.isEmpty()) {
74 String basicAuthContent =
75 HttpUtils.basicAuthContent(this.fileServerData.userId, this.fileServerData.password);
76 this.client = this.client.headers(h -> h.add("Authorization", basicAuthContent));
81 public void collectFile(String remoteFile, Path localFile) throws DatafileTaskException {
82 logger.trace("Prepare to collectFile {}", localFile);
83 CountDownLatch latch = new CountDownLatch(1);
84 final AtomicReference<Exception> errorMessage = new AtomicReference<>();
86 Consumer<Throwable> onError = processFailedConnectionWithServer(latch, errorMessage);
87 Consumer<InputStream> onSuccess = processDataFromServer(localFile, latch, errorMessage);
89 Flux<InputStream> responseContent = getServerResponse(remoteFile);
90 disposableClient = responseContent.subscribe(onSuccess, onError);
94 } catch (InterruptedException e) {
95 throw new DatafileTaskException("Interrupted exception after datafile download - ", e);
98 if (isDownloadFailed(errorMessage)) {
99 if (errorMessage.get() instanceof NonRetryableDatafileTaskException) {
100 throw (NonRetryableDatafileTaskException) errorMessage.get();
102 throw (DatafileTaskException) errorMessage.get();
105 logger.trace("HTTP collectFile OK");
108 protected boolean isDownloadFailed(AtomicReference<Exception> errorMessage) {
109 return (errorMessage.get() != null);
112 protected Consumer<Throwable> processFailedConnectionWithServer(CountDownLatch latch,
113 AtomicReference<Exception> errorMessages) {
114 return (Throwable response) -> {
115 Exception e = new Exception("Error in connection has occurred during file download", response);
116 errorMessages.set(new DatafileTaskException(response.getMessage(), e));
117 if (response instanceof NonRetryableDatafileTaskException) {
118 errorMessages.set(new NonRetryableDatafileTaskException(response.getMessage(), e));
124 protected Consumer<InputStream> processDataFromServer(Path localFile, CountDownLatch latch,
125 AtomicReference<Exception> errorMessages) {
126 return (InputStream response) -> {
127 logger.trace("Starting to process response.");
129 long numBytes = Files.copy(response, localFile);
130 logger.trace("Transmission was successful - {} bytes downloaded.", numBytes);
131 logger.trace("CollectFile fetched: {}", localFile);
133 } catch (IOException e) {
134 errorMessages.set(new DatafileTaskException("Error fetching file with", e));
141 protected Flux<InputStream> getServerResponse(String remoteFile) {
142 return client.get().uri(HttpUtils.prepareHttpUri(fileServerData, remoteFile))
143 .response((responseReceiver, byteBufFlux) -> {
144 logger.trace("HTTP response status - {}", responseReceiver.status());
145 if (isResponseOk(responseReceiver)) {
146 return byteBufFlux.aggregate().asInputStream();
148 if (isErrorInConnection(responseReceiver)) {
149 return Mono.error(new NonRetryableDatafileTaskException(
150 HttpUtils.nonRetryableResponse(getResponseCode(responseReceiver))));
153 .error(new DatafileTaskException(HttpUtils.retryableResponse(getResponseCode(responseReceiver))));
157 protected boolean isResponseOk(HttpClientResponse httpClientResponse) {
158 return getResponseCode(httpClientResponse) == 200;
161 private int getResponseCode(HttpClientResponse responseReceiver) {
162 return responseReceiver.status().code();
165 protected boolean isErrorInConnection(HttpClientResponse httpClientResponse) {
166 return getResponseCode(httpClientResponse) >= 400;
170 public void close() {
171 logger.trace("Starting http client disposal.");
172 disposableClient.dispose();
173 logger.trace("Http client disposed.");