2 * Copyright © 2017-2019 AT&T, Bell Canada, Nordix Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.blueprintsprocessor.rest.service
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
30 interface BlueprintWebClientService {
32 fun defaultHeaders(): Map<String, String>
34 fun host(uri: String): String
36 fun httpClient(): CloseableHttpClient {
37 return HttpClients.custom()
38 .addInterceptorFirst(WebClientUtils.logRequest())
39 .addInterceptorLast(WebClientUtils.logResponse())
43 fun exchangeResource(methodType: String, path: String, request: String): String {
44 return this.exchangeResource(methodType, path, request, defaultHeaders())
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)")
59 fun convertToBasicHeaders(headers: Map<String, String>): Array<BasicHeader> {
60 return headers.map{ BasicHeader(it.key, it.value)}.toTypedArray()
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())
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())
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())
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())
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())