org.jivesoftware.smackx.search.UserSearchManager Java Examples

The following examples show how to use org.jivesoftware.smackx.search.UserSearchManager. 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: XmppTool.java    From xmpp with Apache License 2.0 6 votes vote down vote up
/**
 * 查找用户
 *
 * @param
 * @param userName
 * @return
 */
public List<XmppUser> searchUsers(String userName) {
    List<XmppUser> list = new ArrayList<XmppUser>();
    UserSearchManager userSearchManager = new UserSearchManager(con);
    try {
        Form searchForm = userSearchManager.getSearchForm("search."
                + con.getServiceName());
        Form answerForm = searchForm.createAnswerForm();
        answerForm.setAnswer("Username", true);
        answerForm.setAnswer("Name", true);
        answerForm.setAnswer("search", userName);
        ReportedData data = userSearchManager.getSearchResults(answerForm,
                "search." + con.getServiceName());
        Iterator<ReportedData.Row> rows = data.getRows();
        while (rows.hasNext()) {
            XmppUser user = new XmppUser(null, null);
            ReportedData.Row row = rows.next();
            user.setUserName(row.getValues("Username").next().toString());
            user.setName(row.getValues("Name").next().toString());
            list.add(user);
        }
    } catch (XMPPException e) {
        SLog.e(tag, Log.getStackTraceString(e));
    }
    return list;
}
 
Example #2
Source File: XmppConnection.java    From weixin with Apache License 2.0 5 votes vote down vote up
/**
 * 查询用户
 * 
 * @param userName
 * @return
 * @throws XMPPException
 */
public List<HashMap<String, String>> searchUsers(String userName) {
	if (getConnection() == null)
		return null;
	HashMap<String, String> user = null;
	List<HashMap<String, String>> results = new ArrayList<HashMap<String, String>>();
	try {
		new ServiceDiscoveryManager(getConnection());

		UserSearchManager usm = new UserSearchManager(getConnection());

		Form searchForm = usm.getSearchForm(getConnection().getServiceName());
		Form answerForm = searchForm.createAnswerForm();
		answerForm.setAnswer("userAccount", true);
		answerForm.setAnswer("userPhote", userName);
		ReportedData data = usm.getSearchResults(answerForm, "search" + getConnection().getServiceName());

		Iterator<Row> it = data.getRows();
		Row row = null;
		while (it.hasNext()) {
			user = new HashMap<String, String>();
			row = it.next();
			user.put("userAccount", row.getValues("userAccount").next().toString());
			user.put("userPhote", row.getValues("userPhote").next().toString());
			results.add(user);
			// 若存在,则有返回,UserName一定非空,其他两个若是有设,一定非空
		}
	} catch (XMPPException e) {
		e.printStackTrace();
	}
	return results;
}
 
Example #3
Source File: FriendAddActivity.java    From yiim_v2 with GNU General Public License v2.0 4 votes vote down vote up
private void searchEjaaberd() {
	// 此处一定要加上 search.
	try {
		Connection connection = getXmppBinder().getXmppConnection();
		UserSearchManager search = new UserSearchManager(connection);
		Form searchForm = search.getSearchForm("vjud."
				+ connection.getServiceName());
		Form answerForm = searchForm.createAnswerForm();
		// answerForm.setAnswer("nick", mSearchEditText.getText()
		// .toString().trim() + "*");
		answerForm.setAnswer("user", mSearchEditText.getText().toString()
				.trim() + "*");
		// answerForm.setAnswer("search", mSearchEditText.getText()
		// .toString().trim());
		ReportedData data = search.getSearchResults(answerForm, "vjud."
				+ connection.getServiceName());
		Iterator<Row> it = data.getRows();
		Row row = null;

		ArrayList<FriendAddModel> list = new ArrayList<FriendAddModel>();
		while (it.hasNext()) {
			row = it.next();
			String userId = StringUtils.escapeUserResource(row
					.getValues("jid").next().toString());
			FriendAddModel model = new FriendAddModel(userId);
			model.setMsg(userId);
			String nick = row.getValues("nick").next().toString();
			if (!YiUtils.isStringInvalid(nick)) {
				model.setName(nick);
			}

			XmppVcard vCard = new XmppVcard(getXmppBinder()
					.getServiceContext());
			vCard.load(connection, model.getMsg());

			// 加载用户的个性签名
			String sign = vCard.getSign();
			if (sign != null && sign.length() > 0) {
				if (model.getSubMsg().length() > 1) {
					sign = ' ' + sign;
				}
				model.setSubMsg(model.getSubMsg() + sign);
			}
			list.add(model);
		}
		Message message = getHandler().obtainMessage(MSG_ON_SEARCH_SUCCESS,
				list);
		message.sendToTarget();
	} catch (Exception e) {
		// TODO: handle exception
		e.printStackTrace();
	}
}
 
