思特奇智慧酒店云平台 v0.0.5版本的 MD5 值为:dedb172bb322639d79d7e0e26262fb5c

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


package org.jivesoftware.smackx.caps;

import com.autonavi.base.amap.mapcore.FileUtil;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Queue;
import java.util.TreeSet;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.AbstractConnectionListener;
import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.StanzaListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPConnectionRegistry;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.PresenceTypeFilter;
import org.jivesoftware.smack.filter.StanzaExtensionFilter;
import org.jivesoftware.smack.filter.StanzaFilter;
import org.jivesoftware.smack.filter.StanzaTypeFilter;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.roster.AbstractPresenceEventListener;
import org.jivesoftware.smack.roster.Roster;
import org.jivesoftware.smack.util.stringencoder.Base64;
import org.jivesoftware.smackx.caps.cache.EntityCapsPersistentCache;
import org.jivesoftware.smackx.caps.packet.CapsExtension;
import org.jivesoftware.smackx.disco.AbstractNodeInformationProvider;
import org.jivesoftware.smackx.disco.DiscoInfoLookupShortcutMechanism;
import org.jivesoftware.smackx.disco.EntityCapabilitiesChangedListener;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
import org.jivesoftware.smackx.xdata.FormField;
import org.jivesoftware.smackx.xdata.packet.DataForm;
public final class EntityCapsManager extends Manager {
    public static final String DEFAULT_HASH = "SHA-1";
    public static final String ELEMENT = "c";
    public static final String NAMESPACE = "http://jabber.org/protocol/caps";
    public static EntityCapsPersistentCache persistentCache;
    public CapsVersionAndHash currentCapsVersion;
    public boolean entityCapsEnabled;
    public String entityNode;
    public final Queue<CapsVersionAndHash> lastLocalCapsVersions;
    public volatile Presence presenceSend;
    public final ServiceDiscoveryManager sdm;
    public static final Logger LOGGER = Logger.getLogger(EntityCapsManager.class.getName());
    public static final Map<String, MessageDigest> SUPPORTED_HASHES = new HashMap();
    public static String DEFAULT_ENTITY_NODE = "http://www.igniterealtime.org/projects/smack";
    public static boolean autoEnableEntityCaps = true;
    public static final Map<XMPPConnection, EntityCapsManager> instances = new WeakHashMap();
    public static final StanzaFilter PRESENCES_WITH_CAPS = new AndFilter(new StanzaTypeFilter(Presence.class), new StanzaExtensionFilter("c", "http://jabber.org/protocol/caps"));
    public static final tx3<String, DiscoverInfo> CAPS_CACHE = new tx3<>(1000);
    public static final tx3<xw3, NodeVerHash> JID_TO_NODEVER_CACHE = new tx3<>(10000);

    public static class NodeVerHash {
        public String hash;
        public String node;
        public String nodeVer;
        public String ver;

        public NodeVerHash(String str, CapsVersionAndHash capsVersionAndHash) {
            this(str, capsVersionAndHash.version, capsVersionAndHash.hash);
        }

        public String getHash() {
            return this.hash;
        }

        public String getNode() {
            return this.node;
        }

        public String getNodeVer() {
            return this.nodeVer;
        }

        public String getVer() {
            return this.ver;
        }

        public NodeVerHash(String str, String str2, String str3) {
            this.node = str;
            this.ver = str2;
            this.hash = str3;
            this.nodeVer = str + "#" + str2;
        }
    }

