From 6632345efc73c6cc3f854035f75f42ec02ccb58a Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Mon, 18 May 2015 12:55:29 +0200 Subject: [PATCH 1/2] Register IQ request handler for Offer(Request|Revoke) IQs Fixes SMACK-664. --- .../smackx/workgroup/agent/AgentSession.java | 53 ++++++++++++------- .../packet/OfferRequestProvider.java | 5 +- .../workgroup/packet/OfferRevokeProvider.java | 5 +- 3 files changed, 43 insertions(+), 20 deletions(-) diff --git a/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/agent/AgentSession.java b/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/agent/AgentSession.java index 41e44f293..eb0dddb64 100644 --- a/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/agent/AgentSession.java +++ b/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/agent/AgentSession.java @@ -41,6 +41,8 @@ import org.jivesoftware.smack.filter.AndFilter; import org.jivesoftware.smack.filter.FromMatchesFilter; import org.jivesoftware.smack.filter.OrFilter; import org.jivesoftware.smack.filter.StanzaTypeFilter; +import org.jivesoftware.smack.iqrequest.AbstractIqRequestHandler; +import org.jivesoftware.smack.iqrequest.IQRequestHandler.Mode; import org.jivesoftware.smack.packet.DefaultExtensionElement; import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.Message; @@ -142,8 +144,6 @@ public class AgentSession { // Create a filter to listen for packets we're interested in. OrFilter filter = new OrFilter( - new StanzaTypeFilter(OfferRequestProvider.OfferRequestPacket.class), - new StanzaTypeFilter(OfferRevokeProvider.OfferRevokePacket.class), new StanzaTypeFilter(Presence.class), new StanzaTypeFilter(Message.class)); @@ -158,6 +158,37 @@ public class AgentSession { } }; connection.addAsyncStanzaListener(packetListener, filter); + + connection.registerIQRequestHandler(new AbstractIqRequestHandler( + OfferRequestProvider.OfferRequestPacket.ELEMENT, + OfferRequestProvider.OfferRequestPacket.NAMESPACE, IQ.Type.set, + Mode.async) { + + @Override + public IQ handleIQRequest(IQ iqRequest) { + // Acknowledge the IQ set. + IQ reply = IQ.createResultIQ(iqRequest); + + fireOfferRequestEvent((OfferRequestProvider.OfferRequestPacket) iqRequest); + return reply; + } + }); + + connection.registerIQRequestHandler(new AbstractIqRequestHandler( + OfferRevokeProvider.OfferRevokePacket.ELEMENT, + OfferRevokeProvider.OfferRevokePacket.NAMESPACE, IQ.Type.set, + Mode.async) { + + @Override + public IQ handleIQRequest(IQ iqRequest) { + // Acknowledge the IQ set. + IQ reply = IQ.createResultIQ(iqRequest); + + fireOfferRevokeEvent((OfferRevokeProvider.OfferRevokePacket) iqRequest); + return reply; + } + }); + // Create the agent associated to this session agent = new Agent(connection, workgroupJID); } @@ -689,15 +720,8 @@ public class AgentSession { // PacketListener Implementation. - private void handlePacket(Stanza packet) throws NotConnectedException { - if (packet instanceof OfferRequestProvider.OfferRequestPacket) { - // Acknowledge the IQ set. - IQ reply = IQ.createResultIQ((IQ) packet); - connection.sendStanza(reply); - - fireOfferRequestEvent((OfferRequestProvider.OfferRequestPacket)packet); - } - else if (packet instanceof Presence) { + private void handlePacket(Stanza packet) { + if (packet instanceof Presence) { Presence presence = (Presence)packet; // The workgroup can send us a number of different presence packets. We @@ -779,13 +803,6 @@ public class AgentSession { message.getFrom(), metaData); } } - else if (packet instanceof OfferRevokeProvider.OfferRevokePacket) { - // Acknowledge the IQ set. - IQ reply = IQ.createResultIQ((OfferRevokeProvider.OfferRevokePacket) packet); - connection.sendStanza(reply); - - fireOfferRevokeEvent((OfferRevokeProvider.OfferRevokePacket)packet); - } } /** diff --git a/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/packet/OfferRequestProvider.java b/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/packet/OfferRequestProvider.java index f3c166fca..c72af17c5 100644 --- a/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/packet/OfferRequestProvider.java +++ b/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/packet/OfferRequestProvider.java @@ -112,6 +112,9 @@ public class OfferRequestProvider extends IQProvider { public static class OfferRequestPacket extends IQ { + public static final String ELEMENT = "offer"; + public static final String NAMESPACE = "http://jabber.org/protocol/workgroup"; + private int timeout; private String userID; private String userJID; @@ -122,7 +125,7 @@ public class OfferRequestProvider extends IQProvider { public OfferRequestPacket(String userJID, String userID, int timeout, Map> metaData, String sessionID, OfferContent content) { - super("offer", "http://jabber.org/protocol/workgroup"); + super(ELEMENT, NAMESPACE); this.userJID = userJID; this.userID = userID; this.timeout = timeout; diff --git a/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/packet/OfferRevokeProvider.java b/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/packet/OfferRevokeProvider.java index d384347fb..59442c8d4 100644 --- a/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/packet/OfferRevokeProvider.java +++ b/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/packet/OfferRevokeProvider.java @@ -67,13 +67,16 @@ public class OfferRevokeProvider extends IQProvider { public class OfferRevokePacket extends IQ { + public static final String ELEMENT = "offer-revoke"; + public static final String NAMESPACE = "http://jabber.org/protocol/workgroup"; + private String userJID; private String userID; private String sessionID; private String reason; public OfferRevokePacket (String userJID, String userID, String cause, String sessionID) { - super("offer-revoke", "http://jabber.org/protocol/workgroup"); + super(ELEMENT, NAMESPACE); this.userJID = userJID; this.userID = userID; this.reason = cause; From 7a3ca4fd68e51d72c9cb5370e61a5c6ea8ea1a4f Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Wed, 27 May 2015 22:44:27 +0200 Subject: [PATCH 2/2] Fix memory leak caused by RosterGroup SMACK-672 --- .../smack/roster/RosterGroup.java | 21 ++++++------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/smack-im/src/main/java/org/jivesoftware/smack/roster/RosterGroup.java b/smack-im/src/main/java/org/jivesoftware/smack/roster/RosterGroup.java index f80f472d9..db2edf801 100644 --- a/smack-im/src/main/java/org/jivesoftware/smack/roster/RosterGroup.java +++ b/smack-im/src/main/java/org/jivesoftware/smack/roster/RosterGroup.java @@ -23,7 +23,7 @@ import java.util.List; import java.util.Locale; import java.util.Set; -import org.jivesoftware.smack.PacketCollector; +import org.jivesoftware.smack.Manager; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.SmackException.NoResponseException; import org.jivesoftware.smack.SmackException.NotConnectedException; @@ -38,10 +38,9 @@ import org.jxmpp.util.XmppStringUtils; * @see Roster#getGroup(String) * @author Matt Tucker */ -public class RosterGroup { +public class RosterGroup extends Manager { private final String name; - private final XMPPConnection connection; private final Set entries; /** @@ -51,8 +50,8 @@ public class RosterGroup { * @param connection the connection the group belongs to. */ RosterGroup(String name, XMPPConnection connection) { + super(connection); this.name = name; - this.connection = connection; entries = new LinkedHashSet(); } @@ -85,7 +84,7 @@ public class RosterGroup { item.removeGroupName(this.name); item.addGroupName(name); packet.addRosterItem(item); - connection.createPacketCollectorAndSend(packet).nextResultOrThrow(); + connection().createPacketCollectorAndSend(packet).nextResultOrThrow(); } } } @@ -171,7 +170,6 @@ public class RosterGroup { * @throws NotConnectedException */ public void addEntry(RosterEntry entry) throws NoResponseException, XMPPErrorException, NotConnectedException { - PacketCollector collector = null; // Only add the entry if it isn't already in the list. synchronized (entries) { if (!entries.contains(entry)) { @@ -181,12 +179,9 @@ public class RosterGroup { item.addGroupName(getName()); packet.addRosterItem(item); // Wait up to a certain number of seconds for a reply from the server. - collector = connection.createPacketCollectorAndSend(packet); + connection().createPacketCollectorAndSend(packet).nextResultOrThrow(); } } - if (collector != null) { - collector.nextResultOrThrow(); - } } /** @@ -202,7 +197,6 @@ public class RosterGroup { * @throws NotConnectedException */ public void removeEntry(RosterEntry entry) throws NoResponseException, XMPPErrorException, NotConnectedException { - PacketCollector collector = null; // Only remove the entry if it's in the entry list. // Remove the entry locally, if we wait for RosterPacketListenerprocess>>Packet(Packet) // to take place the entry will exist in the group until a packet is received from the @@ -215,12 +209,9 @@ public class RosterGroup { item.removeGroupName(this.getName()); packet.addRosterItem(item); // Wait up to a certain number of seconds for a reply from the server. - collector = connection.createPacketCollectorAndSend(packet); + connection().createPacketCollectorAndSend(packet).nextResultOrThrow(); } } - if (collector != null) { - collector.nextResultOrThrow(); - } } void addEntryLocal(RosterEntry entry) {