2 * ============LICENSE_START====================================
3 * DCAEGEN2-SERVICES-SDK
4 * =========================================================
5 * Copyright (C) 2019-2021 Nokia. All rights reserved.
6 * =========================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=====================================
21 package org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.impl;
23 import com.google.common.primitives.Bytes;
25 import io.vavr.Tuple2;
26 import io.vavr.control.Option;
27 import org.apache.commons.lang3.ArrayUtils;
28 import org.onap.dcaegen2.services.sdk.model.streams.AafCredentials;
29 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpResponse;
31 import java.nio.charset.Charset;
32 import java.nio.charset.StandardCharsets;
33 import java.util.Base64;
36 * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
44 static String extractFailReason(HttpResponse httpResponse) {
45 return String.format("%d %s%n%s", httpResponse.statusCode(), httpResponse.statusReason(),
46 httpResponse.bodyAsString());
49 static Tuple2<String, String> basicAuthHeader(AafCredentials credentials) {
50 Charset utf8 = StandardCharsets.UTF_8;
51 byte[] username = toBytes(credentials.username(), utf8);
52 byte[] separator = ":".getBytes(utf8);
53 byte[] password = toBytes(credentials.password(), utf8);
54 byte[] combined = ArrayUtils.addAll(Bytes.concat(username, separator, password));
55 String userCredentials = Base64.getEncoder().encodeToString(combined);
56 return Tuple.of("Authorization", "Basic " + userCredentials);
59 private static byte[] toBytes(String text, Charset charset) {
60 return Option.of(text)
61 .map(s -> s.getBytes(charset))
62 .getOrElse(new byte[0]);