package fr.synchrotron.soleil.ica.msvervice.management;

import fr.synchrotron.soleil.ica.msvervice.management.handlers.POMExportHandler;
import fr.synchrotron.soleil.ica.msvervice.management.handlers.POMImportHandler;
import fr.synchrotron.soleil.ica.msvervice.vertx.lib.utilities.VertxConfigLoader;
import io.netty.handler.codec.http.HttpResponseStatus;
import org.vertx.java.core.Handler;
import org.vertx.java.core.eventbus.EventBus;
import org.vertx.java.core.http.HttpServerRequest;
import org.vertx.java.core.http.RouteMatcher;
import org.vertx.java.core.json.JsonObject;
import org.vertx.java.platform.Verticle;

/**
 * @author Gregory Boissinot
 */
public class HttpEndpointManager extends Verticle {

    public static final long SEND_MS_TIMEOUT = 10 * 1000l; // in ms

    @Override
    public void start() {

        try {
            final EventBus eventBus = vertx.eventBus();

//            //-- Deploy Required Verticle
//            final AsyncResultHandler<String> asyncResultHandler = new AsyncResultHandler<String>() {
//                @Override
//                public void handle(AsyncResult<String> asyncResult) {
//                    onVerticleLoaded(asyncResult);
//                }
//            };
//
            final VertxConfigLoader vertxConfigLoader = new VertxConfigLoader();
            final JsonObject config = vertxConfigLoader.createConfig(container.config());

            //Deploy mod-mavenmetadata (a runnable module and nested module)
            container.deployModule("fr.synchrotron.soleil~mod-mavenmetadata~1.0.0", container.config(), 1);

            RouteMatcher routeMatcher = new RouteMatcher();

            //-- POM IMPORTER
            final POMImportHandler pomImportHandler = new POMImportHandler(eventBus);
            routeMatcher.post("/pom/import", pomImportHandler);
            routeMatcher.put("/pom/import", pomImportHandler);

            //--POM EXPORTER
            final POMExportHandler pomExportHandler = new POMExportHandler(eventBus);
            routeMatcher.post("/pom/export", pomExportHandler);

            routeMatcher.allWithRegEx(".*", new Handler<HttpServerRequest>() {
                @Override
                public void handle(HttpServerRequest request) {
                    request.response().setStatusCode(HttpResponseStatus.NOT_FOUND.code());
                    request.response().end("Path or Http method not supported.\n");
                }
            });

            final int serverPort = getServerPort(config);
            vertx.createHttpServer().requestHandler(routeMatcher).listen(serverPort);
            container.logger().info("Webserver  started on " + serverPort);

        } catch (Throwable e) {
            container.logger().error("Can't start the main verticle: " + e.getMessage());
        }
    }

    private int getServerPort(JsonObject config) {
        final Integer port = config.getInteger("port");
        if (port == null) {
            throw new ConfigurationException("A port number is required");
        }
        return port;
    }

//    private void onVerticleLoaded(AsyncResult<String> asyncResult) {
//        if (!asyncResult.succeeded()) {
//            container.logger().info(asyncResult.cause());
//        }
//    }

}

@@ -345,24 +345,111 @@
 onseStatus;%0A
+import org.vertx.java.core.AsyncResult;%0Aimport org.vertx.java.core.AsyncResultHandler;%0A
 import org.v
@@ -464,32 +464,32 @@
 a.core.Handler;%0A
-
 import org.vertx
@@ -976,54 +976,8 @@
 );%0A%0A
-//            //-- Deploy Required Verticle%0A//
     
@@ -1069,26 +1069,24 @@
 String%3E() %7B%0A
-//
             
@@ -1099,18 +1099,16 @@
 verride%0A
-//
         
@@ -1161,34 +1161,32 @@
  asyncResult) %7B%0A
-//
                 
@@ -1191,24 +1191,16 @@
       on
-Verticle
 Loaded(a
@@ -1212,18 +1212,16 @@
 esult);%0A
-//
         
@@ -1226,26 +1226,24 @@
           %7D%0A
-//
             
@@ -1245,18 +1245,16 @@
      %7D;%0A
-//
 %0A       
@@ -1417,17 +1417,16 @@
 fig());%0A
-%0A
         
@@ -1582,29 +1582,37 @@
 0%22, 
-container.
 config
-()
 , 1
+, asyncResultHandler
 );%0A%0A
@@ -3083,18 +3083,16 @@
 %0A    %7D%0A%0A
-//
     priv
@@ -3102,24 +3102,16 @@
  void on
-Verticle
 Loaded(A
@@ -3140,26 +3140,24 @@
 ncResult) %7B%0A
-//
         if (
@@ -3184,18 +3184,16 @@
 ed()) %7B%0A
-//
         
@@ -3242,18 +3242,16 @@
 use());%0A
-//
         
@@ -3252,18 +3252,16 @@
       %7D%0A
-//
     %7D%0A%0A%7D

Add handler on deployed Module