Java源码示例:org.jivesoftware.smackx.search.UserSearch

示例1
/**
 * 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;
  }
}