2018-02-21 20:28:26 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Copyright 2018 Paul Schaub
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
package org.jivesoftware.smackx.sid;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.WeakHashMap;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.ConnectionCreationListener;
|
|
|
|
import org.jivesoftware.smack.Manager;
|
|
|
|
import org.jivesoftware.smack.XMPPConnection;
|
|
|
|
import org.jivesoftware.smack.XMPPConnectionRegistry;
|
|
|
|
import org.jivesoftware.smack.filter.AndFilter;
|
|
|
|
import org.jivesoftware.smack.filter.MessageTypeFilter;
|
|
|
|
import org.jivesoftware.smack.filter.NotFilter;
|
2019-08-31 19:36:10 +02:00
|
|
|
import org.jivesoftware.smack.filter.StanzaExtensionFilter;
|
2018-02-21 20:28:26 +01:00
|
|
|
import org.jivesoftware.smack.filter.StanzaFilter;
|
|
|
|
import org.jivesoftware.smack.filter.ToTypeFilter;
|
|
|
|
import org.jivesoftware.smack.packet.Message;
|
2019-10-25 13:57:18 +02:00
|
|
|
import org.jivesoftware.smack.packet.MessageBuilder;
|
|
|
|
import org.jivesoftware.smack.util.Consumer;
|
|
|
|
import org.jivesoftware.smack.util.Predicate;
|
2019-10-29 21:12:22 +01:00
|
|
|
|
2018-02-21 20:28:26 +01:00
|
|
|
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
|
|
|
import org.jivesoftware.smackx.sid.element.OriginIdElement;
|
|
|
|
|
2019-08-31 19:36:17 +02:00
|
|
|
/**
|
|
|
|
* Manager class for Stable and Unique Stanza IDs.
|
|
|
|
*
|
|
|
|
* In order to start automatically appending origin ids to outgoing messages, use {@link #enable()}.
|
|
|
|
* This will announce support via the {@link ServiceDiscoveryManager}. If you want to stop appending origin-ids
|
|
|
|
* and de-announce support, call {@link #disable()}.
|
|
|
|
*
|
|
|
|
* @see <a href="https://xmpp.org/extensions/xep-0359.html">XEP-0359: Stable and Unique Stanza IDs</a>
|
|
|
|
*/
|
2018-02-21 20:28:26 +01:00
|
|
|
public final class StableUniqueStanzaIdManager extends Manager {
|
|
|
|
|
|
|
|
public static final String NAMESPACE = "urn:xmpp:sid:0";
|
|
|
|
|
|
|
|
private static final Map<XMPPConnection, StableUniqueStanzaIdManager> INSTANCES = new WeakHashMap<>();
|
|
|
|
|
2019-09-02 17:39:27 +02:00
|
|
|
private static boolean enabledByDefault = true;
|
|
|
|
|
2018-02-21 20:28:26 +01:00
|
|
|
// Filter for outgoing stanzas.
|
|
|
|
private static final StanzaFilter OUTGOING_FILTER = new AndFilter(
|
|
|
|
MessageTypeFilter.NORMAL_OR_CHAT_OR_HEADLINE,
|
|
|
|
ToTypeFilter.ENTITY_FULL_OR_BARE_JID);
|
|
|
|
|
2019-08-31 19:36:10 +02:00
|
|
|
// Filter that filters for messages with an origin id
|
|
|
|
private static final StanzaFilter ORIGIN_ID_FILTER = new StanzaExtensionFilter(OriginIdElement.ELEMENT, NAMESPACE);
|
|
|
|
|
2018-02-21 20:28:26 +01:00
|
|
|
// Listener for outgoing stanzas that adds origin-ids to outgoing stanzas.
|
2019-10-25 13:57:18 +02:00
|
|
|
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);
|
2018-02-21 20:28:26 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static {
|
|
|
|
XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
|
|
|
|
@Override
|
|
|
|
public void connectionCreated(XMPPConnection connection) {
|
2019-09-02 17:39:27 +02:00
|
|
|
if (enabledByDefault) {
|
|
|
|
getInstanceFor(connection).enable();
|
|
|
|
}
|
2018-02-21 20:28:26 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Private constructor.
|
2019-08-31 19:36:17 +02:00
|
|
|
* @param connection XMPP connection
|
2018-02-21 20:28:26 +01:00
|
|
|
*/
|
|
|
|
private StableUniqueStanzaIdManager(XMPPConnection connection) {
|
|
|
|
super(connection);
|
2019-09-02 17:39:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void setEnabledByDefault(boolean enabled) {
|
|
|
|
enabledByDefault = enabled;
|
2018-02-21 20:28:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an instance of the StableUniqueStanzaIdManager for the given connection.
|
|
|
|
*
|
|
|
|
* @param connection xmpp-connection
|
|
|
|
* @return manager instance for the connection
|
|
|
|
*/
|
2019-04-14 21:41:59 +02:00
|
|
|
public static synchronized StableUniqueStanzaIdManager getInstanceFor(XMPPConnection connection) {
|
2018-02-21 20:28:26 +01:00
|
|
|
StableUniqueStanzaIdManager manager = INSTANCES.get(connection);
|
|
|
|
if (manager == null) {
|
|
|
|
manager = new StableUniqueStanzaIdManager(connection);
|
|
|
|
INSTANCES.put(connection, manager);
|
|
|
|
}
|
|
|
|
return manager;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start appending origin-id elements to outgoing stanzas and add the feature to disco.
|
|
|
|
*/
|
|
|
|
public synchronized void enable() {
|
2019-10-25 13:57:18 +02:00
|
|
|
connection().addMessageInterceptor(ADD_ORIGIN_ID_INTERCEPTOR, ADD_ORIGIN_ID_PREDICATE);
|
2018-02-21 20:28:26 +01:00
|
|
|
ServiceDiscoveryManager.getInstanceFor(connection()).addFeature(NAMESPACE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop appending origin-id elements to outgoing stanzas and remove the feature from disco.
|
|
|
|
*/
|
|
|
|
public synchronized void disable() {
|
|
|
|
ServiceDiscoveryManager.getInstanceFor(connection()).removeFeature(NAMESPACE);
|
2019-10-25 13:57:18 +02:00
|
|
|
connection().removeMessageInterceptor(ADD_ORIGIN_ID_INTERCEPTOR);
|
2018-02-21 20:28:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true, if we automatically append origin-id elements to outgoing stanzas.
|
|
|
|
*
|
|
|
|
* @return true if functionality is enabled, otherwise false.
|
|
|
|
*/
|
|
|
|
public synchronized boolean isEnabled() {
|
|
|
|
ServiceDiscoveryManager disco = ServiceDiscoveryManager.getInstanceFor(connection());
|
|
|
|
return disco.includesFeature(NAMESPACE);
|
|
|
|
}
|
|
|
|
}
|