4
4
import com .squareup .moshi .JsonAdapter ;
5
5
import com .squareup .moshi .Moshi ;
6
6
import okhttp3 .Headers ;
7
- import okhttp3 .HttpUrl ;
8
7
import okhttp3 .MediaType ;
9
8
import okhttp3 .OkHttpClient ;
10
9
import okhttp3 .Request ;
42
41
import java .net .DatagramPacket ;
43
42
import java .net .DatagramSocket ;
44
43
import java .net .InetAddress ;
44
+ import java .net .InetSocketAddress ;
45
45
import java .net .SocketException ;
46
+ import java .net .URI ;
47
+ import java .net .URISyntaxException ;
46
48
import java .net .UnknownHostException ;
47
49
import java .nio .charset .StandardCharsets ;
48
50
import java .util .ArrayList ;
@@ -78,7 +80,7 @@ public class InfluxDBImpl implements InfluxDB {
78
80
*/
79
81
private static final LogLevel LOG_LEVEL = LogLevel .parseLogLevel (System .getProperty (LOG_LEVEL_PROPERTY ));
80
82
81
- private final InetAddress hostAddress ;
83
+ private final String hostName ;
82
84
private String version ;
83
85
private final Retrofit retrofit ;
84
86
private final InfluxDBService influxDBService ;
@@ -116,7 +118,7 @@ public class InfluxDBImpl implements InfluxDB {
116
118
public InfluxDBImpl (final String url , final String username , final String password , final OkHttpClient .Builder client ,
117
119
final ResponseFormat responseFormat ) {
118
120
this .messagePack = ResponseFormat .MSGPACK .equals (responseFormat );
119
- this .hostAddress = parseHostAddress (url );
121
+ this .hostName = parseHost (url );
120
122
121
123
this .loggingInterceptor = new HttpLoggingInterceptor ();
122
124
setLogLevel (LOG_LEVEL );
@@ -162,7 +164,7 @@ public InfluxDBImpl(final String url, final String username, final String passwo
162
164
final InfluxDBService influxDBService , final JsonAdapter <QueryResult > adapter ) {
163
165
super ();
164
166
this .messagePack = false ;
165
- this .hostAddress = parseHostAddress (url );
167
+ this .hostName = parseHost (url );
166
168
167
169
this .loggingInterceptor = new HttpLoggingInterceptor ();
168
170
setLogLevel (LOG_LEVEL );
@@ -187,18 +189,25 @@ public InfluxDBImpl(final String url, final String username, final String passwo
187
189
setRetentionPolicy (retentionPolicy );
188
190
}
189
191
190
- private InetAddress parseHostAddress (final String url ) {
191
- HttpUrl httpUrl = HttpUrl .parse (url );
192
+ private String parseHost (final String url ) {
193
+ String hostName ;
194
+ try {
195
+ URI uri = new URI (url );
196
+ hostName = uri .getHost ();
197
+ } catch (URISyntaxException e1 ) {
198
+ throw new IllegalArgumentException ("Unable to parse url: " + url , e1 );
199
+ }
192
200
193
- if (httpUrl == null ) {
194
- throw new IllegalArgumentException ("Unable to parse url: " + url );
195
- }
201
+ if (hostName == null ) {
202
+ throw new IllegalArgumentException ("Unable to parse url: " + url );
203
+ }
196
204
197
- try {
198
- return InetAddress .getByName (httpUrl .host ());
199
- } catch (UnknownHostException e ) {
200
- throw new InfluxDBIOException (e );
201
- }
205
+ try {
206
+ InetAddress .getByName (hostName );
207
+ } catch (UnknownHostException e ) {
208
+ throw new InfluxDBIOException (e );
209
+ }
210
+ return hostName ;
202
211
}
203
212
204
213
@ Override
@@ -465,7 +474,7 @@ public void write(final int udpPort, final String records) {
465
474
initialDatagramSocket ();
466
475
byte [] bytes = records .getBytes (StandardCharsets .UTF_8 );
467
476
try {
468
- datagramSocket .send (new DatagramPacket (bytes , bytes .length , hostAddress , udpPort ));
477
+ datagramSocket .send (new DatagramPacket (bytes , bytes .length , new InetSocketAddress ( hostName , udpPort ) ));
469
478
} catch (IOException e ) {
470
479
throw new InfluxDBIOException (e );
471
480
}
0 commit comments