15d459f82f4021316cd1a3a4e6b6f171deb97e67
[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.web;
17 import java.io.IOException;
18
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import org.springframework.http.HttpRequest;
22 import org.springframework.http.client.ClientHttpRequestExecution;
23 import org.springframework.http.client.ClientHttpRequestInterceptor;
24 import org.springframework.http.client.ClientHttpResponse;
25
26 public class RequestResponseLoggingInterceptor implements ClientHttpRequestInterceptor {
27
28     private final Logger log = LoggerFactory.getLogger(this.getClass());
29
30     @Override
31     public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
32         logRequest(request, body);
33         ClientHttpResponse response = execution.execute(request, body);
34         logResponse(response);
35         return response;
36     }
37
38     private void logRequest(HttpRequest request, byte[] body) throws IOException {
39         if (log.isDebugEnabled()) {
40             log.debug("===========================request begin================================================");
41             log.debug("URI         : {}", request.getURI());
42             log.debug("Method      : {}", request.getMethod());
43             log.debug("Headers     : {}", request.getHeaders());
44             log.debug("Request body: {}", new String(body, "UTF-8"));
45             log.debug("==========================request end================================================");
46         }
47     }
48
49     private void logResponse(ClientHttpResponse response) throws IOException {
50         if (log.isDebugEnabled()) {
51             log.debug("============================response begin==========================================");
52             log.debug("Status code  : {}", response.getStatusCode());
53             log.debug("Status text  : {}", response.getStatusText());
54             log.debug("Headers      : {}", response.getHeaders());
55             log.debug("=======================response end=================================================");
56         }
57     }
58 }