mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2025-12-14 09:01:08 +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
|
|
@ -28,14 +28,16 @@ import org.jivesoftware.smack.PacketListener;
|
|||
import org.jivesoftware.smack.Manager;
|
||||
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.iqrequest.AbstractIqRequestHandler;
|
||||
import org.jivesoftware.smack.iqrequest.IQRequestHandler.Mode;
|
||||
import org.jivesoftware.smack.packet.IQ;
|
||||
import org.jivesoftware.smack.packet.IQ.Type;
|
||||
import org.jivesoftware.smack.packet.Message;
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
import org.jivesoftware.smack.packet.Presence;
|
||||
import org.jivesoftware.smack.packet.XMPPError;
|
||||
import org.jivesoftware.smack.packet.XMPPError.Condition;
|
||||
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
||||
import org.jivesoftware.smackx.iqlast.packet.LastActivity;
|
||||
|
||||
|
|
@ -89,8 +91,8 @@ import org.jivesoftware.smackx.iqlast.packet.LastActivity;
|
|||
|
||||
public class LastActivityManager extends Manager {
|
||||
private static final Map<XMPPConnection, LastActivityManager> instances = new WeakHashMap<XMPPConnection, LastActivityManager>();
|
||||
private static final PacketFilter IQ_GET_LAST_FILTER = new AndFilter(IQTypeFilter.GET,
|
||||
new PacketTypeFilter(LastActivity.class));
|
||||
// private static final PacketFilter IQ_GET_LAST_FILTER = new AndFilter(IQTypeFilter.GET,
|
||||
// new PacketTypeFilter(LastActivity.class));
|
||||
|
||||
private static boolean enabledPerDefault = true;
|
||||
|
||||
|
|
@ -160,21 +162,22 @@ public class LastActivityManager extends Manager {
|
|||
}, PacketTypeFilter.MESSAGE);
|
||||
|
||||
// Register a listener for a last activity query
|
||||
connection.addAsyncPacketListener(new PacketListener() {
|
||||
|
||||
public void processPacket(Packet packet) throws NotConnectedException {
|
||||
if (!enabled) return;
|
||||
connection.registerIQRequestHandler(new AbstractIqRequestHandler(LastActivity.ELEMENT, LastActivity.NAMESPACE,
|
||||
Type.get, Mode.async) {
|
||||
@Override
|
||||
public IQ handleIQRequest(IQ iqRequest) {
|
||||
if (!enabled)
|
||||
return IQ.createErrorResponse(iqRequest, new XMPPError(Condition.not_acceptable));
|
||||
LastActivity message = new LastActivity();
|
||||
message.setType(IQ.Type.result);
|
||||
message.setTo(packet.getFrom());
|
||||
message.setFrom(packet.getTo());
|
||||
message.setPacketID(packet.getPacketID());
|
||||
message.setTo(iqRequest.getFrom());
|
||||
message.setFrom(iqRequest.getTo());
|
||||
message.setPacketID(iqRequest.getPacketID());
|
||||
message.setLastActivity(getIdleTime());
|
||||
|
||||
connection().sendPacket(message);
|
||||
return message;
|
||||
}
|
||||
|
||||
}, IQ_GET_LAST_FILTER);
|
||||
});
|
||||
|
||||
if (enabledPerDefault) {
|
||||
enable();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue