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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
14 * ============LICENSE_END========================================================================
16 package org.onap.dcaegen2.collectors.datafile.web;
17 import java.io.IOException;
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;
26 public class RequestResponseLoggingInterceptor implements ClientHttpRequestInterceptor {
28 private final Logger log = LoggerFactory.getLogger(this.getClass());
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);
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================================================");
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=================================================");