9eb6ee62699762ccb675843c71224da0dbdd572a
[dcaegen2/services/prh.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * PNF-REGISTRATION-HANDLER
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.prh.service.consumer;
22
23 import static org.onap.dcaegen2.services.prh.model.logging.MdcVariables.RESPONSE_CODE;
24 import static org.onap.dcaegen2.services.prh.model.logging.MdcVariables.SERVICE_NAME;
25
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.slf4j.MDC;
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 7/4/18
35  */
36 public class DMaaPReactiveWebClient {
37
38     private final Logger logger = LoggerFactory.getLogger(this.getClass());
39
40     /**
41      * Construct Reactive WebClient with appropriate settings.
42      *
43      * @return WebClient
44      */
45     public WebClient build() {
46         return WebClient.builder()
47             .filter(logRequest())
48             .filter(logResponse())
49             .build();
50     }
51
52     private ExchangeFilterFunction logResponse() {
53         return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
54             MDC.put(RESPONSE_CODE, String.valueOf(clientResponse.statusCode()));
55             logger.info("Response Status {}", clientResponse.statusCode());
56             MDC.remove(RESPONSE_CODE);
57             return Mono.just(clientResponse);
58         });
59     }
60
61     private ExchangeFilterFunction logRequest() {
62         return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
63             MDC.put(SERVICE_NAME, String.valueOf(clientRequest.url()));
64             logger.info("Request: {} {}", clientRequest.method(), clientRequest.url());
65             clientRequest.headers()
66                 .forEach((name, values) -> values.forEach(value -> logger.info("{}={}", name, value)));
67             MDC.remove(SERVICE_NAME);
68             return Mono.just(clientRequest);
69         });
70     }
71
72 }