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

import io.netty.handler.codec.http.HttpResponseStatus;
import org.vertx.java.core.Handler;
import org.vertx.java.core.http.HttpServer;
import org.vertx.java.core.http.HttpServerRequest;
import org.vertx.java.core.http.RouteMatcher;
import org.vertx.java.core.json.JsonArray;
import org.vertx.java.core.json.JsonObject;
import org.vertx.java.platform.Verticle;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

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

    public static final String PROXY_PATH = "/multiRepoProxy";

    @Override
    public void start() {

        final JsonObject config = container.config();

        int port = config.getInteger("port");
        final JsonArray repositories = config.getArray("repositories");
        final List<RepositoryObject> repos = buildRepoUrls(repositories);

        final HttpServer httpServer = vertx.createHttpServer();
        RouteMatcher routeMatcher = new RouteMatcher();
        routeMatcher.headWithRegEx(PROXY_PATH + "/.*", new ProxyRequestHandler(vertx, repos));
        routeMatcher.getWithRegEx(PROXY_PATH + "/.*", new ProxyRequestHandler(vertx, repos));


        routeMatcher.allWithRegEx(PROXY_PATH + "/.*", new Handler<HttpServerRequest>() {
            @Override
            public void handle(HttpServerRequest request) {
                request.response().setStatusCode(HttpResponseStatus.BAD_REQUEST.code());
                request.response().end();
            }
        });

        httpServer.requestHandler(routeMatcher);
        httpServer.listen(port);

        container.logger().info("Webserver proxy started, listening on port:" + port);

    }


    private List<RepositoryObject> buildRepoUrls(JsonArray repositories) {
        List<RepositoryObject> result = new ArrayList<RepositoryObject>();
        for (Object repository : repositories) {
            JsonObject repositoryOject = (JsonObject) repository;

            final Map<String, Object> stringObjectMap = repositoryOject.toMap();
            for (Map.Entry<String, Object> stringObjectEntry : stringObjectMap.entrySet()) {
                final Map<String, String> repoMap = (Map<String, String>) stringObjectEntry.getValue();
                result.add(new RepositoryObject(repoMap.get("host"), Integer.parseInt(repoMap.get("port")), repoMap.get("uri")));
            }
        }
        return result;

    }
}

@@ -1497,19 +1497,328 @@
 tus.
-BAD_REQUEST
+METHOD_NOT_ALLOWED.code());%0A                request.response().end();%0A            %7D%0A        %7D);%0A%0A        routeMatcher.noMatch(new Handler%3CHttpServerRequest%3E() %7B%0A            @Override%0A            public void handle(HttpServerRequest request) %7B%0A                request.response().setStatusCode(HttpResponseStatus.NOT_FOUND
 .cod

Add no match