f3177e4c8be45578b710b91bb933b2a9e7c3a249
[dcaegen2/services/sdk.git] /
1 /*
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration;
22
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;
32
33 /**
34  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 11/15/18
35  */
36
37 public class CloudHttpClient {
38
39     private static final Logger LOGGER = LoggerFactory.getLogger(CloudHttpClient.class);
40
41     private final Gson gson;
42     private final WebClient webClient;
43
44     public CloudHttpClient() {
45         this(WebClient.builder().filter(logRequest()).filter(logResponse()).build());
46     }
47
48     CloudHttpClient(WebClient webClient) {
49         this.gson = new Gson();
50         this.webClient = webClient;
51     }
52
53     public <T> Mono<T> callHttpGet(String url, Class<T> genericClassDeclaration) {
54         return webClient
55             .get()
56             .uri(url)
57             .retrieve()
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));
62     }
63
64     private RuntimeException getException(ClientResponse response) {
65         return new RuntimeException(String.format("Request for cloud config failed: HTTP %d",
66             response.statusCode().value()));
67     }
68
69     private <T> Mono<T> getJsonFromRequest(String body, Class<T> genericClassDeclaration) {
70         try {
71             return Mono.just(parseJson(body, genericClassDeclaration));
72         } catch (JsonSyntaxException | IllegalStateException e) {
73             return Mono.error(e);
74         }
75     }
76
77     private <T> T parseJson(String body, Class<T> genericClassDeclaration) {
78         return gson.fromJson(body, genericClassDeclaration);
79     }
80
81     private static ExchangeFilterFunction logResponse() {
82         return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
83             LOGGER.info("Response status {}", clientResponse.statusCode());
84             return Mono.just(clientResponse);
85         });
86     }
87
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);
94         });
95     }
96
97 }