mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2025-12-14 00:51:19 +01:00
Add IQ request handler API
This also moves the logic to send error IQ replies from "when there is no IQ provider registerd" to "when there is no IQ request handler registered". Which has for example the advantage that IQ parsing no longer asks for a connection instance.
This commit is contained in:
parent
fcb4844d10
commit
bb8dcc9874
28 changed files with 533 additions and 317 deletions
|
|
@ -26,14 +26,13 @@ import org.jivesoftware.smack.SmackException.NotConnectedException;
|
|||
import org.jivesoftware.smack.ConnectionCreationListener;
|
||||
import org.jivesoftware.smack.XMPPConnection;
|
||||
import org.jivesoftware.smack.Manager;
|
||||
import org.jivesoftware.smack.PacketListener;
|
||||
import org.jivesoftware.smack.XMPPConnectionRegistry;
|
||||
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
|
||||
import org.jivesoftware.smack.filter.AndFilter;
|
||||
import org.jivesoftware.smack.filter.IQTypeFilter;
|
||||
import org.jivesoftware.smack.filter.PacketFilter;
|
||||
import org.jivesoftware.smack.filter.PacketTypeFilter;
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
import org.jivesoftware.smack.iqrequest.AbstractIqRequestHandler;
|
||||
import org.jivesoftware.smack.iqrequest.IQRequestHandler.Mode;
|
||||
import org.jivesoftware.smack.packet.IQ;
|
||||
import org.jivesoftware.smack.packet.XMPPError;
|
||||
import org.jivesoftware.smack.packet.XMPPError.Condition;
|
||||
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
||||
import org.jivesoftware.smackx.iqversion.packet.Version;
|
||||
|
||||
|
|
@ -56,8 +55,6 @@ import org.jivesoftware.smackx.iqversion.packet.Version;
|
|||
public class VersionManager extends Manager {
|
||||
private static final Map<XMPPConnection, VersionManager> INSTANCES = new WeakHashMap<XMPPConnection, VersionManager>();
|
||||
|
||||
private static final PacketFilter PACKET_FILTER = new AndFilter(new PacketTypeFilter(Version.class), IQTypeFilter.GET);
|
||||
|
||||
private static Version defaultVersion;
|
||||
|
||||
private Version ourVersion = defaultVersion;
|
||||
|
|
@ -86,19 +83,17 @@ public class VersionManager extends Manager {
|
|||
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
|
||||
sdm.addFeature(Version.NAMESPACE);
|
||||
|
||||
connection.addAsyncPacketListener(new PacketListener() {
|
||||
/**
|
||||
* Sends a Version reply on request
|
||||
* @throws NotConnectedException
|
||||
*/
|
||||
public void processPacket(Packet packet) throws NotConnectedException {
|
||||
if (ourVersion == null)
|
||||
return;
|
||||
connection.registerIQRequestHandler(new AbstractIqRequestHandler(Version.ELEMENT, Version.NAMESPACE, IQ.Type.get,
|
||||
Mode.async) {
|
||||
@Override
|
||||
public IQ handleIQRequest(IQ iqRequest) {
|
||||
if (ourVersion == null) {
|
||||
return IQ.createErrorResponse(iqRequest, new XMPPError(Condition.not_acceptable));
|
||||
}
|
||||
|
||||
connection().sendPacket(Version.createResultFor(packet, ourVersion));
|
||||
return Version.createResultFor(iqRequest, ourVersion);
|
||||
}
|
||||
}
|
||||
, PACKET_FILTER);
|
||||
});
|
||||
}
|
||||
|
||||
public static synchronized VersionManager getInstanceFor(XMPPConnection connection) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue