@@ -15,30 +15,39 @@ private AddressNormalizer(){
15
15
}
16
16
17
17
static String normalizeAddress (String urlString , boolean usePlaintext ) {
18
- URL url ;
18
+ URI uri ;
19
19
try {
20
- url = new URL (urlString );
21
- } catch (MalformedURLException e ) {
22
- url = tryParseHostAndPort (urlString );
20
+ uri = new URI (urlString );
21
+ } catch (URISyntaxException e ) {
22
+ throw new SDKException ("error trying to parse URL [" + urlString + "]" , e );
23
+ }
24
+
25
+ if (uri .getHost () == null ) {
26
+ // if there is no host then we are likely dealing with a host and port
27
+ try {
28
+ uri = new URI (usePlaintext ? "http" : "https" , null , uri .getScheme (), Integer .parseInt (uri .getSchemeSpecificPart ()), null , null , null );
29
+ } catch (URISyntaxException e ) {
30
+ throw new SDKException ("error trying to create URL for host and port[" + urlString + "]" , e );
31
+ }
23
32
}
24
33
final int port ;
25
- if (url .getPort () == -1 ) {
26
- port = "http" . equals ( url . getProtocol ()) ? 80 : 443 ;
34
+ if (uri .getPort () == -1 ) {
35
+ port = usePlaintext ? 80 : 443 ;
27
36
} else {
28
- port = url .getPort ();
37
+ port = uri .getPort ();
29
38
}
30
- final String protocol = usePlaintext && "http" . equals ( url . getProtocol ()) ? "http" : "https" ;
39
+ final String scheme = usePlaintext ? "http" : "https" ;
31
40
32
41
try {
33
- var returnUrl = new URL ( protocol , url .getHost (), port , "" ).toString ();
42
+ var returnUrl = new URI ( scheme , null , uri .getHost (), port , null , null , null ).toString ();
34
43
logger .debug ("normalized url [{}] to [{}]" , urlString , returnUrl );
35
44
return returnUrl ;
36
- } catch (MalformedURLException e ) {
45
+ } catch (URISyntaxException e ) {
37
46
throw new SDKException ("error creating KAS address" , e );
38
47
}
39
48
}
40
49
41
- private static URL tryParseHostAndPort (String urlString ) {
50
+ private static URI tryParseHostAndPort (String urlString ) {
42
51
URI uri ;
43
52
try {
44
53
uri = new URI (null , urlString , null , null , null ).parseServerAuthority ();
@@ -47,8 +56,8 @@ private static URL tryParseHostAndPort(String urlString) {
47
56
}
48
57
49
58
try {
50
- return new URL (uri .getPort () == 443 ? "https" : "http" , uri .getHost (), uri .getPort (), "" );
51
- } catch (MalformedURLException e ) {
59
+ return new URI (uri .getPort () == 443 ? "https" : "http" , null , uri .getHost (), uri .getPort (), "" , "" , "" );
60
+ } catch (URISyntaxException e ) {
52
61
throw new SDKException ("error trying to create URL from host and port" , e );
53
62
}
54
63
}
0 commit comments