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