initial integration done
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package zw.qantra.tm.rest;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -8,7 +10,9 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.http.client.BufferingClientHttpRequestFactory;
|
||||
import org.springframework.http.client.ClientHttpRequestExecution;
|
||||
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
@@ -17,7 +21,9 @@ import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import zw.qantra.tm.exceptions.RestTemplateResponseErrorHandler;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@@ -53,16 +59,15 @@ public class RestService {
|
||||
)
|
||||
);
|
||||
|
||||
this.restTemplate.setErrorHandler(new RestTemplateResponseErrorHandler());
|
||||
|
||||
// bootstrap rest template
|
||||
List<ClientHttpRequestInterceptor> interceptors
|
||||
= this.restTemplate.getInterceptors();
|
||||
if (CollectionUtils.isEmpty(interceptors)) {
|
||||
interceptors = new ArrayList<>();
|
||||
}
|
||||
interceptors.add(new RestTemplateInterceptor());
|
||||
interceptors.add(new LoggingInterceptor(this));
|
||||
this.restTemplate.setInterceptors(interceptors);
|
||||
|
||||
}
|
||||
|
||||
public String getUserToken(){
|
||||
@@ -230,4 +235,52 @@ public class RestService {
|
||||
public <T> ResponseEntity<?> exchangeGetNoAuth(String path, HttpEntity<?> httpEntity, Class<?> clazz, String pathVariable) {
|
||||
return this.restTemplate.exchange(path, HttpMethod.GET, httpEntity, clazz, pathVariable);
|
||||
}
|
||||
|
||||
private String prettyPrintJson(String json) {
|
||||
try {
|
||||
Object jsonObject = gson.fromJson(json, Object.class);
|
||||
return new GsonBuilder().setPrettyPrinting().create().toJson(jsonObject);
|
||||
} catch (JsonSyntaxException e) {
|
||||
return json;
|
||||
}
|
||||
}
|
||||
|
||||
private static class LoggingInterceptor implements ClientHttpRequestInterceptor {
|
||||
Logger logger = LoggerFactory.getLogger(LoggingInterceptor.class);
|
||||
private final RestService restService;
|
||||
|
||||
public LoggingInterceptor(RestService restService) {
|
||||
this.restService = restService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
|
||||
ClientHttpRequestExecution execution) throws IOException {
|
||||
logRequest(request, body);
|
||||
ClientHttpResponse response = execution.execute(request, body);
|
||||
logResponse(response);
|
||||
return response;
|
||||
}
|
||||
|
||||
private void logRequest(HttpRequest request, byte[] body) {
|
||||
logger.info("=== REST REQUEST ===");
|
||||
logger.info("Method: {} | URI: {}", request.getMethod(), request.getURI());
|
||||
logger.info("Headers: {}", request.getHeaders());
|
||||
if (body.length > 0) {
|
||||
String bodyStr = new String(body, StandardCharsets.UTF_8);
|
||||
logger.info("Body: {}", restService.prettyPrintJson(bodyStr));
|
||||
}
|
||||
}
|
||||
|
||||
private void logResponse(ClientHttpResponse response) throws IOException {
|
||||
logger.info("=== REST RESPONSE ===");
|
||||
logger.info("Status: {}", response.getStatusCode());
|
||||
logger.info("Headers: {}", response.getHeaders());
|
||||
byte[] responseBody = response.getBody().readAllBytes();
|
||||
if (responseBody.length > 0) {
|
||||
String bodyStr = new String(responseBody, StandardCharsets.UTF_8);
|
||||
logger.info("Body: {}", restService.prettyPrintJson(bodyStr));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user