e44c4eb048cdcd09930685f4de086355a366ea18
[ccsdk/cds.git] /
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.definition
21
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.functions.k8s.K8sConnectionPluginConfiguration
29 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
30 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.RestLoggerService
31 import java.io.IOException
32 import java.nio.charset.Charset
33 import java.nio.file.Files
34 import java.nio.file.Path
35
36 class K8sUploadFileRestClientService(
37     k8sConfiguration: K8sConnectionPluginConfiguration,
38     definition: String,
39     definitionVersion: String
40 ) : K8sDefinitionRestClient(k8sConfiguration, definition, definitionVersion) {
41
42     @Throws(IOException::class, ClientProtocolException::class)
43     private fun performHttpCall(httpUriRequest: HttpUriRequest): BlueprintWebClientService.WebClientResponse<String> {
44         val httpResponse = httpClient().execute(httpUriRequest)
45         val statusCode = httpResponse.statusLine.statusCode
46         httpResponse.entity.content.use {
47             val body = IOUtils.toString(it, Charset.defaultCharset())
48             return BlueprintWebClientService.WebClientResponse(statusCode, body)
49         }
50     }
51
52     fun uploadBinaryFile(path: String, filePath: Path): BlueprintWebClientService.WebClientResponse<String> {
53         val convertedHeaders: Array<BasicHeader> = convertToBasicHeaders(defaultHeaders())
54         val httpPost = HttpPost(host(path))
55         val entity = EntityBuilder.create().setBinary(Files.readAllBytes(filePath)).build()
56         httpPost.setEntity(entity)
57         RestLoggerService.httpInvoking(convertedHeaders)
58         httpPost.setHeaders(convertedHeaders)
59         return performHttpCall(httpPost)
60     }
61 }