package fr.synchrotron.soleil.ica.ci.ms.vertx.maven.legacyproxy.server.httprepo.push;

import fr.synchrotron.soleil.ica.ci.ms.vertx.maven.legacyproxy.server.httprepo.HttpArtifactCaller;
import fr.synchrotron.soleil.ica.ci.ms.vertx.maven.legacyproxy.server.httprepo.VertxDomainObject;
import io.netty.handler.codec.http.HttpResponseStatus;
import org.vertx.java.core.Handler;
import org.vertx.java.core.MultiMap;
import org.vertx.java.core.buffer.Buffer;
import org.vertx.java.core.http.HttpClient;
import org.vertx.java.core.http.HttpClientRequest;
import org.vertx.java.core.http.HttpClientResponse;
import org.vertx.java.core.http.HttpServerRequest;
import org.vertx.java.core.streams.Pump;

import java.util.Map;

/**
 * @author Gregory Boissinot
 */
public class HttpArtifactPushHandler {

    private final VertxDomainObject vertxDomainObject;
    private final HttpArtifactCaller httpArtifactCaller;

    public HttpArtifactPushHandler(VertxDomainObject vertxDomainObject, HttpArtifactCaller httpArtifactCaller) {
        this.vertxDomainObject = vertxDomainObject;
        this.httpArtifactCaller = httpArtifactCaller;
    }

    public void handle(final HttpServerRequest request) {

        final HttpClient pClient = httpArtifactCaller.getPClient();
        final String path = httpArtifactCaller.buildRequestPath(request);
        vertxDomainObject.getLogger().info("Uploading to " + path);

        final HttpClientRequest clientRequest = pClient.put(path, new Handler<HttpClientResponse>() {
            @Override
            public void handle(HttpClientResponse clientResponse) {
                final int statusCode = clientResponse.statusCode();
                request.response().setStatusCode(statusCode);
                request.response().setStatusMessage(clientResponse.statusMessage());
                request.response().headers().set(clientResponse.headers());
                clientResponse.endHandler(new Handler<Void>() {
                    public void handle(Void event) {
                        request.response().end();
                    }
                });

                if (statusCode == HttpResponseStatus.NOT_MODIFIED.code()
                        || statusCode == HttpResponseStatus.OK.code()) {
                    //Send result to original client
                    Pump.createPump(clientResponse, request.response().setChunked(true)).start();
                }
            }
        });

        final MultiMap headers = request.headers();
        for (Map.Entry<String, String> headerEntry : headers) {
            final String key = headerEntry.getKey();
            if ("Authorization".equalsIgnoreCase(key)) {
                clientRequest.putHeader(key, headerEntry.getValue());
            }
        }

        request.dataHandler(new Handler<Buffer>() {
            @Override
            public void handle(Buffer data) {
                clientRequest.putHeader("Content-Length", String.valueOf(data.getBytes().length));
                clientRequest.write(data);

                //Also Track
                if (path.endsWith(".pom")) {
                    vertxDomainObject.getVertx().eventBus().send("pom.track", data.toString());
                }
            }
        });

        request.endHandler(new Handler<Void>() {
            @Override
            public void handle(Void event) {
                clientRequest.end();
            }
        });

    }


}
@@ -708,16 +708,63 @@
 til.Map;
+%0Aimport java.util.concurrent.ConcurrentHashMap;
 %0A%0A/**%0A *
@@ -1169,24 +1169,141 @@
 ler;%0A    %7D%0A%0A
+    private ConcurrentHashMap%3CInteger, StringBuilder%3E pomStorage = new ConcurrentHashMap%3CInteger, StringBuilder%3E();%0A%0A
     public v
@@ -2256,310 +2256,8 @@
 %7D);%0A
-%0A                if (statusCode == HttpResponseStatus.NOT_MODIFIED.code()%0A                        %7C%7C statusCode == HttpResponseStatus.OK.code()) %7B%0A                    //Send result to original client%0A                    Pump.createPump(clientResponse, request.response().setChunked(true)).start();%0A    
     
@@ -2270,32 +2270,34 @@
   %7D%0A            
+//
 %7D%0A        %7D);%0A%0A 
@@ -2612,24 +2612,228 @@
 %0A        %7D%0A%0A
+        final String contentLengthHeader = headers.get(%22Content-Length%22);%0A        if (contentLengthHeader != null) %7B%0A            clientRequest.putHeader(%22Content-Length%22, contentLengthHeader);%0A        %7D%0A%0A
         requ
@@ -2974,121 +2974,80 @@
 est.
-putHeader(%22Content-Length%22, String.valueOf(data.getBytes().length));%0A                clientRequest.write(data
+write(data);%0A                int requestId = request.path().hashCode(
 );%0A
-%0A
     
@@ -3062,144 +3062,267 @@
     
-//Also Track%0A                if (path.endsWith(%22.pom%22)) %7B%0A                    vertxDomainObject.getVertx().eventBus().send(%22pom.track%22, 
+StringBuilder content = pomStorage.get(requestId);%0A                if (content == null) %7B%0A                    pomStorage.put(requestId, new StringBuilder(data.toString()));%0A                %7D else %7B%0A                    pomStorage.put(requestId, content.append(
 data
@@ -3333,16 +3333,17 @@
 tring())
+)
 ;%0A      
@@ -3529,24 +3529,311 @@
 quest.end();
+%0A%0A                if (path.endsWith(%22.pom%22)) %7B%0A                    int code = request.path().hashCode();%0A                    StringBuilder content = pomStorage.get(code);%0A                    vertxDomainObject.getVertx().eventBus().send(%22pom.track%22, content.toString());%0A                %7D
 %0A           

Fix truncation