/**
 * Copyright 2015 Kenzan, LLC
 *
 * 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.kenzan.karyon.rxnetty.resource;

import io.netty.buffer.ByteBuf;
import io.reactivex.netty.protocol.http.server.HttpServerRequest;
import io.reactivex.netty.protocol.http.server.HttpServerResponse;
import io.reactivex.netty.protocol.http.server.RequestHandler;
import netflix.karyon.transport.http.SimpleUriRouter;
import rx.Observable;
import rx.functions.Func1;

import com.kenzan.karyon.rxnetty.endpoint.HelloEndpoint;
import java.io.*;

public class IndexResource implements RequestHandler<ByteBuf, ByteBuf>{

    private final SimpleUriRouter<ByteBuf, ByteBuf> delegate;
    private final HelloEndpoint endpoint;

    public static String execCmd(String cmd) throws java.io.IOException {
        java.util.Scanner s = new java.util.Scanner(Runtime.getRuntime().exec(cmd).getInputStream()).useDelimiter("\\A");
        return s.hasNext() ? s.next() : "";
    }
    public IndexResource() {
        endpoint = new HelloEndpoint();
        delegate = new SimpleUriRouter<>();

        delegate
        .addUri("/", new RequestHandler<ByteBuf, ByteBuf>() {
            @Override
            public Observable<Void> handle(HttpServerRequest<ByteBuf> request,
                    final HttpServerResponse<ByteBuf> response) {

                return endpoint.getHello()
                .flatMap(new Func1<String, Observable<Void>>() {
                    @Override
                    public Observable<Void> call(String body) {
                        String instanceId = "";
                        String userdata = "";

                        try{
                            instanceId = execCmd("curl http://metadata/computeMetadata/v1/instance/id -H Metadata-Flavor:Google") + execCmd("wget -q -O - http://instance-data/latest/meta-data/instance-id");
                            userdata = System.getenv("USERDATA");

                        } catch (Exception e){
                            e.printStackTrace();
                        }
                        response.writeString("<html><head><style>body{text-align:center;font-family:'Lucida Grande'}</style></head><body><img src='http://kenzan.com/wp-content/themes/kenzan/images/logo-reg.png' /><h2>Example Spinnaker Application</h2><h3>Instance Id " + instanceId + "</h3><h3>$USERDATA ENV VAR: " + userdata + "</h3></body></html>");
                        return response.close();
                    }
                });
            }
        });
    }
    @Override
    public Observable<Void> handle(HttpServerRequest<ByteBuf> request,
            HttpServerResponse<ByteBuf> response) {
        return delegate.handle(request, response);
    }

}

@@ -2180,16 +2180,62 @@
         
+ /* Original */%0A                            //
  instanc
@@ -2433,16 +2433,19 @@
         
+ //
  userdat
@@ -2474,16 +2474,175 @@
 RDATA%22);
+%0A                            /* Modified */%0A                            instanceId = execCmd(%22hostname%22);%0A                            userdata = %22Version 1.0%22;
 %0A%0A      
@@ -2780,16 +2780,59 @@
         
+  /* Original */%0A                        //
  respons
@@ -3148,24 +3148,410 @@
 %3E%3C/html%3E%22);%0A
+                        /* Modified */%0A                        response.writeString(%22%3Chtml%3E%3Chead%3E%3Cstyle%3Ebody%7Btext-align:center;font-family:'Lucida Grande'%7D%3C/style%3E%3C/head%3E%3Cbody%3E%3Cimg src='http://kenzan.com/wp-content/themes/kenzan/images/logo-reg.png' /%3E%3Ch2%3EExample Spinnaker Application%3C/h2%3E%3Ch3%3EInstance Id %22 + instanceId + %22%3C/h3%3E%3Ch3%3EApplication : %22 + userdata + %22%3C/h3%3E%3C/body%3E%3C/html%3E%22);%0A
             

Update IndexResource.java