[core] Use UInt16 for ConnectionConfiguration 'port'

This commit is contained in:
Florian Schmaus 2020-05-24 13:02:19 +02:00
parent 9a8ee3c8e3
commit cac874bdc7
3 changed files with 17 additions and 10 deletions

View File

@ -35,6 +35,7 @@ import javax.net.ssl.SSLContext;
import javax.net.ssl.X509TrustManager; import javax.net.ssl.X509TrustManager;
import javax.security.auth.callback.CallbackHandler; import javax.security.auth.callback.CallbackHandler;
import org.jivesoftware.smack.datatypes.UInt16;
import org.jivesoftware.smack.debugger.SmackDebuggerFactory; import org.jivesoftware.smack.debugger.SmackDebuggerFactory;
import org.jivesoftware.smack.packet.id.StandardStanzaIdSource; import org.jivesoftware.smack.packet.id.StandardStanzaIdSource;
import org.jivesoftware.smack.packet.id.StanzaIdSource; import org.jivesoftware.smack.packet.id.StanzaIdSource;
@ -101,7 +102,7 @@ public abstract class ConnectionConfiguration {
protected final InetAddress hostAddress; protected final InetAddress hostAddress;
protected final DnsName host; protected final DnsName host;
protected final int port; protected final UInt16 port;
private final String keystorePath; private final String keystorePath;
private final String keystoreType; private final String keystoreType;
@ -237,7 +238,7 @@ public abstract class ConnectionConfiguration {
return hostAddress; return hostAddress;
} }
public int getPort() { public UInt16 getPort() {
return port; return port;
} }
@ -621,7 +622,7 @@ public abstract class ConnectionConfiguration {
private DomainBareJid xmppServiceDomain; private DomainBareJid xmppServiceDomain;
private InetAddress hostAddress; private InetAddress hostAddress;
private DnsName host; private DnsName host;
private int port = 5222; private UInt16 port = UInt16.from(5222);
private boolean allowEmptyOrNullUsername = false; private boolean allowEmptyOrNullUsername = false;
private boolean saslMechanismsSealed; private boolean saslMechanismsSealed;
private Set<String> enabledSaslMechanisms; private Set<String> enabledSaslMechanisms;
@ -837,7 +838,12 @@ public abstract class ConnectionConfiguration {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Port must be a 16-bit unsigned integer (i.e. between 0-65535. Port was: " + port); "Port must be a 16-bit unsigned integer (i.e. between 0-65535. Port was: " + port);
} }
this.port = port; UInt16 portUint16 = UInt16.from(port);
return setPort(portUint16);
}
public B setPort(UInt16 port) {
this.port = Objects.requireNonNull(port);
return getThis(); return getThis();
} }

View File

@ -42,7 +42,7 @@ public final class IpTcpRemoteConnectionEndpoint<IARR extends InternetAddressRR>
this.internetAddressResourceRecord = internetAddressResourceRecord; this.internetAddressResourceRecord = internetAddressResourceRecord;
} }
public static IpTcpRemoteConnectionEndpoint<InternetAddressRR> from(CharSequence host, int port, public static IpTcpRemoteConnectionEndpoint<InternetAddressRR> from(CharSequence host, UInt16 port,
InetAddress inetAddress) { InetAddress inetAddress) {
InternetAddressRR internetAddressResourceRecord; InternetAddressRR internetAddressResourceRecord;
// TODO: Use InternetAddressRR.from(InetAddress) once MiniDNS is updated. // TODO: Use InternetAddressRR.from(InetAddress) once MiniDNS is updated.
@ -52,7 +52,7 @@ public final class IpTcpRemoteConnectionEndpoint<IARR extends InternetAddressRR>
internetAddressResourceRecord = new AAAA((Inet6Address) inetAddress); internetAddressResourceRecord = new AAAA((Inet6Address) inetAddress);
} }
return new IpTcpRemoteConnectionEndpoint<InternetAddressRR>(host, UInt16.from(port), return new IpTcpRemoteConnectionEndpoint<InternetAddressRR>(host, port,
internetAddressResourceRecord); internetAddressResourceRecord);
} }

View File

@ -26,6 +26,7 @@ import java.util.logging.Logger;
import org.jivesoftware.smack.ConnectionConfiguration; import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.ConnectionConfiguration.DnssecMode; import org.jivesoftware.smack.ConnectionConfiguration.DnssecMode;
import org.jivesoftware.smack.datatypes.UInt16;
import org.jivesoftware.smack.util.DNSUtil; import org.jivesoftware.smack.util.DNSUtil;
import org.jivesoftware.smack.util.dns.DNSResolver; import org.jivesoftware.smack.util.dns.DNSResolver;
import org.jivesoftware.smack.util.rce.RemoteConnectionEndpoint; import org.jivesoftware.smack.util.rce.RemoteConnectionEndpoint;
@ -72,7 +73,7 @@ public class RemoteXmppTcpConnectionEndpoints {
if (hostAddresses != null) { if (hostAddresses != null) {
discoveredRemoteConnectionEndpoints = new ArrayList<>(hostAddresses.size()); discoveredRemoteConnectionEndpoints = new ArrayList<>(hostAddresses.size());
int port = config.getPort(); UInt16 port = config.getPort();
for (InetAddress inetAddress : hostAddresses) { for (InetAddress inetAddress : hostAddresses) {
IpTcpRemoteConnectionEndpoint<InternetAddressRR> connectionEndpoint = IpTcpRemoteConnectionEndpoint.from( IpTcpRemoteConnectionEndpoint<InternetAddressRR> connectionEndpoint = IpTcpRemoteConnectionEndpoint.from(
host, port, inetAddress); host, port, inetAddress);
@ -198,13 +199,13 @@ public class RemoteXmppTcpConnectionEndpoints {
LOGGER.info("Could not resolve DNS SRV resource records for " + srvDomain + ". Consider adding those."); LOGGER.info("Could not resolve DNS SRV resource records for " + srvDomain + ". Consider adding those.");
} }
int defaultPort; UInt16 defaultPort;
switch (domainType) { switch (domainType) {
case client: case client:
defaultPort = 5222; defaultPort = UInt16.from(5222);
break; break;
case server: case server:
defaultPort = 5269; defaultPort = UInt16.from(5269);
break; break;
default: default:
throw new AssertionError(); throw new AssertionError();