Ensure that delivery receipts/requests have ID set

Fixes SMACK-708.
This commit is contained in:
Florian Schmaus 2015-12-13 22:41:58 +01:00
parent f34f37a20c
commit 09364e571c
2 changed files with 21 additions and 4 deletions

View File

@ -1,6 +1,6 @@
/**
*
* Copyright 2013-2014 the original author or authors
* Copyright 2013-2015 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -22,6 +22,7 @@ import java.util.Map;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.provider.EmbeddedExtensionProvider;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder;
/**
@ -42,7 +43,7 @@ public class DeliveryReceipt implements ExtensionElement
public DeliveryReceipt(String id)
{
this.id = id;
this.id = StringUtils.requireNotNullOrEmpty(id, "id must not be null");
}
public String getId()

View File

@ -20,6 +20,7 @@ import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.logging.Logger;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
@ -38,6 +39,7 @@ import org.jivesoftware.smack.filter.StanzaTypeFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.roster.Roster;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
/**
@ -73,6 +75,8 @@ public class DeliveryReceiptManager extends Manager {
private static final StanzaFilter MESSAGES_WITH_DELIVERY_RECEIPT = new AndFilter(StanzaTypeFilter.MESSAGE,
new StanzaExtensionFilter(DeliveryReceipt.ELEMENT, DeliveryReceipt.NAMESPACE));
private static final Logger LOGGER = Logger.getLogger(DeliveryReceiptManager.class.getName());
private static Map<XMPPConnection, DeliveryReceiptManager> instances = new WeakHashMap<XMPPConnection, DeliveryReceiptManager>();
static {
@ -159,6 +163,11 @@ public class DeliveryReceiptManager extends Manager {
final Message messageWithReceiptRequest = (Message) packet;
Message ack = receiptMessageFor(messageWithReceiptRequest);
if (ack == null) {
LOGGER.warning("Received message stanza with receipt request from '" + from
+ "' without a stanza ID set. Message: " + messageWithReceiptRequest);
return;
}
connection.sendStanza(ack);
}
}, MESSAGES_WITH_DEVLIERY_RECEIPT_REQUEST);
@ -300,14 +309,21 @@ public class DeliveryReceiptManager extends Manager {
/**
* Create and return a new message including a delivery receipt extension for the given message.
* <p>
* If {@code messageWithReceiptRequest} does not have a Stanza ID set, then {@code null} will be returned.
* </p>
*
* @param messageWithReceiptRequest the given message with a receipt request extension.
* @return a new message with a receipt.
* @return a new message with a receipt or <code>null</code>.
* @since 4.1
*/
public static Message receiptMessageFor(Message messageWithReceiptRequest) {
String stanzaId = messageWithReceiptRequest.getStanzaId();
if (StringUtils.isNullOrEmpty(stanzaId)) {
return null;
}
Message message = new Message(messageWithReceiptRequest.getFrom(), messageWithReceiptRequest.getType());
message.addExtension(new DeliveryReceipt(messageWithReceiptRequest.getStanzaId()));
message.addExtension(new DeliveryReceipt(stanzaId));
return message;
}
}