2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T 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=========================================================
22 package org.onap.logging.filter.spring;
24 import java.io.IOException;
25 import java.nio.charset.Charset;
26 import java.nio.charset.StandardCharsets;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.http.HttpRequest;
30 import org.springframework.http.client.ClientHttpRequestExecution;
31 import org.springframework.http.client.ClientHttpRequestInterceptor;
32 import org.springframework.http.client.ClientHttpResponse;
33 import org.springframework.util.StreamUtils;
35 public class SpringClientPayloadFilter implements ClientHttpRequestInterceptor {
36 private final Logger logger = LoggerFactory.getLogger(this.getClass());
39 public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
41 logRequest(request, body);
42 ClientHttpResponse response = execution.execute(request, body);
43 logResponse(response);
47 protected void logRequest(HttpRequest request, byte[] body) throws IOException {
48 if (logger.isDebugEnabled()) {
49 logger.debug("===========================request begin================================================");
50 logger.debug("URI : {}", request.getURI());
51 logger.debug("Method : {}", request.getMethod());
52 logger.debug("Headers : {}", request.getHeaders());
53 logger.debug("Request body: {}", new String(body, StandardCharsets.UTF_8));
54 logger.debug("==========================request end================================================");
58 protected void logResponse(ClientHttpResponse response) throws IOException {
59 if (logger.isDebugEnabled()) {
60 logger.debug("============================response begin==========================================");
61 logger.debug("Status code : {}", response.getStatusCode());
62 logger.debug("Status text : {}", response.getStatusText());
63 logger.debug("Headers : {}", response.getHeaders());
64 logger.debug("Response body: {}", StreamUtils.copyToString(response.getBody(), Charset.defaultCharset()));
65 logger.debug("=======================response end=================================================");