Updating README.md
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / k8s-profile-upload / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / k8s / profile / upload / K8sUploadFileRestClientService.kt
1 /*
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.
6  *
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 package org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.profile.upload
21
22 import org.onap.ccsdk.cds.blueprintsprocessor.rest.BasicAuthRestClientProperties
23 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
24 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.RestLoggerService
25 import org.apache.commons.io.IOUtils
26 import org.apache.http.client.entity.EntityBuilder
27 import org.apache.http.message.BasicHeader
28 import org.apache.http.client.methods.HttpPost
29 import org.apache.http.client.methods.HttpUriRequest
30 import org.apache.http.client.ClientProtocolException
31 import java.io.IOException
32 import java.nio.file.Files
33 import java.nio.file.Path
34 import org.springframework.http.HttpHeaders
35 import org.springframework.http.MediaType
36 import java.nio.charset.Charset
37 import java.util.Base64
38
39 class K8sUploadFileRestClientService(
40     private val restClientProperties:
41         BasicAuthRestClientProperties
42 ) : BlueprintWebClientService {
43
44     override fun defaultHeaders(): Map<String, String> {
45
46         val encodedCredentials = setBasicAuth(
47             restClientProperties.username,
48             restClientProperties.password
49         )
50         return mapOf(
51             HttpHeaders.CONTENT_TYPE to MediaType.APPLICATION_JSON_VALUE,
52             HttpHeaders.ACCEPT to MediaType.APPLICATION_JSON_VALUE,
53             HttpHeaders.AUTHORIZATION to "Basic $encodedCredentials"
54         )
55     }
56
57     override fun host(uri: String): String {
58         return restClientProperties.url + uri
59     }
60
61     override fun convertToBasicHeaders(headers: Map<String, String>):
62         Array<BasicHeader> {
63             val customHeaders: MutableMap<String, String> = headers.toMutableMap()
64             // inject additionalHeaders
65             customHeaders.putAll(verifyAdditionalHeaders(restClientProperties))
66
67             if (!headers.containsKey(HttpHeaders.AUTHORIZATION)) {
68                 val encodedCredentials = setBasicAuth(
69                     restClientProperties.username,
70                     restClientProperties.password
71                 )
72                 customHeaders[HttpHeaders.AUTHORIZATION] =
73                     "Basic $encodedCredentials"
74             }
75             return super.convertToBasicHeaders(customHeaders)
76         }
77
78     private fun setBasicAuth(username: String, password: String): String {
79         val credentialsString = "$username:$password"
80         return Base64.getEncoder().encodeToString(
81             credentialsString.toByteArray(Charset.defaultCharset())
82         )
83     }
84
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)
92         }
93     }
94
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)
103     }
104 }