org.jivesoftware.smackx.search.UserSearch Java Examples

The following examples show how to use org.jivesoftware.smackx.search.UserSearch. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XMPPUtils.java    From saros with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Tries to find the user in the user directory of the server. This method may fail for different
 * XMPP Server implementation, because the 'user' variable is not mandatory neither that it must
 * be named 'user'. Currently works with ejabberd XMPP servers.
 */

// TODO: remove this method, add more logic and let the GUI handle search
// stuff

private static boolean isListedInUserDirectory(Connection connection, JID jid) {

  String userDirectoryService = null;

  try {
    userDirectoryService = getUserDirectoryService(connection, connection.getServiceName());

    if (userDirectoryService == null) return false;

    UserSearch search = new UserSearch();

    Form form = search.getSearchForm(connection, userDirectoryService);

    String userFieldVariable = null;

    for (Iterator<FormField> it = form.getFields(); it.hasNext(); ) {
      FormField formField = it.next();

      if ("user".equalsIgnoreCase(formField.getVariable())) {
        userFieldVariable = formField.getVariable();
        break;
      }
    }

    if (userFieldVariable == null) return false;

    Form answerForm = form.createAnswerForm();

    answerForm.setAnswer(userFieldVariable, jid.getName());

    ReportedData data = search.sendSearchForm(connection, answerForm, userDirectoryService);

    for (Iterator<Row> it = data.getRows(); it.hasNext(); ) {

      Row row = it.next();

      Iterator<String> vit = row.getValues("jid");

      if (vit == null) continue;

      while (vit.hasNext()) {
        JID returnedJID = new JID(vit.next());
        if (jid.equals(returnedJID)) return true;
      }
    }

    return false;
  } catch (XMPPException e) {
    log.error("searching in the user directory + '" + userDirectoryService + "' failed", e);
    return false;
  }
}