/** * Returns the values a String. Note that you should use {@link #getValues()} whenever possible instead of this * method. * * @return a list of Strings representing the values * @see #getValues() * @since 4.3 */ public List<String> getValuesAsString() { List<CharSequence> valuesAsCharSequence = getValues(); List<String> res = new ArrayList<>(valuesAsCharSequence.size()); for (CharSequence value : valuesAsCharSequence) { res.add(value.toString()); } return res; }
@Override public String toString() { StringBuilder result = new StringBuilder(getClass().getName() + " Content ["); for (FormField formField : getFields()) { result.append('('); result.append(formField.getVariable()); result.append(':'); StringBuilder valuesBuilder = new StringBuilder(); for (CharSequence value : formField.getValues()) { if (valuesBuilder.length() > 0) result.append(','); valuesBuilder.append(value); } if (valuesBuilder.length() == 0) valuesBuilder.append("NOT SET"); result.append(valuesBuilder); result.append(')'); } result.append(']'); return result.toString(); }
/** * MAM query IQ constructor. * * @param queryId * @param node * @param dataForm */ public MamQueryIQ(String queryId, String node, DataForm dataForm) { super(ELEMENT, NAMESPACE); this.queryId = queryId; this.node = node; this.dataForm = dataForm; if (dataForm != null) { FormField field = dataForm.getHiddenFormTypeField(); if (field == null) { throw new IllegalArgumentException("If a data form is given it must posses a hidden form type field"); } if (!field.getValues().get(0).toString().equals(MamElements.NAMESPACE)) { throw new IllegalArgumentException( "Value of the hidden form type field must be '" + MamElements.NAMESPACE + "'"); } addExtension(dataForm); } }
private StreamNegotiator getOutgoingNegotiator(final FormField field) throws NoAcceptableTransferMechanisms { boolean isByteStream = false; boolean isIBB = false; for (CharSequence variable : field.getValues()) { String variableString = variable.toString(); if (variableString.equals(Bytestream.NAMESPACE) && !IBB_ONLY) { isByteStream = true; } else if (variableString.equals(DataPacketExtension.NAMESPACE)) { isIBB = true; } } if (!isByteStream && !isIBB) { throw new FileTransferException.NoAcceptableTransferMechanisms(); } if (isByteStream && isIBB) { return new FaultTolerantNegotiator(connection(), byteStreamTransferManager, inbandTransferManager); } else if (isByteStream) { return byteStreamTransferManager; } else { return inbandTransferManager; } }
/** * Sets the default value as the value of a given form's field. The field whose variable matches * the requested variable will be completed with its default value. If no field could be found * for the specified variable then an exception will be raised. * * @param variable the variable to complete with its default value. * @throws IllegalStateException if the form is not of type "submit". * @throws IllegalArgumentException if the form does not include the specified variable. */ public void setDefaultAnswer(String variable) { if (!isSubmitType()) { throw new IllegalStateException("Cannot set an answer if the form is not of type " + "\"submit\""); } FormField field = getField(variable); if (field != null) { // Clear the old values field.resetValues(); // Set the default value for (CharSequence value : field.getValues()) { field.addValue(value); } } else { throw new IllegalArgumentException("Couldn't find a field for the specified variable."); } }
/** * Returns a DataForm that serves to send this Form to the server. If the form is of type * submit, it may contain fields with no value. These fields will be removed since they only * exist to assist the user while editing/completing the form in a UI. * * @return the wrapped DataForm. */ public DataForm getDataFormToSend() { if (isSubmitType()) { // Create a new DataForm that contains only the answered fields DataForm dataFormToSend = new DataForm(getType()); for (FormField field : getFields()) { if (!field.getValues().isEmpty()) { dataFormToSend.addField(field); } } return dataFormToSend; } return dataForm; }
/** * Creates a new ReportedData based on the returned dataForm from a search *(namespace "jabber:iq:search"). * * @param dataForm the dataForm returned from a search (namespace "jabber:iq:search"). */ private ReportedData(DataForm dataForm) { // Add the columns to the report based on the reported data fields for (FormField field : dataForm.getReportedData().getFields()) { columns.add(new Column(field.getLabel(), field.getVariable(), field.getType())); } // Add the rows to the report based on the form's items for (Item item : dataForm.getItems()) { List<Field> fieldList = new ArrayList<>(columns.size()); for (FormField field : item.getFields()) { // The field is created with all the values of the data form's field List<CharSequence> values = new ArrayList<>(); values.addAll(field.getValues()); fieldList.add(new Field(field.getVariable(), values)); } rows.add(new Row(fieldList)); } // Set the report's title this.title = dataForm.getTitle(); }
if (form != null) { FormField descField = form.getField("muc#roominfo_description"); if (descField != null && !descField.getValues().isEmpty()) { if (subjField != null && !subjField.getValues().isEmpty()) { subject = subjField.getFirstValue(); if (occCountField != null && !occCountField.getValues().isEmpty()) { occupantsCount = Integer.parseInt(occCountField.getFirstValue()); if (maxhistoryfetchField != null && !maxhistoryfetchField.getValues().isEmpty()) { maxhistoryfetch = Integer.parseInt(maxhistoryfetchField.getFirstValue()); if (contactJidField != null && !contactJidField.getValues().isEmpty()) { List<CharSequence> contactJidValues = contactJidField.getValues(); contactJid = JidUtil.filterEntityBareJidList(JidUtil.jidSetFrom(contactJidValues)); if (langField != null && !langField.getValues().isEmpty()) { lang = langField.getFirstValue(); if (ldapgroupField != null && !ldapgroupField.getValues().isEmpty()) { ldapgroup = ldapgroupField.getFirstValue(); if (subjectmodField != null && !subjectmodField.getValues().isEmpty()) { String firstValue = subjectmodField.getFirstValue(); subjectmod = ("true".equals(firstValue) || "1".equals(firstValue)); if (urlField != null && !urlField.getValues().isEmpty()) { String urlString = urlField.getFirstValue();
/** * Creates a node with specified configuration. * * Note: This is the only way to create a collection node. * * @param nodeId 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 * @throws XMPPErrorException * @throws NoResponseException * @throws NotConnectedException * @throws InterruptedException */ public Node createNode(String nodeId, Form config) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { PubSub request = PubSub.createPubsubPacket(pubSubService, Type.set, new NodeExtension(PubSubElementType.CREATE, nodeId)); 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().get(0).toString().equals(NodeType.leaf.toString()); } // Errors will cause exceptions in getReply, so it only returns // on success. sendPubsubPacket(request); Node newNode = isLeafNode ? new LeafNode(this, nodeId) : new CollectionNode(this, nodeId); nodeMap.put(newNode.getId(), newNode); return newNode; }
values.addAll(field.getValues()); form.setAnswer(field.getVariable(), values);
List<CharSequence> ownerStrings = answerForm.getField(MUC_ROOMCONFIG_ROOMOWNERS).getValues(); owners = new ArrayList<>(ownerStrings.size()); JidUtil.jidsFrom(ownerStrings, owners, null);
@Test public void checkMamQueryIQProvider() throws Exception { // example 1 IQ iq1 = PacketParserUtils.parseStanza(exampleMamQueryIQ1); MamQueryIQ mamQueryIQ1 = (MamQueryIQ) iq1; Assert.assertEquals(mamQueryIQ1.getType(), Type.set); Assert.assertEquals(mamQueryIQ1.getQueryId(), "test"); DataForm dataForm1 = (DataForm) mamQueryIQ1.getExtension(DataForm.NAMESPACE); Assert.assertEquals(dataForm1.getType(), DataForm.Type.submit); List<FormField> fields1 = dataForm1.getFields(); Assert.assertEquals(fields1.get(0).getType(), FormField.Type.hidden); Assert.assertEquals(fields1.get(1).getType(), FormField.Type.text_single); Assert.assertEquals(fields1.get(1).getValues().get(0).toString(), "Where arth thou, my Juliet?"); Assert.assertEquals(fields1.get(2).getValues().get(0).toString(), "{http://jabber.org/protocol/mood}mood/lonely"); // example2 IQ iq2 = PacketParserUtils.parseStanza(exampleMamQueryIQ2); MamQueryIQ mamQueryIQ2 = (MamQueryIQ) iq2; Assert.assertEquals(mamQueryIQ2.getType(), Type.result); Assert.assertNull(mamQueryIQ2.getQueryId()); DataForm dataForm2 = (DataForm) mamQueryIQ2.getExtension(DataForm.NAMESPACE); Assert.assertEquals(dataForm2.getType(), DataForm.Type.form); List<FormField> fields2 = dataForm2.getFields(); Assert.assertEquals(fields2.get(0).getValues().get(0).toString(), "urn:xmpp:mam:1"); Assert.assertTrue(fields2.get(0).getValues().size() == 1); Assert.assertEquals(fields2.get(1).getType(), FormField.Type.jid_single); Assert.assertEquals(fields2.get(2).getType(), FormField.Type.text_single); Assert.assertEquals(fields2.get(2).getValues(), new ArrayList<>()); Assert.assertEquals(fields2.get(4).getVariable(), "urn:example:xmpp:free-text-search"); }
@Test public void checkPageQueryStanza() throws Exception { DataForm dataForm = getNewMamForm(); int max = 10; RSMSet rsmSet = new RSMSet(max); MamQueryIQ mamQueryIQ = new MamQueryIQ(queryId, dataForm); mamQueryIQ.setStanzaId("sarasa"); mamQueryIQ.setType(IQ.Type.set); mamQueryIQ.addExtension(rsmSet); Assert.assertEquals(mamQueryIQ.getDataForm(), dataForm); Assert.assertEquals(mamQueryIQ.getDataForm().getFields().get(0).getValues().get(0).toString(), "urn:xmpp:mam:1"); Assert.assertEquals(mamQueryIQ.toXML(StreamOpen.CLIENT_NAMESPACE).toString(), pagingStanza); }
@Override public XmlStringBuilder toXML(String enclosingNamespace) { XmlStringBuilder buf = new XmlStringBuilder(this); // Add attributes buf.optAttribute("label", getLabel()); buf.optAttribute("var", getVariable()); buf.optAttribute("type", getType()); buf.rightAngleBracket(); // Add elements buf.optElement("desc", getDescription()); buf.condEmptyElement(isRequired(), "required"); // Loop through all the values and append them to the string buffer for (CharSequence value : getValues()) { buf.element("value", value); } // Loop through all the values and append them to the string buffer for (Option option : getOptions()) { buf.append(option.toXML(null)); } buf.optElement(validateElement); buf.closeElement(this); return buf; }
assertNotNull(completedForm.getField("description")); assertEquals( completedForm.getField("name").getValues().get(0).toString(), "Credit card number invalid"); assertNotNull(completedForm.getField("time")); assertNotNull(completedForm.getField("age")); assertEquals("The age is bad", "20", completedForm.getField("age").getValues().get(0).toString());
/** * MAM query IQ constructor. * * @param queryId * @param node * @param dataForm */ public MamQueryIQ(String queryId, String node, DataForm dataForm) { super(ELEMENT, NAMESPACE); this.queryId = queryId; this.node = node; this.dataForm = dataForm; if (dataForm != null) { FormField field = dataForm.getHiddenFormTypeField(); if (field == null) { throw new IllegalArgumentException("If a data form is given it must posses a hidden form type field"); } if (!field.getValues().get(0).equals(MamElements.NAMESPACE)) { throw new IllegalArgumentException( "Value of the hidden form type field must be '" + MamElements.NAMESPACE + "'"); } addExtension(dataForm); } }
/** * Retrieve the date (in yyyyMMdd) format of the time the room was created. * * @param roomJID the jid of the room. * @return the formatted date. * @throws Exception throws an exception if we are unable to retrieve the date. */ public static String getCreationDate(EntityBareJid roomJID) throws Exception { ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(SparkManager.getConnection()); final DateFormat dateFormatter = new SimpleDateFormat("yyyyMMdd'T'HH:mm:ss"); DiscoverInfo infoResult = discoManager.discoverInfo(roomJID); DataForm dataForm = infoResult.getExtension("x", "jabber:x:data"); if (dataForm == null) { return "Not available"; } String creationDate = ""; for ( final FormField field : dataForm.getFields() ) { String label = field.getLabel(); if (label != null && "Creation date".equalsIgnoreCase(label)) { for ( CharSequence value : field.getValues() ) { creationDate = value.toString(); Date date = dateFormatter.parse(creationDate); creationDate = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.MEDIUM).format(date); } } } return creationDate; }