01830ff4242a82fc0bed1c05ce6249ada0e13a44
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2024 OpenInfra Foundation Europe. 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.ccsdk.oran.a1policymanagementservice.util.v3;
22
23 import org.reactivestreams.Publisher;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.springframework.core.io.buffer.DataBuffer;
27 import org.springframework.http.server.reactive.ServerHttpRequest;
28 import org.springframework.http.server.reactive.ServerHttpRequestDecorator;
29 import org.springframework.http.server.reactive.ServerHttpResponse;
30 import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
31 import org.springframework.util.MultiValueMap;
32 import org.springframework.web.server.ServerWebExchange;
33 import org.springframework.web.server.WebFilter;
34 import org.springframework.web.server.WebFilterChain;
35 import reactor.core.publisher.Flux;
36 import reactor.core.publisher.Mono;
37
38 import java.io.ByteArrayOutputStream;
39 import java.io.IOException;
40 import java.lang.invoke.MethodHandles;
41 import java.nio.channels.Channels;
42 import java.nio.charset.StandardCharsets;
43
44 public class ReactiveEntryExitFilter implements WebFilter {
45
46     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
47     @Override
48     public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
49         ServerHttpRequest httpRequest = exchange.getRequest();
50         ServerHttpResponse httpResponse = exchange.getResponse();
51         MultiValueMap<String, String> queryParams = httpRequest.getQueryParams();
52         logger.info("Request received with path: {}, and the Request Id: {}, with HTTP Method: {}", httpRequest.getPath(),
53                 exchange.getRequest().getId(), exchange.getRequest().getMethod());
54         if (!queryParams.isEmpty())
55             logger.trace("For the request Id: {}, the Query parameters are: {}", exchange.getRequest().getId(), queryParams);
56
57         ServerHttpRequestDecorator loggingServerHttpRequestDecorator = new ServerHttpRequestDecorator(exchange.getRequest()) {
58             String requestBody;
59             @Override
60             public Flux<DataBuffer> getBody() {
61                 return super.getBody().doOnNext(dataBuffer -> {
62                     requestBody = dataBuffer.toString(StandardCharsets.UTF_8); // Read the bytes from the DataBuffer
63                     if (!(requestBody.isEmpty() && requestBody.isBlank()))
64                         logger.trace("For the request ID: {} the received request body: {}", exchange.getRequest().getId(), requestBody);
65                 });
66             }
67         };
68         ServerHttpResponseDecorator loggingServerHttpResponseDecorator = new ServerHttpResponseDecorator(exchange.getResponse()) {
69             String responseBody;
70             @Override
71             public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
72                 Flux<DataBuffer> buffer = Flux.from(body);
73                 return super.writeWith(buffer.doOnNext(dataBuffer -> {
74 //                    try {
75                         dataBuffer.toString(StandardCharsets.UTF_8);
76                         logger.info("For the request ID: {} the Status code of the response: {}", exchange.getRequest().getId(), httpResponse.getStatusCode());
77                         logger.trace("For the request ID: {} the response is: {} ", exchange.getRequest().getId(), responseBody);
78 //                    } catch (Exception e) {
79 //                        logger.info("For the request ID: {} we got an Error during processing: {}", exchange.getRequest().getId(), e.getMessage());
80 //                        logger.trace("For the request ID: {} the response body :: {}", exchange.getRequest().getId(), responseBody);
81 //                    }
82                 }));
83             }
84         };
85         return chain.filter(exchange.mutate().request(loggingServerHttpRequestDecorator).response(loggingServerHttpResponseDecorator).build());
86     }
87
88
89 }