use of org.jivesoftware.smackx.xdata.packet.DataForm in project Smack by igniterealtime.
the class EntityCapsManager method verifyPacketExtensions.
/**
* Verify that the given discovery info is not ill-formed.
*
* @param info the discovery info to verify.
* @return true if the stanza extensions is not ill-formed
*/
private static boolean verifyPacketExtensions(DiscoverInfo info) {
Set<String> foundFormTypes = new HashSet<>();
List<DataForm> dataForms = info.getExtensions(DataForm.class);
for (DataForm dataForm : dataForms) {
FormField formFieldTypeField = dataForm.getHiddenFormTypeField();
if (formFieldTypeField == null) {
continue;
}
String type = formFieldTypeField.getFirstValue();
boolean noDuplicate = foundFormTypes.add(type);
if (!noDuplicate) {
// Ill-formed extension: duplicate forms (by form field type string).
return false;
}
}
return true;
}
use of org.jivesoftware.smackx.xdata.packet.DataForm in project Smack by igniterealtime.
the class EnablePushNotificationsIQ method getIQChildElementBuilder.
@Override
protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
xml.attribute("jid", jid);
xml.attribute("node", node);
xml.rightAngleBracket();
if (publishOptions != null) {
DataForm.Builder dataForm = DataForm.builder();
// TODO: Shouldn't this use some potentially existing PubSub API? Also FORM_TYPE fields are usually of type
// 'hidden', but the examples in XEP-0357 do also not set the value to hidden and FORM_TYPE itself appears
// to be more convention than specification.
FormField formTypeField = FormField.buildHiddenFormType(PubSub.NAMESPACE + "#publish-options");
dataForm.addField(formTypeField);
Iterator<Map.Entry<String, String>> publishOptionsIterator = publishOptions.entrySet().iterator();
while (publishOptionsIterator.hasNext()) {
Map.Entry<String, String> pairVariableValue = publishOptionsIterator.next();
TextSingleFormField.Builder field = FormField.builder(pairVariableValue.getKey());
field.setValue(pairVariableValue.getValue());
dataForm.addField(field.build());
}
xml.append(dataForm.build());
}
return xml;
}
use of org.jivesoftware.smackx.xdata.packet.DataForm in project Smack by igniterealtime.
the class FormTest method testFilloutForm.
/**
* 1. Create a form to fill out and send it to the other user
* 2. Retrieve the form to fill out, complete it and return it to the requestor
* 3. Retrieve the completed form and check that everything is OK
*
* @throws InterruptedException if the calling thread was interrupted.
* @throws NotConnectedException if the XMPP connection is not connected.
*/
@SuppressWarnings("deprecation")
@SmackIntegrationTest
public void testFilloutForm() throws NotConnectedException, InterruptedException {
DataForm.Builder formToSend = DataForm.builder(DataForm.Type.form);
formToSend.setInstructions("Fill out this form to report your case.\nThe case will be created automatically.");
formToSend.setTitle("Case configurations");
formToSend.setFormType("https://igniterealtime.org/projects/smack/sinttest/form-test/1");
// Add a hidden variable
FormField field = FormField.hiddenBuilder("hidden_var").setValue("Some value for the hidden variable").build();
formToSend.addField(field);
// Add a fixed variable
field = FormField.fixedBuilder().setValue("Section 1: Case description").build();
formToSend.addField(field);
// Add a text-single variable
field = FormField.textSingleBuilder("name").setLabel("Enter a name for the case").build();
formToSend.addField(field);
// Add a text-multi variable
field = FormField.textMultiBuilder("description").setLabel("Enter a description").build();
formToSend.addField(field);
// Add a boolean variable
field = FormField.booleanBuilder("time").setLabel("Is this your first case?").build();
formToSend.addField(field);
// Add a text variable where an int value is expected
field = FormField.textSingleBuilder("age").setLabel("How old are you?").build();
formToSend.addField(field);
// Create the chats between the two participants
org.jivesoftware.smack.chat.Chat chat = org.jivesoftware.smack.chat.ChatManager.getInstanceFor(conOne).createChat(conTwo.getUser(), null);
StanzaCollector collector = conOne.createStanzaCollector(new ThreadFilter(chat.getThreadID()));
StanzaCollector collector2 = conTwo.createStanzaCollector(new ThreadFilter(chat.getThreadID()));
Message msg = StanzaBuilder.buildMessage().setBody("To enter a case please fill out this form and send it back to me").addExtension(formToSend.build()).build();
try {
// Send the message with the form to fill out
chat.sendMessage(msg);
// Get the message with the form to fill out
Message msg2 = collector2.nextResult();
assertNotNull(msg2, "Message not found");
// Retrieve the form to fill out
Form formToRespond = Form.from(msg2);
assertNotNull(formToRespond);
assertNotNull(formToRespond.getField("name"));
assertNotNull(formToRespond.getField("description"));
// Obtain the form to send with the replies
final FillableForm completedForm = formToRespond.getFillableForm();
assertNotNull(completedForm.getField("hidden_var"));
// Check that a field of type String does not accept booleans
assertThrows(IllegalArgumentException.class, () -> completedForm.setAnswer("name", true));
completedForm.setAnswer("name", "Credit card number invalid");
completedForm.setAnswer("description", "The ATM says that my credit card number is invalid. What's going on?");
completedForm.setAnswer("time", true);
completedForm.setAnswer("age", 20);
// Create a new message to send with the completed form
msg2 = StanzaBuilder.buildMessage().to(conOne.getUser().asBareJid()).setThread(msg.getThread()).ofType(Message.Type.chat).setBody("To enter a case please fill out this form and send it back to me").addExtension(completedForm.getDataFormToSubmit()).build();
// Send the message with the completed form
conTwo.sendStanza(msg2);
// Get the message with the completed form
Message msg3 = collector.nextResult();
assertNotNull(msg3, "Message not found");
// Retrieve the completed form
final DataForm completedForm2 = DataForm.from(msg3);
assertNotNull(completedForm2);
assertNotNull(completedForm2.getField("name"));
assertNotNull(completedForm2.getField("description"));
assertEquals(completedForm2.getField("name").getValues().get(0).toString(), "Credit card number invalid");
assertNotNull(completedForm2.getField("time"));
assertNotNull(completedForm2.getField("age"));
assertEquals("20", completedForm2.getField("age").getValues().get(0).toString(), "The age is bad");
} finally {
collector.cancel();
collector2.cancel();
}
}
use of org.jivesoftware.smackx.xdata.packet.DataForm in project Smack by igniterealtime.
the class MamQueryIQProvider method parse.
@Override
public MamQueryIQ parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
MamElementFactory elementFactory = MamElementFactory.forParser(parser);
DataForm dataForm = null;
String queryId = parser.getAttributeValue("", "queryid");
String node = parser.getAttributeValue("", "node");
outerloop: while (true) {
final XmlPullParser.Event eventType = parser.next();
switch(eventType) {
case START_ELEMENT:
final String name = parser.getName();
switch(name) {
case DataForm.ELEMENT:
dataForm = DataFormProvider.INSTANCE.parse(parser);
break;
}
break;
case END_ELEMENT:
if (parser.getDepth() == initialDepth) {
break outerloop;
}
break;
default:
// Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
break;
}
}
return elementFactory.newQueryIQ(queryId, node, dataForm);
}
use of org.jivesoftware.smackx.xdata.packet.DataForm in project Smack by igniterealtime.
the class FiltersTest method checkWithJidFilter.
@Test
public void checkWithJidFilter() throws Exception {
Jid jid = JidTestUtil.BARE_JID_1;
MamQueryArgs mamQueryArgs = MamQueryArgs.builder().limitResultsToJid(jid).build();
DataForm dataForm = mamQueryArgs.getDataForm(MamVersion.MAM2);
List<String> fields = new ArrayList<>();
fields.add("with");
List<CharSequence> values = new ArrayList<>();
values.add(jid);
assertEquals(getMamXMemberWith(fields, values), dataForm.toXML().toString());
}
Aggregations