001/**
002 *
003 * Copyright 2019 Paul Schaub
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.message_fastening;
018
019import java.util.List;
020import java.util.WeakHashMap;
021
022import org.jivesoftware.smack.ConnectionCreationListener;
023import org.jivesoftware.smack.Manager;
024import org.jivesoftware.smack.XMPPConnection;
025import org.jivesoftware.smack.XMPPConnectionRegistry;
026import org.jivesoftware.smack.packet.MessageBuilder;
027import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
028import org.jivesoftware.smackx.message_fastening.element.FasteningElement;
029
030/**
031 * Smacks API for XEP-0422: Message Fastening.
032 * The API is still very bare bones, as the XEP intends Message Fastening to be used as a tool by other protocols.
033 *
034 * To enable / disable auto-announcing support for this feature, call {@link #setEnabledByDefault(boolean)} (default true).
035 *
036 * To fasten a payload to a previous message, create an {@link FasteningElement} using the builder provided by
037 * {@link FasteningElement#builder()}.
038 *
039 * You need to provide the {@link org.jivesoftware.smackx.sid.element.OriginIdElement} of the message you want to reference.
040 * Then add wrapped payloads using {@link FasteningElement.Builder#addWrappedPayloads(List)}
041 * and external payloads using {@link FasteningElement.Builder#addExternalPayloads(List)}.
042 *
043 * If you fastened some payloads onto the message previously and now want to replace the previous fastening, call
044 * {@link FasteningElement.Builder#isRemovingElement()}.
045 * Once you are finished, build the {@link FasteningElement} using {@link FasteningElement.Builder#build()} and add it to
046 * a stanza by calling {@link FasteningElement#applyTo(MessageBuilder)}.
047 *
048 * @see <a href="https://xmpp.org/extensions/xep-0422.html">XEP-0422: Message Fastening</a>
049 */
050public final class MessageFasteningManager extends Manager {
051
052    public static final String NAMESPACE = "urn:xmpp:fasten:0";
053
054    private static boolean ENABLED_BY_DEFAULT = false;
055
056    private static final WeakHashMap<XMPPConnection, MessageFasteningManager> INSTANCES = new WeakHashMap<>();
057
058    static {
059        XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
060            @Override
061            public void connectionCreated(XMPPConnection connection) {
062                if (ENABLED_BY_DEFAULT) {
063                    MessageFasteningManager.getInstanceFor(connection).announceSupport();
064                }
065            }
066        });
067    }
068
069    private MessageFasteningManager(XMPPConnection connection) {
070        super(connection);
071    }
072
073    public static synchronized MessageFasteningManager getInstanceFor(XMPPConnection connection) {
074        MessageFasteningManager manager = INSTANCES.get(connection);
075        if (manager == null) {
076            manager = new MessageFasteningManager(connection);
077            INSTANCES.put(connection, manager);
078        }
079        return manager;
080    }
081
082    /**
083     * Enable or disable auto-announcing support for Message Fastening.
084     * Default is enabled.
085     *
086     * @param enabled enabled
087     */
088    public static synchronized void setEnabledByDefault(boolean enabled) {
089        ENABLED_BY_DEFAULT = enabled;
090    }
091
092    /**
093     * Announce support for Message Fastening via Service Discovery.
094     */
095    public void announceSupport() {
096        ServiceDiscoveryManager discoveryManager = ServiceDiscoveryManager.getInstanceFor(connection());
097        discoveryManager.addFeature(NAMESPACE);
098    }
099
100    /**
101     * Stop announcing support for Message Fastening.
102     */
103    public void stopAnnouncingSupport() {
104        ServiceDiscoveryManager discoveryManager = ServiceDiscoveryManager.getInstanceFor(connection());
105        discoveryManager.removeFeature(NAMESPACE);
106    }
107}