2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2018 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=========================================================
21 package org.onap.so.logging.jaxrs.filter.jersey;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.springframework.http.HttpRequest;
26 import org.springframework.http.client.ClientHttpRequestExecution;
27 import org.springframework.http.client.ClientHttpRequestInterceptor;
28 import org.springframework.http.client.ClientHttpResponse;
29 import org.springframework.util.StreamUtils;
31 import java.io.IOException;
32 import java.nio.charset.Charset;
34 public class SpringClientFilter implements ClientHttpRequestInterceptor {
36 private final Logger log = LoggerFactory.getLogger(this.getClass());
39 public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
40 logRequest(request, body);
41 ClientHttpResponse response = execution.execute(request, body);
42 logResponse(response);
46 private void logRequest(HttpRequest request, byte[] body) throws IOException {
47 if (log.isDebugEnabled()) {
48 log.debug("===========================request begin================================================");
49 log.debug("URI : {}", request.getURI());
50 log.debug("Method : {}", request.getMethod());
51 log.debug("Headers : {}", request.getHeaders());
52 log.debug("Request body: {}", new String(body, "UTF-8"));
53 log.debug("==========================request end================================================");
57 private void logResponse(ClientHttpResponse response) throws IOException {
58 if (log.isDebugEnabled()) {
59 log.debug("============================response begin==========================================");
60 log.debug("Status code : {}", response.getStatusCode());
61 log.debug("Status text : {}", response.getStatusText());
62 log.debug("Headers : {}", response.getHeaders());
63 log.debug("Response body: {}", StreamUtils.copyToString(response.getBody(), Charset.defaultCharset()));
64 log.debug("=======================response end=================================================");