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 Thread.currentThread().interrupt();
96 throw new DatafileTaskException("Interrupted exception after datafile download - ", e);
99 if (isDownloadFailed(errorMessage)) {
100 if (errorMessage.get() instanceof NonRetryableDatafileTaskException nonRetryableException) {
101 throw nonRetryableException;
103 throw (DatafileTaskException) errorMessage.get();
106 logger.trace("HTTP collectFile OK");
109 protected boolean isDownloadFailed(AtomicReference<Exception> errorMessage) {
110 return (errorMessage.get() != null);
113 protected Consumer<Throwable> processFailedConnectionWithServer(CountDownLatch latch,
114 AtomicReference<Exception> errorMessages) {
115 return (Throwable response) -> {
116 Exception e = new Exception("Error in connection has occurred during file download", response);
117 errorMessages.set(new DatafileTaskException(response.getMessage(), e));
118 if (response instanceof NonRetryableDatafileTaskException) {
119 errorMessages.set(new NonRetryableDatafileTaskException(response.getMessage(), e));
125 protected Consumer<InputStream> processDataFromServer(Path localFile, CountDownLatch latch,
126 AtomicReference<Exception> errorMessages) {
127 return (InputStream response) -> {
128 logger.trace("Starting to process response.");
130 long numBytes = Files.copy(response, localFile);
131 logger.trace("Transmission was successful - {} bytes downloaded.", numBytes);
132 logger.trace("CollectFile fetched: {}", localFile);
134 } catch (IOException e) {
135 errorMessages.set(new DatafileTaskException("Error fetching file with", e));
142 protected Flux<InputStream> getServerResponse(String remoteFile) {
143 return client.get().uri(HttpUtils.prepareHttpUri(fileServerData, remoteFile))
144 .response((responseReceiver, byteBufFlux) -> {
145 logger.trace("HTTP response status - {}", responseReceiver.status());
146 if (isResponseOk(responseReceiver)) {
147 return byteBufFlux.aggregate().asInputStream();
149 if (isErrorInConnection(responseReceiver)) {
150 return Mono.error(new NonRetryableDatafileTaskException(
151 HttpUtils.nonRetryableResponse(getResponseCode(responseReceiver))));
154 .error(new DatafileTaskException(HttpUtils.retryableResponse(getResponseCode(responseReceiver))));
158 protected boolean isResponseOk(HttpClientResponse httpClientResponse) {
159 return getResponseCode(httpClientResponse) == 200;
162 private int getResponseCode(HttpClientResponse responseReceiver) {
163 return responseReceiver.status().code();
166 protected boolean isErrorInConnection(HttpClientResponse httpClientResponse) {
167 return getResponseCode(httpClientResponse) >= 400;
171 public void close() {
172 logger.trace("Starting http client disposal.");
173 disposableClient.dispose();
174 logger.trace("Http client disposed.");