From 1eb315d6b7ed7fd6a36e3af6b9edb06121c82977 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Mon, 6 Apr 2015 14:00:27 +0200 Subject: [PATCH] Add Manager.getAuthenticatedConnectionOrThrow() --- .../java/org/jivesoftware/smack/Manager.java | 15 +++++++++++++ .../org/jivesoftware/smack/roster/Roster.java | 21 ++++--------------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/Manager.java b/smack-core/src/main/java/org/jivesoftware/smack/Manager.java index 8f9299832..47f5e0ade 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/Manager.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/Manager.java @@ -18,6 +18,7 @@ package org.jivesoftware.smack; import java.lang.ref.WeakReference; +import org.jivesoftware.smack.SmackException.NotLoggedInException; import org.jivesoftware.smack.util.Objects; public abstract class Manager { @@ -33,4 +34,18 @@ public abstract class Manager { protected final XMPPConnection connection() { return weakConnection.get(); } + + /** + * Get the XMPPConnection of this Manager if it's authenticated, i.e. logged in. Otherwise throw a {@link NotLoggedInException}. + * + * @return the XMPPConnection of this Manager. + * @throws NotLoggedInException if the connection is not authenticated. + */ + protected final XMPPConnection getAuthenticatedConnectionOrThrow() throws NotLoggedInException { + XMPPConnection connection = connection(); + if (!connection.isAuthenticated()) { + throw new NotLoggedInException(); + } + return connection; + } } diff --git a/smack-im/src/main/java/org/jivesoftware/smack/roster/Roster.java b/smack-im/src/main/java/org/jivesoftware/smack/roster/Roster.java index 2f5c3b656..cfde00dbe 100644 --- a/smack-im/src/main/java/org/jivesoftware/smack/roster/Roster.java +++ b/smack-im/src/main/java/org/jivesoftware/smack/roster/Roster.java @@ -287,10 +287,7 @@ public final class Roster extends Manager { * @throws InterruptedException */ public void reload() throws NotLoggedInException, NotConnectedException, InterruptedException{ - final XMPPConnection connection = connection(); - if (!connection.isAuthenticated()) { - throw new NotLoggedInException(); - } + final XMPPConnection connection = getAuthenticatedConnectionOrThrow(); RosterPacket packet = new RosterPacket(); if (rosterStore != null && isRosterVersioningSupported()) { @@ -457,10 +454,7 @@ public final class Roster extends Manager { * @throws InterruptedException */ public void createEntry(Jid user, String name, String[] groups) throws NotLoggedInException, NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { - final XMPPConnection connection = connection(); - if (!connection.isAuthenticated()) { - throw new NotLoggedInException(); - } + final XMPPConnection connection = getAuthenticatedConnectionOrThrow(); // Create and send roster entry creation packet. RosterPacket rosterPacket = new RosterPacket(); @@ -532,11 +526,7 @@ public final class Roster extends Manager { * @since 4.2 */ public boolean isSubscriptionPreApprovalSupported() throws NotLoggedInException { - final XMPPConnection connection = connection(); - if (!connection.isAuthenticated()) { - throw new NotLoggedInException(); - } - + final XMPPConnection connection = getAuthenticatedConnectionOrThrow(); return connection.hasFeature(SubscriptionPreApproval.ELEMENT, SubscriptionPreApproval.NAMESPACE); } @@ -554,10 +544,7 @@ public final class Roster extends Manager { * @throws InterruptedException */ public void removeEntry(RosterEntry entry) throws NotLoggedInException, NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { - final XMPPConnection connection = connection(); - if (!connection.isAuthenticated()) { - throw new NotLoggedInException(); - } + final XMPPConnection connection = getAuthenticatedConnectionOrThrow(); // Only remove the entry if it's in the entry list. // The actual removal logic takes place in RosterPacketListenerprocess>>Packet(Packet)