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()); }
/** * Creates a node with specified configuration. * * Note: This is the only way to create a collection node. * * @param name The name of the node, which must be unique within the * pubsub service * @param config The configuration for the node * @return The node that was created * @exception XMPPException */ public Node createNode(String name, Form config) throws XMPPException { PubSub request = createPubsubPacket(to, Type.SET, new NodeExtension(PubSubElementType.CREATE, name)); boolean isLeafNode = true; if (config != null) { request.addExtension(new FormNode(FormNodeType.CONFIGURE, config)); FormField nodeTypeField = config.getField(ConfigureNodeFields.node_type.getFieldName()); if (nodeTypeField != null) isLeafNode = nodeTypeField.getValues().next().equals(NodeType.leaf.toString()); } // Errors will cause exceptions in getReply, so it only returns // on success. sendPubsubPacket(con, to, Type.SET, request); Node newNode = isLeafNode ? new LeafNode(con, name) : new CollectionNode(con, name); newNode.setTo(to); nodeMap.put(newNode.getId(), newNode); return newNode; }
private FormField.Option parseOption(XmlPullParser parser) throws Exception { boolean done = false; FormField.Option option = null; String label = parser.getAttributeValue("", "label"); while (!done) { int eventType = parser.next(); if (eventType == XmlPullParser.START_TAG) { if (parser.getName().equals("value")) { option = new FormField.Option(label, parser.nextText()); } } else if (eventType == XmlPullParser.END_TAG) { if (parser.getName().equals("option")) { done = true; } } } return option; }
private String getItemsToSearch() { StringBuilder buf = new StringBuilder(); if (form == null) { form = Form.getFormFrom(this); } if (form == null) { return ""; } Iterator<FormField> fields = form.getFields(); while (fields.hasNext()) { FormField field = fields.next(); String name = field.getVariable(); String value = getSingleValue(field); if (value.trim().length() > 0) { buf.append("<").append(name).append(">").append(value).append("</").append(name).append(">"); } } return buf.toString(); }
/** * Creates the initiation acceptance packet to forward to the stream * initiator. * * @param streamInitiationOffer The offer from the stream initiator to connect for a stream. * @param namespaces The namespace that relates to the accepted means of transfer. * @return The response to be forwarded to the initiator. */ public StreamInitiation createInitiationAccept( StreamInitiation streamInitiationOffer, String[] namespaces) { StreamInitiation response = new StreamInitiation(); response.setTo(streamInitiationOffer.getFrom()); response.setFrom(streamInitiationOffer.getTo()); response.setType(IQ.Type.RESULT); response.setPacketID(streamInitiationOffer.getPacketID()); DataForm form = new DataForm(Form.TYPE_SUBMIT); FormField field = new FormField( FileTransferNegotiator.STREAM_DATA_FIELD_NAME); for (String namespace : namespaces) { field.addValue(namespace); } form.addField(field); response.setFeatureNegotiationForm(form); return response; }
RoomInfo(DiscoverInfo info) { super(); this.room = info.getFrom(); // Get the information based on the discovered features this.membersOnly = info.containsFeature("muc_membersonly"); this.moderated = info.containsFeature("muc_moderated"); this.nonanonymous = info.containsFeature("muc_nonanonymous"); this.passwordProtected = info.containsFeature("muc_passwordprotected"); this.persistent = info.containsFeature("muc_persistent"); // Get the information based on the discovered extended information Form form = Form.getFormFrom(info); if (form != null) { FormField descField = form.getField("muc#roominfo_description"); this.description = ( descField == null || !(descField.getValues().hasNext()) )? "" : descField.getValues().next(); FormField subjField = form.getField("muc#roominfo_subject"); this.subject = ( subjField == null || !(subjField.getValues().hasNext()) ) ? "" : subjField.getValues().next(); FormField occCountField = form.getField("muc#roominfo_occupants"); this.occupantsCount = occCountField == null ? -1 : Integer.parseInt(occCountField.getValues() .next()); } }
private DataForm.Item parseItem(XmlPullParser parser) throws Exception { boolean done = false; List<FormField> fields = new ArrayList<FormField>(); while (!done) { int eventType = parser.next(); if (eventType == XmlPullParser.START_TAG) { if (parser.getName().equals("field")) { fields.add(parseField(parser)); } } else if (eventType == XmlPullParser.END_TAG) { if (parser.getName().equals("item")) { done = true; } } } return new DataForm.Item(fields); }
/** * * @param info * @return true if the packet extensions is ill-formed */ protected static boolean verifyPacketExtensions(DiscoverInfo info) { List<FormField> foundFormTypes = new LinkedList<FormField>(); for (Iterator<PacketExtension> i = info.getExtensions().iterator(); i.hasNext();) { PacketExtension pe = i.next(); if (pe.getNamespace().equals(Form.NAMESPACE)) { DataForm df = (DataForm) pe; for (Iterator<FormField> it = df.getFields(); it.hasNext();) { FormField f = it.next(); if (f.getVariable().equals("FORM_TYPE")) { for (FormField fft : foundFormTypes) { if (f.equals(fft)) return true; } foundFormTypes.add(f); } } } } return false; }
/** * 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()); } }
@Override public String toString() { StringBuilder result = new StringBuilder(getClass().getName() + " Content ["); Iterator<FormField> fields = getFields(); while (fields.hasNext()) { FormField formField = fields.next(); result.append('('); result.append(formField.getVariable()); result.append(':'); Iterator<String> values = formField.getValues(); StringBuilder valuesBuilder = new StringBuilder(); while (values.hasNext()) { if (valuesBuilder.length() > 0) result.append(','); String value = (String)values.next(); valuesBuilder.append(value); } if (valuesBuilder.length() == 0) valuesBuilder.append("NOT SET"); result.append(valuesBuilder); result.append(')'); } result.append(']'); return result.toString(); }
private void addField(ConfigureNodeFields nodeField, String type) { String fieldName = nodeField.getFieldName(); if (getField(fieldName) == null) { FormField field = new FormField(fieldName); field.setType(type); addField(field); } }
/** * Sets the list of {@link PresenceState} for which an entity wants * to receive notifications. * * @param stateValues The list of states */ public void setShowValues(Collection<PresenceState> stateValues) { ArrayList<String> values = new ArrayList<String>(stateValues.size()); for (PresenceState state : stateValues) { values.add(state.toString()); } addField(SubscribeOptionFields.show_values, FormField.TYPE_LIST_MULTI); setAnswer(SubscribeOptionFields.show_values.getFieldName(), values); }
private void addField(SubscribeOptionFields nodeField, String type) { String fieldName = nodeField.getFieldName(); if (getField(fieldName) == null) { FormField field = new FormField(fieldName); field.setType(type); addField(field); } }
public String toXML() { StringBuilder buf = new StringBuilder(); buf.append("<item>"); // Loop through all the form items and append them to the string buffer for (Iterator<FormField> i = getFields(); i.hasNext();) { FormField field = i.next(); buf.append(field.toXML()); } buf.append("</item>"); return buf.toString(); }
private static String getSingleValue(FormField formField) { Iterator<String> values = formField.getValues(); while (values.hasNext()) { return values.next(); } return ""; }
private FormField getStreamMethodField(DataForm form) { FormField field = null; for (Iterator<FormField> it = form.getFields(); it.hasNext();) { field = it.next(); if (field.getVariable().equals(STREAM_DATA_FIELD_NAME)) { break; } field = null; } return field; }
private StreamNegotiator getNegotiator(final FormField field) throws XMPPException { String variable; boolean isByteStream = false; boolean isIBB = false; for (Iterator<FormField.Option> it = field.getOptions(); it.hasNext();) { variable = it.next().getValue(); if (variable.equals(Socks5BytestreamManager.NAMESPACE) && !IBB_ONLY) { isByteStream = true; } else if (variable.equals(InBandBytestreamManager.NAMESPACE)) { isIBB = true; } } if (!isByteStream && !isIBB) { XMPPError error = new XMPPError(XMPPError.Condition.bad_request, "No acceptable transfer mechanism"); throw new XMPPException(error.getMessage(), error); } if (isByteStream && isIBB && field.getType().equals(FormField.TYPE_LIST_MULTI)) { return new FaultTolerantNegotiator(connection, byteStreamTransferManager, inbandTransferManager); } else if (isByteStream) { return byteStreamTransferManager; } else { return inbandTransferManager; } }
private StreamNegotiator getOutgoingNegotiator(final FormField field) throws XMPPException { String variable; boolean isByteStream = false; boolean isIBB = false; for (Iterator<String> it = field.getValues(); it.hasNext();) { variable = it.next(); if (variable.equals(Socks5BytestreamManager.NAMESPACE) && !IBB_ONLY) { isByteStream = true; } else if (variable.equals(InBandBytestreamManager.NAMESPACE)) { isIBB = true; } } if (!isByteStream && !isIBB) { XMPPError error = new XMPPError(XMPPError.Condition.bad_request, "No acceptable transfer mechanism"); throw new XMPPException(error.getMessage(), error); } if (isByteStream && isIBB) { return new FaultTolerantNegotiator(connection, byteStreamTransferManager, inbandTransferManager); } else if (isByteStream) { return byteStreamTransferManager; } else { return inbandTransferManager; } }
private DataForm createDefaultInitiationForm() { DataForm form = new DataForm(Form.TYPE_FORM); FormField field = new FormField(STREAM_DATA_FIELD_NAME); field.setType(FormField.TYPE_LIST_MULTI); if (!IBB_ONLY) { field.addOption(new FormField.Option(Socks5BytestreamManager.NAMESPACE)); } field.addOption(new FormField.Option(InBandBytestreamManager.NAMESPACE)); form.addField(field); return form; }
private FormField parseField(XmlPullParser parser) throws Exception { boolean done = false; FormField formField = new FormField(parser.getAttributeValue("", "var")); formField.setLabel(parser.getAttributeValue("", "label")); formField.setType(parser.getAttributeValue("", "type")); while (!done) { int eventType = parser.next(); if (eventType == XmlPullParser.START_TAG) { if (parser.getName().equals("desc")) { formField.setDescription(parser.nextText()); } else if (parser.getName().equals("value")) { formField.addValue(parser.nextText()); } else if (parser.getName().equals("required")) { formField.setRequired(true); } else if (parser.getName().equals("option")) { formField.addOption(parseOption(parser)); } } else if (eventType == XmlPullParser.END_TAG) { if (parser.getName().equals("field")) { done = true; } } } return formField; }
public String toXML() { StringBuilder buf = new StringBuilder(); buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append( "\" type=\"" + getType() +"\">"); if (getTitle() != null) { buf.append("<title>").append(getTitle()).append("</title>"); } for (Iterator<String> it=getInstructions(); it.hasNext();) { buf.append("<instructions>").append(it.next()).append("</instructions>"); } // Append the list of fields returned from a search if (getReportedData() != null) { buf.append(getReportedData().toXML()); } // Loop through all the items returned from a search and append them to the string buffer for (Iterator<Item> i = getItems(); i.hasNext();) { Item item = i.next(); buf.append(item.toXML()); } // Loop through all the form fields and append them to the string buffer for (Iterator<FormField> i = getFields(); i.hasNext();) { FormField field = i.next(); buf.append(field.toXML()); } buf.append("</").append(getElementName()).append(">"); return buf.toString(); }
public String toXML() { StringBuilder buf = new StringBuilder(); buf.append("<reported>"); // Loop through all the form items and append them to the string buffer for (Iterator i = getFields(); i.hasNext();) { FormField field = (FormField) i.next(); buf.append(field.toXML()); } buf.append("</reported>"); return buf.toString(); }
/** * Sets the policy that determines who may associate children with the node. * * @param policy The policy being set */ public void setChildrenAssociationPolicy(ChildrenAssociationPolicy policy) { addField(ConfigureNodeFields.children_association_policy, FormField.TYPE_LIST_SINGLE); List<String> values = new ArrayList<String>(1); values.add(policy.toString()); setAnswer(ConfigureNodeFields.children_association_policy.getFieldName(), values); }
private StreamNegotiator getNegotiator(final FormField field) throws XMPPException { String variable; boolean isByteStream = false; boolean isIBB = false; for (Iterator<FormField.Option> it = field.getOptions(); it.hasNext();) { variable = it.next().getValue(); if (variable.equals(Socks5BytestreamManager.NAMESPACE) && !IBB_ONLY) { isByteStream = true; } else if (variable.equals(InBandBytestreamManager.NAMESPACE)) { isIBB = true; } } if (!isByteStream && !isIBB) { XMPPError error = new XMPPError(XMPPError.Condition.bad_request, "No acceptable transfer mechanism"); throw new XMPPException(error.getMessage(), error); } //if (isByteStream && isIBB && field.getType().equals(FormField.TYPE_LIST_MULTI)) { if (isByteStream && isIBB) { return new FaultTolerantNegotiator(connection, byteStreamTransferManager, inbandTransferManager); } else if (isByteStream) { return byteStreamTransferManager; } else { return inbandTransferManager; } }
public String toXML() { StringBuilder buf = new StringBuilder(); buf.append("<reported>"); // Loop through all the form items and append them to the string buffer for (Iterator<FormField> i = getFields(); i.hasNext();) { FormField field = i.next(); buf.append(field.toXML()); } buf.append("</reported>"); return buf.toString(); }
/** * 创建聊天室 * * @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(); }
public ReportedData(List<FormField> fields) { this.fields = fields; }
/** * Set the list of child node ids that are associated with a collection node. * * @param children */ public void setChildren(List<String> children) { addField(ConfigureNodeFields.children, FormField.TYPE_TEXT_MULTI); setAnswer(ConfigureNodeFields.children.getFieldName(), children); }
/** * Sets the policy that determines who may associate children with the node. * * @param policy The policy being set */ public void setChildrenAssociationPolicy(ChildrenAssociationPolicy policy) { addField(ConfigureNodeFields.children_association_policy, FormField.TYPE_LIST_SINGLE); setAnswer(ConfigureNodeFields.children_association_policy.getFieldName(), policy.toString()); }
/** * Returns an Iterator for the fields that are part of the form. * * @return an Iterator for the fields that are part of the form. */ public Iterator<FormField> getFields() { synchronized (fields) { return Collections.unmodifiableList(new ArrayList<FormField>(fields)).iterator(); } }
/** * Sets the collection node which the node is affiliated with. * * @param collection The node id of the collection node */ public void setCollection(String collection) { addField(ConfigureNodeFields.collection, FormField.TYPE_TEXT_SINGLE); setAnswer(ConfigureNodeFields.collection.getFieldName(), collection); }