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;
        }
    }
@Test
    public void isRegisteredUserFalseWillReturnFalseForLocalNonUsers() {
        final boolean result = userManager.isRegisteredUser(new JID("unknown-user", Fixtures.XMPP_DOMAIN, null), false);
        assertThat(result, is(false));
    }