diff --git a/webclient/api/src/main/java/io/helidon/webclient/api/DefaultDnsResolver.java b/webclient/api/src/main/java/io/helidon/webclient/api/DefaultDnsResolver.java index 63c37b82545..4193c39ed6f 100644 --- a/webclient/api/src/main/java/io/helidon/webclient/api/DefaultDnsResolver.java +++ b/webclient/api/src/main/java/io/helidon/webclient/api/DefaultDnsResolver.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, 2023 Oracle and/or its affiliates. + * Copyright (c) 2022, 2024 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,7 @@ package io.helidon.webclient.api; import java.net.InetAddress; +import java.net.UnknownHostException; import io.helidon.webclient.spi.DnsResolver; @@ -46,7 +47,15 @@ public boolean useDefaultJavaResolver() { @Override public InetAddress resolveAddress(String hostname, DnsAddressLookup dnsAddressLookup) { - throw new IllegalStateException("This DNS resolver is not meant to be used."); + try { + InetAddress[] addresses = InetAddress.getAllByName(hostname); + addresses = dnsAddressLookup.filter(addresses); + if (addresses.length > 0) { + return addresses[0]; + } + } catch (UnknownHostException e) { + // falls through + } + throw new IllegalArgumentException("Failed to get address from host: " + hostname); } - } diff --git a/webclient/api/src/main/java/io/helidon/webclient/api/TcpClientConnection.java b/webclient/api/src/main/java/io/helidon/webclient/api/TcpClientConnection.java index 9ca73b822a6..54c4ae19ae5 100644 --- a/webclient/api/src/main/java/io/helidon/webclient/api/TcpClientConnection.java +++ b/webclient/api/src/main/java/io/helidon/webclient/api/TcpClientConnection.java @@ -22,7 +22,6 @@ import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketException; -import java.net.UnknownHostException; import java.security.cert.Certificate; import java.security.cert.X509Certificate; import java.time.Duration; @@ -246,21 +245,8 @@ private String createChannelId(Socket socket) { private InetSocketAddress inetSocketAddress() { DnsResolver dnsResolver = connectionKey.dnsResolver(); - if (dnsResolver.useDefaultJavaResolver()) { - try { - InetAddress[] addresses = InetAddress.getAllByName(connectionKey.host()); - addresses = connectionKey.dnsAddressLookup().filter(addresses); - if (addresses.length > 0) { - return new InetSocketAddress(addresses[0], connectionKey.port()); - } - } catch (UnknownHostException e) { - // falls through - } - throw new IllegalArgumentException("Failed to get address from host: " + connectionKey.host()); - } else { - InetAddress address = dnsResolver.resolveAddress(connectionKey.host(), connectionKey.dnsAddressLookup()); - return new InetSocketAddress(address, connectionKey.port()); - } + InetAddress address = dnsResolver.resolveAddress(connectionKey.host(), connectionKey.dnsAddressLookup()); + return new InetSocketAddress(address, connectionKey.port()); } private void debugTls(SSLSocket sslSocket) { diff --git a/webclient/api/src/test/java/io/helidon/webclient/api/DefaultDnsResolverTest.java b/webclient/api/src/test/java/io/helidon/webclient/api/DefaultDnsResolverTest.java new file mode 100644 index 00000000000..fb66280257c --- /dev/null +++ b/webclient/api/src/test/java/io/helidon/webclient/api/DefaultDnsResolverTest.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.helidon.webclient.api; + +import java.net.InetAddress; + +import org.junit.jupiter.api.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.CoreMatchers.is; + +class DefaultDnsResolverTest { + + @Test + void testIpv4Resolution() { + DefaultDnsResolver resolver = DefaultDnsResolver.create(); + InetAddress inetAddress = resolver.resolveAddress("localhost", DnsAddressLookup.IPV4_PREFERRED); + assertThat(inetAddress.getHostAddress(), is("127.0.0.1")); + } + + @Test + void testIpv6Resolution() { + DefaultDnsResolver resolver = DefaultDnsResolver.create(); + InetAddress inetAddress = resolver.resolveAddress("localhost", DnsAddressLookup.IPV6_PREFERRED); + assertThat(inetAddress.getHostAddress(), is("0:0:0:0:0:0:0:1")); + } +}