Skip to content

Commit

Permalink
fix #211 - wait url can contain user information
Browse files Browse the repository at this point in the history
Replace ping implementation from java.net.HttpURLConnection
to Apache HttpClient. Generate ping errors diagnostics info
when maven debug level is active.

Signed-off-by: Tomasz Domzal <tdomzal@gmail.com>
  • Loading branch information
tdomzal committed Jul 11, 2015
1 parent 468c078 commit 8acf869
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/jolokia/docker/maven/StartMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private void waitIfRequested(DockerAccess docker, ImageConfiguration imageConfig
ArrayList<String> logOut = new ArrayList<>();
if (wait.getUrl() != null) {
String waitUrl = StrSubstitutor.replace(wait.getUrl(), projectProperties);
checkers.add(new WaitUtil.HttpPingChecker(waitUrl));
checkers.add(new WaitUtil.HttpPingChecker(waitUrl, log));
logOut.add("on url " + waitUrl);
}
if (wait.getLog() != null) {
Expand Down
48 changes: 39 additions & 9 deletions src/main/java/org/jolokia/docker/maven/util/WaitUtil.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package org.jolokia.docker.maven.util;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.TimeoutException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

/**
* @author roland
* @since 18.10.14
Expand Down Expand Up @@ -75,31 +80,56 @@ private static long delta(long now) {
public static class HttpPingChecker implements WaitChecker {

private String url;
private Logger log;

/**
* Ping the given URL
*
* @param url URL to check
* @param log Mojo logger
* @param timeout
*/
public HttpPingChecker(String url) {
public HttpPingChecker(String url, Logger log) {
this.url = url;
this.log = log;
}

@Override
public boolean check() {
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setConnectTimeout(HTTP_PING_TIMEOUT);
connection.setReadTimeout(HTTP_PING_TIMEOUT);
connection.setRequestMethod("HEAD");
int responseCode = connection.getResponseCode();
return (responseCode >= 200 && responseCode <= 399);
return ping();
} catch (ClientProtocolException exception) {
log.debug(String.format("wait url '%s' exception: %s", url, exception.getMessage()));
return false;
} catch (IOException exception) {
log.debug(String.format("wait url '%s' exception: %s", url, exception.getMessage()));
return false;
}
}

private boolean ping() throws IOException, ClientProtocolException {
RequestConfig requestConfig = RequestConfig.custom() //
.setSocketTimeout(HTTP_PING_TIMEOUT) //
.setConnectTimeout(HTTP_PING_TIMEOUT) //
.setConnectionRequestTimeout(HTTP_PING_TIMEOUT) //
.build();
CloseableHttpClient httpClient = HttpClientBuilder.create() //
.setDefaultRequestConfig(requestConfig) //
.build();
try {
CloseableHttpResponse response = httpClient.execute(new HttpHead(url));
try {
int responseCode = response.getStatusLine().getStatusCode();
log.debug(String.format("wait url '%s' response code: %s", url, responseCode));
return (responseCode >= 200 && responseCode <= 399);
} finally {
response.close();
}
} finally {
httpClient.close();
}
}

@Override
public void cleanUp() { }
}
Expand Down
12 changes: 10 additions & 2 deletions src/test/java/org/jolokia/docker/maven/util/WaitUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import java.util.concurrent.TimeoutException;

import com.sun.net.httpserver.*;

import mockit.Mocked;

import org.junit.BeforeClass;
import org.junit.Test;

Expand All @@ -22,18 +25,23 @@ public class WaitUtilTest {
static int port;
static String httpPingUrl;

@Mocked Logger log;

@Test(expected = TimeoutException.class)
public void httpFail() throws TimeoutException {
WaitUtil.HttpPingChecker checker = new WaitUtil.HttpPingChecker(httpPingUrl);
WaitUtil.HttpPingChecker checker = new WaitUtil.HttpPingChecker(httpPingUrl, log);
long waited = WaitUtil.wait(500,checker);
}

@Test
public void httpSuccess() throws TimeoutException {
server.start();
System.out.println("Check URL " + httpPingUrl);
WaitUtil.HttpPingChecker checker = new WaitUtil.HttpPingChecker(httpPingUrl);

// preload - first time use almost always lasts much longer (i'm assuming its http client initialization behavior)
WaitUtil.wait(700,new WaitUtil.HttpPingChecker(httpPingUrl, log));

WaitUtil.HttpPingChecker checker = new WaitUtil.HttpPingChecker(httpPingUrl, log);
long waited = WaitUtil.wait(700,checker);
assertTrue("Waited longer than 500ms: " + waited,waited < 700);
server.stop(10);
Expand Down

0 comments on commit 8acf869

Please sign in to comment.