2 * Copyright © 2017-2018 AT&T Intellectual Property.
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 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
29 import java.security.KeyStore
30 import java.security.cert.X509Certificate
33 class SSLBasicAuthRestClientService(private val restClientProperties: SSLBasicAuthRestClientProperties) : BlueprintWebClientService {
35 override fun webClient(): WebClient {
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())
43 val trustCertCollection = ks.aliases().toList().map { alias ->
44 ks.getCertificate(alias) as X509Certificate
46 val sslContext = SslContextBuilder
48 .trustManager(*trustCertCollection)
52 val httpClient = HttpClient.create().secure { t -> t.sslContext(sslContext) }
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()
62 override fun <T> getResource(path: String, responseType: Class<T>): T {
63 return getResource(path, null, responseType)
66 override fun <T> getResource(path: String, headers: Map<String, String>?, responseType: Class<T>): T {
67 return webClient().get()
69 .headers { httpHeaders ->
71 httpHeaders.set(it.key, it.value)
75 .bodyToMono(responseType).block()!!
78 override fun <T> postResource(path: String, request: Any, responseType: Class<T>): T {
79 return postResource(path, null, request, responseType)
82 override fun <T> postResource(path: String, headers: Map<String, String>?, request: Any, responseType: Class<T>): T {
83 return webClient().post()
85 .headers { httpHeaders ->
87 httpHeaders.set(it.key, it.value)
90 .body(BodyInserters.fromObject(request))
91 .retrieve().bodyToMono(responseType).block()!!
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.