71727b935117fbbea16e36cb946ad7d028eb939d
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.ccsdk.apps.blueprintsprocessor.rest.service
18
19 import io.netty.handler.ssl.SslContextBuilder
20 import org.onap.ccsdk.apps.blueprintsprocessor.rest.SSLBasicAuthRestClientProperties
21 import org.onap.ccsdk.apps.blueprintsprocessor.rest.utils.WebClientUtils
22 import org.springframework.http.HttpHeaders
23 import org.springframework.http.MediaType
24 import org.springframework.http.client.reactive.ReactorClientHttpConnector
25 import org.springframework.web.reactive.function.BodyInserters
26 import org.springframework.web.reactive.function.client.WebClient
27 import reactor.netty.http.client.HttpClient
28 import java.io.File
29 import java.security.KeyStore
30 import java.security.cert.X509Certificate
31
32
33 class SSLBasicAuthRestClientService(private val restClientProperties: SSLBasicAuthRestClientProperties) : BlueprintWebClientService {
34
35     override fun webClient(): WebClient {
36
37         // Load the Keystore Information
38         val ketInputStream = File(restClientProperties.sslKey).inputStream()
39         val ks = KeyStore.getInstance(restClientProperties.keyStoreInstance)
40         ks.load(ketInputStream, restClientProperties.sslKeyPasswd.toCharArray())
41
42         // Manage Trust Store
43         val trustCertCollection = ks.aliases().toList().map { alias ->
44             ks.getCertificate(alias) as X509Certificate
45         }.toTypedArray()
46         val sslContext = SslContextBuilder
47                 .forClient()
48                 .trustManager(*trustCertCollection)
49                 .build()
50
51         // Create Http Client
52         val httpClient = HttpClient.create().secure { t -> t.sslContext(sslContext) }
53
54         return WebClient.builder()
55                 .baseUrl(restClientProperties.url)
56                 .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
57                 .defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
58                 .filter(WebClientUtils.logRequest())
59                 .clientConnector(ReactorClientHttpConnector(httpClient)).build()
60     }
61
62     override fun <T> getResource(path: String, responseType: Class<T>): T {
63         return getResource(path, null, responseType)
64     }
65
66     override fun <T> getResource(path: String, headers: Map<String, String>?, responseType: Class<T>): T {
67         return webClient().get()
68                 .uri(path)
69                 .headers { httpHeaders ->
70                     headers?.forEach {
71                         httpHeaders.set(it.key, it.value)
72                     }
73                 }
74                 .retrieve()
75                 .bodyToMono(responseType).block()!!
76     }
77
78     override fun <T> postResource(path: String, request: Any, responseType: Class<T>): T {
79         return postResource(path, null, request, responseType)
80     }
81
82     override fun <T> postResource(path: String, headers: Map<String, String>?, request: Any, responseType: Class<T>): T {
83         return webClient().post()
84                 .uri(path)
85                 .headers { httpHeaders ->
86                     headers?.forEach {
87                         httpHeaders.set(it.key, it.value)
88                     }
89                 }
90                 .body(BodyInserters.fromObject(request))
91                 .retrieve().bodyToMono(responseType).block()!!
92     }
93
94     override fun <T> exchangeResource(methodType: String, path: String, request: Any, responseType: Class<T>): T {
95         TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
96     }
97 }