1 20 21 package org.jivesoftware.smackx; 22 23 import org.jivesoftware.smack.PacketCollector; 24 import org.jivesoftware.smack.SmackConfiguration; 25 import org.jivesoftware.smack.XMPPConnection; 26 import org.jivesoftware.smack.XMPPException; 27 import org.jivesoftware.smack.filter.*; 28 import org.jivesoftware.smack.packet.IQ; 29 import org.jivesoftware.smack.packet.Message; 30 import org.jivesoftware.smack.packet.Packet; 31 import org.jivesoftware.smackx.packet.DiscoverInfo; 32 import org.jivesoftware.smackx.packet.DiscoverItems; 33 import org.jivesoftware.smackx.packet.OfflineMessageInfo; 34 import org.jivesoftware.smackx.packet.OfflineMessageRequest; 35 36 import java.util.ArrayList ; 37 import java.util.Iterator ; 38 import java.util.List ; 39 40 56 public class OfflineMessageManager { 57 58 private final static String namespace = "http://jabber.org/protocol/offline"; 59 60 private XMPPConnection connection; 61 62 private PacketFilter packetFilter; 63 64 public OfflineMessageManager(XMPPConnection connection) { 65 this.connection = connection; 66 packetFilter = 67 new AndFilter(new PacketExtensionFilter("offline", namespace), 68 new PacketTypeFilter(Message.class)); 69 } 70 71 79 public boolean supportsFlexibleRetrieval() throws XMPPException { 80 DiscoverInfo info = ServiceDiscoveryManager.getInstanceFor(connection).discoverInfo(null); 81 return info.containsFeature(namespace); 82 } 83 84 91 public int getMessageCount() throws XMPPException { 92 DiscoverInfo info = ServiceDiscoveryManager.getInstanceFor(connection).discoverInfo(null, 93 namespace); 94 Form extendedInfo = Form.getFormFrom(info); 95 if (extendedInfo != null) { 96 String value = (String ) extendedInfo.getField("number_of_messages").getValues().next(); 97 return Integer.parseInt(value); 98 } 99 return 0; 100 } 101 102 112 public Iterator getHeaders() throws XMPPException { 113 List answer = new ArrayList (); 114 DiscoverItems items = ServiceDiscoveryManager.getInstanceFor(connection).discoverItems( 115 null, namespace); 116 for (Iterator it = items.getItems(); it.hasNext();) { 117 DiscoverItems.Item item = (DiscoverItems.Item) it.next(); 118 answer.add(new OfflineMessageHeader(item)); 119 } 120 return answer.iterator(); 121 } 122 123 135 public Iterator getMessages(final List nodes) throws XMPPException { 136 List messages = new ArrayList (); 137 OfflineMessageRequest request = new OfflineMessageRequest(); 138 for (Iterator it = nodes.iterator(); it.hasNext();) { 139 OfflineMessageRequest.Item item = new OfflineMessageRequest.Item((String ) it.next()); 140 item.setAction("view"); 141 request.addItem(item); 142 } 143 PacketFilter responseFilter = new PacketIDFilter(request.getPacketID()); 145 PacketCollector response = connection.createPacketCollector(responseFilter); 146 PacketFilter messageFilter = new AndFilter(packetFilter, new PacketFilter() { 148 public boolean accept(Packet packet) { 149 OfflineMessageInfo info = (OfflineMessageInfo) packet.getExtension("offline", 150 namespace); 151 return nodes.contains(info.getNode()); 152 } 153 }); 154 PacketCollector messageCollector = connection.createPacketCollector(messageFilter); 155 connection.sendPacket(request); 157 IQ answer = (IQ) response.nextResult(SmackConfiguration.getPacketReplyTimeout()); 159 response.cancel(); 161 162 if (answer == null) { 163 throw new XMPPException("No response from server."); 164 } else if (answer.getError() != null) { 165 throw new XMPPException(answer.getError()); 166 } 167 168 Message message = (Message) messageCollector.nextResult( 170 SmackConfiguration.getPacketReplyTimeout()); 171 while (message != null) { 172 messages.add(message); 173 message = 174 (Message) messageCollector.nextResult( 175 SmackConfiguration.getPacketReplyTimeout()); 176 } 177 messageCollector.cancel(); 179 return messages.iterator(); 180 } 181 182 191 public Iterator getMessages() throws XMPPException { 192 List messages = new ArrayList (); 193 OfflineMessageRequest request = new OfflineMessageRequest(); 194 request.setFetch(true); 195 PacketFilter responseFilter = new PacketIDFilter(request.getPacketID()); 197 PacketCollector response = connection.createPacketCollector(responseFilter); 198 PacketCollector messageCollector = connection.createPacketCollector(packetFilter); 200 connection.sendPacket(request); 202 IQ answer = (IQ) response.nextResult(SmackConfiguration.getPacketReplyTimeout()); 204 response.cancel(); 206 207 if (answer == null) { 208 throw new XMPPException("No response from server."); 209 } else if (answer.getError() != null) { 210 throw new XMPPException(answer.getError()); 211 } 212 213 Message message = (Message) messageCollector.nextResult( 215 SmackConfiguration.getPacketReplyTimeout()); 216 while (message != null) { 217 messages.add(message); 218 message = 219 (Message) messageCollector.nextResult( 220 SmackConfiguration.getPacketReplyTimeout()); 221 } 222 messageCollector.cancel(); 224 return messages.iterator(); 225 } 226 227 235 public void deleteMessages(List nodes) throws XMPPException { 236 OfflineMessageRequest request = new OfflineMessageRequest(); 237 for (Iterator it = nodes.iterator(); it.hasNext();) { 238 OfflineMessageRequest.Item item = new OfflineMessageRequest.Item((String ) it.next()); 239 item.setAction("remove"); 240 request.addItem(item); 241 } 242 PacketFilter responseFilter = new PacketIDFilter(request.getPacketID()); 244 PacketCollector response = connection.createPacketCollector(responseFilter); 245 connection.sendPacket(request); 247 IQ answer = (IQ) response.nextResult(SmackConfiguration.getPacketReplyTimeout()); 249 response.cancel(); 251 252 if (answer == null) { 253 throw new XMPPException("No response from server."); 254 } else if (answer.getError() != null) { 255 throw new XMPPException(answer.getError()); 256 } 257 } 258 259 265 public void deleteMessages() throws XMPPException { 266 OfflineMessageRequest request = new OfflineMessageRequest(); 267 request.setPurge(true); 268 PacketFilter responseFilter = new PacketIDFilter(request.getPacketID()); 270 PacketCollector response = connection.createPacketCollector(responseFilter); 271 connection.sendPacket(request); 273 IQ answer = (IQ) response.nextResult(SmackConfiguration.getPacketReplyTimeout()); 275 response.cancel(); 277 278 if (answer == null) { 279 throw new XMPPException("No response from server."); 280 } else if (answer.getError() != null) { 281 throw new XMPPException(answer.getError()); 282 } 283 } 284 } 285 | Popular Tags |