Example #4
Source File: FriendAddActivity.java    From yiim_v2 with GNU General Public License v2.0 4 votes vote down vote up
private void searchOpenfire() {
	// 此处一定要加上 search.
	try {
		Connection connection = getXmppBinder().getXmppConnection();
		UserSearchManager search = new UserSearchManager(connection);
		Form searchForm = search.getSearchForm("search."
				+ connection.getServiceName());
		Form answerForm = searchForm.createAnswerForm();
		answerForm.setAnswer("Username", true);
		answerForm.setAnswer("search", mSearchEditText.getText().toString()
				.trim());
		ReportedData data = search.getSearchResults(answerForm, "search."
				+ connection.getServiceName());
		Iterator<Row> it = data.getRows();
		Row row = null;

		ArrayList<FriendAddModel> list = new ArrayList<FriendAddModel>();
		while (it.hasNext()) {
			row = it.next();
			String userId = row.getValues("Username").next().toString()
					+ "@" + XmppConnectionUtils.getXmppHost();
			FriendAddModel model = new FriendAddModel(userId);
			model.setMsg(userId);
			model.setName(row.getValues("Name").next().toString());

			XmppVcard vCard = new XmppVcard(getXmppBinder()
					.getServiceContext());
			vCard.load(connection, userId);

			// 加载用户的个性签名
			String sign = vCard.getSign();
			if (sign != null && sign.length() > 0) {
				if (model.getSubMsg().length() > 1) {
					sign = ' ' + sign;
				}
				model.setSubMsg(model.getSubMsg() + sign);
			}
			list.add(model);
		}
		Message message = getHandler().obtainMessage(MSG_ON_SEARCH_SUCCESS,
				list);
		message.sendToTarget();
	} catch (Exception e) {
		// TODO: handle exception
		e.printStackTrace();
	}
}
 
Example #5
Source File: RosterDialog.java    From Spark with Apache License 2.0 4 votes vote down vote up
/**
    * Creates a Popupdialog above the Search Button displaying matching
    * Contacts
    * 
    * @param byname
    *            , the Searchname, atleast 5 Chars long
    * @param event
    *            , the MouseEvent which triggered it
    * @throws XMPPException
    * @throws InterruptedException 
    */
   public void searchForContact(String byname, MouseEvent event)
           throws XMPPException, SmackException.NotConnectedException, SmackException.NoResponseException, InterruptedException
   {

   	UIManager.put("OptionPane.okButtonText", Res.getString("ok"));
   	
if (byname.contains("@")) {
    byname = byname.substring(0, byname.indexOf("@"));
}

if (byname.length() <= 1) {
    JOptionPane.showMessageDialog(jidField,
	    Res.getString("message.search.input.short"),
	    Res.getString("title.notification"),
	    JOptionPane.ERROR_MESSAGE);

} else {

    JPopupMenu popup = new JPopupMenu();
    JMenuItem header = new JMenuItem(
	    Res.getString("group.search.results") + ":");
    header.setBackground(UIManager.getColor("List.selectionBackground"));
    header.setForeground(Color.red);
    popup.add(header);

    for (DomainBareJid search : _usersearchservice) {

	ReportedData data;
	UserSearchManager usersearchManager = new UserSearchManager(
		SparkManager.getConnection());

	Form f = usersearchManager.getSearchForm(search);

	Form answer = f.createAnswerForm();
	answer.setAnswer("Name", true);
	answer.setAnswer("Email", true);
	answer.setAnswer("Username", true);
	answer.setAnswer("search", byname);

	data = usersearchManager.getSearchResults(answer, search);

	ArrayList<String> columnnames = new ArrayList<>();
	for ( ReportedData.Column column : data.getColumns() ) {
	    String label = column.getLabel();
	    columnnames.add(label);
	}

	for (ReportedData.Row row : data.getRows() ) {
	    if (!row.getValues(columnnames.get(0)).isEmpty()) {
		String s = row.getValues(columnnames.get(0))
			.get(0).toString();
		final JMenuItem item = new JMenuItem(s);
		popup.add(item);
		item.addActionListener( e -> {
           jidField.setText(item.getText());
           nicknameField.setText(XmppStringUtils
               .parseLocalpart(item.getText()));
           } );
	    }

	}
    }

    if (popup.getComponentCount() > 2) {
	popup.setVisible(true);
	popup.show(_searchForName, event.getX(), event.getY());
    } else if (popup.getComponentCount() == 2) {
	jidField.setText(((JMenuItem) popup.getComponent(1)).getText());
	nicknameField.setText(XmppStringUtils.parseLocalpart(((JMenuItem) popup
		.getComponent(1)).getText()));
    } else {
	JOptionPane.showMessageDialog(jidField,
		Res.getString("message.no.results.found"),
		Res.getString("title.notification"),
		JOptionPane.ERROR_MESSAGE);
    }
}
   }
 
Example #6
Source File: UserSearchForm.java    From Spark with Apache License 2.0 3 votes vote down vote up
/**
 * Initializes the UserSearchForm with all available search services.
 *
 * @param searchServices a Collection of all search services found.
 */
public UserSearchForm(Collection<? extends CharSequence> searchServices) {
    setLayout(new GridBagLayout());

    cardPanel.setLayout(cardLayout);

    this.searchServices = searchServices;

    searchManager = new UserSearchManager(SparkManager.getConnection());

    addSearchServices();

    showService(getSearchService());
}