1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-12-01 16:32:08 +01:00

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 java.lang.ref.WeakReference;
import org.jivesoftware.smack.SmackException.NotLoggedInException;
import org.jivesoftware.smack.util.Objects; import org.jivesoftware.smack.util.Objects;
public abstract class Manager { public abstract class Manager {
@ -33,4 +34,18 @@ public abstract class Manager {
protected final XMPPConnection connection() { protected final XMPPConnection connection() {
return weakConnection.get(); 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 * @throws InterruptedException
*/ */
public void reload() throws NotLoggedInException, NotConnectedException, InterruptedException{ public void reload() throws NotLoggedInException, NotConnectedException, InterruptedException{
final XMPPConnection connection = connection(); final XMPPConnection connection = getAuthenticatedConnectionOrThrow();
if (!connection.isAuthenticated()) {
throw new NotLoggedInException();
}
RosterPacket packet = new RosterPacket(); RosterPacket packet = new RosterPacket();
if (rosterStore != null && isRosterVersioningSupported()) { if (rosterStore != null && isRosterVersioningSupported()) {
@ -457,10 +454,7 @@ public final class Roster extends Manager {
* @throws InterruptedException * @throws InterruptedException
*/ */
public void createEntry(Jid user, String name, String[] groups) throws NotLoggedInException, NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { public void createEntry(Jid user, String name, String[] groups) throws NotLoggedInException, NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
final XMPPConnection connection = connection(); final XMPPConnection connection = getAuthenticatedConnectionOrThrow();
if (!connection.isAuthenticated()) {
throw new NotLoggedInException();
}
// Create and send roster entry creation packet. // Create and send roster entry creation packet.
RosterPacket rosterPacket = new RosterPacket(); RosterPacket rosterPacket = new RosterPacket();
@ -532,11 +526,7 @@ public final class Roster extends Manager {
* @since 4.2 * @since 4.2
*/ */
public boolean isSubscriptionPreApprovalSupported() throws NotLoggedInException { public boolean isSubscriptionPreApprovalSupported() throws NotLoggedInException {
final XMPPConnection connection = connection(); final XMPPConnection connection = getAuthenticatedConnectionOrThrow();
if (!connection.isAuthenticated()) {
throw new NotLoggedInException();
}
return connection.hasFeature(SubscriptionPreApproval.ELEMENT, SubscriptionPreApproval.NAMESPACE); return connection.hasFeature(SubscriptionPreApproval.ELEMENT, SubscriptionPreApproval.NAMESPACE);
} }
@ -554,10 +544,7 @@ public final class Roster extends Manager {
* @throws InterruptedException * @throws InterruptedException
*/ */
public void removeEntry(RosterEntry entry) throws NotLoggedInException, NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { public void removeEntry(RosterEntry entry) throws NotLoggedInException, NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
final XMPPConnection connection = connection(); final XMPPConnection connection = getAuthenticatedConnectionOrThrow();
if (!connection.isAuthenticated()) {
throw new NotLoggedInException();
}
// Only remove the entry if it's in the entry list. // Only remove the entry if it's in the entry list.
// The actual removal logic takes place in RosterPacketListenerprocess>>Packet(Packet) // The actual removal logic takes place in RosterPacketListenerprocess>>Packet(Packet)