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.
16 * SPDX-License-Identifier: Apache-2.0
19 package org.onap.ccsdk.cds.blueprintsprocessor.rest.service
21 import org.apache.commons.io.IOUtils
22 import org.apache.http.client.methods.*
23 import org.apache.http.entity.StringEntity
24 import org.apache.http.impl.client.CloseableHttpClient
25 import org.apache.http.impl.client.HttpClients
26 import org.apache.http.message.BasicHeader
27 import org.onap.ccsdk.cds.blueprintsprocessor.rest.utils.WebClientUtils
28 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
29 import org.springframework.http.HttpMethod
30 import java.nio.charset.Charset
32 interface BlueprintWebClientService {
34 fun defaultHeaders(): Map<String, String>
36 fun host(uri: String): String
38 fun httpClient(): CloseableHttpClient {
39 return HttpClients.custom()
40 .addInterceptorFirst(WebClientUtils.logRequest())
41 .addInterceptorLast(WebClientUtils.logResponse())
45 fun exchangeResource(methodType: String, path: String, request: String): String {
46 return this.exchangeResource(methodType, path, request, defaultHeaders())
49 fun exchangeResource(methodType: String, path: String, request: String, headers: Map<String, String>): String {
50 val convertedHeaders: Array<BasicHeader> = convertToBasicHeaders(headers)
51 return when (HttpMethod.resolve(methodType)) {
52 HttpMethod.DELETE -> delete(path, convertedHeaders)
53 HttpMethod.GET -> get(path, convertedHeaders)
54 HttpMethod.POST -> post(path, request, convertedHeaders)
55 HttpMethod.PUT -> put(path, request, convertedHeaders)
56 HttpMethod.PATCH -> patch(path, request, convertedHeaders)
57 else -> throw BluePrintProcessorException("Unsupported methodType($methodType)")
61 fun convertToBasicHeaders(headers: Map<String, String>): Array<BasicHeader> {
62 val convertedHeaders = Array<BasicHeader>(headers.size){ BasicHeader("","") }
63 var currentElement = 0
64 for ((name, value) in headers) {
65 convertedHeaders[currentElement++] = BasicHeader(name, value)
67 return convertedHeaders
70 fun delete(path: String, headers: Array<BasicHeader>): String {
71 val httpDelete = HttpDelete(host(path))
72 httpDelete.setHeaders(headers)
73 httpClient().execute(httpDelete).entity.content.use {
74 return IOUtils.toString(it, Charset.defaultCharset())
78 fun get(path: String, headers: Array<BasicHeader>): String {
79 val httpGet = HttpGet(host(path))
80 httpGet.setHeaders(headers)
81 httpClient().execute(httpGet).entity.content.use {
82 return IOUtils.toString(it, Charset.defaultCharset())
86 fun post(path: String, request: String, headers: Array<BasicHeader>): String {
87 val httpPost = HttpPost(host(path))
88 val entity = StringEntity(request)
89 httpPost.entity = entity
90 httpPost.setHeaders(headers)
91 httpClient().execute(httpPost).entity.content.use {
92 return IOUtils.toString(it, Charset.defaultCharset())
96 fun put(path: String, request: String, headers: Array<BasicHeader>): String {
97 val httpPut = HttpPut(host(path))
98 val entity = StringEntity(request)
99 httpPut.entity = entity
100 httpPut.setHeaders(headers)
101 httpClient().execute(httpPut).entity.content.use {
102 return IOUtils.toString(it, Charset.defaultCharset())
106 fun patch(path: String, request: String, headers: Array<BasicHeader>): String {
107 val httpPatch = HttpPatch(host(path))
108 val entity = StringEntity(request)
109 httpPatch.entity = entity
110 httpPatch.setHeaders(headers)
111 httpClient().execute(httpPatch).entity.content.use {
112 return IOUtils.toString(it, Charset.defaultCharset())