导航菜单

页面标题

页面副标题

bbinstant v6.24.0 - ServiceDiscoveryManager.java 源代码

正在查看: bbinstant v6.24.0 应用的 ServiceDiscoveryManager.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package org.jivesoftware.smackx.disco;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang3.time.DateUtils;
import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPConnectionRegistry;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.iqrequest.AbstractIqRequestHandler;
import org.jivesoftware.smack.iqrequest.IQRequestHandler;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smackx.caps.EntityCapsManager;
import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
import org.jivesoftware.smackx.disco.packet.DiscoverItems;
import org.jivesoftware.smackx.xdata.packet.DataForm;
import org.jxmpp.jid.i;
import org.jxmpp.util.cache.a;
import org.jxmpp.util.cache.b;

public final class ServiceDiscoveryManager extends Manager {
    private EntityCapsManager capsManager;
    private DataForm extendedInfo;
    private final Set<String> features;
    private Set<DiscoverInfo.Identity> identities;
    private DiscoverInfo.Identity identity;
    private Map<String, NodeInformationProvider> nodeInformationProviders;
    private a services;
    private static final Logger LOGGER = Logger.getLogger(ServiceDiscoveryManager.class.getName());
    private static final String DEFAULT_IDENTITY_CATEGORY = "client";
    private static final String DEFAULT_IDENTITY_NAME = "Smack";
    private static final String DEFAULT_IDENTITY_TYPE = "pc";
    private static DiscoverInfo.Identity defaultIdentity = new DiscoverInfo.Identity(DEFAULT_IDENTITY_CATEGORY, DEFAULT_IDENTITY_NAME, DEFAULT_IDENTITY_TYPE);
    private static Map<XMPPConnection, ServiceDiscoveryManager> instances = new WeakHashMap();

