diff options
Diffstat (limited to 'ms/blueprintsprocessor/modules/commons/rest-lib/src/main')
8 files changed, 355 insertions, 163 deletions
diff --git a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/BluePrintRestLibConfiguration.kt b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/BluePrintRestLibConfiguration.kt index 05143f3d8..fa4ff231d 100644 --- a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/BluePrintRestLibConfiguration.kt +++ b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/BluePrintRestLibConfiguration.kt @@ -1,6 +1,7 @@ /* * Copyright © 2017-2018 AT&T Intellectual Property. * Modifications Copyright © 2019 IBM. + * Modifications Copyright © 2019 Huawei. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,6 +34,8 @@ class RestLibConstants { const val TYPE_TOKEN_AUTH = "token-auth" const val TYPE_BASIC_AUTH = "basic-auth" const val TYPE_SSL_BASIC_AUTH = "ssl-basic-auth" + const val TYPE_SSL_TOKEN_AUTH = "ssl-token-auth" + const val TYPE_SSL_NO_AUTH = "ssl-no-auth" const val TYPE_DME2_PROXY = "dme2-proxy" const val TYPE_POLICY_MANAGER = "policy-manager" } diff --git a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/BluePrintRestLibData.kt b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/BluePrintRestLibData.kt index 4e6e62efb..70ec9501a 100644 --- a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/BluePrintRestLibData.kt +++ b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/BluePrintRestLibData.kt @@ -1,5 +1,6 @@ /* * Copyright © 2017-2018 AT&T Intellectual Property. + * Modifications Copyright © 2019 Huawei. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,6 +22,22 @@ open class RestClientProperties { lateinit var url: String } +open class SSLRestClientProperties : RestClientProperties() { + lateinit var keyStoreInstance: String // JKS, PKCS12 + lateinit var sslTrust: String + lateinit var sslTrustPassword: String + var sslKey: String? = null + var sslKeyPassword: String? = null +} + +open class SSLBasicAuthRestClientProperties : SSLRestClientProperties() { + var basicAuth: BasicAuthRestClientProperties? = null +} + +open class SSLTokenAuthRestClientProperties : SSLRestClientProperties() { + var tokenAuth: TokenAuthRestClientProperties? = null +} + open class BasicAuthRestClientProperties : RestClientProperties() { lateinit var password: String lateinit var username: String @@ -30,14 +47,6 @@ open class TokenAuthRestClientProperties : RestClientProperties() { var token: String? = null } -open class SSLBasicAuthRestClientProperties : RestClientProperties() { - lateinit var keyStoreInstance: String // JKS, PKCS12 - lateinit var sslTrust: String - lateinit var sslTrustPassword: String - lateinit var sslKey: String - lateinit var sslKeyPassword: String -} - open class DME2RestClientProperties : RestClientProperties() { lateinit var service: String lateinit var subContext: String diff --git a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BasicAuthRestClientService.kt b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BasicAuthRestClientService.kt index 67dbd0d72..3190cd1c6 100644 --- a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BasicAuthRestClientService.kt +++ b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BasicAuthRestClientService.kt @@ -23,11 +23,14 @@ import org.springframework.http.MediaType import java.nio.charset.Charset import java.util.* -class BasicAuthRestClientService(private val restClientProperties: BasicAuthRestClientProperties) : - BlueprintWebClientService { +class BasicAuthRestClientService(private val restClientProperties: + BasicAuthRestClientProperties) : + BlueprintWebClientService { override fun defaultHeaders(): Map<String, String> { - val encodedCredentials = setBasicAuth(restClientProperties.username, restClientProperties.password) + + val encodedCredentials = setBasicAuth(restClientProperties.username, + restClientProperties.password) return mapOf( HttpHeaders.CONTENT_TYPE to MediaType.APPLICATION_JSON_VALUE, HttpHeaders.ACCEPT to MediaType.APPLICATION_JSON_VALUE, @@ -38,18 +41,25 @@ class BasicAuthRestClientService(private val restClientProperties: BasicAuthRest return restClientProperties.url + uri } - override fun convertToBasicHeaders(headers: Map<String, String>): Array<BasicHeader> { + override fun convertToBasicHeaders(headers: Map<String, String>): + Array<BasicHeader> { + val customHeaders: MutableMap<String, String> = headers.toMutableMap() if (!headers.containsKey(HttpHeaders.AUTHORIZATION)) { - val encodedCredentials = setBasicAuth(restClientProperties.username, restClientProperties.password) - customHeaders[HttpHeaders.AUTHORIZATION] = "Basic $encodedCredentials" + val encodedCredentials = setBasicAuth( + restClientProperties.username, + restClientProperties.password) + customHeaders[HttpHeaders.AUTHORIZATION] = + "Basic $encodedCredentials" } return super.convertToBasicHeaders(customHeaders) } private fun setBasicAuth(username: String, password: String): String { + val credentialsString = "$username:$password" - return Base64.getEncoder().encodeToString(credentialsString.toByteArray(Charset.defaultCharset())) + return Base64.getEncoder().encodeToString( + credentialsString.toByteArray(Charset.defaultCharset())) } diff --git a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BluePrintRestLibPropertyService.kt b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BluePrintRestLibPropertyService.kt index dc974468b..a6bbc39d8 100644 --- a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BluePrintRestLibPropertyService.kt +++ b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BluePrintRestLibPropertyService.kt @@ -1,6 +1,7 @@ /* * Copyright © 2017-2019 AT&T, Bell Canada * Modifications Copyright © 2019 IBM. + * Modifications Copyright © 2019 Huawei. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,15 +26,19 @@ import org.onap.ccsdk.cds.blueprintsprocessor.rest.PolicyManagerRestClientProper import org.onap.ccsdk.cds.blueprintsprocessor.rest.RestClientProperties import org.onap.ccsdk.cds.blueprintsprocessor.rest.RestLibConstants import org.onap.ccsdk.cds.blueprintsprocessor.rest.SSLBasicAuthRestClientProperties +import org.onap.ccsdk.cds.blueprintsprocessor.rest.SSLRestClientProperties +import org.onap.ccsdk.cds.blueprintsprocessor.rest.SSLTokenAuthRestClientProperties import org.onap.ccsdk.cds.blueprintsprocessor.rest.TokenAuthRestClientProperties import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils import org.springframework.stereotype.Service @Service(RestLibConstants.SERVICE_BLUEPRINT_REST_LIB_PROPERTY) -open class BluePrintRestLibPropertyService(private var bluePrintProperties: BluePrintProperties) { +open class BluePrintRestLibPropertyService(private var bluePrintProperties: + BluePrintProperties) { - fun blueprintWebClientService(jsonNode: JsonNode): BlueprintWebClientService { + fun blueprintWebClientService(jsonNode: JsonNode): + BlueprintWebClientService { val restClientProperties = restClientProperties(jsonNode) return blueprintWebClientService(restClientProperties) } @@ -45,7 +50,8 @@ open class BluePrintRestLibPropertyService(private var bluePrintProperties: Blue } fun restClientProperties(prefix: String): RestClientProperties { - val type = bluePrintProperties.propertyBeanType("$prefix.type", String::class.java) + val type = bluePrintProperties.propertyBeanType( + "$prefix.type", String::class.java) return when (type) { RestLibConstants.TYPE_BASIC_AUTH -> { basicAuthRestClientProperties(prefix) @@ -53,6 +59,12 @@ open class BluePrintRestLibPropertyService(private var bluePrintProperties: Blue RestLibConstants.TYPE_SSL_BASIC_AUTH -> { sslBasicAuthRestClientProperties(prefix) } + RestLibConstants.TYPE_SSL_TOKEN_AUTH -> { + sslTokenAuthRestClientProperties(prefix) + } + RestLibConstants.TYPE_SSL_NO_AUTH -> { + sslNoAuthRestClientProperties(prefix) + } RestLibConstants.TYPE_DME2_PROXY -> { dme2ProxyClientProperties(prefix) } @@ -60,69 +72,123 @@ open class BluePrintRestLibPropertyService(private var bluePrintProperties: Blue policyManagerRestClientProperties(prefix) } else -> { - throw BluePrintProcessorException("Rest adaptor($type) is not supported") + throw BluePrintProcessorException("Rest adaptor($type) is" + + " not supported") } } } - private fun restClientProperties(jsonNode: JsonNode): RestClientProperties { + fun restClientProperties(jsonNode: JsonNode): RestClientProperties { + val type = jsonNode.get("type").textValue() return when (type) { RestLibConstants.TYPE_TOKEN_AUTH -> { - JacksonUtils.readValue(jsonNode, TokenAuthRestClientProperties::class.java)!! + JacksonUtils.readValue(jsonNode, + TokenAuthRestClientProperties::class.java)!! } RestLibConstants.TYPE_BASIC_AUTH -> { - JacksonUtils.readValue(jsonNode, BasicAuthRestClientProperties::class.java)!! - } - RestLibConstants.TYPE_SSL_BASIC_AUTH -> { - JacksonUtils.readValue(jsonNode, SSLBasicAuthRestClientProperties::class.java)!! + JacksonUtils.readValue(jsonNode, + BasicAuthRestClientProperties::class.java)!! } RestLibConstants.TYPE_DME2_PROXY -> { - JacksonUtils.readValue(jsonNode, DME2RestClientProperties::class.java)!! + JacksonUtils.readValue(jsonNode, + DME2RestClientProperties::class.java)!! } RestLibConstants.TYPE_POLICY_MANAGER -> { - JacksonUtils.readValue(jsonNode, PolicyManagerRestClientProperties::class.java)!! + JacksonUtils.readValue(jsonNode, + PolicyManagerRestClientProperties::class.java)!! + } + RestLibConstants.TYPE_SSL_BASIC_AUTH -> { + JacksonUtils.readValue(jsonNode, + SSLBasicAuthRestClientProperties::class.java)!! + } + RestLibConstants.TYPE_SSL_TOKEN_AUTH -> { + JacksonUtils.readValue(jsonNode, + SSLTokenAuthRestClientProperties::class.java)!! + } + RestLibConstants.TYPE_SSL_NO_AUTH -> { + JacksonUtils.readValue( + jsonNode, SSLRestClientProperties::class.java)!! } else -> { - throw BluePrintProcessorException("Rest adaptor($type) is not supported") + throw BluePrintProcessorException("Rest adaptor($type) is" + + " not supported") } } } + + private fun blueprintWebClientService( + restClientProperties: RestClientProperties): + BlueprintWebClientService { - private fun blueprintWebClientService(restClientProperties: RestClientProperties): BlueprintWebClientService { when (restClientProperties) { + is SSLRestClientProperties -> { + return SSLRestClientService(restClientProperties) + } is TokenAuthRestClientProperties -> { return TokenAuthRestClientService(restClientProperties) } is BasicAuthRestClientProperties -> { return BasicAuthRestClientService(restClientProperties) } - is SSLBasicAuthRestClientProperties -> { - return SSLBasicAuthRestClientService(restClientProperties) - } is DME2RestClientProperties -> { return DME2ProxyRestClientService(restClientProperties) } else -> { - throw BluePrintProcessorException("couldn't get rest service for") + throw BluePrintProcessorException("couldn't get rest " + + "service for") } } } - private fun basicAuthRestClientProperties(prefix: String): BasicAuthRestClientProperties { - return bluePrintProperties.propertyBeanType(prefix, BasicAuthRestClientProperties::class.java) + private fun basicAuthRestClientProperties(prefix: String): + BasicAuthRestClientProperties { + return bluePrintProperties.propertyBeanType( + prefix, BasicAuthRestClientProperties::class.java) + } + + private fun sslBasicAuthRestClientProperties(prefix: String): + SSLRestClientProperties { + + val sslProps: SSLBasicAuthRestClientProperties = + bluePrintProperties.propertyBeanType( + prefix, SSLBasicAuthRestClientProperties::class.java) + val basicProps : BasicAuthRestClientProperties = + bluePrintProperties.propertyBeanType( + prefix, BasicAuthRestClientProperties::class.java) + sslProps.basicAuth = basicProps + return sslProps + } + + private fun sslTokenAuthRestClientProperties(prefix: String): + SSLRestClientProperties { + + val sslProps: SSLTokenAuthRestClientProperties = + bluePrintProperties.propertyBeanType(prefix, + SSLTokenAuthRestClientProperties::class.java) + val basicProps : TokenAuthRestClientProperties = + bluePrintProperties.propertyBeanType(prefix, + TokenAuthRestClientProperties::class.java) + sslProps.tokenAuth = basicProps + return sslProps } - private fun sslBasicAuthRestClientProperties(prefix: String): SSLBasicAuthRestClientProperties { - return bluePrintProperties.propertyBeanType(prefix, SSLBasicAuthRestClientProperties::class.java) + private fun sslNoAuthRestClientProperties(prefix: String): + SSLRestClientProperties { + return bluePrintProperties.propertyBeanType( + prefix, SSLRestClientProperties::class.java) } - private fun dme2ProxyClientProperties(prefix: String): DME2RestClientProperties { - return bluePrintProperties.propertyBeanType(prefix, DME2RestClientProperties::class.java) + private fun dme2ProxyClientProperties(prefix: String): + DME2RestClientProperties { + return bluePrintProperties.propertyBeanType( + prefix, DME2RestClientProperties::class.java) } - private fun policyManagerRestClientProperties(prefix: String): PolicyManagerRestClientProperties { - return bluePrintProperties.propertyBeanType(prefix, PolicyManagerRestClientProperties::class.java) + private fun policyManagerRestClientProperties(prefix: String): + PolicyManagerRestClientProperties { + return bluePrintProperties.propertyBeanType( + prefix, PolicyManagerRestClientProperties::class.java) } } diff --git a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BlueprintWebClientService.kt b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BlueprintWebClientService.kt index 1c1d510fc..d6d02b330 100644 --- a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BlueprintWebClientService.kt +++ b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BlueprintWebClientService.kt @@ -1,6 +1,7 @@ /* * Copyright © 2017-2019 AT&T, Bell Canada, Nordix Foundation * Modifications Copyright © 2018-2019 IBM. + * Modifications Copyright © 2019 Huawei. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,7 +22,11 @@ import com.fasterxml.jackson.databind.JsonNode import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import org.apache.commons.io.IOUtils -import org.apache.http.client.methods.* +import org.apache.http.client.methods.HttpDelete +import org.apache.http.client.methods.HttpGet +import org.apache.http.client.methods.HttpPatch +import org.apache.http.client.methods.HttpPost +import org.apache.http.client.methods.HttpPut import org.apache.http.entity.StringEntity import org.apache.http.impl.client.CloseableHttpClient import org.apache.http.impl.client.HttpClients @@ -30,6 +35,7 @@ import org.onap.ccsdk.cds.blueprintsprocessor.rest.utils.WebClientUtils import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils import org.springframework.http.HttpMethod +import java.io.InputStream import java.nio.charset.Charset interface BlueprintWebClientService { @@ -45,19 +51,24 @@ interface BlueprintWebClientService { .build() } - fun exchangeResource(methodType: String, path: String, request: String): String { - return this.exchangeResource(methodType, path, request, defaultHeaders()) + fun exchangeResource(methodType: String, path: String, request: String): + String { + return this.exchangeResource(methodType, path, request, + defaultHeaders()) } - fun exchangeResource(methodType: String, path: String, request: String, headers: Map<String, String>): String { - val convertedHeaders: Array<BasicHeader> = convertToBasicHeaders(headers) + fun exchangeResource(methodType: String, path: String, request: String, + headers: Map<String, String>): String { + val convertedHeaders: Array<BasicHeader> = convertToBasicHeaders( + headers) return when (HttpMethod.resolve(methodType)) { HttpMethod.DELETE -> delete(path, convertedHeaders) HttpMethod.GET -> get(path, convertedHeaders) HttpMethod.POST -> post(path, request, convertedHeaders) HttpMethod.PUT -> put(path, request, convertedHeaders) HttpMethod.PATCH -> patch(path, request, convertedHeaders) - else -> throw BluePrintProcessorException("Unsupported methodType($methodType)") + else -> throw BluePrintProcessorException("Unsupported met" + + "hodType($methodType)") } } @@ -81,7 +92,8 @@ interface BlueprintWebClientService { } } - fun post(path: String, request: String, headers: Array<BasicHeader>): String { + fun post(path: String, request: String, headers: Array<BasicHeader>): + String { val httpPost = HttpPost(host(path)) val entity = StringEntity(request) httpPost.entity = entity @@ -91,7 +103,8 @@ interface BlueprintWebClientService { } } - fun put(path: String, request: String, headers: Array<BasicHeader>): String { + fun put(path: String, request: String, headers: Array<BasicHeader>): + String { val httpPut = HttpPut(host(path)) val entity = StringEntity(request) httpPut.entity = entity @@ -101,7 +114,8 @@ interface BlueprintWebClientService { } } - fun patch(path: String, request: String, headers: Array<BasicHeader>): String { + fun patch(path: String, request: String, headers: Array<BasicHeader>): + String { val httpPatch = HttpPatch(host(path)) val entity = StringEntity(request) httpPatch.entity = entity @@ -111,28 +125,23 @@ interface BlueprintWebClientService { } } - // Non Blocking Rest Implementation - suspend fun httpClientNB(): CloseableHttpClient { - return HttpClients.custom() - .addInterceptorFirst(WebClientUtils.logRequest()) - .addInterceptorLast(WebClientUtils.logResponse()) - .build() - } suspend fun getNB(path: String): String { return getNB(path, null, String::class.java) } - suspend fun getNB(path: String, additionalHeaders: Map<String, String>?): String { + suspend fun getNB(path: String, additionalHeaders: Map<String, String>?): + String { return getNB(path, additionalHeaders, String::class.java) } suspend fun <T> getNB(path: String, additionalHeaders: Map<String, String>?, - responseType: Class<T>): T = withContext(Dispatchers.IO) { + responseType: Class<T>): T = + withContext(Dispatchers.IO) { val httpGet = HttpGet(host(path)) httpGet.setHeaders(basicHeaders(additionalHeaders)) httpClientNB().execute(httpGet).entity.content.use { - JacksonUtils.readValue(it, responseType)!! + getResponse(it, responseType) } } @@ -140,18 +149,20 @@ interface BlueprintWebClientService { return postNB(path, request, null, String::class.java) } - suspend fun postNB(path: String, request: Any, additionalHeaders: Map<String, String>?): String { + suspend fun postNB(path: String, request: Any, + additionalHeaders: Map<String, String>?): String { return postNB(path, request, additionalHeaders, String::class.java) } - suspend fun <T> postNB(path: String, request: Any, additionalHeaders: Map<String, String>?, + suspend fun <T> postNB(path: String, request: Any, + additionalHeaders: Map<String, String>?, responseType: Class<T>): T = withContext(Dispatchers.IO) { val httpPost = HttpPost(host(path)) httpPost.entity = StringEntity(strRequest(request)) httpPost.setHeaders(basicHeaders(additionalHeaders)) httpClientNB().execute(httpPost).entity.content.use { - JacksonUtils.readValue(it, responseType)!! + getResponse(it, responseType) } } @@ -159,17 +170,20 @@ interface BlueprintWebClientService { return putNB(path, request, null, String::class.java) } - suspend fun putNB(path: String, request: Any, additionalHeaders: Map<String, String>?): String { + suspend fun putNB(path: String, request: Any, + additionalHeaders: Map<String, String>?): String { return putNB(path, request, additionalHeaders, String::class.java) } - suspend fun <T> putNB(path: String, request: Any, additionalHeaders: Map<String, String>?, - responseType: Class<T>): T = withContext(Dispatchers.IO) { + suspend fun <T> putNB(path: String, request: Any, + additionalHeaders: Map<String, String>?, + responseType: Class<T>): T = + withContext(Dispatchers.IO) { val httpPut = HttpPut(host(path)) httpPut.entity = StringEntity(strRequest(request)) httpPut.setHeaders(basicHeaders(additionalHeaders)) httpClientNB().execute(httpPut).entity.content.use { - JacksonUtils.readValue(it, responseType)!! + getResponse(it, responseType) } } @@ -177,46 +191,61 @@ interface BlueprintWebClientService { return deleteNB(path, null, String::class.java) } - suspend fun <T> deleteNB(path: String, additionalHeaders: Map<String, String>?): String { + suspend fun <T> deleteNB(path: String, + additionalHeaders: Map<String, String>?): String { return deleteNB(path, additionalHeaders, String::class.java) } - suspend fun <T> deleteNB(path: String, additionalHeaders: Map<String, String>?, responseType: Class<T>): T = + suspend fun <T> deleteNB(path: String, + additionalHeaders: Map<String, String>?, + responseType: Class<T>): T = withContext(Dispatchers.IO) { - val httpDelete = HttpDelete(host(path)) - httpDelete.setHeaders(basicHeaders(additionalHeaders)) - httpClient().execute(httpDelete).entity.content.use { - JacksonUtils.readValue(it, responseType)!! - } - } + val httpDelete = HttpDelete(host(path)) + httpDelete.setHeaders(basicHeaders(additionalHeaders)) + httpClient().execute(httpDelete).entity.content.use { + getResponse(it, responseType) + } + } - suspend fun <T> patchNB(path: String, request: Any, additionalHeaders: Map<String, String>?, - responseType: Class<T>): T = withContext(Dispatchers.IO) { + suspend fun <T> patchNB(path: String, request: Any, + additionalHeaders: Map<String, String>?, + responseType: Class<T>): T = + withContext(Dispatchers.IO) { val httpPatch = HttpPatch(host(path)) httpPatch.entity = StringEntity(strRequest(request)) httpPatch.setHeaders(basicHeaders(additionalHeaders)) httpClient().execute(httpPatch).entity.content.use { - JacksonUtils.readValue(it, responseType)!! + getResponse(it, responseType) } } - suspend fun exchangeNB(methodType: String, path: String, request: Any): String { - return exchangeNB(methodType, path, request, hashMapOf(), String::class.java) + suspend fun exchangeNB(methodType: String, path: String, request: Any): + String { + return exchangeNB(methodType, path, request, hashMapOf(), + String::class.java) } - suspend fun exchangeNB(methodType: String, path: String, request: Any, additionalHeaders: Map<String, String>?): String { - return exchangeNB(methodType, path, request, additionalHeaders, String::class.java) + suspend fun exchangeNB(methodType: String, path: String, request: Any, + additionalHeaders: Map<String, String>?): String { + return exchangeNB(methodType, path, request, additionalHeaders, + String::class.java) } - suspend fun <T> exchangeNB(methodType: String, path: String, request: Any, additionalHeaders: Map<String, String>?, + suspend fun <T> exchangeNB(methodType: String, path: String, request: Any, + additionalHeaders: Map<String, String>?, responseType: Class<T>): T { + return when (HttpMethod.resolve(methodType)) { HttpMethod.GET -> getNB(path, additionalHeaders, responseType) - HttpMethod.POST -> postNB(path, request, additionalHeaders, responseType) + HttpMethod.POST -> postNB(path, request, additionalHeaders, + responseType) HttpMethod.DELETE -> deleteNB(path, additionalHeaders, responseType) - HttpMethod.PUT -> putNB(path, request, additionalHeaders, responseType) - HttpMethod.PATCH -> patchNB(path, request, additionalHeaders, responseType) - else -> throw BluePrintProcessorException("Unsupported methodType($methodType)") + HttpMethod.PUT -> putNB(path, request, additionalHeaders, + responseType) + HttpMethod.PATCH -> patchNB(path, request, additionalHeaders, + responseType) + else -> throw BluePrintProcessorException("Unsupported method" + + "Type($methodType)") } } @@ -228,7 +257,17 @@ interface BlueprintWebClientService { } } - private fun basicHeaders(headers: Map<String, String>?): Array<BasicHeader> { + private fun <T> getResponse(it: InputStream, responseType: Class<T>): T { + return if (responseType == String::class.java) { + IOUtils.toString(it, Charset.defaultCharset()) as T + } else { + JacksonUtils.readValue(it, responseType)!! + } + } + + private fun basicHeaders(headers: Map<String, String>?): + Array<BasicHeader> { + val basicHeaders = mutableListOf<BasicHeader>() defaultHeaders().forEach { name, value -> basicHeaders.add(BasicHeader(name, value)) @@ -238,4 +277,12 @@ interface BlueprintWebClientService { } return basicHeaders.toTypedArray() } + + // Non Blocking Rest Implementation + suspend fun httpClientNB(): CloseableHttpClient { + return HttpClients.custom() + .addInterceptorFirst(WebClientUtils.logRequest()) + .addInterceptorLast(WebClientUtils.logResponse()) + .build() + } }
\ No newline at end of file diff --git a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/SSLBasicAuthRestClientService.kt b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/SSLBasicAuthRestClientService.kt deleted file mode 100644 index 3d3b0a57e..000000000 --- a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/SSLBasicAuthRestClientService.kt +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright © 2017-2019 AT&T, Bell Canada - * - * 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.cds.blueprintsprocessor.rest.service - -import org.apache.http.conn.ssl.SSLConnectionSocketFactory -import org.apache.http.impl.client.CloseableHttpClient -import org.apache.http.impl.client.HttpClients -import org.apache.http.ssl.SSLContextBuilder -import org.onap.ccsdk.cds.blueprintsprocessor.rest.SSLBasicAuthRestClientProperties -import org.onap.ccsdk.cds.blueprintsprocessor.rest.utils.WebClientUtils -import org.springframework.http.HttpHeaders -import org.springframework.http.MediaType -import java.io.File -import java.io.FileInputStream -import java.security.KeyStore -import java.security.cert.X509Certificate - -class SSLBasicAuthRestClientService(private val restClientProperties: SSLBasicAuthRestClientProperties) : - BlueprintWebClientService { - - override fun defaultHeaders(): Map<String, String> { - return mapOf( - HttpHeaders.CONTENT_TYPE to MediaType.APPLICATION_JSON_VALUE, - HttpHeaders.ACCEPT to MediaType.APPLICATION_JSON_VALUE) - } - - override fun host(uri: String): String { - return restClientProperties.url + uri - } - - override fun httpClient(): CloseableHttpClient { - - val keystoreInstance = restClientProperties.keyStoreInstance - val sslKey = restClientProperties.sslKey - val sslKeyPwd = restClientProperties.sslKeyPassword - val sslTrust = restClientProperties.sslTrust - val sslTrustPwd = restClientProperties.sslTrustPassword - - val acceptingTrustStrategy = { chain: Array<X509Certificate>, authType: String -> true } - - FileInputStream(sslKey).use { keyInput -> - val keyStore = KeyStore.getInstance(keystoreInstance) - keyStore.load(keyInput, sslKeyPwd.toCharArray()) - - val sslContext = - SSLContextBuilder.create() - .loadKeyMaterial(keyStore, sslKeyPwd.toCharArray()) - .loadTrustMaterial(File(sslTrust), sslTrustPwd.toCharArray(), acceptingTrustStrategy).build() - - val csf = SSLConnectionSocketFactory(sslContext!!) - - return HttpClients.custom() - .addInterceptorFirst(WebClientUtils.logRequest()) - .addInterceptorLast(WebClientUtils.logResponse()) - .setSSLSocketFactory(csf).build() - } - } -}
\ No newline at end of file diff --git a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/SSLRestClientService.kt b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/SSLRestClientService.kt new file mode 100644 index 000000000..30dd49018 --- /dev/null +++ b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/SSLRestClientService.kt @@ -0,0 +1,125 @@ +/* + * Copyright © 2017-2019 AT&T, Bell Canada + * Modifications Copyright © 2019 Huawei. + * + * 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.cds.blueprintsprocessor.rest.service + +import org.apache.http.conn.ssl.SSLConnectionSocketFactory +import org.apache.http.impl.client.CloseableHttpClient +import org.apache.http.impl.client.HttpClients +import org.apache.http.message.BasicHeader +import org.apache.http.ssl.SSLContextBuilder +import org.onap.ccsdk.cds.blueprintsprocessor.rest.SSLBasicAuthRestClientProperties +import org.onap.ccsdk.cds.blueprintsprocessor.rest.SSLRestClientProperties +import org.onap.ccsdk.cds.blueprintsprocessor.rest.SSLTokenAuthRestClientProperties +import org.onap.ccsdk.cds.blueprintsprocessor.rest.utils.WebClientUtils +import org.springframework.http.HttpHeaders +import org.springframework.http.MediaType +import java.io.File +import java.io.FileInputStream +import java.security.KeyStore +import java.security.cert.X509Certificate + +class SSLRestClientService(private val restClientProperties: + SSLRestClientProperties) : + BlueprintWebClientService { + + var auth: BlueprintWebClientService? = null + + init { + auth = getAuthService() + } + + private fun getAuthService() : BlueprintWebClientService? { + + return when(restClientProperties) { + is SSLBasicAuthRestClientProperties -> { + val basic = restClientProperties.basicAuth!! + BasicAuthRestClientService(basic) + } + is SSLTokenAuthRestClientProperties -> { + val token = restClientProperties.tokenAuth!! + TokenAuthRestClientService(token) + } + else -> { + //Returns null for No auth + null + } + } + } + + + override fun defaultHeaders(): Map<String, String> { + + if (auth != null) { + return auth!!.defaultHeaders() + } + return mapOf( + HttpHeaders.CONTENT_TYPE to MediaType.APPLICATION_JSON_VALUE, + HttpHeaders.ACCEPT to MediaType.APPLICATION_JSON_VALUE) + } + + override fun host(uri: String): String { + + return restClientProperties.url + uri + } + + override fun httpClient(): CloseableHttpClient { + + val keystoreInstance = restClientProperties.keyStoreInstance + val sslKey = restClientProperties.sslKey + val sslKeyPwd = restClientProperties.sslKeyPassword + val sslTrust = restClientProperties.sslTrust + val sslTrustPwd = restClientProperties.sslTrustPassword + + val acceptingTrustStrategy = { chain: Array<X509Certificate>, + authType: String -> true } + val sslContext = SSLContextBuilder.create() + + if (sslKey != null && sslKeyPwd != null) { + FileInputStream(sslKey).use { keyInput -> + val keyStore = KeyStore.getInstance(keystoreInstance) + keyStore.load(keyInput, sslKeyPwd.toCharArray()) + sslContext.loadKeyMaterial(keyStore, sslKeyPwd.toCharArray()) + } + } + + sslContext.loadTrustMaterial(File(sslTrust), sslTrustPwd.toCharArray(), + acceptingTrustStrategy) + val csf = SSLConnectionSocketFactory(sslContext.build()) + return HttpClients.custom() + .addInterceptorFirst(WebClientUtils.logRequest()) + .addInterceptorLast(WebClientUtils.logResponse()) + .setSSLSocketFactory(csf).build() + + } + + // Non Blocking Rest Implementation + override suspend fun httpClientNB(): CloseableHttpClient { + return httpClient() + } + + override fun convertToBasicHeaders(headers: Map<String, String>): Array<BasicHeader> { + var head1: Map<String, String> = defaultHeaders() + var head2: MutableMap<String, String> = head1.toMutableMap() + head2.putAll(headers) + if (auth != null) { + return auth!!.convertToBasicHeaders(head2) + } + return super.convertToBasicHeaders(head2) + } + +}
\ No newline at end of file diff --git a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/TokenAuthRestClientService.kt b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/TokenAuthRestClientService.kt index 6f81ee1a8..82446994c 100644 --- a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/TokenAuthRestClientService.kt +++ b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/TokenAuthRestClientService.kt @@ -21,17 +21,21 @@ import org.onap.ccsdk.cds.blueprintsprocessor.rest.TokenAuthRestClientProperties import org.springframework.http.HttpHeaders import org.springframework.http.MediaType -class TokenAuthRestClientService(private val restClientProperties: TokenAuthRestClientProperties) : - BlueprintWebClientService { +class TokenAuthRestClientService(private val restClientProperties: + TokenAuthRestClientProperties) : + BlueprintWebClientService { override fun defaultHeaders(): Map<String, String> { + return mapOf( HttpHeaders.CONTENT_TYPE to MediaType.APPLICATION_JSON_VALUE, HttpHeaders.ACCEPT to MediaType.APPLICATION_JSON_VALUE, HttpHeaders.AUTHORIZATION to restClientProperties.token!!) } - override fun convertToBasicHeaders(headers: Map<String, String>): Array<BasicHeader> { + override fun convertToBasicHeaders(headers: Map<String, String>): + Array<BasicHeader> { + val customHeaders: MutableMap<String, String> = headers.toMutableMap() if (!headers.containsKey(HttpHeaders.AUTHORIZATION)) { customHeaders[HttpHeaders.AUTHORIZATION] = restClientProperties.token!! |