-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue-ID: CCSDK-4040 Change-Id: I0dc6a7d398b4d414a5c82bb31f61e36be31134a0 Signed-off-by: Raviteja Karumuri <raviteja.karumuri@est.tech>
- Loading branch information
1 parent
e76775d
commit 3477cf3
Showing
4 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...nap/ccsdk/oran/a1policymanagementservice/configuration/ReactiveEntryExitFilterConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.onap.ccsdk.oran.a1policymanagementservice.configuration; | ||
|
||
import org.onap.ccsdk.oran.a1policymanagementservice.util.v3.ReactiveEntryExitFilter; | ||
import org.onap.ccsdk.oran.a1policymanagementservice.util.v3.ReactiveEntryExitFilterCondition; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Conditional; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.server.WebFilter; | ||
|
||
@Configuration | ||
public class ReactiveEntryExitFilterConfig { | ||
@Bean | ||
@Conditional(ReactiveEntryExitFilterCondition.class) | ||
public WebFilter reactiveEntryExitFilter() { | ||
return new ReactiveEntryExitFilter(); | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
...n/java/org/onap/ccsdk/oran/a1policymanagementservice/util/v3/ReactiveEntryExitFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/*- | ||
* ========================LICENSE_START================================= | ||
* ONAP : ccsdk oran | ||
* ====================================================================== | ||
* Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved. | ||
* ====================================================================== | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* ========================LICENSE_END=================================== | ||
*/ | ||
|
||
package org.onap.ccsdk.oran.a1policymanagementservice.util.v3; | ||
|
||
import org.reactivestreams.Publisher; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.core.io.buffer.DataBuffer; | ||
import org.springframework.http.server.reactive.ServerHttpRequest; | ||
import org.springframework.http.server.reactive.ServerHttpRequestDecorator; | ||
import org.springframework.http.server.reactive.ServerHttpResponse; | ||
import org.springframework.http.server.reactive.ServerHttpResponseDecorator; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.MultiValueMap; | ||
import org.springframework.web.server.ServerWebExchange; | ||
import org.springframework.web.server.WebFilter; | ||
import org.springframework.web.server.WebFilterChain; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.lang.invoke.MethodHandles; | ||
import java.nio.channels.Channels; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
public class ReactiveEntryExitFilter implements WebFilter { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); | ||
@Override | ||
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { | ||
ServerHttpRequest httpRequest = exchange.getRequest(); | ||
ServerHttpResponse httpResponse = exchange.getResponse(); | ||
MultiValueMap<String, String> queryParams = httpRequest.getQueryParams(); | ||
logger.info("Request received with path: {} and the Request Id: {}", httpRequest.getPath(), exchange.getRequest().getId()); | ||
if (!queryParams.isEmpty()) | ||
logger.trace("For the request Id: {}, the Query parameters are: {}", exchange.getRequest().getId(), queryParams); | ||
|
||
ServerHttpRequestDecorator loggingServerHttpRequestDecorator = new ServerHttpRequestDecorator(exchange.getRequest()) { | ||
String requestBody = ""; | ||
@Override | ||
public Flux<DataBuffer> getBody() { | ||
return super.getBody().doOnNext(dataBuffer -> { | ||
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) { | ||
Channels.newChannel(byteArrayOutputStream).write(dataBuffer.asByteBuffer().asReadOnlyBuffer()); | ||
requestBody = byteArrayOutputStream.toString(StandardCharsets.UTF_8); | ||
if (requestBody != null) | ||
logger.trace("For the request ID: {} the received request body: {}", exchange.getRequest().getId(), requestBody); | ||
} catch (IOException e) { | ||
logger.info("For the request ID: {} we got the error during processing: {}", exchange.getRequest().getId(), e.getMessage()); | ||
logger.trace("For the request ID: {} the received request body: {}", exchange.getRequest().getId(), requestBody); | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
ServerHttpResponseDecorator loggingServerHttpResponseDecorator = new ServerHttpResponseDecorator(exchange.getResponse()) { | ||
String responseBody = ""; | ||
@Override | ||
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) { | ||
Flux<DataBuffer> buffer = Flux.from(body); | ||
return super.writeWith(buffer.doOnNext(dataBuffer -> { | ||
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) { | ||
Channels.newChannel(byteArrayOutputStream).write(dataBuffer.toByteBuffer().asReadOnlyBuffer()); | ||
responseBody = byteArrayOutputStream.toString(StandardCharsets.UTF_8); | ||
logger.info("For the request ID: {} the Status code of the response: {}", exchange.getRequest().getId(), httpResponse.getStatusCode()); | ||
logger.trace("For the request ID: {} the response is: {} ", exchange.getRequest().getId(), responseBody); | ||
} catch (Exception e) { | ||
logger.info("For the request ID: {} we got an Error during processing: {}", exchange.getRequest().getId(), e.getMessage()); | ||
logger.trace("For the request ID: {} the response body :: {}", exchange.getRequest().getId(), responseBody); | ||
} | ||
})); | ||
} | ||
}; | ||
return chain.filter(exchange.mutate().request(loggingServerHttpRequestDecorator).response(loggingServerHttpResponseDecorator).build()); | ||
} | ||
|
||
|
||
} |
21 changes: 21 additions & 0 deletions
21
...g/onap/ccsdk/oran/a1policymanagementservice/util/v3/ReactiveEntryExitFilterCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.onap.ccsdk.oran.a1policymanagementservice.util.v3; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.context.annotation.Condition; | ||
import org.springframework.context.annotation.ConditionContext; | ||
import org.springframework.core.type.AnnotatedTypeMetadata; | ||
|
||
import java.lang.invoke.MethodHandles; | ||
|
||
public class ReactiveEntryExitFilterCondition implements Condition { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); | ||
|
||
@Override | ||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { | ||
String reactiveFilterEnabled = context.getEnvironment().getProperty("logging.reactive-entry-exit-filter-enabled", "false"); | ||
logger.info("Reactive Entry Exit filter is enabled: {}", reactiveFilterEnabled); | ||
return Boolean.parseBoolean(reactiveFilterEnabled); | ||
} | ||
} |