updating logging filters
[logging-analytics.git] / reference / logging-filter / logging-filter-spring / src / main / java / org / onap / logging / filter / spring / SpringClientPayloadFilter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - Logging
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
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
22 package org.onap.logging.filter.spring;
23
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;
34
35 public class SpringClientPayloadFilter implements ClientHttpRequestInterceptor {
36     private final Logger logger = LoggerFactory.getLogger(this.getClass());
37
38     @Override
39     public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
40             throws IOException {
41         logRequest(request, body);
42         ClientHttpResponse response = execution.execute(request, body);
43         logResponse(response);
44         return response;
45     }
46
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================================================");
55         }
56     }
57
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=================================================");
66         }
67     }
68
69 }