Add Stanza.setNewStanzaId() and ensureStanzaIdSet()

Also deprecate setStanzaId() since it was not clear if this would
create a new stanza ID or just ensure that one is set.
This commit is contained in:
Florian Schmaus 2019-10-05 23:14:41 +02:00
parent 133ee29150
commit e23babf147
4 changed files with 39 additions and 9 deletions

View File

@ -138,9 +138,43 @@ public abstract class Stanza implements TopLevelStreamElement {
* *
* @return the stanza id. * @return the stanza id.
* @since 4.2 * @since 4.2
* @deprecated use {@link #setNewStanzaId()} instead.
*/ */
@Deprecated
// TODO: Remove in Smack 4.5.
public String setStanzaId() { public String setStanzaId() {
if (!hasStanzaIdSet()) { return ensureStanzaIdSet();
}
/**
* Set a new stanza ID even if there is already one set.
*
* @return the stanza id.
* @since 4.4
*/
public String setNewStanzaId() {
return ensureStanzaIdSet(true);
}
/**
* Ensure a stanza id is set.
*
* @return the stanza id.
* @since 4.4
*/
public String ensureStanzaIdSet() {
return ensureStanzaIdSet(false);
}
/**
* Ensure that a stanza ID is set.
*
* @param forceNew force a new ID even if there is already one set.
* @return the stanza ID.
* @since 4.4
*/
private String ensureStanzaIdSet(boolean forceNew) {
if (forceNew || !hasStanzaIdSet()) {
setStanzaId(StanzaIdUtil.newStanzaId()); setStanzaId(StanzaIdUtil.newStanzaId());
} }
return getStanzaId(); return getStanzaId();

View File

@ -22,7 +22,6 @@ import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Stanza; import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.packet.XmlEnvironment; import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.packet.id.StanzaIdUtil;
import org.jivesoftware.smack.provider.ExtensionElementProvider; import org.jivesoftware.smack.provider.ExtensionElementProvider;
import org.jivesoftware.smack.xml.XmlPullParser; import org.jivesoftware.smack.xml.XmlPullParser;
import org.jivesoftware.smack.xml.XmlPullParserException; import org.jivesoftware.smack.xml.XmlPullParserException;
@ -83,9 +82,7 @@ public class DeliveryReceiptRequest implements ExtensionElement {
* @return the Message ID which will be used as receipt ID * @return the Message ID which will be used as receipt ID
*/ */
public static String addTo(Message message) { public static String addTo(Message message) {
if (message.getStanzaId() == null) { message.ensureStanzaIdSet();
message.setStanzaId(StanzaIdUtil.newStanzaId());
}
message.addExtension(new DeliveryReceiptRequest()); message.addExtension(new DeliveryReceiptRequest());
return message.getStanzaId(); return message.getStanzaId();
} }

View File

@ -1,6 +1,6 @@
/** /**
* *
* Copyright 2014 Florian Schmaus * Copyright 2014-2019 Florian Schmaus
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -27,7 +27,6 @@ import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPConnectionRegistry; import org.jivesoftware.smack.XMPPConnectionRegistry;
import org.jivesoftware.smack.XMPPException.XMPPErrorException; import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.id.StanzaIdUtil;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.vcardtemp.packet.VCard; import org.jivesoftware.smackx.vcardtemp.packet.VCard;
@ -105,7 +104,7 @@ public final class VCardManager extends Manager {
vcard.setType(IQ.Type.set); vcard.setType(IQ.Type.set);
// Also make sure to generate a new stanza id (the given vcard could be a vcard result), in which case we don't // Also make sure to generate a new stanza id (the given vcard could be a vcard result), in which case we don't
// want to use the same stanza id again (although it wouldn't break if we did) // want to use the same stanza id again (although it wouldn't break if we did)
vcard.setStanzaId(StanzaIdUtil.newStanzaId()); vcard.setNewStanzaId();
connection().createStanzaCollectorAndSend(vcard).nextResultOrThrow(); connection().createStanzaCollectorAndSend(vcard).nextResultOrThrow();
} }

View File

@ -67,7 +67,7 @@ public class MamIntegrationTest extends AbstractSmackIntegrationTest {
EntityBareJid userTwo = conTwo.getUser().asEntityBareJid(); EntityBareJid userTwo = conTwo.getUser().asEntityBareJid();
Message message = new Message(userTwo); Message message = new Message(userTwo);
String messageId = message.setStanzaId(); String messageId = message.ensureStanzaIdSet();
final String messageBody = "Test MAM message (" + testRunId + ')'; final String messageBody = "Test MAM message (" + testRunId + ')';
message.setBody(messageBody); message.setBody(messageBody);