/*-
 * ========================LICENSE_START=================================
 * restheart-core
 * %%
 * Copyright (C) 2014 - 2022 SoftInstigate
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * =========================LICENSE_END==================================
 */
package org.restheart.handlers;

import io.undertow.server.HttpServerExchange;
import io.undertow.util.HttpString;
import java.util.Optional;
import org.restheart.Bootstrapper;
import org.restheart.exchange.ByteArrayProxyResponse;
import org.slf4j.MDC;
public class TracingInstrumentationHandler extends PipelinedHandler {
    public TracingInstrumentationHandler() {
    }

    public TracingInstrumentationHandler(final PipelinedHandler next) {
        super(next);
    }

    @Override
    public void handleRequest(HttpServerExchange exchange) throws Exception {
        Bootstrapper.getConfiguration().getTraceHeaders()
                .forEach((traceIdHeader) -> {
                    Optional.ofNullable(exchange.getRequestHeaders()
                            .get(traceIdHeader))
                            .flatMap(x -> Optional.ofNullable(x.peekFirst()))
                            .ifPresent(value -> {
                                // saves the MDC Context
                                // SeeResponse.getMDCContext() javadoc
                                MDC.put(traceIdHeader, value);
                                ByteArrayProxyResponse.of(exchange).setMDCContext(MDC.getCopyOfContextMap());
                                exchange.getResponseHeaders().put(HttpString.tryFromString(traceIdHeader), value);
                            });
                });

        if (!exchange.isResponseComplete() && getNext() != null) {
            next(exchange);
        }

        Bootstrapper.getConfiguration()
                .getTraceHeaders().forEach((traceIdHeader) -> {
                    MDC.remove(traceIdHeader);
                });
    }
}

@@ -996,16 +996,40 @@
 pString;
+%0A%0Aimport java.util.List;
 %0Aimport 
@@ -1238,47 +1238,173 @@
    p
-ublic TracingInstrumentationHandler() %7B
+rivate final List%3CString%3E  traceHeaders;%0A%0A    public TracingInstrumentationHandler() %7B%0A        this.traceHeaders = Bootstrapper.getConfiguration().getTraceHeaders();
 %0A   
@@ -1500,16 +1500,95 @@
 (next);%0A
+        this.traceHeaders = Bootstrapper.getConfiguration().getTraceHeaders();%0A
     %7D%0A%0A 
@@ -1690,44 +1690,14 @@
     
-Bootstrapper.getConfiguration().getT
+this.t
 race
@@ -1699,31 +1699,25 @@
 traceHeaders
-()%0A    
+%0A
             
@@ -1745,37 +1745,16 @@
 er) -%3E %7B
-%0A                    
 Optional
@@ -1806,36 +1806,24 @@
             
-           
- 
 .get(traceId
@@ -1831,28 +1831,16 @@
 eader))%0A
-            
         
@@ -1913,28 +1913,16 @@
         
-          
-  
 .ifPrese
@@ -1955,28 +1955,16 @@
         
-          
-  
 // saves
@@ -2000,28 +2000,16 @@
         
-          
-  
 // SeeRe
@@ -2055,36 +2055,24 @@
             
-            
 MDC.put(trac
@@ -2110,28 +2110,16 @@
         
-         
-   
 ByteArra
@@ -2208,28 +2208,16 @@
         
-         
-   
 exchange
@@ -2295,40 +2295,8 @@
 e);%0A
-                            %7D);%0A
     
@@ -2310,16 +2310,32 @@
      %7D);
+%0A            %7D);
 %0A%0A      
@@ -2473,33 +2473,16 @@
 ration()
-%0A                
 .getTrac
@@ -2518,38 +2518,16 @@
 ader) -%3E
- %7B%0A                   
  MDC.rem
@@ -2544,35 +2544,16 @@
 dHeader)
-;%0A                %7D
 );%0A    %7D

Refactor TracingInstrumentationHandler