package org.security.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

@Slf4j
public class LoggingClientHttpRequestInterceptor implements ClientHttpRequestInterceptor {
	@Override
	public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
		traceRequest(request, body);
		ClientHttpResponse response = execution.execute(request, body);
		traceResponse(response);
		return response;
	}

	private void traceRequest(HttpRequest request, byte[] body) {
		log.info("==================== resttemplate request begin ====================");
		log.info("uri           : {}", request.getURI());
		log.info("method        : {}", request.getMethod());
		log.info("headers       : {}", request.getHeaders());
		log.info("request body  : {}", new String(body, StandardCharsets.UTF_8));
		log.info("==================== resttemplate request end ====================");
	}

	private void traceResponse(ClientHttpResponse httpResponse) throws IOException {
		StringBuilder inputStringBuilder = new StringBuilder();
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpResponse.getBody(), StandardCharsets.UTF_8));
		String line = bufferedReader.readLine();
		while (null != line) {
			inputStringBuilder.append(line);
			inputStringBuilder.append('\n');
			line = bufferedReader.readLine();
		}
		log.info("==================== resttemplate response begin ====================");
		log.info("Status code   : {}", httpResponse.getStatusCode());
		log.info("Status text   : {}", httpResponse.getStatusText());
		log.info("Headers       : {}", httpResponse.getHeaders());
		log.info("Response body : {}", inputStringBuilder.toString());
		log.info("==================== resttemplate response end ====================");
	}
}
