bf40700e120587d87836308d19de1da8f33b2507
[dcaegen2/collectors/datafile.git] /
1 /*
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2018 NOKIA Intellectual Property, 2018 Nordix Foundation. All rights reserved.
4  * ===============================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6  * in compliance with the License. You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software distributed under the License
11  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12  * or implied. See the License for the specific language governing permissions and limitations under
13  * the License.
14  * ============LICENSE_END========================================================================
15  */
16 package org.onap.dcaegen2.collectors.datafile.service.producer;
17 import java.io.IOException;
18 import java.nio.charset.Charset;
19
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import org.springframework.http.HttpRequest;
23 import org.springframework.http.client.ClientHttpRequestExecution;
24 import org.springframework.http.client.ClientHttpRequestInterceptor;
25 import org.springframework.http.client.ClientHttpResponse;
26 import org.springframework.util.StreamUtils;
27
28 public class RequestResponseLoggingInterceptor implements ClientHttpRequestInterceptor {
29
30     private final Logger log = LoggerFactory.getLogger(this.getClass());
31
32     @Override
33     public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
34         logRequest(request, body);
35         ClientHttpResponse response = execution.execute(request, body);
36         logResponse(response);
37         return response;
38     }
39
40     private void logRequest(HttpRequest request, byte[] body) throws IOException {
41         if (log.isDebugEnabled()) {
42             log.debug("===========================request begin================================================");
43             log.debug("URI         : {}", request.getURI());
44             log.debug("Method      : {}", request.getMethod());
45             log.debug("Headers     : {}", request.getHeaders());
46             log.debug("Request body: {}", new String(body, "UTF-8"));
47             log.debug("==========================request end================================================");
48         }
49     }
50
51     private void logResponse(ClientHttpResponse response) throws IOException {
52         if (log.isDebugEnabled()) {
53             log.debug("============================response begin==========================================");
54             log.debug("Status code  : {}", response.getStatusCode());
55             log.debug("Status text  : {}", response.getStatusText());
56             log.debug("Headers      : {}", response.getHeaders());
57             log.debug("Response body: {}", StreamUtils.copyToString(response.getBody(), Charset.defaultCharset()));
58             log.debug("=======================response end=================================================");
59         }
60     }
61 }