package fr.synchrotron.soleil.ica.proxy.utilities;

import io.netty.handler.codec.http.HttpResponseStatus;
import org.vertx.java.core.Handler;
import org.vertx.java.core.http.HttpHeaders;
import org.vertx.java.core.http.HttpServerRequest;
import org.vertx.java.core.http.RouteMatcher;
import org.vertx.java.core.logging.Logger;
import org.vertx.java.core.logging.impl.LoggerFactory;

/**
 * @author Gregory Boissinot
 */
public class RequestHandlerWrapper implements Handler<HttpServerRequest> {

    private static final Logger LOG = LoggerFactory.getLogger(RequestHandlerWrapper.class);
    private final RouteMatcher routeMatcher;

    public RequestHandlerWrapper(RouteMatcher routeMatcher) {
        this.routeMatcher = routeMatcher;
    }

    @Override
    public void handle(final HttpServerRequest request) {

        if (LOG.isDebugEnabled()) {
            LOG.debug("Incoming request : " + request.method() + " " + request.uri());
        }

        request.exceptionHandler(new Handler<Throwable>() {
            @Override
            public void handle(Throwable t) {
                LOG.error("Severe error during request processing :", t);
                request.response().setStatusCode(HttpResponseStatus.INTERNAL_SERVER_ERROR.code());
                request.response().end();
            }
        });

        try {
            request.headers().remove(HttpHeaders.KEEP_ALIVE);
            request.headers().remove(HttpHeaders.CONNECTION);  //not necessary with keepAlive to false
            routeMatcher.handle(request);
        } catch (Throwable t) {
            LOG.error("The routeMatcher throw an error", t);
            LOG.error("Severe error during request processing :", t);
            request.response().setStatusCode(HttpResponseStatus.INTERNAL_SERVER_ERROR.code());
            request.response().end();
        }
    }

}

@@ -132,24 +132,61 @@
 re.Handler;%0A
+import org.vertx.java.core.MultiMap;%0A
 import org.v
@@ -218,16 +218,16 @@
 eaders;%0A
-
 import o
@@ -414,16 +414,39 @@
 ctory;%0A%0A
+import java.util.Map;%0A%0A
 /**%0A * @
@@ -1406,160 +1406,41 @@
     
-request.headers().remove(HttpHeaders.KEEP_ALIVE);%0A            request.headers().remove(HttpHeaders.CONNECTION);  //not necessary with keepAlive to false
+cleanRequestHttpHeaders(request);
 %0A   
@@ -1766,32 +1766,32 @@
 sponse().end();%0A
-
         %7D%0A    %7D%0A
@@ -1791,10 +1791,513 @@
 %0A    %7D%0A%0A
+    private void cleanRequestHttpHeaders(HttpServerRequest request) %7B%0A        final MultiMap headers = request.headers();%0A        for (Map.Entry%3CString, String%3E header : headers) %7B%0A            String headerValue = header.getValue();%0A            if (headerValue == null) %7B%0A                headers.remove(header.getKey());%0A            %7D%0A        %7D%0A        headers.remove(HttpHeaders.KEEP_ALIVE);%0A        headers.remove(HttpHeaders.CONNECTION);  //not necessary with keepAlive to false from clients%0A%0A    %7D%0A%0A
 %7D%0A

Clean headers