2 * Copyright © 2017-2019 AT&T, Bell Canada, Nordix Foundation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.ccsdk.cds.blueprintsprocessor.rest.service
19 import org.apache.http.conn.ssl.NoopHostnameVerifier
20 import org.apache.http.conn.ssl.SSLConnectionSocketFactory
21 import org.apache.http.conn.ssl.TrustAllStrategy
22 import org.apache.http.impl.client.CloseableHttpClient
23 import org.apache.http.impl.client.HttpClients
24 import org.apache.http.message.BasicHeader
25 import org.apache.http.ssl.SSLContextBuilder
26 import org.onap.ccsdk.cds.blueprintsprocessor.rest.BasicAuthRestClientProperties
27 import org.onap.ccsdk.cds.blueprintsprocessor.rest.utils.WebClientUtils
28 import org.springframework.http.HttpHeaders
29 import org.springframework.http.MediaType
31 import java.nio.charset.Charset
32 import java.util.Base64
34 class BasicAuthRestClientService(
35 private val restClientProperties:
36 BasicAuthRestClientProperties
38 BlueprintWebClientService {
40 override fun defaultHeaders(): Map<String, String> {
42 val encodedCredentials = setBasicAuth(
43 restClientProperties.username,
44 restClientProperties.password
47 HttpHeaders.CONTENT_TYPE to MediaType.APPLICATION_JSON_VALUE,
48 HttpHeaders.ACCEPT to MediaType.APPLICATION_JSON_VALUE,
49 HttpHeaders.AUTHORIZATION to "Basic $encodedCredentials"
53 override fun host(uri: String): String {
54 val uri: URI = URI.create(restClientProperties.url + uri)
55 return uri.resolve(uri).toString()
58 override fun httpClient(): CloseableHttpClient {
59 val sslContext = SSLContextBuilder.create()
61 sslContext.loadTrustMaterial(TrustAllStrategy.INSTANCE)
62 val csf = SSLConnectionSocketFactory(sslContext.build(), NoopHostnameVerifier())
63 return HttpClients.custom()
64 .addInterceptorFirst(WebClientUtils.logRequest())
65 .addInterceptorLast(WebClientUtils.logResponse())
66 .setSSLSocketFactory(csf).build()
69 override fun convertToBasicHeaders(headers: Map<String, String>):
71 val customHeaders: MutableMap<String, String> = headers.toMutableMap()
72 // inject additionalHeaders
73 customHeaders.putAll(verifyAdditionalHeaders(restClientProperties))
75 if (!headers.containsKey(HttpHeaders.AUTHORIZATION)) {
76 val encodedCredentials = setBasicAuth(
77 restClientProperties.username,
78 restClientProperties.password
80 customHeaders[HttpHeaders.AUTHORIZATION] =
81 "Basic $encodedCredentials"
83 return super.convertToBasicHeaders(customHeaders)
86 private fun setBasicAuth(username: String, password: String): String {
87 val credentialsString = "$username:$password"
88 return Base64.getEncoder().encodeToString(
89 credentialsString.toByteArray(Charset.defaultCharset())