Java Code Examples for org.jivesoftware.smackx.Form#createAnswerForm()
The following examples show how to use
org.jivesoftware.smackx.Form#createAnswerForm() .
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 |
/** * 查找用户 * * @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: MultiUserChatTest.java From Smack with Apache License 2.0 | 6 votes |
private void makeRoomModerated() throws XMPPException { // User1 (which is the room owner) converts the instant room into a moderated room Form form = muc.getConfigurationForm(); Form answerForm = form.createAnswerForm(); answerForm.setAnswer("muc#roomconfig_moderatedroom", true); answerForm.setAnswer("muc#roomconfig_whois", Arrays.asList("moderators")); // Keep the room owner try { List<String> owners = new ArrayList<String>(); owners.add(getBareJID(0)); answerForm.setAnswer("muc#roomconfig_roomowners", owners); } catch (IllegalArgumentException e) { // Do nothing } muc.sendConfigurationForm(answerForm); }
Example 3
Source File: XmppConnection.java From weixin with Apache License 2.0 | 5 votes |
/** * 查询用户 * * @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 4
Source File: MultiUserChatCreationTest.java From Smack with Apache License 2.0 | 5 votes |
/** * Tests creating a new "Reserved Room". */ public void testCreateReservedRoom() { MultiUserChat muc = new MultiUserChat(getConnection(0), room); try { // Create the room muc.create("testbot1"); // Get the the room's configuration form Form form = muc.getConfigurationForm(); assertNotNull("No room configuration form", form); // Create a new form to submit based on the original form Form submitForm = form.createAnswerForm(); // Add default answers to the form to submit for (Iterator<FormField> fields = form.getFields(); fields.hasNext();) { FormField field = fields.next(); if (!FormField.TYPE_HIDDEN.equals(field.getType()) && field.getVariable() != null) { // Sets the default value as the answer submitForm.setDefaultAnswer(field.getVariable()); } } List<String> owners = new ArrayList<String>(); owners.add(getBareJID(0)); submitForm.setAnswer("muc#roomconfig_roomowners", owners); // Update the new room's configuration muc.sendConfigurationForm(submitForm); // Destroy the new room muc.destroy("The room has almost no activity...", null); } catch (XMPPException e) { e.printStackTrace(); fail(e.getMessage()); } }
Example 5
Source File: FriendAddActivity.java From yiim_v2 with GNU General Public License v2.0 | 4 votes |
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 6
Source File: FriendAddActivity.java From yiim_v2 with GNU General Public License v2.0 | 4 votes |
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 7
Source File: XmppManager.java From weixin with Apache License 2.0 | 4 votes |
/** * 创建房间 * * @param roomName 房间名称 */ public void createRoom(String roomName) { if (!isConnected()) { return; } try { // 创建一个MultiUserChat MultiUserChat muc = new MultiUserChat(getConnection(), roomName + "@conference." + getConnection().getServiceName()); // 创建聊天室 muc.create(roomName); // roomName房间的名字 // 获得聊天室的配置表单 Form form = muc.getConfigurationForm(); // 根据原始表单创建一个要提交的新表单。 Form submitForm = form.createAnswerForm(); // 向要提交的表单添加默认答复 for (Iterator<FormField> fields = form.getFields(); fields.hasNext();) { FormField field = (FormField) fields.next(); if (!FormField.TYPE_HIDDEN.equals(field.getType()) && field.getVariable() != null) { // 设置默认值作为答复 submitForm.setDefaultAnswer(field.getVariable()); } } // 设置聊天室的新拥有者 List<String> owners = new ArrayList<String>(); owners.add(getConnection().getUser());// 用户JID submitForm.setAnswer("muc#roomconfig_roomowners", owners); // 设置聊天室是持久聊天室,即将要被保存下来 submitForm.setAnswer("muc#roomconfig_persistentroom", false); // 房间仅对成员开放 submitForm.setAnswer("muc#roomconfig_membersonly", false); // 允许占有者邀请其他人 submitForm.setAnswer("muc#roomconfig_allowinvites", true); // 进入是否需要密码 //submitForm.setAnswer("muc#roomconfig_passwordprotectedroom", true); // 设置进入密码 //submitForm.setAnswer("muc#roomconfig_roomsecret", "password"); // 能够发现占有者真实 JID 的角色 // submitForm.setAnswer("muc#roomconfig_whois", "anyone"); // 登录房间对话 submitForm.setAnswer("muc#roomconfig_enablelogging", true); // 仅允许注册的昵称登录 submitForm.setAnswer("x-muc#roomconfig_reservednick", true); // 允许使用者修改昵称 submitForm.setAnswer("x-muc#roomconfig_canchangenick", false); // 允许用户注册房间 submitForm.setAnswer("x-muc#roomconfig_registration", false); // 发送已完成的表单(有默认值)到服务器来配置聊天室 submitForm.setAnswer("muc#roomconfig_passwordprotectedroom", true); // 发送已完成的表单(有默认值)到服务器来配置聊天室 muc.sendConfigurationForm(submitForm); } catch (XMPPException e) { e.printStackTrace(); } }
Example 8
Source File: XMPPUtils.java From saros with GNU General Public License v2.0 | 2 votes |
/** * 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; } }