Search in sources :

Example 1 with IQRouter

use of org.jivesoftware.openfire.IQRouter in project Openfire by igniterealtime.

the class ComponentList method getComponentInfo.

private void getComponentInfo() {
    IQRouter iqRouter;
    Collection<String> components = routingTable.getComponentsDomains();
    iqRouter = server.getIQRouter();
    for (String componentDomain : components) {
        IQ iq = new IQ(IQ.Type.get);
        iq.setFrom(server.getServerInfo().getXMPPDomain());
        iq.setTo(componentDomain);
        iq.setChildElement("query", "http://jabber.org/protocol/disco#info");
        iqRouter.addIQResultListener(iq.getID(), this);
        iqRouter.route(iq);
    }
}
Also used : IQ(org.xmpp.packet.IQ) IQRouter(org.jivesoftware.openfire.IQRouter)

Example 2 with IQRouter

use of org.jivesoftware.openfire.IQRouter in project Openfire by igniterealtime.

the class AbstractXepSupport method stop.

public void stop() {
    IQRouter iqRouter = server.getIQRouter();
    IQDiscoInfoHandler iqDiscoInfoHandler = server.getIQDiscoInfoHandler();
    for (IQHandler iqHandler : iqHandlers) {
        element2Handlers.remove(iqHandler.getInfo().getName());
        try {
            iqHandler.stop();
            iqHandler.destroy();
        } catch (Exception e) {
            Log.warn("Unable to stop and destroy " + iqHandler.getClass());
        }
        if (iqHandler instanceof ServerFeaturesProvider) {
            for (Iterator<String> i = ((ServerFeaturesProvider) iqHandler).getFeatures(); i.hasNext(); ) {
                if (iqDiscoInfoHandler != null) {
                    iqDiscoInfoHandler.removeServerFeature(i.next());
                }
            }
        }
        if (muc) {
            MultiUserChatManager manager = server.getMultiUserChatManager();
            for (MultiUserChatService mucService : manager.getMultiUserChatServices()) {
                mucService.removeIQHandler(iqHandler);
                mucService.removeExtraFeature(namespace);
            }
        }
    }
    if (iqRouter != null) {
        iqRouter.removeHandler(iqDispatcher);
    }
}
Also used : IQDiscoInfoHandler(org.jivesoftware.openfire.disco.IQDiscoInfoHandler) IQHandler(org.jivesoftware.openfire.handler.IQHandler) MultiUserChatManager(org.jivesoftware.openfire.muc.MultiUserChatManager) MultiUserChatService(org.jivesoftware.openfire.muc.MultiUserChatService) ServerFeaturesProvider(org.jivesoftware.openfire.disco.ServerFeaturesProvider) IQRouter(org.jivesoftware.openfire.IQRouter) UnauthorizedException(org.jivesoftware.openfire.auth.UnauthorizedException)

Example 3 with IQRouter

use of org.jivesoftware.openfire.IQRouter in project Openfire by igniterealtime.

the class EntityCapabilitiesManager method process.