    static {
        XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
            @Override
            public void connectionCreated(XMPPConnection xMPPConnection) {
                EntityCapsManager.getInstanceFor(xMPPConnection);
            }
        });
        try {
            SUPPORTED_HASHES.put("SHA-1", MessageDigest.getInstance("SHA-1"));
        } catch (NoSuchAlgorithmException unused) {
        }
        ServiceDiscoveryManager.addDiscoInfoLookupShortcutMechanism(new DiscoInfoLookupShortcutMechanism("XEP-0115: Entity Capabilities", 100) {
            @Override
            public DiscoverInfo getDiscoverInfoByUser(ServiceDiscoveryManager serviceDiscoveryManager, xw3 xw3Var) {
                DiscoverInfo discoverInfoByUser = EntityCapsManager.getDiscoverInfoByUser(xw3Var);
                if (discoverInfoByUser != null) {
                    return discoverInfoByUser;
                }
                NodeVerHash nodeVerHashByJid = EntityCapsManager.getNodeVerHashByJid(xw3Var);
                if (nodeVerHashByJid == null) {
                    return null;
                }
                try {
                    DiscoverInfo discoverInfo = serviceDiscoveryManager.discoverInfo(xw3Var, nodeVerHashByJid.getNodeVer());
                    if (EntityCapsManager.verifyDiscoverInfoVersion(nodeVerHashByJid.getVer(), nodeVerHashByJid.getHash(), discoverInfo)) {
                        EntityCapsManager.addDiscoverInfoByNode(nodeVerHashByJid.getNodeVer(), discoverInfo);
                    }
                    return discoverInfo;
                } catch (InterruptedException | SmackException.NoResponseException | SmackException.NotConnectedException | XMPPException.XMPPErrorException unused2) {
                    return null;
                }
            }
        });
    }

    public EntityCapsManager(XMPPConnection xMPPConnection) {
        super(xMPPConnection);
        this.lastLocalCapsVersions = new ConcurrentLinkedQueue();
        this.entityNode = DEFAULT_ENTITY_NODE;
        this.sdm = ServiceDiscoveryManager.getInstanceFor(xMPPConnection);
        instances.put(xMPPConnection, this);
        xMPPConnection.addConnectionListener(new AbstractConnectionListener() {
            private void processCapsStreamFeatureIfAvailable(XMPPConnection xMPPConnection2) {
                CapsExtension capsExtension = (CapsExtension) xMPPConnection2.getFeature("c", "http://jabber.org/protocol/caps");
                if (capsExtension == null) {
                    return;
                }
                EntityCapsManager.addCapsExtensionInfo(xMPPConnection2.getXMPPServiceDomain(), capsExtension);
            }

            @Override
            public void authenticated(XMPPConnection xMPPConnection2, boolean z) {
                processCapsStreamFeatureIfAvailable(xMPPConnection2);
                if (z) {
                    return;
                }
                EntityCapsManager.this.presenceSend = null;
            }

            @Override
            public void connected(XMPPConnection xMPPConnection2) {
                processCapsStreamFeatureIfAvailable(xMPPConnection2);
            }
        });
        updateLocalEntityCaps();
        if (autoEnableEntityCaps) {
            enableEntityCaps();
        }
        xMPPConnection.addAsyncStanzaListener(new StanzaListener() {
            @Override
            public void processStanza(Stanza stanza) {
                if (EntityCapsManager.this.entityCapsEnabled()) {
                    EntityCapsManager.addCapsExtensionInfo(stanza.getFrom(), CapsExtension.from(stanza));
                }
            }
        }, PRESENCES_WITH_CAPS);
        Roster.getInstanceFor(xMPPConnection).addPresenceEventListener(new AbstractPresenceEventListener() {
            @Override
            public void presenceUnavailable(ww3 ww3Var, Presence presence) {
                EntityCapsManager.JID_TO_NODEVER_CACHE.remove(ww3Var);
            }
        });
        xMPPConnection.addStanzaSendingListener(new StanzaListener() {
            @Override
            public void processStanza(Stanza stanza) {
                EntityCapsManager.this.presenceSend = (Presence) stanza;
            }
        }, PresenceTypeFilter.OUTGOING_PRESENCE_BROADCAST);
        xMPPConnection.addStanzaInterceptor(new StanzaListener() {
            @Override
            public void processStanza(Stanza stanza) {
                if (!EntityCapsManager.this.entityCapsEnabled) {
                    stanza.removeExtension("c", "http://jabber.org/protocol/caps");
                    return;
                }
                CapsVersionAndHash capsVersionAndHash = EntityCapsManager.this.getCapsVersionAndHash();
                stanza.overrideExtension(new CapsExtension(EntityCapsManager.this.entityNode, capsVersionAndHash.version, capsVersionAndHash.hash));
            }
        }, PresenceTypeFilter.AVAILABLE);
        this.sdm.addEntityCapabilitiesChangedListener(new EntityCapabilitiesChangedListener() {
            @Override
            public void onEntityCapailitiesChanged() {
                if (EntityCapsManager.this.entityCapsEnabled()) {
                    EntityCapsManager.this.updateLocalEntityCaps();
                }
            }
        });
    }

    public static void addCapsExtensionInfo(xw3 xw3Var, CapsExtension capsExtension) {
        String hash = capsExtension.getHash();
        if (SUPPORTED_HASHES.containsKey(hash.toUpperCase(Locale.US))) {
            String lowerCase = hash.toLowerCase(Locale.US);
            JID_TO_NODEVER_CACHE.put(xw3Var, new NodeVerHash(capsExtension.getNode(), capsExtension.getVer(), lowerCase));
        }
    }

    public static void addDiscoverInfoByNode(String str, DiscoverInfo discoverInfo) {
        CAPS_CACHE.put(str, discoverInfo);
        EntityCapsPersistentCache entityCapsPersistentCache = persistentCache;
        if (entityCapsPersistentCache != null) {
            entityCapsPersistentCache.addDiscoverInfoByNodePersistent(str, discoverInfo);
        }
    }

    public static void clearMemoryCache() {
        JID_TO_NODEVER_CACHE.clear();
        CAPS_CACHE.clear();
    }

    public static void formFieldValuesToCaps(List<CharSequence> list, StringBuilder sb) {
        TreeSet<CharSequence> treeSet = new TreeSet();
        treeSet.addAll(list);
        for (CharSequence charSequence : treeSet) {
            sb.append(charSequence);
            sb.append('<');
        }
    }

    public static CapsVersionAndHash generateVerificationString(DiscoverInfo discoverInfo) {
        return generateVerificationString(discoverInfo, null);
    }

    public static DiscoverInfo getDiscoverInfoByUser(xw3 xw3Var) {
        NodeVerHash a = JID_TO_NODEVER_CACHE.a((tx3<xw3, NodeVerHash>) xw3Var);
        if (a == null) {
            return null;
        }
        return getDiscoveryInfoByNodeVer(a.nodeVer);
    }

    public static DiscoverInfo getDiscoveryInfoByNodeVer(String str) {
        EntityCapsPersistentCache entityCapsPersistentCache;
        DiscoverInfo a = CAPS_CACHE.a((tx3<String, DiscoverInfo>) str);
        if (a == null && (entityCapsPersistentCache = persistentCache) != null && (a = entityCapsPersistentCache.lookup(str)) != null) {
            CAPS_CACHE.put(str, a);
        }
        return a != null ? new DiscoverInfo(a) : a;
    }

    public static synchronized EntityCapsManager getInstanceFor(XMPPConnection xMPPConnection) {
        EntityCapsManager entityCapsManager;
        synchronized (EntityCapsManager.class) {
            if (SUPPORTED_HASHES.size() > 0) {
                entityCapsManager = instances.get(xMPPConnection);
                if (entityCapsManager == null) {
                    entityCapsManager = new EntityCapsManager(xMPPConnection);
                }
            } else {
                throw new IllegalStateException("No supported hashes for EntityCapsManager");
            }
        }
        return entityCapsManager;
    }

    public static NodeVerHash getNodeVerHashByJid(xw3 xw3Var) {
        return JID_TO_NODEVER_CACHE.a((tx3<xw3, NodeVerHash>) xw3Var);
    }

    public static String getNodeVersionByJid(xw3 xw3Var) {
        NodeVerHash a = JID_TO_NODEVER_CACHE.a((tx3<xw3, NodeVerHash>) xw3Var);
        if (a != null) {
            return a.nodeVer;
        }
        return null;
    }

    public static void removeUserCapsNode(xw3 xw3Var) {
        JID_TO_NODEVER_CACHE.remove(xw3Var);
    }

    public static void setDefaultEntityNode(String str) {
        DEFAULT_ENTITY_NODE = str;
    }

    public static void setMaxsCacheSizes(int i, int i2) {
        JID_TO_NODEVER_CACHE.a(i);
        CAPS_CACHE.a(i2);
    }

    public static void setPersistentCache(EntityCapsPersistentCache entityCapsPersistentCache) {
        persistentCache = entityCapsPersistentCache;
    }

    public void updateLocalEntityCaps() {
        XMPPConnection connection = connection();
        DiscoverInfo discoverInfo = new DiscoverInfo();
        discoverInfo.setType(IQ.Type.result);
        this.sdm.addDiscoverInfoTo(discoverInfo);
        this.currentCapsVersion = generateVerificationString(discoverInfo);
        String localNodeVer = getLocalNodeVer();
        discoverInfo.setNode(localNodeVer);
        addDiscoverInfoByNode(localNodeVer, discoverInfo);
        if (this.lastLocalCapsVersions.size() > 10) {
            ServiceDiscoveryManager serviceDiscoveryManager = this.sdm;
            serviceDiscoveryManager.removeNodeInformationProvider(this.entityNode + '#' + this.lastLocalCapsVersions.poll().version);
        }
        this.lastLocalCapsVersions.add(this.currentCapsVersion);
        if (connection != null) {
            JID_TO_NODEVER_CACHE.put(connection.getUser(), new NodeVerHash(this.entityNode, this.currentCapsVersion));
        }
        final LinkedList linkedList = new LinkedList(ServiceDiscoveryManager.getInstanceFor(connection).getIdentities());
        this.sdm.setNodeInformationProvider(localNodeVer, new AbstractNodeInformationProvider() {
            public List<String> features;
            public List<ExtensionElement> packetExtensions;

            {
                this.features = EntityCapsManager.this.sdm.getFeatures();
                this.packetExtensions = EntityCapsManager.this.sdm.getExtendedInfoAsList();
            }

            @Override
            public List<String> getNodeFeatures() {
                return this.features;
            }

            @Override
            public List<DiscoverInfo.Identity> getNodeIdentities() {
                return linkedList;
            }

            @Override
            public List<ExtensionElement> getNodePacketExtensions() {
                return this.packetExtensions;
            }
        });
        if (connection == null || !connection.isAuthenticated() || this.presenceSend == null) {
            return;
        }
        try {
            connection.sendStanza(this.presenceSend.cloneWithNewId());
        } catch (InterruptedException | SmackException.NotConnectedException e) {
            LOGGER.log(Level.WARNING, "Could could not update presence with caps info", e);
        }
    }

    public static boolean verifyDiscoverInfoVersion(String str, String str2, DiscoverInfo discoverInfo) {
        return (discoverInfo.containsDuplicateIdentities() || discoverInfo.containsDuplicateFeatures() || verifyPacketExtensions(discoverInfo) || !str.equals(generateVerificationString(discoverInfo, str2).version)) ? false : true;
    }

    public static boolean verifyPacketExtensions(DiscoverInfo discoverInfo) {
        LinkedList<FormField> linkedList = new LinkedList();
        for (ExtensionElement extensionElement : discoverInfo.getExtensions()) {
            if (extensionElement.getNamespace().equals("jabber:x:data")) {
                for (FormField formField : ((DataForm) extensionElement).getFields()) {
                    if (formField.getVariable().equals(FormField.FORM_TYPE)) {
                        for (FormField formField2 : linkedList) {
                            if (formField.equals(formField2)) {
                                return true;
                            }
                        }
                        linkedList.add(formField);
                    }
                }
                continue;
            }
        }
        return false;
    }

    public boolean areEntityCapsSupported(xw3 xw3Var) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        return this.sdm.supportsFeature(xw3Var, "http://jabber.org/protocol/caps");
    }

    public boolean areEntityCapsSupportedByServer() throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        return areEntityCapsSupported(connection().getXMPPServiceDomain());
    }

    public synchronized void disableEntityCaps() {
        this.entityCapsEnabled = false;
        this.sdm.removeFeature("http://jabber.org/protocol/caps");
    }

    public synchronized void enableEntityCaps() {
        this.sdm.addFeature("http://jabber.org/protocol/caps");
        updateLocalEntityCaps();
        this.entityCapsEnabled = true;
    }

    public boolean entityCapsEnabled() {
        return this.entityCapsEnabled;
    }

    public CapsVersionAndHash getCapsVersionAndHash() {
        return this.currentCapsVersion;
    }

    public String getLocalNodeVer() {
        CapsVersionAndHash capsVersionAndHash = getCapsVersionAndHash();
        if (capsVersionAndHash == null) {
            return null;
        }
        return this.entityNode + '#' + capsVersionAndHash.version;
    }

    public void setEntityNode(String str) {
        this.entityNode = str;
        updateLocalEntityCaps();
    }

    public static CapsVersionAndHash generateVerificationString(DiscoverInfo discoverInfo, String str) {
        byte[] digest;
        if (str == null) {
            str = "SHA-1";
        }
        MessageDigest messageDigest = SUPPORTED_HASHES.get(str.toUpperCase(Locale.US));
        FormField formField = null;
        if (messageDigest == null) {
            return null;
        }
        String lowerCase = str.toLowerCase(Locale.US);
        DataForm from = DataForm.from(discoverInfo);
        StringBuilder sb = new StringBuilder();
        TreeSet<DiscoverInfo.Identity> treeSet = new TreeSet();
        treeSet.addAll(discoverInfo.getIdentities());
        for (DiscoverInfo.Identity identity : treeSet) {
            sb.append(identity.getCategory());
            sb.append(FileUtil.UNIX_SEPARATOR);
            sb.append(identity.getType());
            sb.append(FileUtil.UNIX_SEPARATOR);
            sb.append(identity.getLanguage() == null ? "" : identity.getLanguage());
            sb.append(FileUtil.UNIX_SEPARATOR);
            sb.append(identity.getName() == null ? "" : identity.getName());
            sb.append('<');
        }
        TreeSet<String> treeSet2 = new TreeSet();
        for (DiscoverInfo.Feature feature : discoverInfo.getFeatures()) {
            treeSet2.add(feature.getVar());
        }
        for (String str2 : treeSet2) {
            sb.append(str2);
            sb.append('<');
        }
        if (from != null && from.hasHiddenFormTypeField()) {
            synchronized (from) {
                TreeSet<FormField> treeSet3 = new TreeSet(new Comparator<FormField>() {
                    @Override
                    public int compare(FormField formField2, FormField formField3) {
                        return formField2.getVariable().compareTo(formField3.getVariable());
                    }
                });
                for (FormField formField2 : from.getFields()) {
                    if (formField2.getVariable().equals(FormField.FORM_TYPE)) {
                        formField = formField2;
                    } else {
                        treeSet3.add(formField2);
                    }
                }
                if (formField != null) {
                    formFieldValuesToCaps(formField.getValues(), sb);
                }
                for (FormField formField3 : treeSet3) {
                    sb.append(formField3.getVariable());
                    sb.append('<');
                    formFieldValuesToCaps(formField3.getValues(), sb);
                }
            }
        }
        try {
            byte[] bytes = sb.toString().getBytes("UTF-8");
            synchronized (messageDigest) {
                digest = messageDigest.digest(bytes);
            }
            return new CapsVersionAndHash(Base64.encodeToString(digest), lowerCase);
        } catch (UnsupportedEncodingException e) {
            throw new AssertionError(e);
        }
    }
}