Search in sources :

Example 6 with IQResultListener

use of org.xmpp.component.IQResultListener 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)

Example 7 with IQResultListener

use of org.xmpp.component.IQResultListener in project Openfire by igniterealtime.

the class UserManagerTest method isRegisteredUserTrueWillReturnFalseForUnknownRemoteUsers.

@Test
public void isRegisteredUserTrueWillReturnFalseForUnknownRemoteUsers() {
    final AtomicReference<IQResultListener> iqListener = new AtomicReference<>();
    doAnswer(invocationOnMock -> {
        final IQResultListener listener = invocationOnMock.getArgument(1);
        iqListener.set(listener);
        return null;
    }).when(iqRouter).addIQResultListener(any(), any(), anyLong());
    doAnswer(invocationOnMock -> {
        final IQ iq = invocationOnMock.getArgument(0);
        final Element childElement = iq.getChildElement();
        final IQ response = IQ.createResultIQ(iq);
        response.setChildElement(childElement.createCopy());
        response.setError(new PacketError(PacketError.Condition.item_not_found, PacketError.Condition.item_not_found.getDefaultType()));
        iqListener.get().receivedAnswer(response);
        return null;
    }).when(iqRouter).route(any());
    final boolean result = userManager.isRegisteredUser(new JID(USER_ID, REMOTE_XMPP_DOMAIN, null), true);
    assertThat(result, is(false));
    verify(iqRouter).route(any());
}
Also used : IQResultListener(org.xmpp.component.IQResultListener) JID(org.xmpp.packet.JID) Element(org.dom4j.Element) IQ(org.xmpp.packet.IQ) PacketError(org.xmpp.packet.PacketError) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 8 with IQResultListener

use of org.xmpp.component.IQResultListener in project Openfire by igniterealtime.

the class UserManagerTest method isRegisteredUserTrueWillReturnTrueForRemoteUsers.

@Test
public void isRegisteredUserTrueWillReturnTrueForRemoteUsers() {
    final AtomicReference<IQResultListener> iqListener = new AtomicReference<>();
    doAnswer(invocationOnMock -> {
        final IQResultListener listener = invocationOnMock.getArgument(1);
        iqListener.set(listener);
        return null;
    }).when(iqRouter).addIQResultListener(any(), any(), anyLong());
    doAnswer(invocationOnMock -> {
        iqListener.get().receivedAnswer(Fixtures.iqFrom(USER_FOUND_RESULT));
        return null;
    }).when(iqRouter).route(any());
    final boolean result = userManager.isRegisteredUser(new JID(USER_ID, REMOTE_XMPP_DOMAIN, null), true);
    assertThat(result, is(true));
    verify(iqRouter).route(any());
}
Also used : IQResultListener(org.xmpp.component.IQResultListener) JID(org.xmpp.packet.JID) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 9 with IQResultListener

use of org.xmpp.component.IQResultListener in project Openfire by igniterealtime.

the class UserManagerTest method isRegisteredUserTrueWillReturnFalseForATimeout.

@Test
public void isRegisteredUserTrueWillReturnFalseForATimeout() {
    final AtomicReference<IQResultListener> iqListener = new AtomicReference<>();
    doAnswer(invocationOnMock -> {
        final IQResultListener listener = invocationOnMock.getArgument(1);
        iqListener.set(listener);
        return null;
    }).when(iqRouter).addIQResultListener(any(), any(), anyLong());
    doAnswer(invocationOnMock -> {
        final IQ iq = invocationOnMock.getArgument(0);
        iqListener.get().answerTimeout(iq.getID());
        return null;
    }).when(iqRouter).route(any());
    final boolean result = userManager.isRegisteredUser(new JID(USER_ID, REMOTE_XMPP_DOMAIN, null), true);
    assertThat(result, is(false));
    verify(iqRouter).route(any());
}
Also used : IQResultListener(org.xmpp.component.IQResultListener) JID(org.xmpp.packet.JID) IQ(org.xmpp.packet.IQ) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 10 with IQResultListener

use of org.xmpp.component.IQResultListener in project Openfire by igniterealtime.

the class UserManagerTest method isRegisteredUserWillReturnFalseForATimeout.

@Test
public void isRegisteredUserWillReturnFalseForATimeout() {
    final AtomicReference<IQResultListener> iqListener = new AtomicReference<>();
    doAnswer(invocationOnMock -> {
        final IQResultListener listener = invocationOnMock.getArgument(1);
        iqListener.set(listener);
        return null;
    }).when(iqRouter).addIQResultListener(any(), any(), anyLong());
    doAnswer(invocationOnMock -> {
        final IQ iq = invocationOnMock.getArgument(0);
        iqListener.get().answerTimeout(iq.getID());
        return null;
    }).when(iqRouter).route(any());
    final boolean result = userManager.isRegisteredUser(new JID(USER_ID, REMOTE_XMPP_DOMAIN, null));
    assertThat(result, is(false));
    verify(iqRouter).route(any());
}
Also used : IQResultListener(org.xmpp.component.IQResultListener) JID(org.xmpp.packet.JID) IQ(org.xmpp.packet.IQ) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Aggregations

IQResultListener (org.xmpp.component.IQResultListener)10 JID (org.xmpp.packet.JID)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 Test (org.junit.Test)8 IQ (org.xmpp.packet.IQ)6 Element (org.dom4j.Element)5 PacketError (org.xmpp.packet.PacketError)3 Iterator (java.util.Iterator)1 Semaphore (java.util.concurrent.Semaphore)1 IQRouter (org.jivesoftware.openfire.IQRouter)1 IQResultListenerTask (org.jivesoftware.openfire.cluster.IQResultListenerTask)1 NodeID (org.jivesoftware.openfire.cluster.NodeID)1 IQHandler (org.jivesoftware.openfire.handler.IQHandler)1 PacketRejectedException (org.jivesoftware.openfire.interceptor.PacketRejectedException)1 PrivacyList (org.jivesoftware.openfire.privacy.PrivacyList)1 ClientSession (org.jivesoftware.openfire.session.ClientSession)1 DomainPair (org.jivesoftware.openfire.session.DomainPair)1 LocalClientSession (org.jivesoftware.openfire.session.LocalClientSession)1 Session (org.jivesoftware.openfire.session.Session)1