/*
 * Copyright 2015 JBoss Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.apiman.gateway.platforms.vertx3.engine;

import io.apiman.common.plugin.PluginUtils;
import io.apiman.gateway.engine.async.AsyncResultImpl;
import io.apiman.gateway.engine.async.IAsyncResultHandler;
import io.apiman.gateway.engine.impl.DefaultPluginRegistry;
import io.apiman.gateway.platforms.vertx3.common.config.VertxEngineConfig;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientRequest;
import io.vertx.core.http.HttpClientResponse;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.Map;

/**
 * A vertx implementation of the API Gateway's plugin registry. This version simply extends the default
 * implementation but provides its own (actually asynchronous) downloading
 *
 * @author eric.wittmann@redhat.com
 */
public class VertxPluginRegistry extends DefaultPluginRegistry {

    @SuppressWarnings("nls")
    private static File getTempPluginsDir() {
        try {
            return Files.createTempDirectory("apiman-gateway-plugins-tmp").toFile();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private HttpClient client;


    /**
     * Constructor.
     *
     * @param vertx the vertx
     * @param vxEngineConfig the engine config
     * @param config the plugin config
     */
    public VertxPluginRegistry(Vertx vertx, VertxEngineConfig vxEngineConfig, Map<String, String> config) {
        super(getTempPluginsDir(), PluginUtils.getDefaultMavenRepositories());
        this.client = vertx.createHttpClient();
    }

    /**
     * @see io.apiman.gateway.engine.impl.DefaultPluginRegistry#downloadArtifactTo(java.net.URL, java.io.File,
     *      io.apiman.gateway.engine.async.IAsyncResultHandler)
     */
    @Override
    protected void downloadArtifactTo(final URL artifactUrl, final File pluginFile,
            final IAsyncResultHandler<File> handler) {
        int port = artifactUrl.getPort();
        if (port == -1) {
            port = 80;
        }

        final HttpClientRequest request = client.get(port, artifactUrl.getHost(), artifactUrl.getPath(),
                (Handler<HttpClientResponse>) response -> {

            response.exceptionHandler((Handler<Throwable>) error -> {
                handler.handle(AsyncResultImpl.create(error, File.class));
            });

            // Body Handler
            response.handler((Handler<Buffer>) buffer -> {
                try {
                    Files.write(pluginFile.toPath(), buffer.getBytes(), StandardOpenOption.APPEND,
                            StandardOpenOption.CREATE, StandardOpenOption.WRITE);
                } catch (IOException e) {
                    handler.handle(AsyncResultImpl.create(e, File.class));
                }
            });

            response.endHandler((Handler<Void>) event -> {
                handler.handle(AsyncResultImpl.create(pluginFile));
            });
        });

        request.exceptionHandler((Handler<Throwable>) error -> {
            handler.handle(AsyncResultImpl.create(error, File.class));
        });

        request.end();
    }
}

@@ -3135,17 +3135,21 @@
 esponse.
-h
+bodyH
 andler((
@@ -3380,16 +3380,88 @@
 WRITE);%0A
+                    handler.handle(AsyncResultImpl.create(pluginFile));%0A
         
@@ -3561,32 +3561,32 @@
 , File.class));%0A
+
                 
@@ -3606,152 +3606,8 @@
  %7D);
-%0A%0A            response.endHandler((Handler%3CVoid%3E) event -%3E %7B%0A                handler.handle(AsyncResultImpl.create(pluginFile));%0A            %7D);
 %0A   

Use body handler