CryptoMessage v1.3.2版本的 MD5 值为:66c611ce56a11450659dc23697562928

以下内容为反编译后的 PubSubManager.java 源代码,内容仅作参考


package org.jivesoftware.smackx.pubsub;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.namespace.QName;
import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.db.entities.ServiceEntity;
import org.jivesoftware.smack.packet.EmptyResultIQ;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.StanzaError;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
import org.jivesoftware.smackx.disco.packet.DiscoverInfoBuilder;
import org.jivesoftware.smackx.disco.packet.DiscoverItems;
import org.jivesoftware.smackx.pubsub.PubSubException;
import org.jivesoftware.smackx.pubsub.form.ConfigureForm;
import org.jivesoftware.smackx.pubsub.form.FillableConfigureForm;
import org.jivesoftware.smackx.pubsub.packet.PubSub;
import org.jivesoftware.smackx.pubsub.packet.PubSubNamespace;
import org.jivesoftware.smackx.pubsub.util.NodeUtils;
import org.jxmpp.jid.BareJid;
import org.jxmpp.jid.DomainBareJid;
import org.jxmpp.jid.Jid;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException;

public final class PubSubManager extends Manager {
    public static final String AUTO_CREATE_FEATURE = "http://jabber.org/protocol/pubsub#auto-create";
    public static final String PLUS_NOTIFY = "+notify";
    private final Map<String, Node> nodeMap;
    private final BareJid pubSubService;
    private static final Logger LOGGER = Logger.getLogger(PubSubManager.class.getName());
    private static final Map<XMPPConnection, Map<BareJid, PubSubManager>> INSTANCES = new WeakHashMap();

    public static PubSubManager getInstanceFor(XMPPConnection xMPPConnection) {
        DomainBareJid pubSubService;
        if (xMPPConnection.isAuthenticated()) {
            try {
                pubSubService = getPubSubService(xMPPConnection);
            } catch (InterruptedException e) {
                LOGGER.log(Level.FINE, "Interrupted while trying to determine PubSub service", (Throwable) e);
            } catch (SmackException.NoResponseException e2) {
                e = e2;
                LOGGER.log(Level.WARNING, "Could not determine PubSub service", e);
                pubSubService = null;
                if (pubSubService == null) {
                }
                return getInstanceFor(xMPPConnection, pubSubService);
            } catch (SmackException.NotConnectedException e3) {
                e = e3;
                LOGGER.log(Level.WARNING, "Could not determine PubSub service", e);
                pubSubService = null;
                if (pubSubService == null) {
                }
                return getInstanceFor(xMPPConnection, pubSubService);
            } catch (XMPPException.XMPPErrorException e4) {
                e = e4;
                LOGGER.log(Level.WARNING, "Could not determine PubSub service", e);
                pubSubService = null;
                if (pubSubService == null) {
                }
                return getInstanceFor(xMPPConnection, pubSubService);
            }
            if (pubSubService == null) {
                try {
                    pubSubService = JidCreate.domainBareFrom("pubsub." + ((Object) xMPPConnection.getXMPPServiceDomain()));
                } catch (XmppStringprepException e5) {
                    throw new RuntimeException(e5);
                }
            }
            return getInstanceFor(xMPPConnection, pubSubService);
        }
        pubSubService = null;
        if (pubSubService == null) {
        }
        return getInstanceFor(xMPPConnection, pubSubService);
    }

    public static PubSubManager getInstanceFor(XMPPConnection xMPPConnection, BareJid bareJid) {
        Map<BareJid, PubSubManager> map;
        PubSubManager pubSubManager;
        if (bareJid != null && xMPPConnection.isAuthenticated() && xMPPConnection.getUser().asBareJid().equals((CharSequence) bareJid)) {
            bareJid = null;
        }
        Map<XMPPConnection, Map<BareJid, PubSubManager>> map2 = INSTANCES;
        synchronized (map2) {
            map = map2.get(xMPPConnection);
            if (map == null) {
                map = new HashMap<>();
                map2.put(xMPPConnection, map);
            }
        }
        synchronized (map) {
            pubSubManager = map.get(bareJid);
            if (pubSubManager == null) {
                pubSubManager = new PubSubManager(xMPPConnection, bareJid);
                map.put(bareJid, pubSubManager);
            }
        }
        return pubSubManager;
    }