public void process(Presence packet) {
    if (Presence.Type.unavailable == packet.getType()) {
        if (packet.getFrom() != null) {
            final String oldVer;
            final Lock lock = this.entityCapabilitiesUserMap.getLock(packet.getFrom().asBareJID());
            lock.lock();
            try {
                this.capabilitiesBeingUpdated.remove(packet.getFrom());
                oldVer = this.entityCapabilitiesUserMap.remove(packet.getFrom());
            } finally {
                lock.unlock();
            }
            if (oldVer != null) {
                checkObsolete(oldVer);
            }
        }
        return;
    }
    // Examine the packet and check if it has caps info,
    // if not -- do nothing by returning.
    Element capsElement = packet.getChildElement("c", "http://jabber.org/protocol/caps");
    if (capsElement == null) {
        return;
    }
    // Examine the packet and check if it's in legacy format (pre version 1.4
    // of XEP-0115). If so, do nothing by returning.
    // TODO: if this packet is in legacy format, we SHOULD check the 'node',
    // 'ver', and 'ext' combinations as specified in the archived version
    // 1.3 of the specification, and cache the results. See JM-1447
    final String hashAttribute = capsElement.attributeValue("hash");
    if (hashAttribute == null || hashAttribute.trim().length() == 0) {
        return;
    }
    // Examine the packet and check if it has and a 'ver' hash
    // if not -- do nothing by returning.
    final String newVerAttribute = capsElement.attributeValue("ver");
    if (newVerAttribute == null || newVerAttribute.trim().length() == 0) {
        return;
    }
    // Check to see if the 'ver' hash is already in our cache.
    EntityCapabilities caps;
    if ((caps = entityCapabilitiesMap.get(newVerAttribute)) != null) {
        // The 'ver' hash is in the cache already, so let's update the
        // entityCapabilitiesUserMap for the user that sent the caps
        // packet.
        Log.trace("Registering 'ver' (for recognized caps) for {}", packet.getFrom());
        registerCapabilities(packet.getFrom(), caps);
    } else {
        final Lock lock = this.entityCapabilitiesUserMap.getLock(packet.getFrom().asBareJID());
        lock.lock();
        try {
            // If this entity previously had another registration, that now no longer is valid.
            final String ver = entityCapabilitiesUserMap.remove(packet.getFrom());
            if (ver != null) {
                capabilitiesBeingUpdated.put(packet.getFrom(), ver);
            }
        } finally {
            lock.unlock();
        }
        // The 'ver' hash is not in the cache so send out a disco#info query
        // so that we may begin recognizing this 'ver' hash.
        IQ iq = new IQ(IQ.Type.get);
        iq.setTo(packet.getFrom());
        String serverName = XMPPServer.getInstance().getServerInfo().getXMPPDomain();
        iq.setFrom(serverName);
        iq.setChildElement("query", "http://jabber.org/protocol/disco#info");
        String packetId = iq.getID();
        caps = new EntityCapabilities();
        caps.setHashAttribute(hashAttribute);
        caps.setVerAttribute(newVerAttribute);
        Log.trace("Querying 'ver' for unrecognized caps. Querying: {}", packet.getFrom());
        verAttributes.put(packetId, caps);
        final IQRouter iqRouter = XMPPServer.getInstance().getIQRouter();
        iqRouter.addIQResultListener(packetId, this);
        iqRouter.route(iq);
    }
}
Also used : Element(org.dom4j.Element) IQ(org.xmpp.packet.IQ) IQRouter(org.jivesoftware.openfire.IQRouter) Lock(java.util.concurrent.locks.Lock)

Example 4 with IQRouter

use of org.jivesoftware.openfire.IQRouter in project Openfire by igniterealtime.

the class UserManager method isRegisteredUser.

/**
 * Returns true if the specified JID belongs to a local or remote registered user. If allowed by the 'checkRemoteDomains',
 * argument, for remote users (i.e. domain does not match local domain) a disco#info request is going to be sent to
 * the bare JID of the user. If 'checkRemoteDomains' is false, this method will return 'false' for all JIDs of which the
 * domain-part does not match the local domain.
 *
 * <p>WARNING: If the supplied JID could be a remote user and the disco#info result packet comes back on the same
 * thread as the one the calls this method then it will not be processed, and this method will block for 60 seconds
 * by default. To change the timeout, update the system property <code>usermanager.remote-disco-info-timeout-seconds</code>
 *
 * @param user to JID of the user to check it it's a registered user.
 * @param checkRemoteDomains false the lookup is allowed to include calls to remote XMPP domains.
 * @return true if the specified JID belongs to a registered user.
 */
