2 * Copyright © 2017-2018 AT&T Intellectual Property.
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.apps.blueprintsprocessor.rest.service
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
28 class BasicAuthRestClientService(private val restClientProperties: BasicAuthRestClientProperties) : BlueprintWebClientService {
30 private var webClient: WebClient? = null
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())
47 override fun <T> getResource(path: String, responseType: Class<T>): T {
48 return getResource(path, null, responseType)
51 override fun <T> getResource(path: String, headers: Map<String, String>?, responseType: Class<T>): T {
52 return webClient().get()
54 .headers { httpHeaders ->
56 httpHeaders.set(it.key, it.value)
60 .bodyToMono(responseType).block()!!
63 override fun <T> postResource(path: String, request: Any, responseType: Class<T>): T {
64 return postResource(path, null, request, responseType)
67 override fun <T> postResource(path: String, headers: Map<String, String>?, request: Any, responseType: Class<T>): T {
68 return webClient().post()
70 .headers { httpHeaders ->
72 httpHeaders.set(it.key, it.value)
75 .body(BodyInserters.fromObject(request))
76 .retrieve().bodyToMono(responseType).block()!!
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.