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;
}
}
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());
}
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());
}
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());
}
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());
}
Aggregations