org.jivesoftware.smackx.FormField Java Examples

The following examples show how to use org.jivesoftware.smackx.FormField. 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: MultiUserChatTest.java    From Smack with Apache License 2.0 6 votes vote down vote up
protected void setUp() throws Exception {
    //SmackConfiguration.DEBUG = false;
    super.setUp();
    room = "fruta124@" + getMUCDomain();
    try {
        // User1 creates the room
        muc = new MultiUserChat(getConnection(0), room);
        muc.create("testbot");

        // User1 sends an empty room configuration form which indicates that we want
        // an instant room
        Form form = new Form(Form.TYPE_SUBMIT);
        FormField field = new FormField("muc#roomconfig_whois");
        field.setType("list-single");
        form.addField(field);
        form.setAnswer("muc#roomconfig_whois", Arrays.asList("moderators"));
        muc.sendConfigurationForm(form);
    }
    catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
 
Example #2
Source File: SubscriberUseCases.java    From Smack with Apache License 2.0 6 votes vote down vote up
public void testSubscribeConfigRequired() throws Exception
{
	ConfigureForm form = new ConfigureForm(FormType.submit);
	form.setAccessModel(AccessModel.open);

	// Openfire specific field - nothing in the spec yet
	FormField required = new FormField("pubsub#subscription_required");
	required.setType(FormField.TYPE_BOOLEAN);
	form.addField(required);
	form.setAnswer("pubsub#subscription_required", true);
	LeafNode node = (LeafNode)getManager().createNode("Pubnode" + System.currentTimeMillis(), form);

	Subscription sub = node.subscribe(getJid());

	assertEquals(getJid(), sub.getJid());
	assertNotNull(sub.getId());
	assertEquals(node.getId(), sub.getNode());
	assertEquals(true, sub.isConfigRequired());
}
 
Example #3
Source File: MultiUserChatCreationTest.java    From Smack with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #4
Source File: XSCHelper.java    From PracticeCode with Apache License 2.0 4 votes vote down vote up
/**
 * 创建聊天室
 *
 * @param password 登录密码
 * @param type     登陆类型: 0 - ProjCloud Other - Personal
 * @param roomName 房间名称
 * @param handler  处理信息的Handler
 */
public void createRoom(final String roomName, final int type, final String password, final Handler handler) {
    new Thread(){
        @Override
        public void run() {

            //如果无法连接服务器/超时,Handle Error
            if(getConnection() == null) {
                handler.sendEmptyMessage(ERROR);
                return;
            }

            //如果不是登录状态,Handle Failure
            if (!getConnection().isAuthenticated()) {
                handler.sendEmptyMessage(FAILURE);
                return;
            }

            try {
                String service = type == 0 ? "@cloud." : "@personal.";

                //写清房间地址,房产中心注册,开房,创建房间
                String address = roomName + service + getConnection().getServiceName();
                MultiUserChat room = new MultiUserChat(getConnection(), address);
                room.create(roomName);

                //装修房间配置信息:获取,配置,再提交
                Form originForm = room.getConfigurationForm();
                Form form = originForm.createAnswerForm();

                //---基础:当前域内项非隐藏并且非空(即有实际内容)就设为新表的域
                Iterator<FormField> iterator = originForm.getFields();
                while (iterator.hasNext()) {
                    FormField field = iterator.next();
                    if(!field.getType().equals(FormField.TYPE_HIDDEN)
                            && field.getVariable() != null) {
                        form.setDefaultAnswer(field.getVariable());
                    }
                }

                //---设置当前房间主人
                List<String> ownerList = new ArrayList<>();
                ownerList.add(getConnection().getUser());

                //---杂项
                if(!password.equals("")) {
                    form.setAnswer(ROOM_PSW_REQUIRED, true);  //要求密码
                    form.setAnswer(ROOM_PSW, password);  //设定密码
                }
                form.setAnswer(ROOM_KEEP_ALLOWED, true);  //永久房间
                form.setAnswer(ROOM_MEMBERS_ONLY, false);  //不禁止新入成员
                form.setAnswer(ROOM_INVITE_ALLOWED, true);  //允许邀请
                form.setAnswer(ROOM_LOG_ENABLED, true);  //开启日志
                form.setAnswer(ROOM_NICKNAME_ONLY, true);  //昵称登陆
                form.setAnswer(ROOM_SPECIAL_NICKNAME_ALLOWED, false);  //禁群内昵称
                form.setAnswer(ROOM_REGIST_NEW_ROOM_ALLOWED, false);  //禁开子房

                //发送表单更新房间信息
                room.sendConfigurationForm(form);
                handler.sendEmptyMessage(SUCCESS);
            } catch (XMPPException e) {
                handler.sendEmptyMessage(FAILURE);
                e.printStackTrace();
            }
        }
    }.start();
}
 
Example #5
Source File: XmppManager.java    From weixin with Apache License 2.0 4 votes vote down vote up
/**
 * 创建房间
 * 
 * @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 #6
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;
  }
}