2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2019 IBM.
4 * Modifications Copyright © 2020 Orange.
5 * Modifications Copyright © 2020 Deutsche Telekom AG.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 package org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.profile.upload
22 import org.apache.commons.io.IOUtils
23 import org.apache.http.client.ClientProtocolException
24 import org.apache.http.client.entity.EntityBuilder
25 import org.apache.http.client.methods.HttpPost
26 import org.apache.http.client.methods.HttpUriRequest
27 import org.apache.http.message.BasicHeader
28 import org.onap.ccsdk.cds.blueprintsprocessor.rest.BasicAuthRestClientProperties
29 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
30 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.RestLoggerService
31 import org.springframework.http.HttpHeaders
32 import org.springframework.http.MediaType
33 import java.io.IOException
34 import java.nio.charset.Charset
35 import java.nio.file.Files
36 import java.nio.file.Path
37 import java.util.Base64
39 class K8sUploadFileRestClientService(
40 private val restClientProperties:
41 BasicAuthRestClientProperties
42 ) : BlueprintWebClientService {
44 override fun defaultHeaders(): Map<String, String> {
46 val encodedCredentials = setBasicAuth(
47 restClientProperties.username,
48 restClientProperties.password
51 HttpHeaders.CONTENT_TYPE to MediaType.APPLICATION_JSON_VALUE,
52 HttpHeaders.ACCEPT to MediaType.APPLICATION_JSON_VALUE,
53 HttpHeaders.AUTHORIZATION to "Basic $encodedCredentials"
57 override fun host(uri: String): String {
58 return restClientProperties.url + uri
61 override fun convertToBasicHeaders(headers: Map<String, String>):
63 val customHeaders: MutableMap<String, String> = headers.toMutableMap()
64 // inject additionalHeaders
65 customHeaders.putAll(verifyAdditionalHeaders(restClientProperties))
67 if (!headers.containsKey(HttpHeaders.AUTHORIZATION)) {
68 val encodedCredentials = setBasicAuth(
69 restClientProperties.username,
70 restClientProperties.password
72 customHeaders[HttpHeaders.AUTHORIZATION] =
73 "Basic $encodedCredentials"
75 return super.convertToBasicHeaders(customHeaders)
78 private fun setBasicAuth(username: String, password: String): String {
79 val credentialsString = "$username:$password"
80 return Base64.getEncoder().encodeToString(
81 credentialsString.toByteArray(Charset.defaultCharset())
85 @Throws(IOException::class, ClientProtocolException::class)
86 private fun performHttpCall(httpUriRequest: HttpUriRequest): BlueprintWebClientService.WebClientResponse<String> {
87 val httpResponse = httpClient().execute(httpUriRequest)
88 val statusCode = httpResponse.statusLine.statusCode
89 httpResponse.entity.content.use {
90 val body = IOUtils.toString(it, Charset.defaultCharset())
91 return BlueprintWebClientService.WebClientResponse(statusCode, body)
95 fun uploadBinaryFile(path: String, filePath: Path): BlueprintWebClientService.WebClientResponse<String> {
96 val convertedHeaders: Array<BasicHeader> = convertToBasicHeaders(defaultHeaders())
97 val httpPost = HttpPost(host(path))
98 val entity = EntityBuilder.create().setBinary(Files.readAllBytes(filePath)).build()
99 httpPost.setEntity(entity)
100 RestLoggerService.httpInvoking(convertedHeaders)
101 httpPost.setHeaders(convertedHeaders)
102 return performHttpCall(httpPost)