Skip to content
This repository was archived by the owner on Jan 16, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@
<version>5.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.net.httpserver</groupId>
<artifactId>http</artifactId>
<version>20070405</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
import java.io.IOException;
import java.io.InputStream;
import java.lang.management.ManagementFactory;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
Expand Down Expand Up @@ -237,6 +240,33 @@ public Builder sdkMetricsTags(Map<String, String> tags) {
return this;
}

/**
* For a given server endpoint, validate according to RFC 2396 and attempt
* to make a connection
*
* @return {@code this}
* @throws IllegalStateException
*/
public Builder validateEndpoint() throws IllegalStateException {
URL url = null;

try {
url = new URL(this.server);
} catch (MalformedURLException e) {
throw new IllegalArgumentException(this.server + " is not a valid url", e);
}

try {
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
urlConn.connect();
urlConn.disconnect();
} catch (IOException e) {
throw new IllegalArgumentException("Unable to connect to " + this.server, e);
}

return this;
}

/**
* Creates a new client that flushes directly to a Proxy or Wavefront service.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import com.wavefront.sdk.common.metrics.WavefrontSdkDeltaCounter;
import org.junit.jupiter.api.Test;

import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
Expand All @@ -17,6 +20,8 @@
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

/**
Expand Down Expand Up @@ -64,6 +69,43 @@ private String createString(int size) {
return new String(new char[size]).replace("\0", "a");
}

@Test
public void testUrlFormatForBuilder() {
HttpServer server = runFakeServer("127.0.0.1", 28788);

assertTrue(validateBuilderEndpoint("http://127.0.0.1:28788"));

assertFalse(validateBuilderEndpoint("http://127.0.0.1"));
assertFalse(validateBuilderEndpoint("http://127.0.0.1:28789"));
assertFalse(validateBuilderEndpoint("127.0.0.1:28788"));
assertFalse(validateBuilderEndpoint("not a valid endpoint"));
assertFalse(validateBuilderEndpoint(null));

server.stop(0);
}

private HttpServer runFakeServer(String address, int port) {
HttpServer httpserver = null;

try {
httpserver = HttpServer.create(new InetSocketAddress(address, port), 0);
} catch (IOException e) {
throw new RuntimeException(e);
}

return httpserver;
}

private boolean validateBuilderEndpoint(String uri) {
try {
WavefrontClient.Builder wfClientBuilder = new WavefrontClient.Builder(uri, "token");
wfClientBuilder.validateEndpoint();
return true;
} catch (IllegalArgumentException e) {
return false;
}
}

@Test
public void testUrlFormatForService() {
validateURI(URI.create("http://127.0.0.1:2878"), "http://127.0.0.1:2878/report?f=wavefront");
Expand Down