1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-25 21:42:07 +01:00

Throw NotConnectedException on login()

if connect() was not previously called. Previously calling login() with
arguments would not check for the preconditions.

The check to throw needs to be performed in AbstractXMPPConnection
before every 'abstract login(Non)Anonymously()' call. That's the two
lines that check the preconditions are duplicated.

Also fix NPE in
XMPPTCPConnection.throwNotConnectedExceptionIfAppropriate() when
packetWrite is null (i.e. if the connection was never connected before).
This commit is contained in:
Florian Schmaus 2015-01-17 17:53:36 +01:00
parent 75fd1683d1
commit 6d7f3904d9
2 changed files with 7 additions and 2 deletions

View file

@ -393,9 +393,9 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
* @throws IOException * @throws IOException
*/ */
public void login() throws XMPPException, SmackException, IOException { public void login() throws XMPPException, SmackException, IOException {
throwNotConnectedExceptionIfAppropriate();
throwAlreadyLoggedInExceptionIfAppropriate();
if (isAnonymous()) { if (isAnonymous()) {
throwNotConnectedExceptionIfAppropriate();
throwAlreadyLoggedInExceptionIfAppropriate();
loginAnonymously(); loginAnonymously();
} else { } else {
// The previously used username, password and resource take over precedence over the // The previously used username, password and resource take over precedence over the
@ -440,6 +440,8 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
if (!config.allowNullOrEmptyUsername && StringUtils.isNullOrEmpty(username)) { if (!config.allowNullOrEmptyUsername && StringUtils.isNullOrEmpty(username)) {
throw new IllegalArgumentException("Username must not be null or empty"); throw new IllegalArgumentException("Username must not be null or empty");
} }
throwNotConnectedExceptionIfAppropriate();
throwAlreadyLoggedInExceptionIfAppropriate();
usedUsername = username; usedUsername = username;
usedPassword = password; usedPassword = password;
usedResource = resource; usedResource = resource;

View file

@ -306,6 +306,9 @@ public class XMPPTCPConnection extends AbstractXMPPConnection {
@Override @Override
protected void throwNotConnectedExceptionIfAppropriate() throws NotConnectedException { protected void throwNotConnectedExceptionIfAppropriate() throws NotConnectedException {
if (packetWriter == null) {
throw new NotConnectedException();
}
packetWriter.throwNotConnectedExceptionIfDoneAndResumptionNotPossible(); packetWriter.throwNotConnectedExceptionIfDoneAndResumptionNotPossible();
} }