Merge "Added Microservice links to index"
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / rest-lib / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / rest / service / BlueprintWebClientService.kt
1 /*
2  * Copyright © 2017-2019 AT&T, Bell Canada, Nordix Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.rest.service
18
19 import org.apache.commons.io.IOUtils
20 import org.apache.http.client.methods.*
21 import org.apache.http.entity.StringEntity
22 import org.apache.http.impl.client.CloseableHttpClient
23 import org.apache.http.impl.client.HttpClients
24 import org.apache.http.message.BasicHeader
25 import org.onap.ccsdk.cds.blueprintsprocessor.rest.utils.WebClientUtils
26 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
27 import org.springframework.http.HttpMethod
28 import java.nio.charset.Charset
29
30 interface BlueprintWebClientService {
31
32     fun defaultHeaders(): Map<String, String>
33
34     fun host(uri: String): String
35
36     fun httpClient(): CloseableHttpClient {
37         return HttpClients.custom()
38             .addInterceptorFirst(WebClientUtils.logRequest())
39             .addInterceptorLast(WebClientUtils.logResponse())
40             .build()
41     }
42
43     fun exchangeResource(methodType: String, path: String, request: String): String {
44         return this.exchangeResource(methodType, path, request, defaultHeaders())
45     }
46
47     fun exchangeResource(methodType: String, path: String, request: String, headers: Map<String, String>): String {
48         val convertedHeaders: Array<BasicHeader> = convertToBasicHeaders(headers)
49         return when (HttpMethod.resolve(methodType)) {
50             HttpMethod.DELETE -> delete(path, convertedHeaders)
51             HttpMethod.GET -> get(path, convertedHeaders)
52             HttpMethod.POST -> post(path, request, convertedHeaders)
53             HttpMethod.PUT -> put(path, request, convertedHeaders)
54             HttpMethod.PATCH -> patch(path, request, convertedHeaders)
55             else -> throw BluePrintProcessorException("Unsupported methodType($methodType)")
56         }
57     }
58
59     fun convertToBasicHeaders(headers: Map<String, String>): Array<BasicHeader> {
60         return headers.map{ BasicHeader(it.key, it.value)}.toTypedArray()
61     }
62
63     fun delete(path: String, headers: Array<BasicHeader>): String {
64         val httpDelete = HttpDelete(host(path))
65         httpDelete.setHeaders(headers)
66         httpClient().execute(httpDelete).entity.content.use {
67             return IOUtils.toString(it, Charset.defaultCharset())
68         }
69     }
70
71     fun get(path: String, headers: Array<BasicHeader>): String {
72         val httpGet = HttpGet(host(path))
73         httpGet.setHeaders(headers)
74         httpClient().execute(httpGet).entity.content.use {
75             return IOUtils.toString(it, Charset.defaultCharset())
76         }
77     }
78
79     fun post(path: String, request: String, headers: Array<BasicHeader>): String {
80         val httpPost = HttpPost(host(path))
81         val entity = StringEntity(request)
82         httpPost.entity = entity
83         httpPost.setHeaders(headers)
84         httpClient().execute(httpPost).entity.content.use {
85             return IOUtils.toString(it, Charset.defaultCharset())
86         }
87     }
88
89     fun put(path: String, request: String, headers: Array<BasicHeader>): String {
90         val httpPut = HttpPut(host(path))
91         val entity = StringEntity(request)
92         httpPut.entity = entity
93         httpPut.setHeaders(headers)
94         httpClient().execute(httpPut).entity.content.use {
95             return IOUtils.toString(it, Charset.defaultCharset())
96         }
97     }
98
99     fun patch(path: String, request: String, headers: Array<BasicHeader>): String {
100         val httpPatch = HttpPatch(host(path))
101         val entity = StringEntity(request)
102         httpPatch.entity = entity
103         httpPatch.setHeaders(headers)
104         httpClient().execute(httpPatch).entity.content.use {
105             return IOUtils.toString(it, Charset.defaultCharset())
106         }
107     }
108 }