aboutsummaryrefslogtreecommitdiffstats
path: root/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/service/HttpUtils.java
blob: 69bfcf47b73c607766740d775bfe12ef7002582e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*-
 * ============LICENSE_START======================================================================
 * Copyright (C) 2018-2019 Nordix Foundation. All rights reserved.
 * Modifications Copyright (C) 2020-2021 Nokia. All rights reserved
 * ===============================================================================================
 * 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
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END========================================================================
 */

package org.onap.dcaegen2.collectors.datafile.service;

import org.apache.hc.core5.http.NameValuePair;
import org.apache.http.HttpStatus;
import org.jetbrains.annotations.NotNull;
import org.onap.dcaegen2.collectors.datafile.commons.FileServerData;

import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class HttpUtils implements HttpStatus {

    private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class);
    public static final int HTTP_DEFAULT_PORT = 80;
    public static final int HTTPS_DEFAULT_PORT = 443;
    public static final String JWT_TOKEN_NAME = "access_token";
    public static final String AUTH_JWT_WARN = "Both JWT token and Basic auth data present. Omitting basic auth info.";
    public static final String AUTH_JWT_ERROR = "More than one JWT token present in the queryParameters. Omitting JWT token.";

    private HttpUtils() {
    }

    @NotNull
    public static String nonRetryableResponse(int responseCode) {
        return "Unexpected response code - " + responseCode;
    }

    @NotNull
    public static String retryableResponse(int responseCode) {
        return "Unexpected response code - " + responseCode + ". No retry attempts will be done.";
    }

    public static boolean isSuccessfulResponseCodeWithDataRouter(Integer statusCode) {
        return statusCode >= 200 && statusCode < 300;
    }

    public static boolean isBasicAuthDataFilled(final FileServerData fileServerData) {
        return !fileServerData.userId().isEmpty() && !fileServerData.password().isEmpty();
    }

    public static String basicAuthContent(String username, String password) {
        return "Basic " + Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
    }

    public static String jwtAuthContent(String token) {
        return "Bearer " + token;
    }

    /**
     * Prepare uri to retrieve file from xNF using HTTP connection. If JWT token was included
     * in the queryParameters, it is removed. Other entries are rewritten.
     *
     * @param fileServerData fileServerData including - server address, port, queryParameters and uriRawFragment
     * @param remoteFile file which has to be downloaded
     * @return uri String representing the xNF HTTP location
     */
    @NotNull public static String prepareHttpUri(FileServerData fileServerData, String remoteFile){
        return prepareUri("http", fileServerData, remoteFile, HTTP_DEFAULT_PORT);
    }

    /**
     * Prepare uri to retrieve file from xNF using HTTPS connection. If JWT token was included
     * in the queryParameters, it is removed. Other entries are rewritten.
     *
     * @param fileServerData fileServerData including - server address, port, queryParameters and uriRawFragment
     * @param remoteFile file which has to be downloaded
     * @return uri String representing the xNF HTTPS location
     */
    @NotNull public static String prepareHttpsUri(FileServerData fileServerData, String remoteFile){
        return prepareUri("https", fileServerData, remoteFile, HTTPS_DEFAULT_PORT);
    }

    /**
     * Prepare uri to retrieve file from xNF. If JWT token was included
     * in the queryParameters, it is removed. Other entries are rewritten.
     *
     * @param scheme scheme which is used during the connection
     * @param fileServerData fileServerData including - server address, port, query and fragment
     * @param remoteFile file which has to be downloaded
     * @param defaultPort default port which exchange empty entry for given connection type
     * @return uri String representing the xNF location
     */
    @NotNull public static String prepareUri(String scheme, FileServerData fileServerData, String remoteFile, int defaultPort) {
        int port = fileServerData.port().orElse(defaultPort);
        String query = rewriteQueryWithoutToken(fileServerData.queryParameters().orElse(new ArrayList<>()));
        String fragment = fileServerData.uriRawFragment().orElse("");
        if (!query.isEmpty()) {
            query = "?" + query;
        }
        if (!fragment.isEmpty()) {
            fragment = "#" + fragment;
        }
        return scheme + "://" + fileServerData.serverAddress() + ":" + port + remoteFile + query + fragment;
    }

    /**
     * Returns JWT token string (if single exist) from the queryParameters.
     *
     * @param fileServerData file server data which contain queryParameters where JWT token may exist
     * @return JWT token value if single token entry exist or empty string elsewhere.
     *         If JWT token key has no value, empty string will be returned.
     */
    public static String getJWTToken(FileServerData fileServerData) {
        Optional<List<NameValuePair>> query = fileServerData.queryParameters();
        if (!query.isPresent()) {
            return "";
        }
        List<NameValuePair> queryParameters = query.get();
        if (queryParameters.isEmpty()) {
            return "";
        }
        boolean jwtTokenKeyPresent = HttpUtils.isQueryWithSingleJWT(queryParameters);
        if (!jwtTokenKeyPresent) {
            return "";
        }
        String token = HttpUtils.getJWTToken(query.get());
        if (HttpUtils.isBasicAuthDataFilled(fileServerData)) {
            logger.warn(HttpUtils.AUTH_JWT_WARN);
        }
        return token;
    }

    /**
     * Checks if the queryParameters contains single JWT token entry. Valid queryParameters
     * contains only one token entry.
     *
     * @param query queryParameters
     * @return true if queryParameters contains single token
     */
    public static boolean isQueryWithSingleJWT(List<NameValuePair> query) {
        if (query == null) {
            return false;
        }
        int i = getJWTTokenCount(query);
        if (i == 0) {
            return false;
        }
        if (i > 1) {
            logger.error(AUTH_JWT_ERROR);
            return false;
        }
        return true;
    }

    /**
     * Returns the number of JWT token entries. Valid queryParameters contains only one token entry.
     *
     * @param queryElements elements of the queryParameters
     * @return true if queryParameters contains single JWT token entry
     */
    public static int getJWTTokenCount(List<NameValuePair> queryElements) {
        int i = 0;
        for (NameValuePair element : queryElements) {
            if (element.getName().equals(JWT_TOKEN_NAME)) {
                i++;
            }
        }
        return i;
    }

    private static String getJWTToken(List<NameValuePair> query) {
        for (NameValuePair element : query) {
            if (!element.getName().equals(JWT_TOKEN_NAME)) {
                continue;
            }
            if (element.getValue() != null) {
                return element.getValue();
            }
            return "";
        }
        return "";
    }

    /**
     * Rewrites HTTP queryParameters without JWT token
     *
     * @param query list of NameValuePair of elements sent in the queryParameters
     * @return String representation of queryParameters elements which were provided in the input
     *     Empty string is possible when queryParameters is empty or contains only access_token key.
     */
    public static String rewriteQueryWithoutToken(List<NameValuePair> query) {
        if (query.isEmpty()) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (NameValuePair nvp : query) {
            if (nvp.getName().equals(JWT_TOKEN_NAME)) {
                continue;
            }
            sb.append(nvp.getName());
            if (nvp.getValue() != null) {
                sb.append("=");
                sb.append(nvp.getValue());
            }
            sb.append("&");
        }
        if ((sb.length() > 0) && (sb.charAt(sb.length() - 1 ) == '&')) {
            sb.deleteCharAt(sb.length() - 1 );
        }
        return sb.toString();
    }
}