mirror of
https://codeberg.org/Mercury-IM/Mercury-IM
synced 2024-06-27 14:04:51 +02:00
Refactor XmppConnectionFactory
This commit is contained in:
parent
83c3245c36
commit
647695b081
|
@ -1,17 +1,8 @@
|
||||||
package org.mercury_im.messenger.xmpp;
|
package org.mercury_im.messenger.xmpp;
|
||||||
|
|
||||||
import org.jivesoftware.smack.AbstractXMPPConnection;
|
|
||||||
import org.jivesoftware.smack.XMPPConnection;
|
import org.jivesoftware.smack.XMPPConnection;
|
||||||
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
|
|
||||||
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
|
|
||||||
import org.jivesoftware.smack.util.stringencoder.BareJidEncoder;
|
|
||||||
import org.jxmpp.jid.BareJid;
|
|
||||||
import org.jxmpp.jid.impl.JidCreate;
|
|
||||||
import org.jxmpp.stringprep.XmppStringprepException;
|
|
||||||
import org.mercury_im.messenger.entity.Account;
|
import org.mercury_im.messenger.entity.Account;
|
||||||
|
|
||||||
import java.net.Inet4Address;
|
|
||||||
|
|
||||||
public class MercuryConnection {
|
public class MercuryConnection {
|
||||||
|
|
||||||
private static final XmppConnectionFactory connectionFactory = new XmppConnectionFactory();
|
private static final XmppConnectionFactory connectionFactory = new XmppConnectionFactory();
|
||||||
|
@ -31,25 +22,4 @@ public class MercuryConnection {
|
||||||
public XMPPConnection getConnection() {
|
public XMPPConnection getConnection() {
|
||||||
return connection;
|
return connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ensureAuthenticated() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class XmppConnectionFactory {
|
|
||||||
|
|
||||||
public AbstractXMPPConnection createConnection(Account account) {
|
|
||||||
try {
|
|
||||||
XMPPTCPConnectionConfiguration connectionConfiguration = XMPPTCPConnectionConfiguration.builder()
|
|
||||||
.setConnectTimeout(10 * 1000)
|
|
||||||
.setXmppAddressAndPassword(account.getAddress(), account.getPassword())
|
|
||||||
.setHost(account.getHost() != null ? account.getHost() : JidCreate.domainBareFrom(account.getAddress()).toString())
|
|
||||||
.setPort(account.getPort() != 0 ? account.getPort() : 5222)
|
|
||||||
.build();
|
|
||||||
return new XMPPTCPConnection(connectionConfiguration);
|
|
||||||
} catch (XmppStringprepException e) {
|
|
||||||
throw new AssertionError("Account has invalid address: " + account.getAddress(), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
package org.mercury_im.messenger.xmpp;
|
||||||
|
|
||||||
|
import org.jivesoftware.smack.AbstractXMPPConnection;
|
||||||
|
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
|
||||||
|
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
|
||||||
|
import org.jxmpp.jid.impl.JidCreate;
|
||||||
|
import org.jxmpp.stringprep.XmppStringprepException;
|
||||||
|
import org.mercury_im.messenger.entity.Account;
|
||||||
|
|
||||||
|
public class XmppConnectionFactory {
|
||||||
|
|
||||||
|
private static final int DEFAULT_PORT = 5222;
|
||||||
|
private static final int CONNECTION_TIMEOUT = 30 * 1000;
|
||||||
|
|
||||||
|
public AbstractXMPPConnection createConnection(Account account) {
|
||||||
|
try {
|
||||||
|
return new XMPPTCPConnection(XMPPTCPConnectionConfiguration.builder()
|
||||||
|
.setConnectTimeout(CONNECTION_TIMEOUT)
|
||||||
|
.setXmppAddressAndPassword(account.getAddress(), account.getPassword())
|
||||||
|
.setHost(determineHost(account))
|
||||||
|
.setPort(determinePort(account))
|
||||||
|
.build());
|
||||||
|
} catch (XmppStringprepException e) {
|
||||||
|
throw new AssertionError("Account has invalid address: " + account.getAddress(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String determineHost(Account account) throws XmppStringprepException {
|
||||||
|
return account.getHost() != null ? account.getHost() : JidCreate.domainBareFrom(account.getAddress()).toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private int determinePort(Account account) {
|
||||||
|
return account.getPort() != 0 ? account.getPort() : DEFAULT_PORT;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue