public static Packet ensureUniqueAndStableStanzaID( final Packet packet, final JID self )
    {
        if ( !JiveGlobals.getBooleanProperty( "xmpp.sid.enabled", true ) )
        {
            return packet;
        }

        if ( packet instanceof IQ && !JiveGlobals.getBooleanProperty( "xmpp.sid.iq.enabled", false ) )
        {
            return packet;
        }

        if ( packet instanceof Message && !JiveGlobals.getBooleanProperty( "xmpp.sid.message.enabled", true ) )
        {
            return packet;
        }

        if ( packet instanceof Presence && !JiveGlobals.getBooleanProperty( "xmpp.sid.presence.enabled", false ) )
        {
            return packet;
        }

        final Element parentElement;
        if ( packet instanceof IQ ) {
            parentElement = ((IQ) packet).getChildElement();
        } else {
            parentElement = packet.getElement();
        }

        // The packet likely is an IQ result or error, which can, but are not required to have a child element.
        // To have a consistent behavior for these, we'll not add a stanza-ID here.
        if ( parentElement == null )
        {
            Log.debug( "Unable to find appropriate element. Not adding stanza-id to packet: {}", packet );
            return packet;
        }

        // Stanza ID generating entities, which encounter a <stanza-id/> element where the 'by' attribute matches the 'by'
        // attribute they would otherwise set, MUST delete that element even if they are not adding their own stanza ID.
        final Iterator<Element> existingElementIterator = parentElement.elementIterator( QName.get( "stanza-id", "urn:xmpp:sid:0" ) );
        while (existingElementIterator.hasNext()) {
            final Element element = existingElementIterator.next();

            if (self.toString().equals( element.attributeValue( "by" ) ) ) {
                Log.warn( "Removing a 'stanza-id' element from an inbound stanza, as its 'by' attribute value matches the value that we would set. Offending stanza: {}", packet );
                existingElementIterator.remove();
            }
        }

        final String id = UUID.randomUUID().toString();
        Log.debug( "Using newly generated value '{}' for stanza that has id '{}'.", id, packet.getID() );

        final Element stanzaIdElement = parentElement.addElement( QName.get( "stanza-id", "urn:xmpp:sid:0" ) );
        stanzaIdElement.addAttribute( "id", id );
        stanzaIdElement.addAttribute( "by", self.toString() );

        return packet;
    }
Modifies the stanza that's passed as a packet by adding a Stanza ID on behalf of what is assumed to be a local
entity.

@param packet The inbound packet (cannot be null).
@param self The ID of the 'local' entity that will generate the stanza ID (cannot be null).
@return the updated packet
@see <a href="https://xmpp.org/extensions/xep-0359.html">XEP-0359</a>
xmppserver/src/main/java/org/jivesoftware/openfire/stanzaid/StanzaIDUtil.java