package fr.synchrotron.soleil.ica.ci.service.dormproxy;

import io.netty.handler.codec.http.HttpResponseStatus;
import org.vertx.java.core.*;
import org.vertx.java.core.file.AsyncFile;
import org.vertx.java.core.http.HttpHeaders;
import org.vertx.java.core.http.HttpServerRequest;
import org.vertx.java.core.streams.Pump;

import java.io.File;

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

    private final Vertx vertx;
    private final String fsRepositoryRootDir;
    private final String proxyPath;

    public GETFileHandler(Vertx vertx, String fsRepositoryRootDir, String proxyPath) {
        this.vertx = vertx;
        this.fsRepositoryRootDir = fsRepositoryRootDir;
        this.proxyPath = proxyPath;
    }

    @Override
    public void handle(final HttpServerRequest request) {

        final String path = request.path();
        final String method = request.method();
        System.out.println(method + " " + path);

        String artifactPath = path.substring(proxyPath.length() + 1);

        //TODO check
        final File getterDirectory = new File(fsRepositoryRootDir, artifactPath.substring(0, artifactPath.lastIndexOf("/")));
        final String filename = artifactPath.substring(artifactPath.lastIndexOf("/") + 1);
        final File getterFile = new File(getterDirectory, filename);

        vertx.fileSystem().exists(getterFile.getPath(), new AsyncResultHandler<Boolean>() {
            @Override
            public void handle(AsyncResult<Boolean> asyncResult) {
                if (asyncResult.result().booleanValue()) {
                    vertx.fileSystem().open(getterFile.getPath(), new AsyncResultHandler<AsyncFile>() {
                        public void handle(AsyncResult<AsyncFile> ar) {
                            if (ar.succeeded()) {
                                AsyncFile asyncFile = ar.result();
                                request.response().putHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(getterFile.length()));
                                if ("GET".equals(method)) {
                                    request.response().setChunked(true);
                                    Pump.createPump(asyncFile, request.response()).start();
                                }
                                asyncFile.endHandler(new VoidHandler() {
                                    public void handle() {
                                        request.response().end();
                                    }
                                });
                            } else {
                                request.response().setStatusCode(HttpResponseStatus.NOT_FOUND.code());
                                request.response().end();
                            }
                        }
                    });
                } else {
                    request.response().setStatusCode(HttpResponseStatus.NOT_FOUND.code());
                    request.response().end();
                }

            }
        });


    }
}

@@ -1060,30 +1060,8 @@
  1);
-%0A%0A        //TODO check
 %0A   
@@ -1608,209 +1608,222 @@
     
-vertx.fileSystem().open(getterFile.getPath(), new AsyncResultHandler%3CAsyncFile%3E() %7B%0A                        public void handle(AsyncResult%3CAsyncFile%3E ar) %7B%0A                            if (ar.succeeded(
+request.response().putHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(getterFile.length()));//%0A                    request.response().setChunked(true);%0A%0A                    if (request.path().endsWith(%22.jar.sha1%22
 )) %7B
@@ -1851,59 +1851,162 @@
     
-  
-      AsyncFile asyncFile = ar.result();%0A        
+request.response().headers().set(HttpHeaders.CONTENT_TYPE, %22application/octet-stream%22);%0A                    %7D else if (request.path().endsWith(%22.jar%22)) %7B%0A
     
@@ -2078,51 +2078,40 @@
 ENT_
-LENGTH, String.valueOf(getterFile.length())
+TYPE, %22application/java-archive%22
 );%0A 
@@ -2113,32 +2113,43 @@
 %22);%0A            
+        %7D%0A%0A
                 
@@ -2208,54 +2208,290 @@
     
-            request.response().setChunked(true
+vertx.fileSystem().open(getterFile.getPath(), new AsyncResultHandler%3CAsyncFile%3E() %7B%0A                            public void handle(AsyncResult%3CAsyncFile%3E ar) %7B%0A                                if (ar.succeeded()) %7B%0A                                    AsyncFile asyncFile = ar.result(
 );%0A 
@@ -2594,33 +2594,8 @@
     
-                       %7D%0A
     
@@ -2617,21 +2617,16 @@
         
-     
 asyncFil
@@ -2654,24 +2654,28 @@
 Handler() %7B%0A
+    
             
@@ -2753,32 +2753,36 @@
                 
+    
 request.response
@@ -2776,39 +2776,151 @@
 uest.response().
-end();%0A
+setStatusCode(HttpResponseStatus.OK.code());%0A                                            request.response().end();%0A    
                 
@@ -2933,32 +2933,36 @@
               %7D%0A
+    
                 
@@ -3001,32 +3001,116 @@
                 
+    %7D%0A                            %7D%0A                        %7D);%0A                    
 %7D else %7B%0A       
@@ -3098,32 +3098,24 @@
    %7D else %7B%0A
-        
             
@@ -3174,33 +3174,26 @@
 ponseStatus.
-NOT_FOUND
+OK
 .code());%0A  
@@ -3210,32 +3210,24 @@
             
-  
-      
 request.resp
@@ -3264,67 +3264,9 @@
     
-        %7D%0A                        %7D%0A                    %7D);
+%7D
 %0A   
@@ -3434,33 +3434,32 @@
               %7D%0A
-%0A
             %7D%0A  
@@ -3468,18 +3468,16 @@
     %7D);%0A
-%0A%0A
     %7D%0A%7D%0A

Fix blocking request on HEAD method