From 0c134db07202910389641964f7a341c0e649e0c1 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Tue, 4 Dec 2018 12:44:16 +0100 Subject: [PATCH] Add ConnectionConfiguration.setHostAddressByNameOrIp(CharSequence fqdnOrIp) --- .../smack/ConnectionConfiguration.java | 37 +++++++++ .../smack/ConnectionConfigurationTest.java | 79 +++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 smack-core/src/test/java/org/jivesoftware/smack/ConnectionConfigurationTest.java diff --git a/smack-core/src/main/java/org/jivesoftware/smack/ConnectionConfiguration.java b/smack-core/src/main/java/org/jivesoftware/smack/ConnectionConfiguration.java index c16e5624c..7115a2bc3 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/ConnectionConfiguration.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/ConnectionConfiguration.java @@ -18,6 +18,7 @@ package org.jivesoftware.smack; import java.net.InetAddress; +import java.net.UnknownHostException; import java.security.KeyStore; import java.util.Arrays; import java.util.Collection; @@ -45,6 +46,7 @@ import org.jxmpp.jid.impl.JidCreate; import org.jxmpp.jid.parts.Resourcepart; import org.jxmpp.stringprep.XmppStringprepException; import org.minidns.dnsname.DnsName; +import org.minidns.util.InetAddressUtil; /** * Configuration to use while establishing the connection to the server. @@ -171,6 +173,14 @@ public abstract class ConnectionConfiguration { } + DnsName getHost() { + return host; + } + + InetAddress getHostAddress() { + return hostAddress; + } + /** * Returns the server name of the target server. * @@ -642,6 +652,33 @@ public abstract class ConnectionConfiguration { return getThis(); } + /** + * Set the host to connect to by either its fully qualified domain name (FQDN) or its IP. + * + * @param fqdnOrIp a CharSequence either representing the FQDN or the IP of the host. + * @return a reference to this builder. + * @see #setHost(DnsName) + * @see #setHostAddress(InetAddress) + * @since 4.3.2 + */ + public B setHostAddressByNameOrIp(CharSequence fqdnOrIp) { + String fqdnOrIpString = fqdnOrIp.toString(); + if (InetAddressUtil.isIpAddress(fqdnOrIp)) { + InetAddress hostInetAddress; + try { + hostInetAddress = InetAddress.getByName(fqdnOrIpString); + } + catch (UnknownHostException e) { + // Should never happen. + throw new AssertionError(e); + } + setHostAddress(hostInetAddress); + } else { + setHost(fqdnOrIpString); + } + return getThis(); + } + public B setPort(int port) { if (port < 0 || port > 65535) { throw new IllegalArgumentException( diff --git a/smack-core/src/test/java/org/jivesoftware/smack/ConnectionConfigurationTest.java b/smack-core/src/test/java/org/jivesoftware/smack/ConnectionConfigurationTest.java new file mode 100644 index 000000000..6637eae56 --- /dev/null +++ b/smack-core/src/test/java/org/jivesoftware/smack/ConnectionConfigurationTest.java @@ -0,0 +1,79 @@ +/** + * + * Copyright 2018 Florian Schmaus. + * + * 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 org.jivesoftware.smack; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.jxmpp.jid.JidTestUtil; + +public class ConnectionConfigurationTest { + + @Test + public void setIp() { + DummyConnectionConfiguration.Builder builder = newUnitTestBuilder(); + + final String ip = "192.168.0.1"; + builder.setHostAddressByNameOrIp(ip); + + DummyConnectionConfiguration connectionConfiguration = builder.build(); + assertEquals('/' + ip, connectionConfiguration.getHostAddress().toString()); + } + + @Test + public void setFqdn() { + DummyConnectionConfiguration.Builder builder = newUnitTestBuilder(); + + final String fqdn = "foo.example.org"; + builder.setHostAddressByNameOrIp(fqdn); + + DummyConnectionConfiguration connectionConfiguration = builder.build(); + assertEquals(fqdn, connectionConfiguration.getHost().toString()); + } + + private static DummyConnectionConfiguration.Builder newUnitTestBuilder() { + DummyConnectionConfiguration.Builder builder = DummyConnectionConfiguration.builder(); + builder.setXmppDomain(JidTestUtil.DOMAIN_BARE_JID_1); + return builder; + } + + private static final class DummyConnectionConfiguration extends ConnectionConfiguration { + + protected DummyConnectionConfiguration(Builder builder) { + super(builder); + } + + public static Builder builder() { + return new Builder(); + } + + private static final class Builder + extends ConnectionConfiguration.Builder { + + @Override + public DummyConnectionConfiguration build() { + return new DummyConnectionConfiguration(this); + } + + @Override + protected Builder getThis() { + return this; + } + + } + } +}