Open Kilda Java Documentation
LoggingRequestInterceptor.java
Go to the documentation of this file.
1 /* Copyright 2018 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.testing.tools;
17 
18 import static java.lang.String.format;
19 
20 import com.fasterxml.jackson.databind.ObjectMapper;
21 import com.fasterxml.jackson.databind.SerializationFeature;
22 import com.google.common.base.Strings;
23 import org.apache.commons.io.IOUtils;
24 import org.apache.commons.lang3.StringUtils;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.http.HttpRequest;
28 import org.springframework.http.client.ClientHttpRequestExecution;
29 import org.springframework.http.client.ClientHttpRequestInterceptor;
30 import org.springframework.http.client.ClientHttpResponse;
31 
32 import java.io.BufferedReader;
33 import java.io.IOException;
34 import java.io.InputStreamReader;
35 
36 public class LoggingRequestInterceptor implements ClientHttpRequestInterceptor {
37  private static final Logger log = LoggerFactory.getLogger(LoggingRequestInterceptor.class);
38 
39  @Override
40  public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
41  throws IOException {
42  traceRequest(request, body);
43  ClientHttpResponse response = execution.execute(request, body);
44  traceResponse(response);
45  return response;
46  }
47 
48  private void traceRequest(HttpRequest request, byte[] body) throws IOException {
49  log.debug(format("\n%1$s request begin %1$s\n%2$s%1$s request end %1$s",
50  Strings.repeat("=", 20), genereateCurl(request, body)));
51  }
52 
53  private void traceResponse(ClientHttpResponse response) throws IOException {
54  StringBuilder inputStringBuilder = new StringBuilder();
55  BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getBody(), "UTF-8"));
56  inputStringBuilder.append(IOUtils.toString(bufferedReader));
57  log.debug(format("\n%s response begin %1$s\n"
58  + "Status code : %s\nStatus text : %s\nHeaders : %s\nResponse body: %s\n"
59  + "%1$s response end %1$s", Strings.repeat("=", 20),
60  response.getStatusCode(), response.getStatusText(), response.getHeaders(),
61  toJson(inputStringBuilder.toString())));
62  }
63 
64  private String toJson(String str) {
65  try {
66  ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
67  Object o = mapper.readValue(str, Object.class);
68  return mapper.writeValueAsString(o);
69  } catch (Exception e) {
70  return str;
71  }
72  }
73 
74  private String genereateCurl(HttpRequest request, byte[] payload) throws IOException {
75  StringBuilder sb = new StringBuilder();
76  sb.append(format("curl -X %s \\\n", request.getMethodValue()));
77  sb.append(request.getURI().toString()).append(" \\\n");
78  request.getHeaders().forEach((k, v) -> sb.append(format("-H '%s: %s' \\\n", k,
79  StringUtils.substringBetween(v.toString(), "[", "]"))));
80  String body = new String(payload, "UTF-8");
81  if (!StringUtils.isEmpty(body)) {
82  sb.append(format("-d '%s' \n", body));
83  }
84  return sb.toString();
85  }
86 }
ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)