package se.thinkcode.wait;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Collections;
import java.util.Map;

@Mojo(name = "wait", threadSafe = true, defaultPhase = LifecyclePhase.PRE_INTEGRATION_TEST)
public class HttpWaitMojo extends AbstractMojo {

    @Parameter(defaultValue = "http://localhost")
    String url;

    @Parameter(defaultValue = "3000")
    int timeout;

    @Parameter
    Map<String, String> headers = Collections.emptyMap();

    public void execute() throws MojoExecutionException, MojoFailureException {
        HttpURLConnection con;
        try {
            URL obj = new URL(url);
            con = (HttpURLConnection) obj.openConnection();
            con.setRequestMethod("GET");
        } catch (IOException e) {
            throw new ConnectionException("Connection to " + url + " failed with the message: " + e.getMessage());
        }

        con.setConnectTimeout(timeout);

        for (String key : headers.keySet()) {
            String value = headers.get(key);
            con.setRequestProperty(key, value);
        }

        int responseCode;
        try {
            responseCode = con.getResponseCode();
        } catch (IOException e) {
            throw new TimeoutException("Connection to " + url + " timed out");
        }

        if (responseCode == HttpURLConnection.HTTP_OK) {
            return;
        }

        throw new ConnectionException("Connection to " + url + " failed with response code " + responseCode);
    }
}

@@ -399,24 +399,63 @@
 Connection;%0A
+import java.net.MalformedURLException;%0A
 import java.
@@ -760,9 +760,10 @@
  = %22
-3
+10
 000%22
@@ -943,16 +943,134 @@
 %0A       
+ getLog().info(%22Waiting for %22 + url);%0A        long startTime = System.currentTimeMillis();%0A%0A        int responseCode =
  HttpURL
@@ -1079,20 +1079,31 @@
 nnection
- con
+.HTTP_NOT_FOUND
 ;%0A      
@@ -1126,51 +1126,96 @@
     
-URL obj = new URL(url);%0A            con 
+long endTime = System.currentTimeMillis() + timeout;%0A            while (responseCode =
 = 
-(
 Http
@@ -1231,219 +1231,251 @@
 tion
-) obj.openConnection();%0A            con.setRequestMethod(%22GET%22);%0A        %7D catch (IOException e) %7B%0A            throw new ConnectionException(%22Connection to %22 + url + %22 failed with the message: %22 + e.getMessage()
+.HTTP_NOT_FOUND) %7B%0A                try %7B%0A                    HttpURLConnection con;%0A%0A                    URL obj = new URL(url);%0A                    con = (HttpURLConnection) obj.openConnection();%0A                    con.setRequestMethod(%22GET%22
 );%0A 
@@ -1473,35 +1473,36 @@
 %22GET%22);%0A        
-%7D%0A%0A
+    
         con.setC
@@ -1522,24 +1522,36 @@
 (timeout);%0A%0A
+            
         for 
@@ -1592,24 +1592,36 @@
             
+            
 String value
@@ -1641,16 +1641,28 @@
 t(key);%0A
+            
         
@@ -1713,101 +1713,302 @@
     
-%7D%0A%0A        int responseCode;%0A        try %7B%0A            responseCode = con
+            %7D%0A%0A                    responseCode = con.getResponseCode();%0A                    con.disconnect();%0A                %7D catch (MalformedURLException e) %7B%0A                    throw new ConnectionException(%22Connection to %22 + url + %22 failed with the message: %22 + e
 .get
-ResponseCod
+Messag
 e()
+)
 ;%0A
+        
     
@@ -2053,142 +2053,516 @@
     
-throw new TimeoutException(%22Connection to %22 + url + %22 timed out%22);%0A        %7D%0A%0A        if (responseCode == HttpURLConnection.HTTP_OK) %7B
+        if (System.currentTimeMillis() %3E endTime) %7B%0A                        throw new TimeoutException(%22Connection to %22 + url + %22 timed out%22);%0A                    %7D%0A                %7D%0A%0A                Thread.sleep(100);%0A            %7D%0A        %7D catch (InterruptedException e) %7B%0A            getLog().error(e);%0A        %7D%0A%0A        if (responseCode == HttpURLConnection.HTTP_OK) %7B%0A            long waitingTime = System.currentTimeMillis() - startTime;%0A            getLog().info(%22Waited for %22 + waitingTime + %22ms%22);
 %0A   

Implement the connection properly