From 6d7f3904d93840b579f7b0f7f5effb2a238b0f49 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Sat, 17 Jan 2015 17:53:36 +0100 Subject: [PATCH] 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). --- .../java/org/jivesoftware/smack/AbstractXMPPConnection.java | 6 ++++-- .../java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java | 3 +++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java b/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java index 072025894..53c150b9b 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java @@ -393,9 +393,9 @@ public abstract class AbstractXMPPConnection implements XMPPConnection { * @throws IOException */ public void login() throws XMPPException, SmackException, IOException { - throwNotConnectedExceptionIfAppropriate(); - throwAlreadyLoggedInExceptionIfAppropriate(); if (isAnonymous()) { + throwNotConnectedExceptionIfAppropriate(); + throwAlreadyLoggedInExceptionIfAppropriate(); loginAnonymously(); } else { // 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)) { throw new IllegalArgumentException("Username must not be null or empty"); } + throwNotConnectedExceptionIfAppropriate(); + throwAlreadyLoggedInExceptionIfAppropriate(); usedUsername = username; usedPassword = password; usedResource = resource; diff --git a/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java b/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java index 10e4cca18..a37827bdd 100644 --- a/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java +++ b/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java @@ -306,6 +306,9 @@ public class XMPPTCPConnection extends AbstractXMPPConnection { @Override protected void throwNotConnectedExceptionIfAppropriate() throws NotConnectedException { + if (packetWriter == null) { + throw new NotConnectedException(); + } packetWriter.throwNotConnectedExceptionIfDoneAndResumptionNotPossible(); }