diff options
author | 2018-12-18 16:11:09 -0500 | |
---|---|---|
committer | 2018-12-18 16:11:09 -0500 | |
commit | dbf2464d4afd351b7a4fd336f98118aad0ffbd5a (patch) | |
tree | e24ff6e4b60970ebc2e80a86362f8cfe53356b92 /ms/blueprintsprocessor/modules/commons/rest-lib/src/main | |
parent | 2d08cece19f948b48bb63f926d9910821b432508 (diff) |
Implement Basic Auth rest client service.
Change-Id: Ic8113f718f542aa6864943dc627b2171fccccca2
Issue-ID: CCSDK-699
Signed-off-by: Muthuramalingam, Brinda Santh(bs2796) <bs2796@att.com>
Diffstat (limited to 'ms/blueprintsprocessor/modules/commons/rest-lib/src/main')
6 files changed, 169 insertions, 11 deletions
diff --git a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/BluePrintRestLibData.kt b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/BluePrintRestLibData.kt index faf4fd4df..41e45458d 100644 --- a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/BluePrintRestLibData.kt +++ b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/BluePrintRestLibData.kt @@ -23,10 +23,11 @@ open class RestClientProperties { } open class BasicAuthRestClientProperties : RestClientProperties() { - var passwd: String? = null + var token: String? = null } open class SSLBasicAuthRestClientProperties : RestClientProperties() { + lateinit var keyStoreInstance: String // JKS, PKCS12 lateinit var sslTrust: String lateinit var sslTrustPasswd: String lateinit var sslKey: String diff --git a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/service/BasicAuthRestClientService.kt b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/service/BasicAuthRestClientService.kt index b79034b43..130706d7e 100644 --- a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/service/BasicAuthRestClientService.kt +++ b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/service/BasicAuthRestClientService.kt @@ -16,22 +16,64 @@ package org.onap.ccsdk.apps.blueprintsprocessor.rest.service -import org.onap.ccsdk.apps.blueprintsprocessor.rest.RestClientProperties +import org.onap.ccsdk.apps.blueprintsprocessor.rest.BasicAuthRestClientProperties +import org.onap.ccsdk.apps.blueprintsprocessor.rest.utils.WebClientUtils +import org.springframework.http.HttpHeaders +import org.springframework.http.MediaType +import org.springframework.web.reactive.function.BodyInserters +import org.springframework.web.reactive.function.client.ExchangeFilterFunctions import org.springframework.web.reactive.function.client.WebClient -class BasicAuthRestClientService(restClientProperties: RestClientProperties) : BlueprintWebClientService { +class BasicAuthRestClientService(private val restClientProperties: BasicAuthRestClientProperties) : BlueprintWebClientService { + + private var webClient: WebClient? = null override fun webClient(): WebClient { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + if (webClient == null) { + webClient = WebClient.builder() + .baseUrl(restClientProperties.url) + .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) + .defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE) + .filter(ExchangeFilterFunctions + .basicAuthentication(restClientProperties.userId, restClientProperties.token)) + .filter(WebClientUtils.logRequest()) + .filter(WebClientUtils.logResponse()) + .build() + } + return webClient!! } override fun <T> getResource(path: String, responseType: Class<T>): T { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + return getResource(path, null, responseType) + } + + override fun <T> getResource(path: String, headers: Map<String, String>?, responseType: Class<T>): T { + return webClient().get() + .uri(path) + .headers { httpHeaders -> + headers?.forEach { + httpHeaders.set(it.key, it.value) + } + } + .retrieve() + .bodyToMono(responseType).block()!! } override fun <T> postResource(path: String, request: Any, responseType: Class<T>): T { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + return postResource(path, null, request, responseType) + } + + override fun <T> postResource(path: String, headers: Map<String, String>?, request: Any, responseType: Class<T>): T { + return webClient().post() + .uri(path) + .headers { httpHeaders -> + headers?.forEach { + httpHeaders.set(it.key, it.value) + } + } + .body(BodyInserters.fromObject(request)) + .retrieve().bodyToMono(responseType).block()!! } override fun <T> exchangeResource(methodType: String, path: String, request: Any, responseType: Class<T>): T { diff --git a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/service/BlueprintWebClientService.kt b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/service/BlueprintWebClientService.kt index 232f4bb00..8106c077e 100644 --- a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/service/BlueprintWebClientService.kt +++ b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/service/BlueprintWebClientService.kt @@ -24,8 +24,12 @@ interface BlueprintWebClientService { fun <T> getResource(path: String, responseType: Class<T>): T + fun <T> getResource(path: String, headers: Map<String, String>?, responseType: Class<T>): T + fun <T> postResource(path: String, request: Any, responseType: Class<T>): T + fun <T> postResource(path: String, headers: Map<String, String>?, request: Any, responseType: Class<T>): T + fun <T> exchangeResource(methodType: String, path: String, request: Any, responseType: Class<T>): T } diff --git a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/service/DME2ProxyRestClientService.kt b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/service/DME2ProxyRestClientService.kt index bd46ced1b..27dbe6f81 100644 --- a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/service/DME2ProxyRestClientService.kt +++ b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/service/DME2ProxyRestClientService.kt @@ -28,10 +28,18 @@ class DME2ProxyRestClientService(restClientProperties: RestClientProperties) : B TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } + override fun <T> getResource(path: String, headers: Map<String, String>?, responseType: Class<T>): T { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + override fun <T> postResource(path: String, request: Any, responseType: Class<T>): T { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } + override fun <T> postResource(path: String, headers: Map<String, String>?, request: Any, responseType: Class<T>): T { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + override fun <T> exchangeResource(methodType: String, path: String, request: Any, responseType: Class<T>): T { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } diff --git a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/service/SSLBasicAuthRestClientService.kt b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/service/SSLBasicAuthRestClientService.kt index 8b4add100..71727b935 100644 --- a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/service/SSLBasicAuthRestClientService.kt +++ b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/service/SSLBasicAuthRestClientService.kt @@ -16,20 +16,79 @@ package org.onap.ccsdk.apps.blueprintsprocessor.rest.service -import org.onap.ccsdk.apps.blueprintsprocessor.rest.RestClientProperties +import io.netty.handler.ssl.SslContextBuilder +import org.onap.ccsdk.apps.blueprintsprocessor.rest.SSLBasicAuthRestClientProperties +import org.onap.ccsdk.apps.blueprintsprocessor.rest.utils.WebClientUtils +import org.springframework.http.HttpHeaders +import org.springframework.http.MediaType +import org.springframework.http.client.reactive.ReactorClientHttpConnector +import org.springframework.web.reactive.function.BodyInserters import org.springframework.web.reactive.function.client.WebClient +import reactor.netty.http.client.HttpClient +import java.io.File +import java.security.KeyStore +import java.security.cert.X509Certificate + + +class SSLBasicAuthRestClientService(private val restClientProperties: SSLBasicAuthRestClientProperties) : BlueprintWebClientService { -class SSLBasicAuthRestClientService(restClientProperties: RestClientProperties) : BlueprintWebClientService { override fun webClient(): WebClient { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + + // Load the Keystore Information + val ketInputStream = File(restClientProperties.sslKey).inputStream() + val ks = KeyStore.getInstance(restClientProperties.keyStoreInstance) + ks.load(ketInputStream, restClientProperties.sslKeyPasswd.toCharArray()) + + // Manage Trust Store + val trustCertCollection = ks.aliases().toList().map { alias -> + ks.getCertificate(alias) as X509Certificate + }.toTypedArray() + val sslContext = SslContextBuilder + .forClient() + .trustManager(*trustCertCollection) + .build() + + // Create Http Client + val httpClient = HttpClient.create().secure { t -> t.sslContext(sslContext) } + + return WebClient.builder() + .baseUrl(restClientProperties.url) + .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) + .defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE) + .filter(WebClientUtils.logRequest()) + .clientConnector(ReactorClientHttpConnector(httpClient)).build() } override fun <T> getResource(path: String, responseType: Class<T>): T { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + return getResource(path, null, responseType) + } + + override fun <T> getResource(path: String, headers: Map<String, String>?, responseType: Class<T>): T { + return webClient().get() + .uri(path) + .headers { httpHeaders -> + headers?.forEach { + httpHeaders.set(it.key, it.value) + } + } + .retrieve() + .bodyToMono(responseType).block()!! } override fun <T> postResource(path: String, request: Any, responseType: Class<T>): T { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + return postResource(path, null, request, responseType) + } + + override fun <T> postResource(path: String, headers: Map<String, String>?, request: Any, responseType: Class<T>): T { + return webClient().post() + .uri(path) + .headers { httpHeaders -> + headers?.forEach { + httpHeaders.set(it.key, it.value) + } + } + .body(BodyInserters.fromObject(request)) + .retrieve().bodyToMono(responseType).block()!! } override fun <T> exchangeResource(methodType: String, path: String, request: Any, responseType: Class<T>): T { diff --git a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/utils/WebClientUtils.kt b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/utils/WebClientUtils.kt new file mode 100644 index 000000000..40d6ba631 --- /dev/null +++ b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/utils/WebClientUtils.kt @@ -0,0 +1,44 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.ccsdk.apps.blueprintsprocessor.rest.utils + +import org.slf4j.LoggerFactory +import org.springframework.web.reactive.function.client.ExchangeFilterFunction +import reactor.core.publisher.Mono + + +class WebClientUtils { + companion object { + + val log = LoggerFactory.getLogger(WebClientUtils::class.java)!! + + fun logRequest(): ExchangeFilterFunction { + + return ExchangeFilterFunction.ofRequestProcessor { clientRequest -> + log.info("Rest request method(${clientRequest.method()}), url(${clientRequest.url()})") + Mono.just(clientRequest) + } + } + + fun logResponse(): ExchangeFilterFunction { + return ExchangeFilterFunction.ofResponseProcessor { clientResponse -> + log.info("Response status(${clientResponse.statusCode()})") + Mono.just(clientResponse) + } + } + } +}
\ No newline at end of file |