Skip to content

Commit

Permalink
Rename 'serviceName' to 'xmppServiceDomain'
Browse files Browse the repository at this point in the history
Also use 'null' as default value for the resource, to enforce server
generated resources.

Fixes Smack-665
  • Loading branch information
Flowdalic committed May 19, 2015
1 parent 0c81996 commit f369a00
Show file tree
Hide file tree
Showing 38 changed files with 116 additions and 104 deletions.
Expand Up @@ -113,12 +113,12 @@ public class XMPPBOSHConnection extends AbstractXMPPConnection {
* (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
* @param xmppServiceDomain the XMPP service name
* (e.g. domain.lt for the user alice@domain.lt)
*/
public XMPPBOSHConnection(String username, String password, boolean https, String host, int port, String filePath, DomainBareJid xmppDomain) {
public XMPPBOSHConnection(String username, String password, boolean https, String host, int port, String filePath, DomainBareJid xmppServiceDomain) {
this(BOSHConfiguration.builder().setUseHttps(https).setHost(host)
.setPort(port).setFile(filePath).setServiceName(xmppDomain)
.setPort(port).setFile(filePath).setServiceName(xmppServiceDomain)
.setUsernameAndPassword(username, password).build());
}

Expand All @@ -145,7 +145,7 @@ protected void connectInternal() throws SmackException, InterruptedException {

// Initialize BOSH client
BOSHClientConfig.Builder cfgBuilder = BOSHClientConfig.Builder
.create(config.getURI(), config.getServiceName().toString());
.create(config.getURI(), config.getXMPPServiceDomain().toString());
if (config.isProxyEnabled()) {
cfgBuilder.setProxy(config.getProxyAddress(), config.getProxyPort());
}
Expand Down Expand Up @@ -495,7 +495,7 @@ public void responseReceived(BOSHMessageEvent event) {
XMPPBOSHConnection.XMPP_BOSH_NS).setAttribute(
BodyQName.createWithPrefix(XMPPBOSHConnection.XMPP_BOSH_NS, "restart",
"xmpp"), "true").setAttribute(
BodyQName.create(XMPPBOSHConnection.BOSH_URI, "to"), getServiceName().toString()).build());
BodyQName.create(XMPPBOSHConnection.BOSH_URI, "to"), getXMPPServiceDomain().toString()).build());
Success success = new Success(parser.nextText());
getSASLAuthentication().authenticated(success);
break;
Expand Down
Expand Up @@ -193,7 +193,7 @@ public void testHugeMessage() {
public void testHighestPriority() throws Exception {
// Create another connection for the same user of connection 1
ConnectionConfiguration connectionConfiguration =
new ConnectionConfiguration(getHost(), getPort(), getServiceName());
new ConnectionConfiguration(getHost(), getPort(), getXMPPServiceDomain());
XMPPTCPConnection conn3 = new XMPPConnection(connectionConfiguration);
conn3.connect();
conn3.login(getUsername(0), getPassword(0), "Home");
Expand Down Expand Up @@ -242,7 +242,7 @@ public void testHighestPriority() throws Exception {
public void testHighestShow() throws Exception {
// Create another connection for the same user of connection 1
ConnectionConfiguration connectionConfiguration =
new ConnectionConfiguration(getHost(), getPort(), getServiceName());
new ConnectionConfiguration(getHost(), getPort(), getXMPPServiceDomain());
XMPPTCPConnection conn3 = new XMPPConnection(connectionConfiguration);
conn3.connect();
conn3.login(getUsername(0), getPassword(0), "Home");
Expand Down Expand Up @@ -291,7 +291,7 @@ public void testHighestShow() throws Exception {
public void testMostRecentActive() throws Exception {
// Create another connection for the same user of connection 1
ConnectionConfiguration connectionConfiguration =
new ConnectionConfiguration(getHost(), getPort(), getServiceName());
new ConnectionConfiguration(getHost(), getPort(), getXMPPServiceDomain());
XMPPTCPConnection conn3 = new XMPPConnection(connectionConfiguration);
conn3.connect();
conn3.login(getUsername(0), getPassword(0), "Home");
Expand All @@ -307,7 +307,7 @@ public void testMostRecentActive() throws Exception {
getConnection(0).sendStanza(presence);

connectionConfiguration =
new ConnectionConfiguration(getHost(), getPort(), getServiceName());
new ConnectionConfiguration(getHost(), getPort(), getXMPPServiceDomain());
XMPPTCPConnection conn4 = new XMPPConnection(connectionConfiguration);
conn4.connect();
conn4.login(getUsername(0), getPassword(0), "Home2");
Expand All @@ -326,7 +326,7 @@ public void testMostRecentActive() throws Exception {
PacketCollector coll4 = conn4.createPacketCollector(new MessageTypeFilter(Message.Type.chat));

// Send a message from this resource to indicate most recent activity
conn3.sendStanza(new Message("admin@" + getServiceName()));
conn3.sendStanza(new Message("admin@" + getXMPPServiceDomain()));

// User1 sends a message to the bare JID of User0
Chat chat = getConnection(1).getChatManager().createChat(getBareJID(0), null);
Expand Down
Expand Up @@ -168,7 +168,7 @@ public void testNotAvailablePresence() throws XMPPException {
public void testMultipleResources() throws Exception {
// Create another connection for the same user of connection 1
ConnectionConfiguration connectionConfiguration =
new ConnectionConfiguration(getHost(), getPort(), getServiceName());
new ConnectionConfiguration(getHost(), getPort(), getXMPPServiceDomain());
XMPPTCPConnection conn4 = new XMPPConnection(connectionConfiguration);
conn4.connect();
conn4.login(getUsername(1), getPassword(1), "Home");
Expand Down
Expand Up @@ -434,7 +434,7 @@ public void testRosterPresences() throws Exception {

// Create another connection for the same user of connection 1
ConnectionConfiguration connectionConfiguration =
new ConnectionConfiguration(getHost(), getPort(), getServiceName());
new ConnectionConfiguration(getHost(), getPort(), getXMPPServiceDomain());
XMPPTCPConnection conn4 = new XMPPConnection(connectionConfiguration);
conn4.connect();
conn4.login(getUsername(1), getPassword(1), "Home");
Expand All @@ -455,7 +455,7 @@ public void testRosterPresences() throws Exception {
assertTrue("Returned a null Presence for an existing user", presence.isAvailable());

// Check that the right presence is returned for a user+resource
presence = roster.getPresenceResource(getUsername(1) + "@" + conn4.getServiceName() + "/Home");
presence = roster.getPresenceResource(getUsername(1) + "@" + conn4.getXMPPServiceDomain() + "/Home");
assertEquals("Returned the wrong Presence", "Home",
StringUtils.parseResource(presence.getFrom()));

Expand All @@ -466,7 +466,7 @@ public void testRosterPresences() throws Exception {
StringUtils.parseResource(presence.getFrom()));

// Check the returned presence for a non-existent user+resource
presence = roster.getPresenceResource("noname@" + getServiceName() + "/Smack");
presence = roster.getPresenceResource("noname@" + getXMPPServiceDomain() + "/Smack");
assertFalse("Available presence was returned for a non-existing user", presence.isAvailable());
assertEquals("Returned Presence for a non-existing user has the incorrect type",
Presence.Type.unavailable, presence.getType());
Expand Down Expand Up @@ -501,7 +501,7 @@ public void testRosterPresences() throws Exception {
public void testMultipleResources() throws Exception {
// Create another connection for the same user of connection 1
ConnectionConfiguration connectionConfiguration =
new ConnectionConfiguration(getHost(), getPort(), getServiceName());
new ConnectionConfiguration(getHost(), getPort(), getXMPPServiceDomain());
XMPPTCPConnection conn4 = new XMPPConnection(connectionConfiguration);
conn4.connect();
conn4.login(getUsername(1), getPassword(1), "Home");
Expand Down Expand Up @@ -566,7 +566,7 @@ public void testNotCommonNickname() throws Exception {

// Create another connection for the same user of connection 0
ConnectionConfiguration connectionConfiguration =
new ConnectionConfiguration(getHost(), getPort(), getServiceName());
new ConnectionConfiguration(getHost(), getPort(), getXMPPServiceDomain());
XMPPTCPConnection conn2 = new XMPPConnection(connectionConfiguration);
conn2.connect();
conn2.login(getUsername(0), getPassword(0), "Home");
Expand Down
Expand Up @@ -185,7 +185,7 @@ protected String getPassword(int index) {
* @return the bare XMPP address of the user (e.g. johndoe@jabber.org).
*/
protected String getBareJID(int index) {
return getUsername(index) + "@" + getConnection(index).getServiceName();
return getUsername(index) + "@" + getConnection(index).getXMPPServiceDomain();
}

/**
Expand All @@ -207,7 +207,7 @@ protected int getPort() {
return port;
}

protected String getServiceName() {
protected String getXMPPServiceDomain() {
return serviceName;
}

Expand Down Expand Up @@ -259,7 +259,7 @@ protected void setUp() throws Exception {
// cases, but could fail if the user set a hostname in their XMPP server
// that will not resolve as a network connection.
host = connections[0].getHost();
serviceName = connections[0].getServiceName();
serviceName = connections[0].getXMPPServiceDomain();

if (!createOfflineConnections()) {
for (int i = 0; i < getMaxConnections(); i++) {
Expand Down
Expand Up @@ -111,7 +111,7 @@ public void sortSRVdistributeZeroWeights() {
}

private void xmppClientDomainTest() {
List<HostAddress> hostAddresses = DNSUtil.resolveXMPPDomain(igniterealtimeDomain);
List<HostAddress> hostAddresses = DNSUtil.resolveXMPPServiceDomain(igniterealtimeDomain);
HostAddress ha = hostAddresses.get(0);
assertEquals(ha.getFQDN(), igniterealtimeXMPPServer);
assertEquals(ha.getPort(), igniterealtimeClientPort);
Expand Down
Expand Up @@ -303,12 +303,18 @@ protected ConnectionConfiguration getConfiguration() {
return config;
}

@SuppressWarnings("deprecation")
@Override
public DomainBareJid getServiceName() {
if (serviceName != null) {
return serviceName;
return getXMPPServiceDomain();
}

@Override
public DomainBareJid getXMPPServiceDomain() {
if (xmppServiceDomain != null) {
return xmppServiceDomain;
}
return config.getServiceName();
return config.getXMPPServiceDomain();
}

@Override
Expand Down Expand Up @@ -506,7 +512,7 @@ protected void bindResourceAndEstablishSession(Resourcepart resource) throws XMP
// from the login() arguments and the configurations service name, as, for example, when SASL External is used,
// the username is not given to login but taken from the 'external' certificate.
user = response.getJid();
serviceName = user.asDomainBareJid();
xmppServiceDomain = user.asDomainBareJid();

Session.Feature sessionFeature = getFeature(Session.ELEMENT, Session.NAMESPACE);
// Only bind the session if it's announced as stream feature by the server, is not optional and not disabled
Expand Down Expand Up @@ -549,7 +555,7 @@ public final boolean isAnonymous() {
&& !config.allowNullOrEmptyUsername;
}

private DomainBareJid serviceName;
private DomainBareJid xmppServiceDomain;

protected List<HostAddress> hostAddresses;

Expand All @@ -567,7 +573,7 @@ protected List<HostAddress> populateHostAddresses() {
hostAddress = new HostAddress(config.host, config.port);
hostAddresses.add(hostAddress);
} else {
hostAddresses = DNSUtil.resolveXMPPDomain(config.serviceName.toString(), failedAddresses);
hostAddresses = DNSUtil.resolveXMPPServiceDomain(config.getXMPPServiceDomain().toString(), failedAddresses);
}
// If we reach this, then hostAddresses *must not* be empty, i.e. there is at least one host added, either the
// config.host one or the host representing the service name by DNSUtil
Expand Down
Expand Up @@ -22,8 +22,6 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jivesoftware.smack.packet.Session;
import org.jivesoftware.smack.proxy.ProxyInfo;
Expand Down Expand Up @@ -54,14 +52,13 @@ public abstract class ConnectionConfiguration {
SmackConfiguration.getVersion();
}

private static final Logger LOGGER = Logger.getLogger(ConnectionConfiguration.class.getName());

/**
* Hostname of the XMPP server. Usually servers use the same service name as the name
* The XMPP domain of the XMPP Service. Usually servers use the same service name as the name
* of the server. However, there are some servers like google where host would be
* talk.google.com and the serviceName would be gmail.com.
*/
protected final DomainBareJid serviceName;
protected final DomainBareJid xmppServiceDomain;

protected final String host;
protected final int port;

Expand Down Expand Up @@ -120,9 +117,9 @@ protected ConnectionConfiguration(Builder<?,?> builder) {
// Resource can be null, this means that the server must provide one
resource = builder.resource;

serviceName = builder.serviceName;
if (serviceName == null) {
throw new IllegalArgumentException("Must provide XMPP service name");
xmppServiceDomain = builder.xmppServiceDomain;
if (xmppServiceDomain == null) {
throw new IllegalArgumentException("Must define the XMPP domain");
}
host = builder.host;
port = builder.port;
Expand Down Expand Up @@ -159,9 +156,20 @@ protected ConnectionConfiguration(Builder<?,?> builder) {
* Returns the server name of the target server.
*
* @return the server name of the target server.
* @deprecated use {@link #getXMPPServiceDomain()} instead.
*/
@Deprecated
public DomainBareJid getServiceName() {
return serviceName;
return xmppServiceDomain;
}

/**
* Returns the XMPP domain used by this configuration.
*
* @return the XMPP domain.
*/
public DomainBareJid getXMPPServiceDomain() {
return xmppServiceDomain;
}

/**
Expand Down Expand Up @@ -407,19 +415,6 @@ public Set<String> getEnabledSaslMechanisms() {
* @param <C> the resulting connection configuration type parameter.
*/
public static abstract class Builder<B extends Builder<B, C>, C extends ConnectionConfiguration> {
private static final Resourcepart DEFAULT_RESOURCE;

static {
Resourcepart resourcepart = null;
try {
resourcepart = Resourcepart.from("Smack");
}
catch (XmppStringprepException e) {
LOGGER.log(Level.WARNING, "Could not create default resourcepart", e);
}
DEFAULT_RESOURCE = resourcepart;
}

private SecurityMode securityMode = SecurityMode.ifpossible;
private String keystorePath = System.getProperty("javax.net.ssl.keyStore");
private String keystoreType = "jks";
Expand All @@ -430,14 +425,14 @@ public static abstract class Builder<B extends Builder<B, C>, C extends Connecti
private HostnameVerifier hostnameVerifier;
private CharSequence username;
private String password;
private Resourcepart resource = DEFAULT_RESOURCE;
private Resourcepart resource;
private boolean sendPresence = true;
private boolean legacySessionDisabled = false;
private ProxyInfo proxy;
private CallbackHandler callbackHandler;
private boolean debuggerEnabled = SmackConfiguration.DEBUG;
private SocketFactory socketFactory;
private DomainBareJid serviceName;
private DomainBareJid xmppServiceDomain;
private String host;
private int port = 5222;
private boolean allowEmptyOrNullUsername = false;
Expand Down Expand Up @@ -471,7 +466,7 @@ public B setUsernameAndPassword(CharSequence username, String password) {
* @return a reference to this builder.
*/
public B setServiceName(DomainBareJid serviceName) {
this.serviceName = serviceName;
this.xmppServiceDomain = serviceName;
return getThis();
}

Expand All @@ -496,7 +491,7 @@ public B setResource(Resourcepart resource) {
* @param resource the non-null CharSequence to use a resource.
* @return a reference ot this builder.
* @throws XmppStringprepException if the CharSequence is not a valid resourcepart.
* @see setResource(Resourcepart)
* @see #setResource(Resourcepart)
*/
public B setResource(CharSequence resource) throws XmppStringprepException {
Objects.requireNonNull(resource, "resource must not be null");
Expand Down
Expand Up @@ -183,14 +183,14 @@ public void authenticate(String username, String password)
currentMechanism = selectMechanism();
final CallbackHandler callbackHandler = configuration.getCallbackHandler();
final String host = connection.getHost();
final DomainBareJid xmppDomain = connection.getServiceName();
final DomainBareJid xmppServiceDomain = connection.getXMPPServiceDomain();

synchronized (this) {
if (callbackHandler != null) {
currentMechanism.authenticate(host, xmppDomain, callbackHandler);
currentMechanism.authenticate(host, xmppServiceDomain, callbackHandler);
}
else {
currentMechanism.authenticate(username, host, xmppDomain, password);
currentMechanism.authenticate(username, host, xmppServiceDomain, password);
}
// Wait until SASL negotiation finishes
wait(connection.getPacketReplyTimeout());
Expand Down
Expand Up @@ -78,9 +78,20 @@ public interface XMPPConnection {
* authenticating with the server the returned value may be different.
*
* @return the name of the service provided by the XMPP server.
// TODO remove this once the java bugs are fixed, causing a warning
// * @deprecated use {@link #getXMPPServiceDomain()} instead.
*/
// @Deprecated
public DomainBareJid getServiceName();

/**
* Returns the XMPP Domain of the service provided by the XMPP server and used for this connection. After
* authenticating with the server the returned value may be different.
*
* @return the XMPP domain of this XMPP session.
*/
public DomainBareJid getXMPPServiceDomain();

/**
* Returns the host name of the server where the XMPP server is running. This would be the
* IP address of the server or a name that may be resolved by a DNS server.
Expand Down
Expand Up @@ -149,7 +149,7 @@ public void userHasLogged(FullJid user) {
"User logged (" + connection.getConnectionCounter() + "): "
+ (isAnonymous ? "" : localpart)
+ "@"
+ connection.getServiceName()
+ connection.getXMPPServiceDomain()
+ ":"
+ connection.getPort();
title += "/" + user.getResourcepart();
Expand Down
Expand Up @@ -91,7 +91,7 @@ public IQReplyFilter(IQ iqPacket, XMPPConnection conn) {
throw new IllegalArgumentException("Must have a local (user) JID set. Either you didn't configure one or you where not connected at least once");
}

server = conn.getServiceName();
server = conn.getXMPPServiceDomain();
packetId = iqPacket.getStanzaId();

StanzaFilter iqFilter = new OrFilter(IQTypeFilter.ERROR, IQTypeFilter.RESULT);
Expand Down

0 comments on commit f369a00

Please sign in to comment.