Skip to content

Commit 55bb7f4

Browse files
committed
code review comments
1 parent 97cb58f commit 55bb7f4

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

sdk/src/main/java/io/opentdf/platform/sdk/AddressNormalizer.java

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,39 @@ private AddressNormalizer(){
1515
}
1616

1717
static String normalizeAddress(String urlString, boolean usePlaintext) {
18-
URL url;
18+
URI uri;
1919
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+
}
2332
}
2433
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;
2736
} else {
28-
port = url.getPort();
37+
port = uri.getPort();
2938
}
30-
final String protocol = usePlaintext && "http".equals(url.getProtocol()) ? "http" : "https";
39+
final String scheme = usePlaintext ? "http" : "https";
3140

3241
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();
3443
logger.debug("normalized url [{}] to [{}]", urlString, returnUrl);
3544
return returnUrl;
36-
} catch (MalformedURLException e) {
45+
} catch (URISyntaxException e) {
3746
throw new SDKException("error creating KAS address", e);
3847
}
3948
}
4049

41-
private static URL tryParseHostAndPort(String urlString) {
50+
private static URI tryParseHostAndPort(String urlString) {
4251
URI uri;
4352
try {
4453
uri = new URI(null, urlString, null, null, null).parseServerAuthority();
@@ -47,8 +56,8 @@ private static URL tryParseHostAndPort(String urlString) {
4756
}
4857

4958
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) {
5261
throw new SDKException("error trying to create URL from host and port", e);
5362
}
5463
}

sdk/src/main/java/io/opentdf/platform/sdk/KASClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
* This class provides methods to retrieve public keys, unwrap encrypted keys,
4242
* and manage key caches.
4343
*/
44-
public class KASClient implements SDK.KAS {
44+
class KASClient implements SDK.KAS {
4545

4646
private final OkHttpClient httpClient;
4747
private final BiFunction<OkHttpClient, String, ProtocolClient> protocolClientFactory;

sdk/src/test/java/io/opentdf/platform/sdk/AddressNormalizerTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@ class AddressNormalizerTest {
1010

1111
@Test
1212
void testAddressNormalizationWithHTTPSClient() {
13-
assertThat(normalizeAddress("http://example.org", false)).isEqualTo("https://example.org:80");
13+
assertThat(normalizeAddress("http://example.org", false)).isEqualTo("https://example.org:443");
1414
// default to https if no scheme is provided
1515
assertThat(normalizeAddress("example.org:1234", false)).isEqualTo("https://example.org:1234");
16+
assertThat(normalizeAddress("ftp://example.org", false)).isEqualTo("https://example.org:443");
1617
}
1718

1819
@Test
1920
void testAddressNormaliationWithInsecureHTTPClient() {
2021
assertThat(normalizeAddress("http://localhost:8080", true)).isEqualTo("http://localhost:8080");
21-
assertThat(normalizeAddress("https://example.org", true)).isEqualTo("https://example.org:443");
22+
assertThat(normalizeAddress("http://example.org", true)).isEqualTo("http://example.org:80");
2223
// default to http if no scheme is provided
2324
assertThat(normalizeAddress("example.org:1234", true)).isEqualTo("http://example.org:1234");
25+
assertThat(normalizeAddress("sftp://example.org", true)).isEqualTo("http://example.org:80");
2426
}
2527
}

0 commit comments

Comments
 (0)