mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-09-09 17:19:39 +02:00
Apply builder pattern to ConnectionConfiguration
Introducing a clean split between the constant connection configuration parameters, which are now all in ConnectionConfiguration and the dynamic connection state (e.g. hostAddresses) which are now in AbstractXMPPConnection. Also removed all arguments of login() since the username, password, resource and callback handler need now to be configured via ConnectionConfiguration. Also remove documentation/extensions/messageevents.md, as it's already in documentation/legacy
This commit is contained in:
parent
69f387b344
commit
c81cd34561
24 changed files with 760 additions and 708 deletions
|
@ -22,7 +22,6 @@ import java.net.URISyntaxException;
|
|||
|
||||
import org.jivesoftware.smack.ConnectionConfiguration;
|
||||
import org.jivesoftware.smack.proxy.ProxyInfo;
|
||||
import org.jivesoftware.smack.util.dns.HostAddress;
|
||||
|
||||
/**
|
||||
* Configuration to use while establishing the connection to the XMPP server via
|
||||
|
@ -33,60 +32,17 @@ import org.jivesoftware.smack.util.dns.HostAddress;
|
|||
*/
|
||||
public class BOSHConfiguration extends ConnectionConfiguration {
|
||||
|
||||
private boolean ssl;
|
||||
private String file;
|
||||
private final boolean https;
|
||||
private final String file;
|
||||
|
||||
public BOSHConfiguration(String xmppDomain) {
|
||||
super(xmppDomain, 7070);
|
||||
ssl = false;
|
||||
file = "/http-bind/";
|
||||
}
|
||||
|
||||
public BOSHConfiguration(String xmppDomain, int port) {
|
||||
super(xmppDomain, port);
|
||||
ssl = false;
|
||||
file = "/http-bind/";
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a HTTP Binding configuration.
|
||||
*
|
||||
* @param https true if you want to use SSL
|
||||
* (e.g. false for http://domain.lt:7070/http-bind).
|
||||
* @param host the hostname or IP address of the connection manager
|
||||
* (e.g. domain.lt for http://domain.lt:7070/http-bind).
|
||||
* @param port the port of the connection manager
|
||||
* (e.g. 7070 for http://domain.lt:7070/http-bind).
|
||||
* @param filePath the file which is described by the URL
|
||||
* (e.g. /http-bind for http://domain.lt:7070/http-bind).
|
||||
* @param xmppDomain the XMPP service name
|
||||
* (e.g. domain.lt for the user alice@domain.lt)
|
||||
*/
|
||||
public BOSHConfiguration(boolean https, String host, int port, String filePath, String xmppDomain) {
|
||||
super(host, port, xmppDomain);
|
||||
ssl = https;
|
||||
file = (filePath != null ? filePath : "/");
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a HTTP Binding configuration.
|
||||
*
|
||||
* @param https true if you want to use SSL
|
||||
* (e.g. false for http://domain.lt:7070/http-bind).
|
||||
* @param host the hostname or IP address of the connection manager
|
||||
* (e.g. domain.lt for http://domain.lt:7070/http-bind).
|
||||
* @param port the port of the connection manager
|
||||
* (e.g. 7070 for http://domain.lt:7070/http-bind).
|
||||
* @param filePath the file which is described by the URL
|
||||
* (e.g. /http-bind for http://domain.lt:7070/http-bind).
|
||||
* @param proxy the configuration of a proxy server.
|
||||
* @param xmppDomain the XMPP service name
|
||||
* (e.g. domain.lt for the user alice@domain.lt)
|
||||
*/
|
||||
public BOSHConfiguration(boolean https, String host, int port, String filePath, ProxyInfo proxy, String xmppDomain) {
|
||||
super(host, port, xmppDomain, proxy);
|
||||
ssl = https;
|
||||
file = (filePath != null ? filePath : "/");
|
||||
private BOSHConfiguration(BOSHConfigurationBuilder builder) {
|
||||
super(builder);
|
||||
https = builder.https;
|
||||
if (builder.file.charAt(0) != '/') {
|
||||
file = '/' + builder.file;
|
||||
} else {
|
||||
file = builder.file;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isProxyEnabled() {
|
||||
|
@ -105,24 +61,47 @@ public class BOSHConfiguration extends ConnectionConfiguration {
|
|||
return (proxy != null ? proxy.getProxyPort() : 8080);
|
||||
}
|
||||
|
||||
public boolean isUsingSSL() {
|
||||
return ssl;
|
||||
public boolean isUsingHTTPS() {
|
||||
return https;
|
||||
}
|
||||
|
||||
public URI getURI() throws URISyntaxException {
|
||||
if (file.charAt(0) != '/') {
|
||||
file = '/' + file;
|
||||
return new URI((https ? "https://" : "http://") + this.host + ":" + this.port + file);
|
||||
}
|
||||
|
||||
public static BOSHConfigurationBuilder builder() {
|
||||
return new BOSHConfigurationBuilder();
|
||||
}
|
||||
|
||||
public static class BOSHConfigurationBuilder extends ConnectionConfigurationBuilder<BOSHConfigurationBuilder, BOSHConfiguration> {
|
||||
private boolean https;
|
||||
private String file;
|
||||
|
||||
private BOSHConfigurationBuilder() {
|
||||
}
|
||||
String host;
|
||||
int port;
|
||||
if (hostAddresses != null) {
|
||||
HostAddress hostAddress = hostAddresses.get(0);
|
||||
host = hostAddress.getFQDN();
|
||||
port = hostAddress.getPort();
|
||||
} else {
|
||||
host = getServiceName();
|
||||
port = 80;
|
||||
|
||||
public BOSHConfigurationBuilder setUseHttps(boolean useHttps) {
|
||||
https = useHttps;
|
||||
return this;
|
||||
}
|
||||
|
||||
public BOSHConfigurationBuilder useHttps() {
|
||||
return setUseHttps(true);
|
||||
}
|
||||
|
||||
public BOSHConfigurationBuilder setFile(String file) {
|
||||
this.file = file;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BOSHConfiguration build() {
|
||||
return new BOSHConfiguration(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected BOSHConfigurationBuilder getThis() {
|
||||
return this;
|
||||
}
|
||||
return new URI((ssl ? "https://" : "http://") + host + ":" + port + file);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,7 +21,6 @@ import java.io.IOException;
|
|||
import java.io.PipedReader;
|
||||
import java.io.PipedWriter;
|
||||
import java.io.Writer;
|
||||
import java.util.Locale;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
|
@ -30,7 +29,6 @@ import javax.security.sasl.SaslException;
|
|||
import org.jivesoftware.smack.AbstractXMPPConnection;
|
||||
import org.jivesoftware.smack.SmackException;
|
||||
import org.jivesoftware.smack.SmackException.NotConnectedException;
|
||||
import org.jivesoftware.smack.SmackException.AlreadyLoggedInException;
|
||||
import org.jivesoftware.smack.SmackException.ConnectionException;
|
||||
import org.jivesoftware.smack.SASLAuthentication;
|
||||
import org.jivesoftware.smack.XMPPConnection;
|
||||
|
@ -106,6 +104,8 @@ public class XMPPBOSHConnection extends AbstractXMPPConnection {
|
|||
/**
|
||||
* Create a HTTP Binding connection to a XMPP server.
|
||||
*
|
||||
* @param username the username to use.
|
||||
* @param password the password to use.
|
||||
* @param https true if you want to use SSL
|
||||
* (e.g. false for http://domain.lt:7070/http-bind).
|
||||
* @param host the hostname or IP address of the connection manager
|
||||
|
@ -117,9 +117,10 @@ public class XMPPBOSHConnection extends AbstractXMPPConnection {
|
|||
* @param xmppDomain the XMPP service name
|
||||
* (e.g. domain.lt for the user alice@domain.lt)
|
||||
*/
|
||||
public XMPPBOSHConnection(boolean https, String host, int port, String filePath, String xmppDomain) {
|
||||
super(new BOSHConfiguration(https, host, port, filePath, xmppDomain));
|
||||
this.config = (BOSHConfiguration) getConfiguration();
|
||||
public XMPPBOSHConnection(String username, String password, boolean https, String host, int port, String filePath, String xmppDomain) {
|
||||
this(BOSHConfiguration.builder().setUseHttps(https).setHost(host)
|
||||
.setPort(port).setFile(filePath).setServiceName(xmppDomain)
|
||||
.setUsernameAndPassword(username, password).build());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -134,9 +135,6 @@ public class XMPPBOSHConnection extends AbstractXMPPConnection {
|
|||
|
||||
@Override
|
||||
protected void connectInternal() throws SmackException {
|
||||
if (connected) {
|
||||
throw new IllegalStateException("Already connected to a server.");
|
||||
}
|
||||
done = false;
|
||||
try {
|
||||
// Ensure a clean starting state
|
||||
|
@ -224,18 +222,12 @@ public class XMPPBOSHConnection extends AbstractXMPPConnection {
|
|||
return false;
|
||||
}
|
||||
|
||||
public void login(String username, String password, String resource)
|
||||
@Override
|
||||
protected void loginNonAnonymously()
|
||||
throws XMPPException, SmackException, IOException {
|
||||
if (!isConnected()) {
|
||||
throw new NotConnectedException();
|
||||
}
|
||||
if (authenticated) {
|
||||
throw new AlreadyLoggedInException();
|
||||
}
|
||||
|
||||
// Do partial version of nameprep on the username.
|
||||
username = username.toLowerCase(Locale.US).trim();
|
||||
|
||||
String password = config.getPassword();
|
||||
String resource = config.getResource();
|
||||
String username = config.getUsername();
|
||||
if (saslAuthentication.hasNonAnonymousAuthentication()) {
|
||||
// Authenticate using SASL
|
||||
if (password != null) {
|
||||
|
@ -249,19 +241,11 @@ public class XMPPBOSHConnection extends AbstractXMPPConnection {
|
|||
|
||||
bindResourceAndEstablishSession(resource);
|
||||
|
||||
// Stores the authentication for future reconnection
|
||||
setLoginInfo(username, password, resource);
|
||||
afterSuccessfulLogin(false, false);
|
||||
afterSuccessfulLogin(false);
|
||||
}
|
||||
|
||||
public void loginAnonymously() throws XMPPException, SmackException, IOException {
|
||||
if (!isConnected()) {
|
||||
throw new NotConnectedException();
|
||||
}
|
||||
if (authenticated) {
|
||||
throw new AlreadyLoggedInException();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void loginAnonymously() throws XMPPException, SmackException, IOException {
|
||||
// Wait with SASL auth until the SASL mechanisms have been received
|
||||
saslFeatureReceived.checkIfSuccessOrWaitOrThrow();
|
||||
|
||||
|
@ -275,7 +259,7 @@ public class XMPPBOSHConnection extends AbstractXMPPConnection {
|
|||
|
||||
bindResourceAndEstablishSession(null);
|
||||
|
||||
afterSuccessfulLogin(true, false);
|
||||
afterSuccessfulLogin(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -313,7 +297,7 @@ public class XMPPBOSHConnection extends AbstractXMPPConnection {
|
|||
*/
|
||||
@Override
|
||||
protected void shutdown() {
|
||||
setWasAuthenticated(authenticated);
|
||||
setWasAuthenticated();
|
||||
authID = null;
|
||||
sessionID = null;
|
||||
done = true;
|
||||
|
@ -508,10 +492,7 @@ public class XMPPBOSHConnection extends AbstractXMPPConnection {
|
|||
else {
|
||||
try {
|
||||
if (wasAuthenticated) {
|
||||
connection.login(
|
||||
config.getUsername(),
|
||||
config.getPassword(),
|
||||
config.getResource());
|
||||
connection.login();
|
||||
}
|
||||
for (ConnectionListener listener : getConnectionListeners()) {
|
||||
listener.reconnectionSuccessful();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue