package com.ofg.infrastructure.web.logging;

import feign.Request;
import feign.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

import static com.ofg.infrastructure.web.logging.HttpDataFactory.createHttpData;

public class FeignCallObfuscatingLogger extends feign.Logger.JavaLogger {

    private static final String TAG = "CLIENT";
    private static final Logger log = LoggerFactory.getLogger(RequestResponseLogger.class);

    private final RequestDataProvider requestDataProvider;
    private final RequestIdProvider requestIdProvider;
    private final RequestResponseLogger requestResponseLogger;

    public FeignCallObfuscatingLogger(RequestDataProvider requestDataProvider, RequestIdProvider requestIdProvider, RequestResponseLogger requestResponseLogger) {
        this.requestDataProvider = requestDataProvider;
        this.requestIdProvider = requestIdProvider;
        this.requestResponseLogger = requestResponseLogger;
    }

    @Override
    protected void logRequest(String configKey, feign.Logger.Level logLevel, Request request) {
        HttpData reqData = createHttpData(request);
        String requestId = requestIdProvider.getRequestId();
        requestDataProvider.store(requestId, reqData);
        requestResponseLogger.logObfuscatedRequest(reqData, TAG);
        super.logRequest(configKey, logLevel, request);
    }

    @Override
    protected Response logAndRebufferResponse(String configKey, feign.Logger.Level logLevel, Response response, long elapsedTime) throws IOException {
        String requestId = requestIdProvider.getRequestId();
        HttpData reqData = requestDataProvider.retrieve(requestId);
        if (requestTraceable(reqData)) {
            HttpData resData = createHttpData(response);
            Response rebufferedResponse = Response.create(response.status(), response.reason(), response.headers(), resData.getContent().getBytes());
            requestResponseLogger.logObfuscatedResponse(reqData, resData, TAG);
            requestDataProvider.remove(requestId);
            return super.logAndRebufferResponse(configKey, logLevel, rebufferedResponse, elapsedTime);
        } else {
            log.debug("Cannot obfuscate response, matching request data lost");
            return super.logAndRebufferResponse(configKey, logLevel, response, elapsedTime);
        }
    }

    private boolean requestTraceable(HttpData reqData) {
        return reqData != null;
    }
}
@@ -49,101 +49,80 @@
 ort 
-feign.Request;%0Aimport feign.Response;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory
+static com.ofg.infrastructure.web.logging.HttpDataFactory.createHttpData
 ;%0A%0Ai
@@ -152,88 +152,151 @@
 on;%0A
-%0A
 import 
-static com.ofg.infrastructure.web.logging.HttpDataFactory.createHttpData
+java.nio.charset.StandardCharsets;%0A%0Aimport feign.Request;%0Aimport feign.Response;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory
 ;%0A%0Ap
@@ -1936,24 +1936,44 @@
 e.headers(),
+%0A                   
  resData.get
@@ -1991,16 +1991,45 @@
 etBytes(
+StandardCharsets.UTF_8.name()
 ));%0A    

fix binary data copying