From 6203d163c460bdda4f2ffabff7de898bc8740c54 Mon Sep 17 00:00:00 2001
From: Florian Schmaus
Date: Wed, 16 Aug 2017 13:46:42 +0200
Subject: [PATCH] Deprecate old async API
---
.../smack/AbstractXMPPConnection.java | 7 +++-
.../jivesoftware/smack/ExceptionCallback.java | 8 +++-
.../jivesoftware/smack/XMPPConnection.java | 38 ++++++++++++-------
.../smackx/carbons/CarbonManager.java | 32 +++++++---------
.../org/jivesoftware/smack/roster/Roster.java | 17 ++++++---
5 files changed, 64 insertions(+), 38 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 8814179db..984f876a2 100644
--- a/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java
+++ b/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java
@@ -1476,12 +1476,14 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
streamFeatures.put(key, feature);
}
+ @SuppressWarnings("deprecation")
@Override
public void sendStanzaWithResponseCallback(Stanza stanza, StanzaFilter replyFilter,
StanzaListener callback) throws NotConnectedException, InterruptedException {
sendStanzaWithResponseCallback(stanza, replyFilter, callback, null);
}
+ @SuppressWarnings("deprecation")
@Override
public void sendStanzaWithResponseCallback(Stanza stanza, StanzaFilter replyFilter,
StanzaListener callback, ExceptionCallback exceptionCallback)
@@ -1571,7 +1573,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
return future;
}
- @SuppressWarnings("FutureReturnValueIgnored")
+ @SuppressWarnings({ "FutureReturnValueIgnored", "deprecation" })
@Override
public void sendStanzaWithResponseCallback(Stanza stanza, final StanzaFilter replyFilter,
final StanzaListener callback, final ExceptionCallback exceptionCallback,
@@ -1624,18 +1626,21 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
sendStanza(stanza);
}
+ @SuppressWarnings("deprecation")
@Override
public void sendIqWithResponseCallback(IQ iqRequest, StanzaListener callback)
throws NotConnectedException, InterruptedException {
sendIqWithResponseCallback(iqRequest, callback, null);
}
+ @SuppressWarnings("deprecation")
@Override
public void sendIqWithResponseCallback(IQ iqRequest, StanzaListener callback,
ExceptionCallback exceptionCallback) throws NotConnectedException, InterruptedException {
sendIqWithResponseCallback(iqRequest, callback, exceptionCallback, getReplyTimeout());
}
+ @SuppressWarnings("deprecation")
@Override
public void sendIqWithResponseCallback(IQ iqRequest, final StanzaListener callback,
final ExceptionCallback exceptionCallback, long timeout)
diff --git a/smack-core/src/main/java/org/jivesoftware/smack/ExceptionCallback.java b/smack-core/src/main/java/org/jivesoftware/smack/ExceptionCallback.java
index cc1b98bdb..e13120c83 100644
--- a/smack-core/src/main/java/org/jivesoftware/smack/ExceptionCallback.java
+++ b/smack-core/src/main/java/org/jivesoftware/smack/ExceptionCallback.java
@@ -1,6 +1,6 @@
/**
*
- * Copyright © 2014 Florian Schmaus
+ * Copyright © 2014-2017 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,12 @@
*/
package org.jivesoftware.smack;
+/**
+ * This interface has been deprecated. Please use org.jivesoftware.smack.util.ExceptionCallback instead.
+ *
+ * @deprecated use {@link org.jivesoftware.smack.util.ExceptionCallback} instead.
+ */
+@Deprecated
public interface ExceptionCallback {
public void processException(Exception exception);
diff --git a/smack-core/src/main/java/org/jivesoftware/smack/XMPPConnection.java b/smack-core/src/main/java/org/jivesoftware/smack/XMPPConnection.java
index ce4c2f138..393d9fdae 100644
--- a/smack-core/src/main/java/org/jivesoftware/smack/XMPPConnection.java
+++ b/smack-core/src/main/java/org/jivesoftware/smack/XMPPConnection.java
@@ -428,7 +428,6 @@ public interface XMPPConnection {
*/
public boolean hasFeature(String element, String namespace);
-
/**
* Send an IQ request asynchronously. The connection's default reply timeout will be used.
*
@@ -447,7 +446,8 @@ public interface XMPPConnection {
public SmackFuture sendIqRequestAsync(IQ request, long timeout);
/**
- * Send a stanza asynchronously, waiting for exactly one response stanza using the given reply filter. The connection's default reply timeout will be used.
+ * Send a stanza asynchronously, waiting for exactly one response stanza using the given reply filter. The
+ * connection's default reply timeout will be used.
*
* @param stanza the stanza to send.
* @param replyFilter the filter used for the response stanza.
@@ -477,9 +477,11 @@ public interface XMPPConnection {
* @param replyFilter the filter used to determine response stanza (required)
* @param callback the callback invoked if there is a response (required)
* @throws NotConnectedException
- * @throws InterruptedException
+ * @throws InterruptedException
+ * @deprecated use {@link #sendAsync(Stanza, StanzaFilter)} instead.
*/
- // TODO: Mark deprecated in favor of the new SmackFuture based async API.
+ @Deprecated
+ // TODO: Remove in Smack 4.4.
public void sendStanzaWithResponseCallback(Stanza stanza, StanzaFilter replyFilter,
StanzaListener callback) throws NotConnectedException, InterruptedException;
@@ -497,10 +499,12 @@ public interface XMPPConnection {
* @param exceptionCallback the callback invoked if there is an exception (optional)
* @throws NotConnectedException
* @throws InterruptedException
+ * @deprecated use {@link #sendAsync(Stanza, StanzaFilter)} instead.
*/
- // TODO: Mark deprecated in favor of the new SmackFuture based async API. And do not forget to mark smack.ExceptionCallback deprecated too.
+ @Deprecated
+ // TODO: Remove in Smack 4.4.
public void sendStanzaWithResponseCallback(Stanza stanza, StanzaFilter replyFilter, StanzaListener callback,
- ExceptionCallback exceptionCallback) throws NotConnectedException, InterruptedException;
+ @SuppressWarnings("deprecation") ExceptionCallback exceptionCallback) throws NotConnectedException, InterruptedException;
/**
* Send a stanza and wait asynchronously for a response by using replyFilter
.
@@ -517,10 +521,12 @@ public interface XMPPConnection {
* @param timeout the timeout in milliseconds to wait for a response
* @throws NotConnectedException
* @throws InterruptedException
+ * @deprecated use {@link #sendAsync(Stanza, StanzaFilter, long)} instead.
*/
- // TODO: Mark deprecated in favor of the new SmackFuture based async API.
+ @Deprecated
+ // TODO: Remove in Smack 4.4.
public void sendStanzaWithResponseCallback(Stanza stanza, StanzaFilter replyFilter,
- final StanzaListener callback, final ExceptionCallback exceptionCallback,
+ final StanzaListener callback, @SuppressWarnings("deprecation") final ExceptionCallback exceptionCallback,
long timeout) throws NotConnectedException, InterruptedException;
/**
@@ -532,8 +538,10 @@ public interface XMPPConnection {
* @param callback the callback invoked if there is result response (required)
* @throws NotConnectedException
* @throws InterruptedException
+ * @deprecated use {@link #sendIqRequestAsync(IQ)} instead.
*/
- // TODO: Mark deprecated in favor of the new SmackFuture based async API.
+ @Deprecated
+ // TODO: Remove in Smack 4.4.
public void sendIqWithResponseCallback(IQ iqRequest, StanzaListener callback) throws NotConnectedException, InterruptedException;
/**
@@ -549,10 +557,12 @@ public interface XMPPConnection {
* @param exceptionCallback the callback invoked if there is an Exception optional
* @throws NotConnectedException
* @throws InterruptedException
+ * @deprecated use {@link #sendIqRequestAsync(IQ)} instead.
*/
- // TODO: Mark deprecated in favor of the new SmackFuture based async API.
+ @Deprecated
+ // TODO: Remove in Smack 4.4.
public void sendIqWithResponseCallback(IQ iqRequest, StanzaListener callback,
- ExceptionCallback exceptionCallback) throws NotConnectedException, InterruptedException;
+ @SuppressWarnings("deprecation") ExceptionCallback exceptionCallback) throws NotConnectedException, InterruptedException;
/**
* Send a IQ stanza and invoke callback
if there is a result of
@@ -568,10 +578,12 @@ public interface XMPPConnection {
* @param timeout the timeout in milliseconds to wait for a response
* @throws NotConnectedException
* @throws InterruptedException
+ * @deprecated use {@link #sendIqRequestAsync(IQ, long)} instead.
*/
- // TODO: Mark deprecated in favor of the new SmackFuture based async API.
+ @Deprecated
+ // TODO: Remove in Smack 4.4.
public void sendIqWithResponseCallback(IQ iqRequest, final StanzaListener callback,
- final ExceptionCallback exceptionCallback, long timeout)
+ @SuppressWarnings("deprecation") final ExceptionCallback exceptionCallback, long timeout)
throws NotConnectedException, InterruptedException;
/**
diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/carbons/CarbonManager.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/carbons/CarbonManager.java
index 83243add5..63fe10749 100644
--- a/smack-experimental/src/main/java/org/jivesoftware/smackx/carbons/CarbonManager.java
+++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/carbons/CarbonManager.java
@@ -23,11 +23,11 @@ import java.util.concurrent.CopyOnWriteArraySet;
import org.jivesoftware.smack.AbstractConnectionListener;
import org.jivesoftware.smack.ConnectionCreationListener;
-import org.jivesoftware.smack.ExceptionCallback;
import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
+import org.jivesoftware.smack.SmackFuture;
import org.jivesoftware.smack.StanzaListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPConnectionRegistry;
@@ -42,6 +42,8 @@ import org.jivesoftware.smack.filter.StanzaTypeFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Stanza;
+import org.jivesoftware.smack.util.ExceptionCallback;
+import org.jivesoftware.smack.util.SuccessCallback;
import org.jivesoftware.smackx.carbons.packet.Carbon;
import org.jivesoftware.smackx.carbons.packet.CarbonExtension;
@@ -245,10 +247,9 @@ public final class CarbonManager extends Manager {
*
*
* @param exceptionCallback the optional exception callback.
- * @throws InterruptedException if the thread got interrupted while this action is performed.
* @since 4.2
*/
- public void enableCarbonsAsync(ExceptionCallback exceptionCallback) throws InterruptedException {
+ public void enableCarbonsAsync(ExceptionCallback exceptionCallback) {
sendUseCarbons(true, exceptionCallback);
}
@@ -262,29 +263,24 @@ public final class CarbonManager extends Manager {
*
*
* @param exceptionCallback the optional exception callback.
- * @throws InterruptedException if the thread got interrupted while this action is performed.
* @since 4.2
*/
- public void disableCarbonsAsync(ExceptionCallback exceptionCallback) throws InterruptedException {
+ public void disableCarbonsAsync(ExceptionCallback exceptionCallback) {
sendUseCarbons(false, exceptionCallback);
}
- private void sendUseCarbons(final boolean use, ExceptionCallback exceptionCallback) throws InterruptedException {
+ private void sendUseCarbons(final boolean use, ExceptionCallback exceptionCallback) {
IQ setIQ = carbonsEnabledIQ(use);
- try {
- connection().sendIqWithResponseCallback(setIQ, new StanzaListener() {
- @Override
- public void processStanza(Stanza packet) {
- enabled_state = use;
- }
- }, exceptionCallback);
- }
- catch (NotConnectedException e) {
- if (exceptionCallback != null) {
- exceptionCallback.processException(e);
+ SmackFuture future = connection().sendIqRequestAsync(setIQ);
+
+ future.onSuccess(new SuccessCallback() {
+
+ @Override
+ public void onSuccess(IQ result) {
+ enabled_state = use;
}
- }
+ }).onError(exceptionCallback);
}
/**
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 dfd35a636..06b1b6563 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
@@ -35,13 +35,13 @@ import java.util.logging.Logger;
import org.jivesoftware.smack.AbstractConnectionListener;
import org.jivesoftware.smack.ConnectionCreationListener;
-import org.jivesoftware.smack.ExceptionCallback;
import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackException.FeatureNotSupportedException;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.SmackException.NotLoggedInException;
+import org.jivesoftware.smack.SmackFuture;
import org.jivesoftware.smack.StanzaListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPConnectionRegistry;
@@ -63,7 +63,9 @@ import org.jivesoftware.smack.roster.packet.RosterPacket.Item;
import org.jivesoftware.smack.roster.packet.RosterVer;
import org.jivesoftware.smack.roster.packet.SubscriptionPreApproval;
import org.jivesoftware.smack.roster.rosterstore.RosterStore;
+import org.jivesoftware.smack.util.ExceptionCallback;
import org.jivesoftware.smack.util.Objects;
+import org.jivesoftware.smack.util.SuccessCallback;
import org.jxmpp.jid.BareJid;
import org.jxmpp.jid.EntityBareJid;
@@ -428,7 +430,11 @@ public final class Roster extends Manager {
packet.setVersion(rosterStore.getRosterVersion());
}
rosterState = RosterState.loading;
- connection.sendIqWithResponseCallback(packet, new RosterResultListener(), new ExceptionCallback() {
+
+ SmackFuture future = connection.sendIqRequestAsync(packet);
+
+ future.onSuccess(new RosterResultListener()).onError(new ExceptionCallback() {
+
@Override
public void processException(Exception exception) {
rosterState = RosterState.uninitialized;
@@ -438,11 +444,12 @@ public final class Roster extends Manager {
} else {
logLevel = Level.SEVERE;
}
- LOGGER.log(logLevel, "Exception reloading roster" , exception);
+ LOGGER.log(logLevel, "Exception reloading roster", exception);
for (RosterLoadedListener listener : rosterLoadedListeners) {
listener.onRosterLoadingFailed(exception);
}
}
+
});
}
@@ -1565,10 +1572,10 @@ public final class Roster extends Manager {
/**
* Handles Roster results as described in RFC 6121 2.1.4.
*/
- private class RosterResultListener implements StanzaListener {
+ private class RosterResultListener implements SuccessCallback {
@Override
- public void processStanza(Stanza packet) {
+ public void onSuccess(IQ packet) {
final XMPPConnection connection = connection();
LOGGER.log(Level.FINE, "RosterResultListener received {}", packet);
Collection addedEntries = new ArrayList<>();