1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2025-12-08 22:21:08 +01:00

Add ConnectionConfiguration.setHostAddressByNameOrIp(CharSequence fqdnOrIp)

This commit is contained in:
Florian Schmaus 2018-12-04 12:44:16 +01:00
parent 0332fa54d1
commit 0c134db072
2 changed files with 116 additions and 0 deletions

View file

@ -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(