    static {
        XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
            @Override
            public void connectionCreated(XMPPConnection xMPPConnection) {
                ServiceDiscoveryManager.getInstanceFor(xMPPConnection);
            }
        });
    }

    private ServiceDiscoveryManager(XMPPConnection xMPPConnection) {
        super(xMPPConnection);
        this.identities = new HashSet();
        this.identity = defaultIdentity;
        this.features = new HashSet();
        this.extendedInfo = null;
        this.nodeInformationProviders = new ConcurrentHashMap();
        this.services = new b(25, DateUtils.MILLIS_PER_DAY);
        addFeature(DiscoverInfo.NAMESPACE);
        addFeature(DiscoverItems.NAMESPACE);
        IQ.Type type = IQ.Type.get;
        IQRequestHandler.Mode mode = IQRequestHandler.Mode.async;
        xMPPConnection.registerIQRequestHandler(new AbstractIqRequestHandler("query", DiscoverItems.NAMESPACE, type, mode) {
            @Override
            public IQ handleIQRequest(IQ iq) {
                DiscoverItems discoverItems = (DiscoverItems) iq;
                DiscoverItems discoverItems2 = new DiscoverItems();
                discoverItems2.setType(IQ.Type.result);
                discoverItems2.setTo(discoverItems.getFrom());
                discoverItems2.setStanzaId(discoverItems.getStanzaId());
                discoverItems2.setNode(discoverItems.getNode());
                NodeInformationProvider nodeInformationProvider = ServiceDiscoveryManager.this.getNodeInformationProvider(discoverItems.getNode());
                if (nodeInformationProvider != null) {
                    discoverItems2.addItems(nodeInformationProvider.getNodeItems());
                    discoverItems2.addExtensions(nodeInformationProvider.getNodePacketExtensions());
                } else if (discoverItems.getNode() != null) {
                    discoverItems2.setType(IQ.Type.error);
                    discoverItems2.setError(XMPPError.getBuilder(XMPPError.Condition.item_not_found));
                }
                return discoverItems2;
            }
        });
        xMPPConnection.registerIQRequestHandler(new AbstractIqRequestHandler("query", DiscoverInfo.NAMESPACE, type, mode) {
            @Override
            public IQ handleIQRequest(IQ iq) {
                DiscoverInfo discoverInfo = (DiscoverInfo) iq;
                DiscoverInfo discoverInfo2 = new DiscoverInfo();
                discoverInfo2.setType(IQ.Type.result);
                discoverInfo2.setTo(discoverInfo.getFrom());
                discoverInfo2.setStanzaId(discoverInfo.getStanzaId());
                discoverInfo2.setNode(discoverInfo.getNode());
                if (discoverInfo.getNode() == null) {
                    ServiceDiscoveryManager.this.addDiscoverInfoTo(discoverInfo2);
                } else {
                    NodeInformationProvider nodeInformationProvider = ServiceDiscoveryManager.this.getNodeInformationProvider(discoverInfo.getNode());
                    if (nodeInformationProvider != null) {
                        discoverInfo2.addFeatures(nodeInformationProvider.getNodeFeatures());
                        discoverInfo2.addIdentities(nodeInformationProvider.getNodeIdentities());
                        discoverInfo2.addExtensions(nodeInformationProvider.getNodePacketExtensions());
                    } else {
                        discoverInfo2.setType(IQ.Type.error);
                        discoverInfo2.setError(XMPPError.getBuilder(XMPPError.Condition.item_not_found));
                    }
                }
                return discoverInfo2;
            }
        });
    }

    public static synchronized ServiceDiscoveryManager getInstanceFor(XMPPConnection xMPPConnection) {
        ServiceDiscoveryManager serviceDiscoveryManager;
        synchronized (ServiceDiscoveryManager.class) {
            serviceDiscoveryManager = instances.get(xMPPConnection);
            if (serviceDiscoveryManager == null) {
                serviceDiscoveryManager = new ServiceDiscoveryManager(xMPPConnection);
                instances.put(xMPPConnection, serviceDiscoveryManager);
            }
        }
        return serviceDiscoveryManager;
    }

    public NodeInformationProvider getNodeInformationProvider(String str) {
        if (str == null) {
            return null;
        }
        return this.nodeInformationProviders.get(str);
    }

    private void renewEntityCapsVersion() {
        EntityCapsManager entityCapsManager = this.capsManager;
        if (entityCapsManager == null || !entityCapsManager.entityCapsEnabled()) {
            return;
        }
        this.capsManager.updateLocalEntityCaps();
    }

    public static void setDefaultIdentity(DiscoverInfo.Identity identity) {
        defaultIdentity = identity;
    }

    public synchronized void addDiscoverInfoTo(DiscoverInfo discoverInfo) {
        try {
            discoverInfo.addIdentities(getIdentities());
            Iterator<String> it = getFeatures().iterator();
            while (it.hasNext()) {
                discoverInfo.addFeature(it.next());
            }
            discoverInfo.addExtension(this.extendedInfo);
        } catch (Throwable th) {
            throw th;
        }
    }

    public synchronized void addFeature(String str) {
        this.features.add(str);
        renewEntityCapsVersion();
    }

    public synchronized void addIdentity(DiscoverInfo.Identity identity) {
        this.identities.add(identity);
        renewEntityCapsVersion();
    }

    public boolean canPublishItems(i iVar) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        return canPublishItems(discoverInfo(iVar));
    }

    public DiscoverInfo discoverInfo(i iVar) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        if (iVar == null) {
            return discoverInfo(null, null);
        }
        DiscoverInfo discoverInfoByUser = EntityCapsManager.getDiscoverInfoByUser(iVar);
        if (discoverInfoByUser != null) {
            return discoverInfoByUser;
        }
        EntityCapsManager.NodeVerHash nodeVerHashByJid = EntityCapsManager.getNodeVerHashByJid(iVar);
        DiscoverInfo discoverInfo = discoverInfo(iVar, nodeVerHashByJid != null ? nodeVerHashByJid.getNodeVer() : null);
        if (nodeVerHashByJid != null && EntityCapsManager.verifyDiscoverInfoVersion(nodeVerHashByJid.getVer(), nodeVerHashByJid.getHash(), discoverInfo)) {
            EntityCapsManager.addDiscoverInfoByNode(nodeVerHashByJid.getNodeVer(), discoverInfo);
        }
        return discoverInfo;
    }

    public DiscoverItems discoverItems(i iVar) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        return discoverItems(iVar, null);
    }

    public org.jxmpp.jid.b findService(String str, boolean z, String str2, String str3) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        List<DiscoverInfo> findServicesDiscoverInfo = findServicesDiscoverInfo(str, true, z);
        if (findServicesDiscoverInfo.isEmpty()) {
            return null;
        }
        DiscoverInfo discoverInfo = findServicesDiscoverInfo.get(0);
        if (str2 == null || str3 == null) {
            if (str2 != null || str3 != null) {
                throw new IllegalArgumentException("Must specify either both, category and type, or none");
            }
        } else if (!discoverInfo.hasIdentity(str2, str3)) {
            return null;
        }
        return discoverInfo.getFrom().asDomainBareJid();
    }

    public List<org.jxmpp.jid.b> findServices(String str, boolean z, boolean z2) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        List<DiscoverInfo> findServicesDiscoverInfo = findServicesDiscoverInfo(str, z, z2);
        ArrayList arrayList = new ArrayList(findServicesDiscoverInfo.size());
        Iterator<DiscoverInfo> it = findServicesDiscoverInfo.iterator();
        while (it.hasNext()) {
            arrayList.add(it.next().getFrom().asDomainBareJid());
        }
        return arrayList;
    }

    public List<DiscoverInfo> findServicesDiscoverInfo(String str, boolean z, boolean z2) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        List<DiscoverInfo> list;
        org.jxmpp.jid.b xMPPServiceDomain = connection().getXMPPServiceDomain();
        if (z2 && (list = (List) this.services.lookup(str)) != null) {
            return list;
        }
        LinkedList linkedList = new LinkedList();
        try {
            DiscoverInfo discoverInfo = discoverInfo(xMPPServiceDomain);
            if (discoverInfo.containsFeature(str)) {
                linkedList.add(discoverInfo);
                if (z) {
                    if (z2) {
                        this.services.put(str, linkedList);
                    }
                    return linkedList;
                }
            }
            try {
                for (DiscoverItems.Item item : discoverItems(xMPPServiceDomain).getItems()) {
                    try {
                        DiscoverInfo discoverInfo2 = discoverInfo(item.getEntityID());
                        if (discoverInfo2.containsFeature(str)) {
                            linkedList.add(discoverInfo2);
                            if (z) {
                                break;
                            }
                        } else {
                            continue;
                        }
                    } catch (SmackException.NoResponseException | XMPPException.XMPPErrorException e) {
                        LOGGER.log(Level.WARNING, "Exception while discovering info for feature " + str + " of " + ((Object) item.getEntityID()) + " node: " + item.getNode(), e);
                    }
                }
                if (z2) {
                    this.services.put(str, linkedList);
                }
                return linkedList;
            } catch (XMPPException.XMPPErrorException e2) {
                LOGGER.log(Level.WARNING, "Could not discover items about service", (Throwable) e2);
                return linkedList;
            }
        } catch (XMPPException.XMPPErrorException e3) {
            LOGGER.log(Level.WARNING, "Could not discover information about service", (Throwable) e3);
            return linkedList;
        }
    }

    public DataForm getExtendedInfo() {
        return this.extendedInfo;
    }

    public List<ExtensionElement> getExtendedInfoAsList() {
        if (this.extendedInfo == null) {
            return null;
        }
        ArrayList arrayList = new ArrayList(1);
        arrayList.add(this.extendedInfo);
        return arrayList;
    }

    public synchronized List<String> getFeatures() {
        return new ArrayList(this.features);
    }

    public Set<DiscoverInfo.Identity> getIdentities() {
        HashSet hashSet = new HashSet(this.identities);
        hashSet.add(defaultIdentity);
        return Collections.unmodifiableSet(hashSet);
    }

    public DiscoverInfo.Identity getIdentity() {
        return this.identity;
    }

    public String getIdentityName() {
        return this.identity.getName();
    }

    public String getIdentityType() {
        return this.identity.getType();
    }

    public synchronized boolean includesFeature(String str) {
        return this.features.contains(str);
    }

    public void publishItems(i iVar, DiscoverItems discoverItems) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        publishItems(iVar, null, discoverItems);
    }

    public synchronized void removeExtendedInfo() {
        this.extendedInfo = null;
        renewEntityCapsVersion();
    }

    public synchronized void removeFeature(String str) {
        this.features.remove(str);
        renewEntityCapsVersion();
    }

    public synchronized boolean removeIdentity(DiscoverInfo.Identity identity) {
        if (identity.equals(this.identity)) {
            return false;
        }
        this.identities.remove(identity);
        renewEntityCapsVersion();
        return true;
    }

    public void removeNodeInformationProvider(String str) {
        this.nodeInformationProviders.remove(str);
    }

    public boolean serverSupportsFeature(CharSequence charSequence) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        return serverSupportsFeatures(charSequence);
    }

    public boolean serverSupportsFeatures(CharSequence... charSequenceArr) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        return serverSupportsFeatures(Arrays.asList(charSequenceArr));
    }

    public void setEntityCapsManager(EntityCapsManager entityCapsManager) {
        this.capsManager = entityCapsManager;
    }

    public synchronized void setExtendedInfo(DataForm dataForm) {
        this.extendedInfo = dataForm;
        renewEntityCapsVersion();
    }

    public synchronized void setIdentity(DiscoverInfo.Identity identity) {
        this.identity = (DiscoverInfo.Identity) Objects.requireNonNull(identity, "Identity can not be null");
        renewEntityCapsVersion();
    }

    public void setNodeInformationProvider(String str, NodeInformationProvider nodeInformationProvider) {
        this.nodeInformationProviders.put(str, nodeInformationProvider);
    }

    public boolean supportsFeature(i iVar, CharSequence charSequence) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        return supportsFeatures(iVar, charSequence);
    }

    public boolean supportsFeatures(i iVar, CharSequence... charSequenceArr) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        return supportsFeatures(iVar, Arrays.asList(charSequenceArr));
    }

    public DiscoverItems discoverItems(i iVar, String str) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        DiscoverItems discoverItems = new DiscoverItems();
        discoverItems.setType(IQ.Type.get);
        discoverItems.setTo(iVar);
        discoverItems.setNode(str);
        return (DiscoverItems) connection().createStanzaCollectorAndSend(discoverItems).nextResultOrThrow();
    }

    public void publishItems(i iVar, String str, DiscoverItems discoverItems) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        discoverItems.setType(IQ.Type.set);
        discoverItems.setTo(iVar);
        discoverItems.setNode(str);
        connection().createStanzaCollectorAndSend(discoverItems).nextResultOrThrow();
    }

    public boolean serverSupportsFeatures(Collection<? extends CharSequence> collection) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        return supportsFeatures(connection().getXMPPServiceDomain(), collection);
    }

    public boolean supportsFeatures(i iVar, Collection<? extends CharSequence> collection) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        DiscoverInfo discoverInfo = discoverInfo(iVar);
        Iterator<? extends CharSequence> it = collection.iterator();
        while (it.hasNext()) {
            if (!discoverInfo.containsFeature(it.next())) {
                return false;
            }
        }
        return true;
    }

    public static boolean canPublishItems(DiscoverInfo discoverInfo) {
        return discoverInfo.containsFeature("http://jabber.org/protocol/disco#publish");
    }

    public DiscoverInfo discoverInfo(i iVar, String str) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        DiscoverInfo discoverInfo = new DiscoverInfo();
        discoverInfo.setType(IQ.Type.get);
        discoverInfo.setTo(iVar);
        discoverInfo.setNode(str);
        return (DiscoverInfo) connection().createStanzaCollectorAndSend(discoverInfo).nextResultOrThrow();
    }

    public org.jxmpp.jid.b findService(String str, boolean z) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        return findService(str, z, null, null);
    }
}