130706d7e768aed1c368a6c037adf80cd3f8849a
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
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.onap.ccsdk.apps.blueprintsprocessor.rest.BasicAuthRestClientProperties
20 import org.onap.ccsdk.apps.blueprintsprocessor.rest.utils.WebClientUtils
21 import org.springframework.http.HttpHeaders
22 import org.springframework.http.MediaType
23 import org.springframework.web.reactive.function.BodyInserters
24 import org.springframework.web.reactive.function.client.ExchangeFilterFunctions
25 import org.springframework.web.reactive.function.client.WebClient
26
27
28 class BasicAuthRestClientService(private val restClientProperties: BasicAuthRestClientProperties) : BlueprintWebClientService {
29
30     private var webClient: WebClient? = null
31
32     override fun webClient(): WebClient {
33         if (webClient == null) {
34             webClient = WebClient.builder()
35                     .baseUrl(restClientProperties.url)
36                     .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
37                     .defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
38                     .filter(ExchangeFilterFunctions
39                             .basicAuthentication(restClientProperties.userId, restClientProperties.token))
40                     .filter(WebClientUtils.logRequest())
41                     .filter(WebClientUtils.logResponse())
42                     .build()
43         }
44         return webClient!!
45     }
46
47     override fun <T> getResource(path: String, responseType: Class<T>): T {
48         return getResource(path, null, responseType)
49     }
50
51     override fun <T> getResource(path: String, headers: Map<String, String>?, responseType: Class<T>): T {
52         return webClient().get()
53                 .uri(path)
54                 .headers { httpHeaders ->
55                     headers?.forEach {
56                         httpHeaders.set(it.key, it.value)
57                     }
58                 }
59                 .retrieve()
60                 .bodyToMono(responseType).block()!!
61     }
62
63     override fun <T> postResource(path: String, request: Any, responseType: Class<T>): T {
64         return postResource(path, null, request, responseType)
65     }
66
67     override fun <T> postResource(path: String, headers: Map<String, String>?, request: Any, responseType: Class<T>): T {
68         return webClient().post()
69                 .uri(path)
70                 .headers { httpHeaders ->
71                     headers?.forEach {
72                         httpHeaders.set(it.key, it.value)
73                     }
74                 }
75                 .body(BodyInserters.fromObject(request))
76                 .retrieve().bodyToMono(responseType).block()!!
77     }
78
79     override fun <T> exchangeResource(methodType: String, path: String, request: Any, responseType: Class<T>): T {
80         TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
81     }
82 }