2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2019 IBM.
4 * Modifications Copyright © 2020 Orange.
5 * Modifications Copyright © 2020 Deutsche Telekom AG.
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.
20 package org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s
22 import org.apache.http.message.BasicHeader
23 import org.onap.ccsdk.cds.blueprintsprocessor.rest.BasicAuthRestClientProperties
24 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
25 import org.springframework.http.HttpHeaders.ACCEPT
26 import org.springframework.http.HttpHeaders.AUTHORIZATION
27 import org.springframework.http.HttpHeaders.CONTENT_TYPE
28 import org.springframework.http.MediaType.APPLICATION_JSON_VALUE
29 import java.nio.charset.Charset
30 import java.util.Base64
32 abstract class K8sAbstractRestClientService(val username: String, val password: String) : BlueprintWebClientService {
34 private val restClientProperties: BasicAuthRestClientProperties = getBasicAuthRestClientProperties()
36 private fun getBasicAuthRestClientProperties(): BasicAuthRestClientProperties {
37 val basicAuthRestClientProperties = BasicAuthRestClientProperties()
38 basicAuthRestClientProperties.username = username
39 basicAuthRestClientProperties.password = password
40 basicAuthRestClientProperties.url = apiUrl()
41 basicAuthRestClientProperties.additionalHeaders = getHeaders()
42 return basicAuthRestClientProperties
45 private fun getHeaders(): HashMap<String, String> {
46 val mapOfHeaders = hashMapOf<String, String>()
47 mapOfHeaders["Accept"] = "application/json"
48 mapOfHeaders["Content-Type"] = "application/json"
49 mapOfHeaders["cache-control"] = " no-cache"
50 mapOfHeaders["Accept"] = "application/json"
54 private fun setBasicAuth(username: String, password: String): String {
55 val credentialsString = "$username:$password"
56 return Base64.getEncoder().encodeToString(credentialsString.toByteArray(Charset.defaultCharset()))
59 override fun defaultHeaders(): Map<String, String> {
60 val encodedCredentials = setBasicAuth(
61 restClientProperties.username,
62 restClientProperties.password
65 CONTENT_TYPE to APPLICATION_JSON_VALUE,
66 ACCEPT to APPLICATION_JSON_VALUE,
67 AUTHORIZATION to "Basic $encodedCredentials"
71 override fun host(uri: String): String {
72 return restClientProperties.url + uri
75 override fun convertToBasicHeaders(headers: Map<String, String>): Array<BasicHeader> {
76 val customHeaders: MutableMap<String, String> = headers.toMutableMap()
77 // inject additionalHeaders
78 customHeaders.putAll(verifyAdditionalHeaders(restClientProperties))
80 if (!headers.containsKey(AUTHORIZATION)) {
81 val encodedCredentials = setBasicAuth(
82 restClientProperties.username,
83 restClientProperties.password
85 customHeaders[AUTHORIZATION] = "Basic $encodedCredentials"
87 return super.convertToBasicHeaders(customHeaders)
90 abstract fun apiUrl(): String