    @Deprecated
    public static PubSubManager getInstance(XMPPConnection xMPPConnection) {
        return getInstanceFor(xMPPConnection);
    }

    @Deprecated
    public static PubSubManager getInstance(XMPPConnection xMPPConnection, BareJid bareJid) {
        return getInstanceFor(xMPPConnection, bareJid);
    }

    PubSubManager(XMPPConnection xMPPConnection, BareJid bareJid) {
        super(xMPPConnection);
        this.nodeMap = new ConcurrentHashMap();
        this.pubSubService = bareJid;
    }

    private void checkIfXmppErrorBecauseOfNotLeafNode(String str, XMPPException.XMPPErrorException xMPPErrorException) throws XMPPException.XMPPErrorException, PubSubException.NotALeafNodeException {
        if (xMPPErrorException.getStanzaError().getCondition() == StanzaError.Condition.feature_not_implemented) {
            throw new PubSubException.NotALeafNodeException(str, this.pubSubService);
        }
        throw xMPPErrorException;
    }

    public LeafNode createNode() throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        LeafNode leafNode = new LeafNode(this, ((NodeExtension) sendPubsubPacket(IQ.Type.set, new NodeExtension(PubSubElementType.CREATE), null).getExtension(new QName(PubSubNamespace.basic.getXmlns(), "create"))).getNode());
        this.nodeMap.put(leafNode.getId(), leafNode);
        return leafNode;
    }

    public LeafNode createNode(String str) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        return (LeafNode) createNode(str, null);
    }

    public Node createNode(String str, FillableConfigureForm fillableConfigureForm) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        PubSub createPubsubPacket = PubSub.createPubsubPacket(this.pubSubService, IQ.Type.set, new NodeExtension(PubSubElementType.CREATE, str));
        boolean z = true;
        if (fillableConfigureForm != null) {
            createPubsubPacket.addExtension(new FormNode(FormNodeType.CONFIGURE, fillableConfigureForm.getDataFormToSubmit()));
            NodeType nodeType = fillableConfigureForm.getNodeType();
            if (nodeType != null && nodeType != NodeType.leaf) {
                z = false;
            }
        }
        sendPubsubPacket(createPubsubPacket);
        Node leafNode = z ? new LeafNode(this, str) : new CollectionNode(this, str);
        this.nodeMap.put(leafNode.getId(), leafNode);
        return leafNode;
    }

    public Node getNode(String str) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, PubSubException.NotAPubSubNodeException {
        StringUtils.requireNotNullNorEmpty(str, "The node ID can not be null or the empty string");
        Node node = this.nodeMap.get(str);
        if (node == null) {
            XMPPConnection connection = connection();
            DiscoverInfo discoverInfo = (DiscoverInfo) connection.createStanzaCollectorAndSend(((DiscoverInfoBuilder) DiscoverInfo.builder(connection).to((Jid) this.pubSubService)).setNode(str).build()).nextResultOrThrow();
            if (discoverInfo.hasIdentity("pubsub", "leaf")) {
                node = new LeafNode(this, str);
            } else if (discoverInfo.hasIdentity("pubsub", "collection")) {
                node = new CollectionNode(this, str);
            } else {
                throw new PubSubException.NotAPubSubNodeException(str, discoverInfo);
            }
            this.nodeMap.put(str, node);
        }
        return node;
    }

    public LeafNode getOrCreateLeafNode(String str) throws SmackException.NoResponseException, SmackException.NotConnectedException, InterruptedException, XMPPException.XMPPErrorException, PubSubException.NotALeafNodeException {
        try {
            return getLeafNode(str);
        } catch (XMPPException.XMPPErrorException e) {
            if (e.getStanzaError().getCondition() == StanzaError.Condition.item_not_found) {
                try {
                    return createNode(str);
                } catch (XMPPException.XMPPErrorException e2) {
                    if (e2.getStanzaError().getCondition() == StanzaError.Condition.conflict) {
                        try {
                            return getLeafNode(str);
                        } catch (PubSubException.NotAPubSubNodeException e3) {
                            throw new IllegalStateException(e3);
                        }
                    }
                    throw e2;
                }
            }
            if (e.getStanzaError().getCondition() == StanzaError.Condition.service_unavailable) {
                LOGGER.warning("The PubSub service " + ((Object) this.pubSubService) + " threw an DiscoInfoNodeAssertionError, trying workaround for Prosody bug #805 (https://prosody.im/issues/issue/805)");
                return getOrCreateLeafNodeProsodyWorkaround(str);
            }
            throw e;
        } catch (PubSubException.NotAPubSubNodeException unused) {
            return createNode(str);
        }
    }

    public LeafNode getLeafNode(String str) throws PubSubException.NotALeafNodeException, SmackException.NoResponseException, SmackException.NotConnectedException, InterruptedException, XMPPException.XMPPErrorException, PubSubException.NotAPubSubNodeException {
        try {
            Node node = getNode(str);
            if (node instanceof LeafNode) {
                return (LeafNode) node;
            }
            throw new PubSubException.NotALeafNodeException(str, this.pubSubService);
        } catch (XMPPException.XMPPErrorException e) {
            if (e.getStanzaError().getCondition() == StanzaError.Condition.service_unavailable) {
                return getLeafNodeProsodyWorkaround(str);
            }
            throw e;
        }
    }

    private LeafNode getLeafNodeProsodyWorkaround(String str) throws SmackException.NoResponseException, SmackException.NotConnectedException, InterruptedException, PubSubException.NotALeafNodeException, XMPPException.XMPPErrorException {
        LeafNode leafNode = new LeafNode(this, str);
        try {
            leafNode.getItems(1);
        } catch (XMPPException.XMPPErrorException e) {
            checkIfXmppErrorBecauseOfNotLeafNode(str, e);
        }
        this.nodeMap.put(str, leafNode);
        return leafNode;
    }

    private LeafNode getOrCreateLeafNodeProsodyWorkaround(String str) throws XMPPException.XMPPErrorException, SmackException.NoResponseException, SmackException.NotConnectedException, InterruptedException, PubSubException.NotALeafNodeException {
        try {
            return createNode(str);
        } catch (XMPPException.XMPPErrorException e) {
            if (e.getStanzaError().getCondition() == StanzaError.Condition.conflict) {
                return getLeafNodeProsodyWorkaround(str);
            }
            throw e;
        }
    }

    public <I extends Item> LeafNode tryToPublishAndPossibleAutoCreate(String str, I i) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, PubSubException.NotALeafNodeException {
        LeafNode leafNode = new LeafNode(this, str);
        try {
            leafNode.publish((LeafNode) i);
        } catch (XMPPException.XMPPErrorException e) {
            checkIfXmppErrorBecauseOfNotLeafNode(str, e);
        }
        this.nodeMap.put(str, leafNode);
        return leafNode;
    }

    public DiscoverItems discoverNodes(String str) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        DiscoverItems discoverItems = new DiscoverItems();
        if (str != null) {
            discoverItems.setNode(str);
        }
        discoverItems.setTo(this.pubSubService);
        return (DiscoverItems) connection().createStanzaCollectorAndSend(discoverItems).nextResultOrThrow();
    }

    public List<Subscription> getSubscriptions() throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        IQ.Type type = IQ.Type.get;
        PubSubElementType pubSubElementType = PubSubElementType.SUBSCRIPTIONS;
        return ((SubscriptionsExtension) sendPubsubPacket(type, new NodeExtension(pubSubElementType), null).getExtensionElement(pubSubElementType.getElementName(), pubSubElementType.getNamespace().getXmlns())).getSubscriptions();
    }

    public List<Affiliation> getAffiliations() throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        IQ.Type type = IQ.Type.get;
        PubSubElementType pubSubElementType = PubSubElementType.AFFILIATIONS;
        return ((AffiliationsExtension) sendPubsubPacket(type, new NodeExtension(pubSubElementType), null).getExtension(pubSubElementType)).getAffiliations();
    }

    public boolean deleteNode(String str) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        boolean z;
        try {
            IQ.Type type = IQ.Type.set;
            PubSubElementType pubSubElementType = PubSubElementType.DELETE;
            sendPubsubPacket(type, new NodeExtension(pubSubElementType, str), pubSubElementType.getNamespace());
            z = true;
        } catch (XMPPException.XMPPErrorException e) {
            if (e.getStanzaError().getCondition() != StanzaError.Condition.item_not_found) {
                throw e;
            }
            z = false;
        }
        this.nodeMap.remove(str);
        return z;
    }

    public ConfigureForm getDefaultConfiguration() throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        IQ.Type type = IQ.Type.get;
        PubSubElementType pubSubElementType = PubSubElementType.DEFAULT;
        return NodeUtils.getFormFromPacket(sendPubsubPacket(type, new NodeExtension(pubSubElementType), pubSubElementType.getNamespace()), pubSubElementType);
    }

    public BareJid getServiceJid() {
        return this.pubSubService;
    }

    public DiscoverInfo getSupportedFeatures() throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        return ServiceDiscoveryManager.getInstanceFor(connection()).discoverInfo(this.pubSubService);
    }

    public boolean supportsAutomaticNodeCreation() throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(this.pubSubService, AUTO_CREATE_FEATURE);
    }

    public boolean canCreateNodesAndPublishItems() throws SmackException.NoResponseException, SmackException.NotConnectedException, InterruptedException, XMPPException.XMPPErrorException {
        try {
            LeafNode createNode = createNode();
            if (createNode == null) {
                return true;
            }
            deleteNode(createNode.getId());
            return true;
        } catch (XMPPException.XMPPErrorException e) {
            if (e.getStanzaError().getCondition() == StanzaError.Condition.forbidden) {
                return false;
            }
            throw e;
        }
    }

    private PubSub sendPubsubPacket(IQ.Type type, ExtensionElement extensionElement, PubSubNamespace pubSubNamespace) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        return sendPubsubPacket(this.pubSubService, type, Collections.singletonList(extensionElement), pubSubNamespace);
    }

    public XMPPConnection getConnection() {
        return connection();
    }

    PubSub sendPubsubPacket(Jid jid, IQ.Type type, List<ExtensionElement> list, PubSubNamespace pubSubNamespace) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        PubSub pubSub = new PubSub(jid, type, pubSubNamespace);
        Iterator<ExtensionElement> it = list.iterator();
        while (it.hasNext()) {
            pubSub.addExtension(it.next());
        }
        return sendPubsubPacket(pubSub);
    }

    public PubSub sendPubsubPacket(PubSub pubSub) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        IQ iq = (IQ) connection().createStanzaCollectorAndSend(pubSub).nextResultOrThrow();
        if (iq instanceof EmptyResultIQ) {
            return null;
        }
        return (PubSub) iq;
    }

    public static DomainBareJid getPubSubService(XMPPConnection xMPPConnection) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        return ServiceDiscoveryManager.getInstanceFor(xMPPConnection).findService("http://jabber.org/protocol/pubsub", true, "pubsub", ServiceEntity.TABLE_NAME);
    }
}