package de.holisticon.util.tracee.outbound.httpclient;

import de.holisticon.util.tracee.Tracee;
import de.holisticon.util.tracee.TraceeBackend;
import de.holisticon.util.tracee.TraceeConstants;
import de.holisticon.util.tracee.transport.HttpJsonHeaderTransport;
import de.holisticon.util.tracee.transport.TransportSerialization;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.protocol.HttpContext;

import java.io.IOException;

/**
 * TO DO: how to use it.
 *
 * @author Daniel Wegener (Holisticon AG)
 */
public class TraceeHttpRequestInterceptor implements HttpRequestInterceptor {

    private final TraceeBackend backend = Tracee.getBackend();
    private final TransportSerialization<String> transportSerialization = new HttpJsonHeaderTransport(backend.getLoggerFactory());

    @Override
    public final void process(HttpRequest httpRequest, HttpContext httpContext) throws HttpException, IOException {

		if (!backend.isEmpty()) {
            final String contextAsHeader = transportSerialization.render(backend);
            httpRequest.setHeader(TraceeConstants.HTTP_HEADER_NAME, contextAsHeader);
        }

    }

}

@@ -188,16 +188,90 @@
 stants;%0A
+import de.holisticon.util.tracee.configuration.TraceeFilterConfiguration;%0A
 import d
@@ -754,50 +754,56 @@
  %7B%0A%0A
-    private final TraceeBackend backend = 
+%09public TraceeHttpRequestInterceptor() %7B%0A%09%09this(
 Trac
@@ -821,58 +821,103 @@
 nd()
+)
 ;%0A
-    private final TransportSerialization%3CString%3E
+%09%7D%0A%0A%09TraceeHttpRequestInterceptor(TraceeBackend backend) %7B%0A%09%09this.backend = backend;%0A%09%09this.
  tra
@@ -937,16 +937,17 @@
 zation =
+ 
  new Htt
@@ -996,21 +996,131 @@
 ory());%0A
-%0A    
+%09%7D%0A%0A%0A%09private final TraceeBackend backend;%0A%09private final TransportSerialization%3CString%3E transportSerialization;%0A%0A%09
 @Overrid
@@ -1121,20 +1121,17 @@
 verride%0A
-    
+%09
 public f
@@ -1263,24 +1263,121 @@
 ty()
-) %7B%0A            
+ && backend.getConfiguration().shouldProcessContext(TraceeFilterConfiguration.MessageType.OutgoingRequest)) %7B%0A%09%09%09
 fina
@@ -1447,20 +1447,11 @@
 d);%0A
-            
+%09%09%09
 http
@@ -1524,23 +1524,14 @@
 r);%0A
-        %7D%0A%0A    
+%09%09%7D%0A%0A%09
 %7D%0A%0A%7D

make TraceeHttpRequestInterceptor configurable