Add Manager.getAuthenticatedConnectionOrThrow()

This commit is contained in:
Florian Schmaus 2015-04-06 14:00:27 +02:00
parent dddba8f08c
commit 1eb315d6b7
2 changed files with 19 additions and 17 deletions

View File

@ -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;
}
}

View File

@ -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)