2 * ============LICENSE_START=======================================================
3 * DCAEGEN2-SERVICES-SDK
4 * ================================================================================
5 * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration;
23 import com.google.gson.Gson;
24 import com.google.gson.JsonSyntaxException;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.http.HttpStatus;
28 import org.springframework.web.reactive.function.client.ClientResponse;
29 import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
30 import org.springframework.web.reactive.function.client.WebClient;
31 import reactor.core.publisher.Mono;
34 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 11/15/18
37 public class CloudHttpClient {
39 private static final Logger LOGGER = LoggerFactory.getLogger(CloudHttpClient.class);
41 private final Gson gson;
42 private final WebClient webClient;
44 public CloudHttpClient() {
45 this(WebClient.builder().filter(logRequest()).filter(logResponse()).build());
48 CloudHttpClient(WebClient webClient) {
49 this.gson = new Gson();
50 this.webClient = webClient;
53 public <T> Mono<T> callHttpGet(String url, Class<T> genericClassDeclaration) {
58 .onStatus(HttpStatus::is4xxClientError, response -> Mono.error(getException(response)))
59 .onStatus(HttpStatus::is5xxServerError, response -> Mono.error(getException(response)))
60 .bodyToMono(String.class)
61 .flatMap(body -> getJsonFromRequest(body, genericClassDeclaration));
64 private RuntimeException getException(ClientResponse response) {
65 return new RuntimeException(String.format("Request for cloud config failed: HTTP %d",
66 response.statusCode().value()));
69 private <T> Mono<T> getJsonFromRequest(String body, Class<T> genericClassDeclaration) {
71 return Mono.just(parseJson(body, genericClassDeclaration));
72 } catch (JsonSyntaxException | IllegalStateException e) {
77 private <T> T parseJson(String body, Class<T> genericClassDeclaration) {
78 return gson.fromJson(body, genericClassDeclaration);
81 private static ExchangeFilterFunction logResponse() {
82 return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
83 LOGGER.info("Response status {}", clientResponse.statusCode());
84 return Mono.just(clientResponse);
88 private static ExchangeFilterFunction logRequest() {
89 return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
90 LOGGER.info("Request: {} {}", clientRequest.method(), clientRequest.url());
91 clientRequest.headers()
92 .forEach((name, values) -> values.forEach(value -> LOGGER.info("{}={}", name, value)));
93 return Mono.just(clientRequest);