Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Merge pull request #97 from ppalaga/HAWKULAR-144
Browse files Browse the repository at this point in the history
HAWKULAR-144 Make pinger support GET requests
  • Loading branch information
pilhuhn committed May 13, 2015
2 parents 6a21e7b + 80c57c9 commit bb30528
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import javax.ejb.ActivationConfigProperty;
import javax.ejb.EJB;
import javax.ejb.MessageDriven;

import java.util.Map;

/**
Expand Down Expand Up @@ -65,8 +66,11 @@ public void onBasicMessage(ResourceEvent message) {
if (!"URL".equals(resource.getType().getId())) {
return;
}
String url = (String) resource.getProperties().get("url");
PingDestination destination = new PingDestination(resource.getId(), url);

Map<String, Object> props = resource.getProperties();
String url = (String) props.get("url");
String method = (String) props.get("method");
PingDestination destination = new PingDestination(resource.getId(), url, method);

if (message.getAction() == Action.Enumerated.CREATED) {
pingManager.addDestination(destination);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,40 @@
package org.hawkular.component.pinger;

/**
* Destination for pinging
* A destination for pinging.
*
* @author Heiko W. Rupp
*/
public class PingDestination {

String resourceId;
String url;
/** The default method {@value} */
public static final String DEFAULT_METHOD = "GET";

final String resourceId;
final String url;
final String method;

/**
* Creates a new {@link PingDestination} using the default method {@value #DEFAULT_METHOD}.
*
* @param resourceId the resourceId of this destination
* @param url the url to ping
*/
public PingDestination(String resourceId, String url) {
this(resourceId, url, DEFAULT_METHOD);
}

/**
* Creates a new {@link PingDestination}
*
* @param url the url to ping
* @param method the HTTP method to use in the ping request or null to use the default method
* {@value #DEFAULT_METHOD}
*/
public PingDestination(String resourceId, String url, String method) {
this.resourceId = resourceId;
this.url = url;
this.method = method == null ? DEFAULT_METHOD : method;
}

@Override
Expand All @@ -39,6 +61,7 @@ public boolean equals(Object o) {
PingDestination that = (PingDestination) o;

if (!resourceId.equals(that.resourceId)) return false;
if (!method.equals(that.method)) return false;
return url.equals(that.url);

}
Expand All @@ -47,6 +70,7 @@ public boolean equals(Object o) {
public int hashCode() {
int result = resourceId.hashCode();
result = 31 * result + url.hashCode();
result = 31 * result + method.hashCode();
return result;
}

Expand All @@ -55,6 +79,7 @@ public String toString() {
return "PingDestination{" +
"resourceId='" + resourceId + '\'' +
", url='" + url + '\'' +
", method='" + method + '\'' +
'}';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ public void startUp() {
String id = (String) m.get("id");
Map<String, String> params = (Map<String, String>) m.get("properties");
String url = params.get("url");
destinations.add(new PingDestination(id, url));
String method = params.get("method");
destinations.add(new PingDestination(id, url, method));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
public class PingStatus {

PingDestination destination;
final PingDestination destination;
int duration;
int code;
boolean timedOut = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@

import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.TrustStrategy;
Expand Down Expand Up @@ -83,10 +84,11 @@ private CloseableHttpClient getHttpClient(final String url) {
@Asynchronous
public Future<PingStatus> ping(final PingStatus status) {

HttpHead head = new HttpHead(status.destination.url);
PingDestination dest = status.destination;
HttpUriRequest request = RequestBuilder.create(dest.method).setUri(dest.url).build();

try (CloseableHttpClient client = getHttpClient(status.destination.url)) {
HttpResponse httpResponse = client.execute(head);
try (CloseableHttpClient client = getHttpClient(dest.url)) {
HttpResponse httpResponse = client.execute(request);
StatusLine statusLine = httpResponse.getStatusLine();
long now = System.currentTimeMillis();

Expand All @@ -98,8 +100,6 @@ public Future<PingStatus> ping(final PingStatus status) {
} catch (IOException e) {
Log.LOG.wPingExeption(e.getMessage());
status.code = 500;
} finally {
head.releaseConnection();
}

return new AsyncResult<>(status);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.hawkular.component.pinger.PingStatus;
import org.hawkular.component.pinger.Pinger;
import org.hawkular.metrics.client.common.SingleMetric;
import org.junit.Assert;
import org.junit.Test;

/**
Expand All @@ -34,27 +35,51 @@
*/
public class PingerTest {

@org.junit.Test
@Test
public void testPinger() throws Exception {

Pinger pinger = new Pinger();
PingDestination destination = new PingDestination("123","http://hawkular.github.io");
PingStatus status = pinger.ping(new PingStatus(destination)).get();

assert status.getCode()==200;
assert status.isTimedOut()==false;
Assert.assertEquals(200, status.getCode());
Assert.assertFalse(status.isTimedOut());

}

@Test
public void testHeadPinger() throws Exception {

Pinger pinger = new Pinger();
PingDestination destination = new PingDestination("123","http://hawkular.github.io", "HEAD");
PingStatus status = pinger.ping(new PingStatus(destination)).get();

Assert.assertEquals(200, status.getCode());
Assert.assertFalse(status.isTimedOut());

}

@org.junit.Test
@Test
public void testPostPinger() throws Exception {

Pinger pinger = new Pinger();
PingDestination destination = new PingDestination("123","https://www.perfcake.org", "POST");
PingStatus status = pinger.ping(new PingStatus(destination)).get();

Assert.assertEquals(200, status.getCode());
Assert.assertFalse(status.isTimedOut());

}

@Test
public void testSslPinger() throws Exception {

Pinger pinger = new Pinger();
PingDestination destination = new PingDestination("123","https://www.perfcake.org");
PingStatus status = pinger.ping(new PingStatus(destination)).get();

assert status.getCode()==200;
assert status.isTimedOut()==false;
Assert.assertEquals(200, status.getCode());
Assert.assertFalse(status.isTimedOut());
}

@Test
Expand Down Expand Up @@ -95,7 +120,7 @@ public void testPingManagerStartup() throws Exception {
}


private class NoOpMetricPublisher extends MetricPublisher {
private static class NoOpMetricPublisher extends MetricPublisher {
@Override
public void sendToMetricsViaRest(String tenantId, List<Map<String, Object>> metrics) {

Expand Down

0 comments on commit bb30528

Please sign in to comment.