9c2caad70d562ec992e3ba47a1fa68bb2fab797b
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2019 AT&T, Bell Canada
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.apps.blueprintsprocessor.rest.service
18
19 import org.apache.commons.io.IOUtils
20 import org.apache.http.client.methods.HttpDelete
21 import org.apache.http.client.methods.HttpGet
22 import org.apache.http.client.methods.HttpPost
23 import org.apache.http.client.methods.HttpPut
24 import org.apache.http.entity.StringEntity
25 import org.apache.http.impl.client.CloseableHttpClient
26 import org.apache.http.impl.client.HttpClients
27 import org.apache.http.message.BasicHeader
28 import org.onap.ccsdk.apps.blueprintsprocessor.rest.utils.WebClientUtils
29 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException
30 import org.springframework.http.HttpMethod
31 import java.nio.charset.Charset
32
33 interface BlueprintWebClientService {
34
35     fun headers(): Array<BasicHeader>
36
37     fun host(uri: String): String
38
39     fun httpClient(): CloseableHttpClient {
40         return HttpClients.custom()
41             .addInterceptorFirst(WebClientUtils.logRequest())
42             .addInterceptorLast(WebClientUtils.logResponse())
43             .build()
44     }
45
46     fun exchangeResource(methodType: String, path: String, request: String): String {
47         return when (HttpMethod.resolve(methodType)) {
48             HttpMethod.DELETE -> delete(path)
49             HttpMethod.GET -> get(path)
50             HttpMethod.POST -> post(path, request)
51             HttpMethod.PUT -> put(path, request)
52             else -> throw BluePrintProcessorException("Unsupported methodType($methodType)")
53         }
54     }
55
56     fun delete(path: String): String {
57         val httpDelete = HttpDelete(host(path))
58         httpDelete.setHeaders(headers())
59         httpClient().execute(httpDelete).entity.content.use {
60             return IOUtils.toString(it, Charset.defaultCharset())
61         }
62     }
63
64     fun get(path: String): String {
65         val httpGet = HttpGet(host(path))
66         httpGet.setHeaders(headers())
67         httpClient().execute(httpGet).entity.content.use {
68             return IOUtils.toString(it, Charset.defaultCharset())
69         }
70     }
71
72     fun post(path: String, request: String): String {
73         val httpPost = HttpPost(host(path))
74         val entity = StringEntity(request)
75         httpPost.entity = entity
76         httpPost.setHeaders(headers())
77         httpClient().execute(httpPost).entity.content.use {
78             return IOUtils.toString(it, Charset.defaultCharset())
79         }
80     }
81
82     fun put(path: String, request: String): String {
83         val httpPut = HttpPut(host(path))
84         val entity = StringEntity(request)
85         httpPut.entity = entity
86         httpPut.setHeaders(headers())
87         httpClient().execute(httpPut).entity.content.use {
88             return IOUtils.toString(it, Charset.defaultCharset())
89         }
90     }
91 }