Add XMPPConnection.getStreamId()

and remove getConnectionID().

Also make streamId a field of AbstractXMPPConnection. Most XMPP
connection types have a streamId, it appears to be optional when BOSH
is used though.
This commit is contained in:
Florian Schmaus 2015-01-28 17:14:33 +01:00
parent 612ca1ad9d
commit 5a56ff011b
5 changed files with 28 additions and 53 deletions

View File

@ -95,11 +95,6 @@ public class XMPPBOSHConnection extends AbstractXMPPConnection {
private PipedWriter readerPipe; private PipedWriter readerPipe;
private Thread readerConsumer; private Thread readerConsumer;
/**
* The BOSH equivalent of the stream ID which is used for DIGEST authentication.
*/
protected String authID = null;
/** /**
* The session ID for the BOSH session with the connection manager. * The session ID for the BOSH session with the connection manager.
*/ */
@ -147,7 +142,6 @@ public class XMPPBOSHConnection extends AbstractXMPPConnection {
client = null; client = null;
} }
sessionID = null; sessionID = null;
authID = null;
// Initialize BOSH client // Initialize BOSH client
BOSHClientConfig.Builder cfgBuilder = BOSHClientConfig.Builder BOSHClientConfig.Builder cfgBuilder = BOSHClientConfig.Builder
@ -206,16 +200,6 @@ public class XMPPBOSHConnection extends AbstractXMPPConnection {
callConnectionConnectedListener(); callConnectionConnectedListener();
} }
public String getConnectionID() {
if (!connected) {
return null;
} else if (authID != null) {
return authID;
} else {
return sessionID;
}
}
public boolean isSecureConnection() { public boolean isSecureConnection() {
// TODO: Implement SSL usage // TODO: Implement SSL usage
return false; return false;
@ -298,7 +282,6 @@ public class XMPPBOSHConnection extends AbstractXMPPConnection {
@Override @Override
protected void shutdown() { protected void shutdown() {
setWasAuthenticated(); setWasAuthenticated();
authID = null;
sessionID = null; sessionID = null;
done = true; done = true;
authenticated = false; authenticated = false;
@ -525,8 +508,8 @@ public class XMPPBOSHConnection extends AbstractXMPPConnection {
if (sessionID == null) { if (sessionID == null) {
sessionID = body.getAttribute(BodyQName.create(XMPPBOSHConnection.BOSH_URI, "sid")); sessionID = body.getAttribute(BodyQName.create(XMPPBOSHConnection.BOSH_URI, "sid"));
} }
if (authID == null) { if (streamId == null) {
authID = body.getAttribute(BodyQName.create(XMPPBOSHConnection.BOSH_URI, "authid")); streamId = body.getAttribute(BodyQName.create(XMPPBOSHConnection.BOSH_URI, "authid"));
} }
final XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser(); final XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);

View File

@ -168,6 +168,11 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
protected boolean connected = false; protected boolean connected = false;
/**
* The stream ID, see RFC 6120 § 4.7.3
*/
protected String streamId;
/** /**
* *
*/ */
@ -354,9 +359,6 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
return port; return port;
} }
@Override
public abstract String getConnectionID();
@Override @Override
public abstract boolean isSecureConnection(); public abstract boolean isSecureConnection();
@ -382,10 +384,16 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
* @return a reference to this object, to chain <code>connect()</code> with <code>login()</code>. * @return a reference to this object, to chain <code>connect()</code> with <code>login()</code>.
*/ */
public synchronized AbstractXMPPConnection connect() throws SmackException, IOException, XMPPException { public synchronized AbstractXMPPConnection connect() throws SmackException, IOException, XMPPException {
// Check if not already connected
throwAlreadyConnectedExceptionIfAppropriate(); throwAlreadyConnectedExceptionIfAppropriate();
// Reset the connection state
saslAuthentication.init(); saslAuthentication.init();
saslFeatureReceived.init(); saslFeatureReceived.init();
lastFeaturesReceived.init(); lastFeaturesReceived.init();
streamId = null;
// Perform the actual connection to the XMPP service
connectInternal(); connectInternal();
return this; return this;
} }
@ -501,6 +509,14 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
return user; return user;
} }
@Override
public String getStreamId() {
if (!isConnected()) {
return null;
}
return streamId;
}
// TODO remove this suppression once "disable legacy session" code has been removed from Smack // TODO remove this suppression once "disable legacy session" code has been removed from Smack
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
protected void bindResourceAndEstablishSession(String resource) throws XMPPErrorException, protected void bindResourceAndEstablishSession(String resource) throws XMPPErrorException,

View File

@ -98,14 +98,14 @@ public interface XMPPConnection {
public String getUser(); public String getUser();
/** /**
* Returns the connection ID for this connection, which is the value set by the server * Returns the stream ID for this connection, which is the value set by the server
* when opening a XMPP stream. If the server does not set a connection ID, this value * when opening a XMPP stream. This value will be <tt>null</tt> if not connected to the server.
* will be null. This value will be <tt>null</tt> if not connected to the server.
* *
* @return the ID of this connection returned from the XMPP server or <tt>null</tt> if * @return the ID of this connection returned from the XMPP server or <tt>null</tt> if
* not connected to the server. * not connected to the server.
* @see <a href="http://xmpp.org/rfcs/rfc6120.html#streams-attr-id">RFC 6120 § 4.7.3. id</a>
*/ */
public String getConnectionID(); public String getStreamId();
/** /**
* Returns true if currently connected to the XMPP server. * Returns true if currently connected to the XMPP server.

View File

@ -48,8 +48,6 @@ public class DummyConnection extends AbstractXMPPConnection {
private boolean reconnect = false; private boolean reconnect = false;
private String connectionID;
private final BlockingQueue<TopLevelStreamElement> queue = new LinkedBlockingQueue<TopLevelStreamElement>(); private final BlockingQueue<TopLevelStreamElement> queue = new LinkedBlockingQueue<TopLevelStreamElement>();
public static ConnectionConfiguration.Builder<?,?> getDummyConfigurationBuilder() { public static ConnectionConfiguration.Builder<?,?> getDummyConfigurationBuilder() {
@ -77,7 +75,7 @@ public class DummyConnection extends AbstractXMPPConnection {
@Override @Override
protected void connectInternal() { protected void connectInternal() {
connected = true; connected = true;
connectionID = "dummy-" + new Random(new Date().getTime()).nextInt(); streamId = "dummy-" + new Random(new Date().getTime()).nextInt();
if (reconnect) { if (reconnect) {
notifyReconnection(); notifyReconnection();
@ -87,24 +85,12 @@ public class DummyConnection extends AbstractXMPPConnection {
@Override @Override
protected void shutdown() { protected void shutdown() {
user = null; user = null;
connectionID = null;
authenticated = false; authenticated = false;
callConnectionClosedListener(); callConnectionClosedListener();
reconnect = true; reconnect = true;
} }
@Override
public String getConnectionID() {
if (!isConnected()) {
return null;
}
if (connectionID == null) {
connectionID = "dummy-" + new Random(new Date().getTime()).nextInt();
}
return connectionID;
}
@Override @Override
public boolean isSecureConnection() { public boolean isSecureConnection() {
return false; return false;

View File

@ -144,8 +144,6 @@ public class XMPPTCPConnection extends AbstractXMPPConnection {
*/ */
private Socket socket; private Socket socket;
private String connectionID = null;
/** /**
* *
*/ */
@ -298,14 +296,6 @@ public class XMPPTCPConnection extends AbstractXMPPConnection {
serviceName).build()); serviceName).build());
} }
@Override
public String getConnectionID() {
if (!isConnected()) {
return null;
}
return connectionID;
}
@Override @Override
protected void throwNotConnectedExceptionIfAppropriate() throws NotConnectedException { protected void throwNotConnectedExceptionIfAppropriate() throws NotConnectedException {
if (packetWriter == null) { if (packetWriter == null) {
@ -893,7 +883,7 @@ public class XMPPTCPConnection extends AbstractXMPPConnection {
if (localpart != null) { if (localpart != null) {
from = XmppStringUtils.completeJidFrom(localpart, to); from = XmppStringUtils.completeJidFrom(localpart, to);
} }
String id = getConnectionID(); String id = getStreamId();
send(new StreamOpen(to, from, id)); send(new StreamOpen(to, from, id));
try { try {
packetReader.parser = PacketParserUtils.newXmppParser(reader); packetReader.parser = PacketParserUtils.newXmppParser(reader);
@ -956,7 +946,7 @@ public class XMPPTCPConnection extends AbstractXMPPConnection {
case "stream": case "stream":
// We found an opening stream. // We found an opening stream.
if ("jabber:client".equals(parser.getNamespace(null))) { if ("jabber:client".equals(parser.getNamespace(null))) {
connectionID = parser.getAttributeValue("", "id"); streamId = parser.getAttributeValue("", "id");
String reportedServiceName = parser.getAttributeValue("", "from"); String reportedServiceName = parser.getAttributeValue("", "from");
assert(reportedServiceName.equals(config.getServiceName())); assert(reportedServiceName.equals(config.getServiceName()));
} }