From 9ab2a91f585864bf18fef788adeaf61f4f450b39 Mon Sep 17 00:00:00 2001 From: Lukasz Rajewski Date: Thu, 18 Aug 2022 21:50:31 +0200 Subject: Refactor rest clients and support timeouts - Refactored rest clients to remove redundant code - Timeouts added to the configuration of rest clients Issue-ID: CCSDK-3716 Signed-off-by: Lukasz Rajewski Change-Id: I706b8efd8447570455b8b65bd5b1a22da474f62b --- .../functions/k8s/K8sAbstractRestClientService.kt | 52 +++------------------- 1 file changed, 6 insertions(+), 46 deletions(-) (limited to 'ms/blueprintsprocessor/functions/k8s-connection-plugin') diff --git a/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/K8sAbstractRestClientService.kt b/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/K8sAbstractRestClientService.kt index 14f14f61b..b9c45e423 100644 --- a/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/K8sAbstractRestClientService.kt +++ b/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/K8sAbstractRestClientService.kt @@ -19,23 +19,20 @@ package org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s -import org.apache.http.message.BasicHeader import org.onap.ccsdk.cds.blueprintsprocessor.rest.BasicAuthRestClientProperties -import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService -import org.springframework.http.HttpHeaders.ACCEPT -import org.springframework.http.HttpHeaders.AUTHORIZATION -import org.springframework.http.HttpHeaders.CONTENT_TYPE -import org.springframework.http.MediaType.APPLICATION_JSON_VALUE -import java.nio.charset.Charset -import java.util.Base64 +import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BasicAuthRestClientService abstract class K8sAbstractRestClientService( private val k8sConfiguration: K8sConnectionPluginConfiguration -) : BlueprintWebClientService { +) : BasicAuthRestClientService(BasicAuthRestClientProperties()) { protected val baseUrl: String = k8sConfiguration.getProperties().url private var restClientProperties: BasicAuthRestClientProperties? = null + override fun getRestClientProperties(): BasicAuthRestClientProperties { + return getBasicAuthRestClientProperties() + } + private fun getBasicAuthRestClientProperties(): BasicAuthRestClientProperties { return if (restClientProperties != null) restClientProperties!! @@ -55,45 +52,8 @@ abstract class K8sAbstractRestClientService( mapOfHeaders["Accept"] = "application/json" mapOfHeaders["Content-Type"] = "application/json" mapOfHeaders["cache-control"] = " no-cache" - mapOfHeaders["Accept"] = "application/json" return mapOfHeaders } - private fun setBasicAuth(username: String, password: String): String { - val credentialsString = "$username:$password" - return Base64.getEncoder().encodeToString(credentialsString.toByteArray(Charset.defaultCharset())) - } - - override fun defaultHeaders(): Map { - val encodedCredentials = setBasicAuth( - getBasicAuthRestClientProperties().username, - getBasicAuthRestClientProperties().password - ) - return mapOf( - CONTENT_TYPE to APPLICATION_JSON_VALUE, - ACCEPT to APPLICATION_JSON_VALUE, - AUTHORIZATION to "Basic $encodedCredentials" - ) - } - - override fun host(uri: String): String { - return getBasicAuthRestClientProperties().url + uri - } - - override fun convertToBasicHeaders(headers: Map): Array { - val customHeaders: MutableMap = headers.toMutableMap() - // inject additionalHeaders - customHeaders.putAll(verifyAdditionalHeaders(getBasicAuthRestClientProperties())) - - if (!headers.containsKey(AUTHORIZATION)) { - val encodedCredentials = setBasicAuth( - getBasicAuthRestClientProperties().username, - getBasicAuthRestClientProperties().password - ) - customHeaders[AUTHORIZATION] = "Basic $encodedCredentials" - } - return super.convertToBasicHeaders(customHeaders) - } - abstract fun apiUrl(): String } -- cgit 1.2.3-korg