Improve Test coverage of DFC
[nonrtric/plt/ranpm.git] / datafilecollector / src / main / java / org / oran / datafile / http / HttpUtils.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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========================================================================
18  */
19
20 package org.oran.datafile.http;
21
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;
27
28 public final class HttpUtils implements HttpStatus {
29
30     public static final int HTTP_DEFAULT_PORT = 80;
31     public static final int HTTPS_DEFAULT_PORT = 443;
32
33     private HttpUtils() {
34     }
35
36     public static String nonRetryableResponse(int responseCode) {
37         return "Unexpected response code - " + responseCode;
38     }
39
40     public static String retryableResponse(int responseCode) {
41         return "Unexpected response code - " + responseCode + ". No retry attempts will be done.";
42     }
43
44     public static boolean isSuccessfulResponseCodeWithDataRouter(Integer statusCode) {
45         return statusCode >= 200 && statusCode < 300;
46     }
47
48     public static String basicAuthContent(String username, String password) {
49         return "Basic " + Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
50     }
51
52     /**
53      * Prepare uri to retrieve file from xNF using HTTP connection. If JWT token was
54      * included
55      * in the queryParameters, it is removed. Other entries are rewritten.
56      *
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
61      */
62     public static String prepareHttpUri(FileServerData fileServerData, String remoteFile) {
63         return prepareUri("http", fileServerData, remoteFile, HTTP_DEFAULT_PORT);
64     }
65
66     /**
67      * Prepare uri to retrieve file from xNF using HTTPS connection. If JWT token
68      * was included
69      * in the queryParameters, it is removed. Other entries are rewritten.
70      *
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
75      */
76     public static String prepareHttpsUri(FileServerData fileServerData, String remoteFile) {
77         return prepareUri("https", fileServerData, remoteFile, HTTPS_DEFAULT_PORT);
78     }
79
80     /**
81      * Prepare uri to retrieve file from xNF. If JWT token was included
82      * in the queryParameters, it is removed. Other entries are rewritten.
83      *
84      * @param scheme scheme which is used during the connection
85      * @param fileServerData fileServerData including - server address, port, query
86      *        and fragment
87      * @param remoteFile file which has to be downloaded
88      * @param defaultPort default port which exchange empty entry for given
89      *        connection type
90      * @return uri String representing the xNF location
91      */
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()) {
97             query = "?" + query;
98         }
99         if (!fragment.isEmpty()) {
100             fragment = "#" + fragment;
101         }
102         return scheme + "://" + fileServerData.serverAddress + ":" + port + remoteFile + query + fragment;
103     }
104
105     /**
106      *
107      *
108      * @param query list of NameValuePair of elements sent in the queryParameters
109      * @return String representation of queryParameters elements which were provided
110      *         in the input
111      *         Empty string is possible when queryParameters is empty.
112      */
113     private static String queryParametersAsString(List<NameValuePair> query) {
114         if (query.isEmpty()) {
115             return "";
116         }
117         StringBuilder sb = new StringBuilder();
118         for (NameValuePair nvp : query) {
119             sb.append(nvp.getName());
120             if (nvp.getValue() != null) {
121                 sb.append("=");
122                 sb.append(nvp.getValue());
123             }
124             sb.append("&");
125         }
126         if ((sb.length() > 0) && (sb.charAt(sb.length() - 1) == '&')) {
127             sb.deleteCharAt(sb.length() - 1);
128         }
129         return sb.toString();
130     }
131 }