2 * ============LICENSE_START======================================================================
3 * Copyright (C) 2018-2023 Nordix Foundation. All rights reserved.
4 * Modifications Copyright (C) 2020-2021 Nokia. All rights reserved
5 * ===============================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END========================================================================
20 package org.oran.datafile.http;
22 import java.util.Base64;
23 import java.util.List;
24 import org.apache.hc.core5.http.NameValuePair;
25 import org.apache.http.HttpStatus;
26 import org.oran.datafile.model.FileServerData;
28 public final class HttpUtils implements HttpStatus {
30 public static final int HTTP_DEFAULT_PORT = 80;
31 public static final int HTTPS_DEFAULT_PORT = 443;
36 public static String nonRetryableResponse(int responseCode) {
37 return "Unexpected response code - " + responseCode;
40 public static String retryableResponse(int responseCode) {
41 return "Unexpected response code - " + responseCode + ". No retry attempts will be done.";
44 public static boolean isSuccessfulResponseCodeWithDataRouter(Integer statusCode) {
45 return statusCode >= 200 && statusCode < 300;
48 public static String basicAuthContent(String username, String password) {
49 return "Basic " + Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
53 * Prepare uri to retrieve file from xNF using HTTP connection. If JWT token was
55 * in the queryParameters, it is removed. Other entries are rewritten.
57 * @param fileServerData fileServerData including - server address, port,
58 * queryParameters and uriRawFragment
59 * @param remoteFile file which has to be downloaded
60 * @return uri String representing the xNF HTTP location
62 public static String prepareHttpUri(FileServerData fileServerData, String remoteFile) {
63 return prepareUri("http", fileServerData, remoteFile, HTTP_DEFAULT_PORT);
67 * Prepare uri to retrieve file from xNF using HTTPS connection. If JWT token
69 * in the queryParameters, it is removed. Other entries are rewritten.
71 * @param fileServerData fileServerData including - server address, port,
72 * queryParameters and uriRawFragment
73 * @param remoteFile file which has to be downloaded
74 * @return uri String representing the xNF HTTPS location
76 public static String prepareHttpsUri(FileServerData fileServerData, String remoteFile) {
77 return prepareUri("https", fileServerData, remoteFile, HTTPS_DEFAULT_PORT);
81 * Prepare uri to retrieve file from xNF. If JWT token was included
82 * in the queryParameters, it is removed. Other entries are rewritten.
84 * @param scheme scheme which is used during the connection
85 * @param fileServerData fileServerData including - server address, port, query
87 * @param remoteFile file which has to be downloaded
88 * @param defaultPort default port which exchange empty entry for given
90 * @return uri String representing the xNF location
92 public static String prepareUri(String scheme, FileServerData fileServerData, String remoteFile, int defaultPort) {
93 int port = fileServerData.port != null ? fileServerData.port : defaultPort;
94 String query = queryParametersAsString(fileServerData.queryParameters);
95 String fragment = fileServerData.uriRawFragment;
96 if (!query.isEmpty()) {
99 if (!fragment.isEmpty()) {
100 fragment = "#" + fragment;
102 return scheme + "://" + fileServerData.serverAddress + ":" + port + remoteFile + query + fragment;
108 * @param query list of NameValuePair of elements sent in the queryParameters
109 * @return String representation of queryParameters elements which were provided
111 * Empty string is possible when queryParameters is empty.
113 private static String queryParametersAsString(List<NameValuePair> query) {
114 if (query.isEmpty()) {
117 StringBuilder sb = new StringBuilder();
118 for (NameValuePair nvp : query) {
119 sb.append(nvp.getName());
120 if (nvp.getValue() != null) {
122 sb.append(nvp.getValue());
126 if ((sb.length() > 0) && (sb.charAt(sb.length() - 1) == '&')) {
127 sb.deleteCharAt(sb.length() - 1);
129 return sb.toString();