public boolean isRegisteredUser(@Nonnull final JID user, final boolean checkRemoteDomains) {
    if (xmppServer.isLocal(user)) {
        try {
            getUser(user.getNode());
            return true;
        } catch (final UserNotFoundException e) {
            return false;
        }
    } else if (!checkRemoteDomains) {
        return false;
    } else {
        // Look up in the cache using the full JID
        Boolean isRegistered = remoteUsersCache.get(user.toString());
        if (isRegistered == null) {
            // Check if the bare JID of the user is cached
            isRegistered = remoteUsersCache.get(user.toBareJID());
            if (isRegistered == null) {
                // No information is cached so check user identity and cache it
                // A disco#info is going to be sent to the bare JID of the user. This packet
                // is going to be handled by the remote server.
                final IQ iq = new IQ(IQ.Type.get);
                iq.setFrom(xmppServer.getServerInfo().getXMPPDomain());
                iq.setTo(user.toBareJID());
                iq.setChildElement("query", "http://jabber.org/protocol/disco#info");
                final Semaphore completionSemaphore = new Semaphore(0);
                // Send the disco#info request to the remote server.
                final IQRouter iqRouter = xmppServer.getIQRouter();
                final long timeoutInMillis = REMOTE_DISCO_INFO_TIMEOUT.getValue().toMillis();
                iqRouter.addIQResultListener(iq.getID(), new IQResultListener() {

                    @Override
                    public void receivedAnswer(final IQ packet) {
                        final JID from = packet.getFrom();
                        // Assume that the user is not a registered user
                        Boolean isRegistered = Boolean.FALSE;
                        // Analyze the disco result packet
                        if (IQ.Type.result == packet.getType()) {
                            final Element child = packet.getChildElement();
                            if (child != null) {
                                for (final Iterator it = child.elementIterator("identity"); it.hasNext(); ) {
                                    final Element identity = (Element) it.next();
                                    final String accountType = identity.attributeValue("type");
                                    if ("registered".equals(accountType) || "admin".equals(accountType)) {
                                        isRegistered = Boolean.TRUE;
                                        break;
                                    }
                                }
                            }
                        }
                        // Update cache of remote registered users
                        remoteUsersCache.put(from.toBareJID(), isRegistered);
                        completionSemaphore.release();
                    }

                    @Override
                    public void answerTimeout(final String packetId) {
                        Log.warn("The result from the disco#info request was never received. request: {}", iq);
                        completionSemaphore.release();
                    }
                }, timeoutInMillis);
                // Send the request
                iqRouter.route(iq);
                // Wait for the response
                try {
                    completionSemaphore.tryAcquire(timeoutInMillis, TimeUnit.MILLISECONDS);
                } catch (final InterruptedException e) {
                    Thread.currentThread().interrupt();
                    Log.warn("Interrupted whilst waiting for response from remote server", e);
                }
                isRegistered = remoteUsersCache.computeIfAbsent(user.toBareJID(), ignored -> Boolean.FALSE);
            }
        }
        return isRegistered;
    }
}
Also used : IQResultListener(org.xmpp.component.IQResultListener) JID(org.xmpp.packet.JID) Element(org.dom4j.Element) IQ(org.xmpp.packet.IQ) Iterator(java.util.Iterator) Semaphore(java.util.concurrent.Semaphore) IQRouter(org.jivesoftware.openfire.IQRouter)

Aggregations

IQRouter (org.jivesoftware.openfire.IQRouter)4 IQ (org.xmpp.packet.IQ)3 Element (org.dom4j.Element)2 Iterator (java.util.Iterator)1 Semaphore (java.util.concurrent.Semaphore)1 Lock (java.util.concurrent.locks.Lock)1 UnauthorizedException (org.jivesoftware.openfire.auth.UnauthorizedException)1 IQDiscoInfoHandler (org.jivesoftware.openfire.disco.IQDiscoInfoHandler)1 ServerFeaturesProvider (org.jivesoftware.openfire.disco.ServerFeaturesProvider)1 IQHandler (org.jivesoftware.openfire.handler.IQHandler)1 MultiUserChatManager (org.jivesoftware.openfire.muc.MultiUserChatManager)1 MultiUserChatService (org.jivesoftware.openfire.muc.MultiUserChatService)1 IQResultListener (org.xmpp.component.IQResultListener)1 JID (org.xmpp.packet.JID)1