diff --git a/src/main/java/org/influxdb/InfluxDBFactory.java b/src/main/java/org/influxdb/InfluxDBFactory.java index aee28d73..a96fbb87 100644 --- a/src/main/java/org/influxdb/InfluxDBFactory.java +++ b/src/main/java/org/influxdb/InfluxDBFactory.java @@ -6,6 +6,8 @@ import okhttp3.OkHttpClient; import org.influxdb.impl.Preconditions; +import java.net.URL; +import java.net.MalformedURLException; import java.util.Objects; @@ -27,6 +29,7 @@ public enum InfluxDBFactory { */ public static InfluxDB connect(final String url) { Preconditions.checkNonEmptyString(url, "url"); + Preconditions.checkHasPort(url); return new InfluxDBImpl(url, null, null, new OkHttpClient.Builder()); } @@ -44,6 +47,7 @@ public static InfluxDB connect(final String url) { */ public static InfluxDB connect(final String url, final String username, final String password) { Preconditions.checkNonEmptyString(url, "url"); + Preconditions.checkHasPort(url); Preconditions.checkNonEmptyString(username, "username"); return new InfluxDBImpl(url, username, password, new OkHttpClient.Builder()); } @@ -59,6 +63,7 @@ public static InfluxDB connect(final String url, final String username, final St */ public static InfluxDB connect(final String url, final OkHttpClient.Builder client) { Preconditions.checkNonEmptyString(url, "url"); + Preconditions.checkHasPort(url); Objects.requireNonNull(client, "client"); return new InfluxDBImpl(url, null, null, client); } @@ -79,6 +84,7 @@ public static InfluxDB connect(final String url, final OkHttpClient.Builder clie */ public static InfluxDB connect(final String url, final String username, final String password, final OkHttpClient.Builder client) { + Preconditions.checkHasPort(url); return connect(url, username, password, client, ResponseFormat.JSON); } @@ -101,8 +107,10 @@ public static InfluxDB connect(final String url, final String username, final St public static InfluxDB connect(final String url, final String username, final String password, final OkHttpClient.Builder client, final ResponseFormat responseFormat) { Preconditions.checkNonEmptyString(url, "url"); + Preconditions.checkHasPort(url); Preconditions.checkNonEmptyString(username, "username"); Objects.requireNonNull(client, "client"); return new InfluxDBImpl(url, username, password, client, responseFormat); } + } diff --git a/src/main/java/org/influxdb/impl/Preconditions.java b/src/main/java/org/influxdb/impl/Preconditions.java index 1e34204a..d948bd03 100644 --- a/src/main/java/org/influxdb/impl/Preconditions.java +++ b/src/main/java/org/influxdb/impl/Preconditions.java @@ -1,5 +1,8 @@ package org.influxdb.impl; +import java.net.URL; +import java.net.MalformedURLException; + /** * Functions for parameter validation. * @@ -59,4 +62,24 @@ public static void checkDuration(final String duration, final String name) throw + " for " + name); } } + + /** + * Check url is legal + * @param url the server url + * @throws IllegalArgumentException if the port of url is miss + */ + public static void checkHasPort(final String url) throws IllegalArgumentException{ + String colon = ":"; + if (!url.contains(colon) || url.endsWith(colon)) { + throw new IllegalArgumentException(String.format("The url [%s] port cannot be null", url)); + } + try { + URL urlObj = new URL(url); + if (-1 == urlObj.getPort()) { + throw new IllegalArgumentException(String.format("The url [%s] port cannot be null", url)); + } + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } + } } diff --git a/src/test/java/org/influxdb/InfluxDBFactoryTest.java b/src/test/java/org/influxdb/InfluxDBFactoryTest.java index 74ebfab7..8ea56f59 100644 --- a/src/test/java/org/influxdb/InfluxDBFactoryTest.java +++ b/src/test/java/org/influxdb/InfluxDBFactoryTest.java @@ -54,4 +54,16 @@ public void testShouldThrowIllegalArgumentWithInvalidUrl() { InfluxDBFactory.connect("invalidUrl"); }); } + + @Test + public void testUrlNotContainsColon() { + Assertions.assertThrows(IllegalArgumentException.class, () -> + InfluxDBFactory.connect("http://" + TestUtils.getInfluxIP())); + } + + @Test + public void testUrlEndWithColon() { + Assertions.assertThrows(IllegalArgumentException.class, () -> + InfluxDBFactory.connect("http://" + TestUtils.getInfluxIP() + ":")); + } }