@Test public void testDontOverwriteStanzaIDElement() throws Exception { final Packet input = new Message(); final JID self = new JID( "foobar" ); final String notExpected = "de305d54-75b4-431b-adb2-eb6b9e546013"; final Element toOverwrite = input.getElement().addElement( "stanza-id", "urn:xmpp:sid:0" ); toOverwrite.addAttribute( "by", new JID( "someoneelse" ).toString() ); toOverwrite.addAttribute( "id", notExpected ); final Packet result = StanzaIDUtil.ensureUniqueAndStableStanzaID( input, self ); Assert.assertNotNull( result ); final List<Element> elements = result.getElement().elements( QName.get( "stanza-id", "urn:xmpp:sid:0" ) ); assertEquals( 2, elements.size() ); }
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(); } if ( parentElement == null ) { Log.debug( "Unable to find appropriate element. Not adding stanza-id to packet: {}", packet ); return packet; } 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 = generateUniqueAndStableStanzaID( packet ); final Element stanzaIdElement = parentElement.addElement( QName.get( "stanza-id", "urn:xmpp:sid:0" ) ); stanzaIdElement.addAttribute( "id", id ); stanzaIdElement.addAttribute( "by", self.toString() ); return packet; }
StanzaIDUtil { 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(); } if ( parentElement == null ) { Log.debug( "Unable to find appropriate element. Not adding stanza-id to packet: {}", packet ); return packet; } 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 = generateUniqueAndStableStanzaID( packet ); final Element stanzaIdElement = parentElement.addElement( QName.get( "stanza-id", "urn:xmpp:sid:0" ) ); stanzaIdElement.addAttribute( "id", id ); stanzaIdElement.addAttribute( "by", self.toString() ); return packet; } }
StanzaIDUtil { 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(); } if ( parentElement == null ) { Log.debug( "Unable to find appropriate element. Not adding stanza-id to packet: {}", packet ); return packet; } 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 = generateUniqueAndStableStanzaID( packet ); final Element stanzaIdElement = parentElement.addElement( QName.get( "stanza-id", "urn:xmpp:sid:0" ) ); stanzaIdElement.addAttribute( "id", id ); stanzaIdElement.addAttribute( "by", self.toString() ); return packet; }  }
StanzaIDUtil { 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(); } if ( parentElement == null ) { Log.debug( "Unable to find appropriate element. Not adding stanza-id to packet: {}", packet ); return packet; } 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 = generateUniqueAndStableStanzaID( packet ); final Element stanzaIdElement = parentElement.addElement( QName.get( "stanza-id", "urn:xmpp:sid:0" ) ); stanzaIdElement.addAttribute( "id", id ); stanzaIdElement.addAttribute( "by", self.toString() ); return packet; }  static Packet ensureUniqueAndStableStanzaID( final Packet packet, final JID self ); static String generateUniqueAndStableStanzaID( final Packet packet ); @Deprecated static UUID parseUniqueAndStableStanzaID( final Packet packet, final String by ); static String findFirstUniqueAndStableStanzaID( final Packet packet, final String by ); }
StanzaIDUtil { 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(); } if ( parentElement == null ) { Log.debug( "Unable to find appropriate element. Not adding stanza-id to packet: {}", packet ); return packet; } 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 = generateUniqueAndStableStanzaID( packet ); final Element stanzaIdElement = parentElement.addElement( QName.get( "stanza-id", "urn:xmpp:sid:0" ) ); stanzaIdElement.addAttribute( "id", id ); stanzaIdElement.addAttribute( "by", self.toString() ); return packet; }  static Packet ensureUniqueAndStableStanzaID( final Packet packet, final JID self ); static String generateUniqueAndStableStanzaID( final Packet packet ); @Deprecated static UUID parseUniqueAndStableStanzaID( final Packet packet, final String by ); static String findFirstUniqueAndStableStanzaID( final Packet packet, final String by );  }