2 * Copyright © 2017-2019 AT&T, Bell Canada
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.apps.blueprintsprocessor.rest.service
19 import org.apache.http.conn.ssl.SSLConnectionSocketFactory
20 import org.apache.http.impl.client.CloseableHttpClient
21 import org.apache.http.impl.client.HttpClients
22 import org.apache.http.ssl.SSLContextBuilder
23 import org.onap.ccsdk.apps.blueprintsprocessor.rest.SSLBasicAuthRestClientProperties
24 import org.onap.ccsdk.apps.blueprintsprocessor.rest.utils.WebClientUtils
25 import org.springframework.http.HttpHeaders
26 import org.springframework.http.MediaType
28 import java.io.FileInputStream
29 import java.security.KeyStore
30 import java.security.cert.X509Certificate
32 class SSLBasicAuthRestClientService(private val restClientProperties: SSLBasicAuthRestClientProperties) :
33 BlueprintWebClientService {
35 override fun defaultHeaders(): Map<String, String> {
37 HttpHeaders.CONTENT_TYPE to MediaType.APPLICATION_JSON_VALUE,
38 HttpHeaders.ACCEPT to MediaType.APPLICATION_JSON_VALUE)
41 override fun host(uri: String): String {
42 return restClientProperties.url + uri
45 override fun httpClient(): CloseableHttpClient {
47 val keystoreInstance = restClientProperties.keyStoreInstance
48 val sslKey = restClientProperties.sslKey
49 val sslKeyPwd = restClientProperties.sslKeyPassword
50 val sslTrust = restClientProperties.sslTrust
51 val sslTrustPwd = restClientProperties.sslTrustPassword
53 val acceptingTrustStrategy = { chain: Array<X509Certificate>, authType: String -> true }
55 FileInputStream(sslKey).use { keyInput ->
56 val keyStore = KeyStore.getInstance(keystoreInstance)
57 keyStore.load(keyInput, sslKeyPwd.toCharArray())
60 SSLContextBuilder.create()
61 .loadKeyMaterial(keyStore, sslKeyPwd.toCharArray())
62 .loadTrustMaterial(File(sslTrust), sslTrustPwd.toCharArray(), acceptingTrustStrategy).build()
64 val csf = SSLConnectionSocketFactory(sslContext!!)
66 return HttpClients.custom()
67 .addInterceptorFirst(WebClientUtils.logRequest())
68 .addInterceptorLast(WebClientUtils.logResponse())
69 .setSSLSocketFactory(csf).build()