/**
 * JBoss, Home of Professional Open Source
 * Copyright 2016, Red Hat, Inc. and/or its affiliates, and individual
 * contributors by the @authors tag. See the copyright.txt in the
 * distribution for a full listing of individual contributors.
 * <p/>
 * 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 com.redhat.developers.msa.aloha;

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

import com.github.kennedyoliveira.hystrix.contrib.vertx.metricsstream.EventMetricsStreamHandler;

import feign.Logger;
import feign.Logger.Level;
import feign.hystrix.HystrixFeign;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.Json;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BodyHandler;

public class AlohaVerticle extends AbstractVerticle {

    @Override
    public void start() throws Exception {
        Router router = Router.router(vertx);
        router.route().handler(BodyHandler.create());

        // Aloha EndPoint
        router.get("/api/aloha").handler(ctx -> {
            addCORSHeaders(ctx.response())
                .end(aloha());
        });

        // Aloha Chained Endpoint
        router.get("/api/aloha-chaining").handler(ctx -> {
            addCORSHeaders(ctx.response())
                .putHeader("Content-Type", "application/json")
                .end(Json.encode(alohaChaining()));
        });

        // Hysrix Stream Endpoint
        router.get(EventMetricsStreamHandler.DEFAULT_HYSTRIX_PREFIX)
            .handler(EventMetricsStreamHandler.createHandler());

        vertx.createHttpServer().requestHandler(router::accept).listen(8080);
        System.out.println("Service running at 0.0.0.0:8080");
    }

    private String aloha() {
        String hostname = System.getenv().getOrDefault("HOSTNAME", "unknown");
        return String.format("Aloha mai %s", hostname);
    }

    private List<String> alohaChaining() {
        List<String> greetings = new ArrayList<>();
        greetings.add(aloha());
        greetings.add(getNextService().bonjour());
        return greetings;
    }

    /**
     * This is were the "magic" happens: it creates a Feign, which is a proxy interface for remote calling a REST endpoint with
     * Hystrix fallback support.
     *
     * @return The feign pointing to the service URL and with Hystrix fallback.
     */
    private BonjourService getNextService() {
        return HystrixFeign.builder()
            .logger(new Logger.ErrorLogger()).logLevel(Level.BASIC)
            .target(BonjourService.class, "http://bonjour:8080/",
                () -> "Bonjour response (fallback)");
    }

    private HttpServerResponse addCORSHeaders(HttpServerResponse response) {
        return response.putHeader("Access-Control-Allow-Origin", "*");
    }

}

@@ -843,60 +843,8 @@
 a;%0A%0A
-import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 impo
@@ -936,17 +936,16 @@
 andler;%0A
-%0A
 import f
@@ -1058,16 +1058,46 @@
 rticle;%0A
+import io.vertx.core.Handler;%0A
 import i
@@ -1244,16 +1244,68 @@
 ndler;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 public c
@@ -1757,32 +1757,72 @@
 andler(ctx -%3E %7B%0A
+            alohaChaining(list -%3E %7B%0A    
             addC
@@ -1840,32 +1840,36 @@
 ctx.response())%0A
+    
                 
@@ -1927,24 +1927,28 @@
             
+    
 .end(Json.en
@@ -1956,24 +1956,29 @@
 ode(
-alohaChaining())
+list));%0A            %7D
 );%0A 
@@ -2493,39 +2493,417 @@
 ate 
-List%3CString%3E alohaChaining() %7B%0A
+void alohaChaining(Handler%3CList%3CString%3E%3E resultHandler) %7B%0A      vertx.%3CString%3EexecuteBlocking(%0A          // Invoke the service in a worker thread, as it's blocking.%0A          future -%3E future.complete(getNextService().bonjour()),%0A          ar -%3E %7B%0A            // Back to the event loop%0A            // result cannot be null, hystrix would have called the fallback.%0A            String result = ar.result();%0A    
     
@@ -2950,32 +2950,36 @@
 st%3C%3E();%0A        
+    
 greetings.add(al
@@ -2978,32 +2978,36 @@
 s.add(aloha());%0A
+    
         greeting
@@ -3016,45 +3016,28 @@
 add(
-getNextService().bonjour());%0A
+result);%0A   
         
 retu
@@ -3036,25 +3036,63 @@
     
+ 
 re
-turn greetings;
+sultHandler.handle(greetings);%0A          %7D%0A      );%0A
 %0A   

Use executeBlocking to call the next service