package io.vertx.example.shell.wget;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.cli.Argument;
import io.vertx.core.cli.CLI;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientResponse;
import io.vertx.example.util.Runner;
import io.vertx.ext.shell.ShellService;
import io.vertx.ext.shell.ShellServiceOptions;
import io.vertx.ext.shell.command.Command;
import io.vertx.ext.shell.command.CommandBuilder;
import io.vertx.ext.shell.command.CommandRegistry;
import io.vertx.ext.shell.term.TelnetTermOptions;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
 */
public class WgetCommand extends AbstractVerticle {

  // Convenience method so you can run it in your IDE
  public static void main(String[] args) {
    Runner.runExample(WgetCommand.class);
  }

  @Override
  public void start() {

    // Create the wget CLI
    CLI cli = CLI.create("wget").setSummary("Wget implemented with Vert.x HTTP client").
      addArgument(new Argument().setIndex(0).setArgName("http-url").setDescription("the HTTP uri to get"));

    // Create the command
    Command helloWorld = CommandBuilder.command(cli).
      processHandler(process -> {
        URL url;
        try {
          url = new URL(process.commandLine().getArgumentValue(0));
        } catch (MalformedURLException e) {
          process.write("Bad url\n").end();
          return;
        }
        HttpClient client = process.vertx().createHttpClient();
        process.write("Connecting to " + url + "\n");
        int port = url.getPort();
        if (port == -1) {
          port = 80;
        }
        client.get(port, url.getHost(), url.getPath(), ar -> {
          if (ar.succeeded()) {
            HttpClientResponse resp = ar.result();
            process.write(resp.statusCode() + " " + resp.statusMessage() + "\n");
            String contentType = resp.getHeader("Content-Type");
            String contentLength = resp.getHeader("Content-Length");
            process.write("Length: " + (contentLength != null ? contentLength : "unspecified"));
            if (contentType != null) {
              process.write("[" + contentType + "]");
            }
            process.write("\n");
            process.end();
          } else {
            process.write("wget: error " + ar.cause().getMessage() + "\n");
            process.end();
          }
        });

      }).build(vertx);

    ShellService service = ShellService.create(vertx, new ShellServiceOptions().setTelnetOptions(
        new TelnetTermOptions().setHost("localhost").setPort(3000)
    ));
    CommandRegistry.getShared(vertx).registerCommand(helloWorld);
    service.start(ar -> {
      if (!ar.succeeded()) {
        ar.cause().printStackTrace();
      }
    });
  }
}

@@ -70,16 +70,45 @@
 rticle;%0A
+import io.vertx.core.Future;%0A
 import i
@@ -232,30 +232,22 @@
 ttp.Http
-ClientResponse
+Method
 ;%0Aimport
@@ -1710,12 +1710,32 @@
 ent.
-get(
+request(HttpMethod.GET, 
 port
@@ -1768,17 +1768,9 @@
 th()
-, ar -%3E %7B
+)
 %0A   
@@ -1780,81 +1780,76 @@
     
-if (ar.succeeded()) %7B%0A            HttpClientRes
+.compose(req -%3E %7B%0A            return req.send().com
 po
-n
 se
- 
+(
 resp 
-= ar.result();%0A
+-%3E %7B%0A  
     
@@ -1918,32 +1918,34 @@
 sage() + %22%5Cn%22);%0A
+  
             Stri
@@ -1997,32 +1997,34 @@
 %22);%0A            
+  
 String contentLe
@@ -2060,24 +2060,26 @@
 t-Length%22);%0A
+  
             
@@ -2171,24 +2171,26 @@
             
+  
 if (contentT
@@ -2210,32 +2210,34 @@
 %7B%0A              
+ 
+ 
 process.write(%22%5B
@@ -2264,32 +2264,34 @@
 %22);%0A            
+  
 %7D%0A            pr
@@ -2280,32 +2280,34 @@
   %7D%0A            
+ 
+ 
 process.write(%22%5C
@@ -2315,32 +2315,34 @@
 %22);%0A            
+  
 process.end();%0A 
@@ -2354,19 +2354,93 @@
     
+ 
-%7D else %7B%0A  
+   return Future.succeededFuture();%0A            %7D);%0A          %7D).onFailure(err -%3E %7B%0A
     
@@ -2476,26 +2476,19 @@
 ror %22 + 
-ar.cause()
+err
 .getMess
@@ -2504,34 +2504,32 @@
 %5Cn%22);%0A          
- 
- 
 process.end();%0A 
@@ -2527,28 +2527,16 @@
 .end();%0A
-          %7D%0A
         
@@ -2539,17 +2539,16 @@
     %7D);%0A
-%0A
       %7D)

Update vertx-shell examples for HTTP client