From 56bcd548836aabdac9dd8d37748903259972b85a Mon Sep 17 00:00:00 2001 From: Gaston Dombiak Date: Wed, 3 Aug 2005 03:03:20 +0000 Subject: [PATCH] Modified #addEntry and #removeEntry to throw an XMPPException if the server replied with an error. SMACK-69 git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@2519 b35dd754-fafc-0310-a699-88a17e54d16e --- .../org/jivesoftware/smack/RosterGroup.java | 37 ++++++++++++++++++- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/source/org/jivesoftware/smack/RosterGroup.java b/source/org/jivesoftware/smack/RosterGroup.java index 03a625125..4c65b562a 100644 --- a/source/org/jivesoftware/smack/RosterGroup.java +++ b/source/org/jivesoftware/smack/RosterGroup.java @@ -23,6 +23,7 @@ package org.jivesoftware.smack; import org.jivesoftware.smack.packet.RosterPacket; import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.util.StringUtils; +import org.jivesoftware.smack.filter.PacketIDFilter; import java.util.*; @@ -170,8 +171,10 @@ public class RosterGroup { * the unfiled list and will be added to this group. * * @param entry a roster entry. + * @throws XMPPException if an error occured while trying to add the entry to the group. */ - public void addEntry(RosterEntry entry) { + public void addEntry(RosterEntry entry) throws XMPPException { + PacketCollector collector = null; // Only add the entry if it isn't already in the list. synchronized (entries) { if (!entries.contains(entry)) { @@ -179,9 +182,23 @@ public class RosterGroup { RosterPacket packet = new RosterPacket(); packet.setType(IQ.Type.SET); packet.addRosterItem(RosterEntry.toRosterItem(entry)); + // Wait up to a certain number of seconds for a reply from the server. + collector = connection + .createPacketCollector(new PacketIDFilter(packet.getPacketID())); connection.sendPacket(packet); } } + if (collector != null) { + IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); + collector.cancel(); + if (response == null) { + throw new XMPPException("No response from the server."); + } + // If the server replied with an error, throw an exception. + else if (response.getType() == IQ.Type.ERROR) { + throw new XMPPException(response.getError()); + } + } } /** @@ -190,8 +207,10 @@ public class RosterGroup { * entries. * * @param entry a roster entry. + * @throws XMPPException if an error occured while trying to remove the entry from the group. */ - public void removeEntry(RosterEntry entry) { + public void removeEntry(RosterEntry entry) throws XMPPException { + 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 @@ -203,11 +222,25 @@ public class RosterGroup { RosterPacket.Item item = RosterEntry.toRosterItem(entry); item.removeGroupName(this.getName()); packet.addRosterItem(item); + // Wait up to a certain number of seconds for a reply from the server. + collector = connection + .createPacketCollector(new PacketIDFilter(packet.getPacketID())); connection.sendPacket(packet); // Remove the entry locally entries.remove(entry); } } + if (collector != null) { + IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); + collector.cancel(); + if (response == null) { + throw new XMPPException("No response from the server."); + } + // If the server replied with an error, throw an exception. + else if (response.getType() == IQ.Type.ERROR) { + throw new XMPPException(response.getError()); + } + } } void addEntryLocal(RosterEntry entry) {