mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-09-09 17:19:39 +02:00
Introduce XMPPConnection.add(Message|Presence)Interceptor
add deprecate addStanzaInterceptor().
This commit is contained in:
parent
5db6191110
commit
e2d206e741
24 changed files with 419 additions and 102 deletions
|
@ -120,18 +120,10 @@ public final class ChatMarkersManager extends Manager {
|
|||
|
||||
chatManager = ChatManager.getInstanceFor(connection);
|
||||
|
||||
connection.addStanzaInterceptor(new StanzaListener() {
|
||||
@Override
|
||||
public void processStanza(Stanza packet)
|
||||
throws
|
||||
NotConnectedException,
|
||||
InterruptedException,
|
||||
SmackException.NotLoggedInException {
|
||||
Message message = (Message) packet;
|
||||
// add a markable extension
|
||||
message.addExtension(ChatMarkersElements.MarkableExtension.INSTANCE);
|
||||
}
|
||||
}, OUTGOING_MESSAGE_FILTER);
|
||||
connection.addMessageInterceptor(mb -> mb.addExtension(ChatMarkersElements.MarkableExtension.INSTANCE),
|
||||
m -> {
|
||||
return OUTGOING_MESSAGE_FILTER.accept(m);
|
||||
});
|
||||
|
||||
connection.addSyncStanzaListener(new StanzaListener() {
|
||||
@Override
|
||||
|
|
|
@ -21,7 +21,6 @@ import java.util.WeakHashMap;
|
|||
|
||||
import org.jivesoftware.smack.ConnectionCreationListener;
|
||||
import org.jivesoftware.smack.Manager;
|
||||
import org.jivesoftware.smack.StanzaListener;
|
||||
import org.jivesoftware.smack.XMPPConnection;
|
||||
import org.jivesoftware.smack.XMPPConnectionRegistry;
|
||||
import org.jivesoftware.smack.filter.AndFilter;
|
||||
|
@ -31,8 +30,9 @@ import org.jivesoftware.smack.filter.StanzaExtensionFilter;
|
|||
import org.jivesoftware.smack.filter.StanzaFilter;
|
||||
import org.jivesoftware.smack.filter.ToTypeFilter;
|
||||
import org.jivesoftware.smack.packet.Message;
|
||||
import org.jivesoftware.smack.packet.Stanza;
|
||||
|
||||
import org.jivesoftware.smack.packet.MessageBuilder;
|
||||
import org.jivesoftware.smack.util.Consumer;
|
||||
import org.jivesoftware.smack.util.Predicate;
|
||||
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
||||
import org.jivesoftware.smackx.sid.element.OriginIdElement;
|
||||
|
||||
|
@ -62,12 +62,12 @@ public final class StableUniqueStanzaIdManager extends Manager {
|
|||
private static final StanzaFilter ORIGIN_ID_FILTER = new StanzaExtensionFilter(OriginIdElement.ELEMENT, NAMESPACE);
|
||||
|
||||
// Listener for outgoing stanzas that adds origin-ids to outgoing stanzas.
|
||||
private static final StanzaListener ADD_ORIGIN_ID_INTERCEPTOR = new StanzaListener() {
|
||||
@Override
|
||||
public void processStanza(Stanza stanza) {
|
||||
Message message = (Message) stanza;
|
||||
OriginIdElement.addOriginId(message);
|
||||
}
|
||||
private static final Consumer<MessageBuilder> ADD_ORIGIN_ID_INTERCEPTOR = mb -> OriginIdElement.addOriginId(mb);
|
||||
|
||||
// We need a filter for outgoing messages that do not carry an origin-id already.
|
||||
private static final StanzaFilter ADD_ORIGIN_ID_FILTER = new AndFilter(OUTGOING_FILTER, new NotFilter(ORIGIN_ID_FILTER));
|
||||
private static final Predicate<Message> ADD_ORIGIN_ID_PREDICATE = m -> {
|
||||
return ADD_ORIGIN_ID_FILTER.accept(m);
|
||||
};
|
||||
|
||||
static {
|
||||
|
@ -112,10 +112,8 @@ public final class StableUniqueStanzaIdManager extends Manager {
|
|||
* Start appending origin-id elements to outgoing stanzas and add the feature to disco.
|
||||
*/
|
||||
public synchronized void enable() {
|
||||
connection().addMessageInterceptor(ADD_ORIGIN_ID_INTERCEPTOR, ADD_ORIGIN_ID_PREDICATE);
|
||||
ServiceDiscoveryManager.getInstanceFor(connection()).addFeature(NAMESPACE);
|
||||
// We need a filter for outgoing messages that do not carry an origin-id already
|
||||
StanzaFilter filter = new AndFilter(OUTGOING_FILTER, new NotFilter(ORIGIN_ID_FILTER));
|
||||
connection().addStanzaInterceptor(ADD_ORIGIN_ID_INTERCEPTOR, filter);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -123,7 +121,7 @@ public final class StableUniqueStanzaIdManager extends Manager {
|
|||
*/
|
||||
public synchronized void disable() {
|
||||
ServiceDiscoveryManager.getInstanceFor(connection()).removeFeature(NAMESPACE);
|
||||
connection().removeStanzaInterceptor(ADD_ORIGIN_ID_INTERCEPTOR);
|
||||
connection().removeMessageInterceptor(ADD_ORIGIN_ID_INTERCEPTOR);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
package org.jivesoftware.smackx.sid.element;
|
||||
|
||||
import org.jivesoftware.smack.packet.Message;
|
||||
import org.jivesoftware.smack.packet.MessageBuilder;
|
||||
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||
|
||||
import org.jivesoftware.smackx.sid.StableUniqueStanzaIdManager;
|
||||
|
@ -38,7 +39,10 @@ public class OriginIdElement extends StableAndUniqueIdElement {
|
|||
*
|
||||
* @param message message.
|
||||
* @return the added origin-id element.
|
||||
* @deprecated use {@link #addOriginId(MessageBuilder)} instead.
|
||||
*/
|
||||
@Deprecated
|
||||
// TODO: Remove in Smack 4.5.
|
||||
public static OriginIdElement addOriginId(Message message) {
|
||||
OriginIdElement originId = new OriginIdElement();
|
||||
message.addExtension(originId);
|
||||
|
@ -47,6 +51,20 @@ public class OriginIdElement extends StableAndUniqueIdElement {
|
|||
return originId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an origin-id element to a message and set the stanzas id to the same id as in the origin-id element.
|
||||
*
|
||||
* @param messageBuilder the message builder to add an origin ID to.
|
||||
* @return the added origin-id element.
|
||||
*/
|
||||
public static OriginIdElement addOriginId(MessageBuilder messageBuilder) {
|
||||
OriginIdElement originId = new OriginIdElement();
|
||||
messageBuilder.addExtension(originId);
|
||||
// TODO: Find solution to have both the originIds stanzaId and a nice to look at incremental stanzaID.
|
||||
// message.setStanzaId(originId.getId());
|
||||
return originId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true, if the message contains a origin-id element.
|
||||
*
|
||||
|
|
|
@ -23,6 +23,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.jivesoftware.smack.packet.Message;
|
||||
import org.jivesoftware.smack.packet.MessageBuilder;
|
||||
import org.jivesoftware.smack.packet.StanzaBuilder;
|
||||
import org.jivesoftware.smack.test.util.SmackTestSuite;
|
||||
import org.jivesoftware.smack.test.util.TestUtils;
|
||||
|
@ -71,12 +72,15 @@ public class StableUniqueStanzaIdTest extends SmackTestSuite {
|
|||
|
||||
@Test
|
||||
public void fromMessageTest() {
|
||||
Message message = StanzaBuilder.buildMessage().build();
|
||||
MessageBuilder messageBuilder = StanzaBuilder.buildMessage();
|
||||
|
||||
Message message = messageBuilder.build();
|
||||
assertFalse(OriginIdElement.hasOriginId(message));
|
||||
assertFalse(StanzaIdElement.hasStanzaId(message));
|
||||
|
||||
OriginIdElement.addOriginId(message);
|
||||
OriginIdElement.addOriginId(messageBuilder);
|
||||
|
||||
message = messageBuilder.build();
|
||||
assertTrue(OriginIdElement.hasOriginId(message));
|
||||
|
||||
StanzaIdElement stanzaId = new StanzaIdElement("alice@wonderland.lit");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue