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;
    }
@Test
    public void testUseOriginIdElement() throws Exception
    {
        // Setup fixture.
        final Packet input = new Message();
        final JID self = new JID( "foobar" );
        final String expected = "de305d54-75b4-431b-adb2-eb6b9e546013";
        final Element toOverwrite = input.getElement().addElement( "origin-id", "urn:xmpp:sid:0" );
        toOverwrite.addAttribute( "id", expected );

        // Execute system under test.
        final Packet result = StanzaIDUtil.ensureUniqueAndStableStanzaID( input, self );

        // Verify results.
        assertNotNull( result );
        final Element stanzaIDElement = result.getElement().element( QName.get( "stanza-id", "urn:xmpp:sid:0" ) );
        assertNotNull( stanzaIDElement );
        assertDoesNotThrow(() -> UUID.fromString( stanzaIDElement.attributeValue( "id" ) ));
        assertNotEquals( expected, stanzaIDElement.attributeValue( "id" ) );
        assertEquals( self.toString(), stanzaIDElement.attributeValue( "by" ) );
    }