1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-09 09:09:38 +02:00

Re-work data form API

Apply builder pattern to form fields and replace getVariable() with
getFieldName(). Refer to the field name as "field name" instead of
"variable" everyone, just as XEP-0004 does.

Improve the high-level form API: introduce FilledForm and FillableForm
which perform stronger validation and consistency checks.

Also add FormFieldRegistry to enable processing of 'submit' forms
where the form field types are omitted.

Smack also now does omit the form field type declaration on 'submit'
type forms, as it is allowed by XEP-0004.
This commit is contained in:
Florian Schmaus 2020-05-13 20:14:41 +02:00
parent 3270c113c5
commit 77e26fc575
97 changed files with 3809 additions and 2427 deletions

View file

@ -278,8 +278,9 @@ public final class MamManager extends Manager {
if (dataForm != null) {
return dataForm;
}
dataForm = getNewMamForm();
dataForm.addFields(formFields.values());
DataForm.Builder dataFormBuilder = getNewMamForm();
dataFormBuilder.addFields(formFields.values());
dataForm = dataFormBuilder.build();
return dataForm;
}
@ -330,7 +331,7 @@ public final class MamManager extends Manager {
}
FormField formField = getWithFormField(withJid);
formFields.put(formField.getVariable(), formField);
formFields.put(formField.getFieldName(), formField);
return this;
}
@ -341,9 +342,9 @@ public final class MamManager extends Manager {
}
FormField formField = FormField.builder(FORM_FIELD_START)
.addValue(start)
.setValue(start)
.build();
formFields.put(formField.getVariable(), formField);
formFields.put(formField.getFieldName(), formField);
FormField endFormField = formFields.get(FORM_FIELD_END);
if (endFormField != null) {
@ -369,9 +370,9 @@ public final class MamManager extends Manager {
}
FormField formField = FormField.builder(FORM_FIELD_END)
.addValue(end)
.setValue(end)
.build();
formFields.put(formField.getVariable(), formField);
formFields.put(formField.getFieldName(), formField);
FormField startFormField = formFields.get(FORM_FIELD_START);
if (startFormField != null) {
@ -418,7 +419,7 @@ public final class MamManager extends Manager {
}
public Builder withAdditionalFormField(FormField formField) {
formFields.put(formField.getVariable(), formField);
formFields.put(formField.getFieldName(), formField);
return this;
}
@ -483,7 +484,7 @@ public final class MamManager extends Manager {
private static FormField getWithFormField(Jid withJid) {
return FormField.builder(FORM_FIELD_WITH)
.addValue(withJid.toString())
.setValue(withJid.toString())
.build();
}
@ -718,9 +719,9 @@ public final class MamManager extends Manager {
throw new SmackException.FeatureNotSupportedException(ADVANCED_CONFIG_NODE, archiveAddress);
}
private static DataForm getNewMamForm() {
FormField field = FormField.hiddenFormType(MamElements.NAMESPACE);
DataForm form = new DataForm(DataForm.Type.submit);
private static DataForm.Builder getNewMamForm() {
FormField field = FormField.buildHiddenFormType(MamElements.NAMESPACE);
DataForm.Builder form = DataForm.builder();
form.addField(field);
return form;
}

View file

@ -24,6 +24,7 @@ import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smackx.pubsub.packet.PubSub;
import org.jivesoftware.smackx.xdata.FormField;
import org.jivesoftware.smackx.xdata.TextSingleFormField;
import org.jivesoftware.smackx.xdata.packet.DataForm;
import org.jxmpp.jid.Jid;
@ -98,24 +99,23 @@ public class EnablePushNotificationsIQ extends IQ {
xml.rightAngleBracket();
if (publishOptions != null) {
DataForm dataForm = new DataForm(DataForm.Type.submit);
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.Builder formTypeField = FormField.builder("FORM_TYPE");
formTypeField.addValue(PubSub.NAMESPACE + "#publish-options");
dataForm.addField(formTypeField.build());
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();
FormField.Builder field = FormField.builder(pairVariableValue.getKey());
field.addValue(pairVariableValue.getValue());
TextSingleFormField.Builder field = FormField.builder(pairVariableValue.getKey());
field.setValue(pairVariableValue.getValue());
dataForm.addField(field.build());
}
xml.append(dataForm);
xml.append(dataForm.build());
}
return xml;

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2016 Fernando Ramirez, 2018 Florian Schmaus
* Copyright 2016 Fernando Ramirez, 2018-2020 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -34,7 +34,7 @@ import org.jxmpp.util.XmppDateTime;
public class FiltersTest extends MamTest {
private static String getMamXMemberWith(List<String> fieldsNames, List<? extends CharSequence> fieldsValues) {
String xml = "<x xmlns='jabber:x:data' type='submit'>" + "<field var='FORM_TYPE' type='hidden'>" + "<value>"
String xml = "<x xmlns='jabber:x:data' type='submit'>" + "<field var='FORM_TYPE'>" + "<value>"
+ MamElements.NAMESPACE + "</value>" + "</field>";
for (int i = 0; i < fieldsNames.size() && i < fieldsValues.size(); i++) {

View file

@ -85,7 +85,7 @@ public class MamQueryIQProviderTest {
assertEquals(fields2.get(1).getType(), FormField.Type.jid_single);
assertEquals(fields2.get(2).getType(), FormField.Type.text_single);
assertEquals(fields2.get(2).getValues(), new ArrayList<>());
assertEquals(fields2.get(4).getVariable(), "urn:example:xmpp:free-text-search");
assertEquals(fields2.get(4).getFieldName(), "urn:example:xmpp:free-text-search");
}
}

View file

@ -49,7 +49,8 @@ public class MamTest extends SmackTestSuite {
IllegalArgumentException, InvocationTargetException {
Method methodGetNewMamForm = MamManager.class.getDeclaredMethod("getNewMamForm");
methodGetNewMamForm.setAccessible(true);
return (DataForm) methodGetNewMamForm.invoke(mamManager);
DataForm.Builder dataFormBuilder = (DataForm.Builder) methodGetNewMamForm.invoke(mamManager);
return dataFormBuilder.build();
}
}

View file

@ -30,7 +30,7 @@ import org.junit.jupiter.api.Test;
public class PagingTest extends MamTest {
private static final String pagingStanza = "<iq id='sarasa' type='set'>" + "<query xmlns='urn:xmpp:mam:1' queryid='testid'>"
+ "<x xmlns='jabber:x:data' type='submit'>" + "<field var='FORM_TYPE' type='hidden'>"
+ "<x xmlns='jabber:x:data' type='submit'>" + "<field var='FORM_TYPE'>"
+ "<value>urn:xmpp:mam:1</value>" + "</field>" + "</x>" + "<set xmlns='http://jabber.org/protocol/rsm'>"
+ "<max>10</max>" + "</set>" + "</query>" + "</iq>";

View file

@ -41,7 +41,7 @@ import org.jxmpp.jid.impl.JidCreate;
public class QueryArchiveTest extends MamTest {
private static final String mamSimpleQueryIQ = "<iq id='sarasa' type='set'>" + "<query xmlns='urn:xmpp:mam:1' queryid='testid'>"
+ "<x xmlns='jabber:x:data' type='submit'>" + "<field var='FORM_TYPE' type='hidden'>" + "<value>"
+ "<x xmlns='jabber:x:data' type='submit'>" + "<field var='FORM_TYPE'>" + "<value>"
+ MamElements.NAMESPACE + "</value>" + "</field>" + "</x>" + "</query>" + "</iq>";
private static final String mamQueryResultExample = "<message to='hag66@shakespeare.lit/pda' from='coven@chat.shakespeare.lit' id='iasd207'>"

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2016 Fernando Ramirez, 2018-2019 Florian Schmaus
* Copyright 2016 Fernando Ramirez, 2018-2020 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -31,7 +31,7 @@ import org.junit.jupiter.api.Test;
public class ResultsLimitTest extends MamTest {
private static final String resultsLimitStanza = "<iq id='sarasa' type='set'>" + "<query xmlns='urn:xmpp:mam:1' queryid='testid'>"
+ "<x xmlns='jabber:x:data' type='submit'>" + "<field var='FORM_TYPE' type='hidden'>" + "<value>"
+ "<x xmlns='jabber:x:data' type='submit'>" + "<field var='FORM_TYPE'>" + "<value>"
+ MamElements.NAMESPACE + "</value>" + "</field>" + "</x>" + "<set xmlns='http://jabber.org/protocol/rsm'>"
+ "<max>10</max>" + "</set>" + "</query>" + "</iq>";

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2016 Fernando Ramirez, 2018-2019 Florian Schmaus
* Copyright 2016 Fernando Ramirez, 2018-2020 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -28,16 +28,17 @@ import org.jivesoftware.smackx.xdata.FormField;
import org.jivesoftware.smackx.xdata.packet.DataForm;
import org.junit.jupiter.api.Test;
import org.jxmpp.jid.JidTestUtil;
public class RetrieveFormFieldsTest extends MamTest {
private static final String retrieveFormFieldStanza = "<iq id='sarasa' type='get'>" + "<query xmlns='" + MamElements.NAMESPACE
+ "' queryid='testid'></query>" + "</iq>";
private static final String additionalFieldsStanza = "<x xmlns='jabber:x:data' type='submit'>" + "<field var='FORM_TYPE' type='hidden'>"
private static final String additionalFieldsStanza = "<x xmlns='jabber:x:data' type='submit'>" + "<field var='FORM_TYPE'>"
+ "<value>" + MamElements.NAMESPACE + "</value>" + "</field>"
+ "<field var='urn:example:xmpp:free-text-search'>" + "<value>Hi</value>" + "</field>"
+ "<field var='urn:example:xmpp:stanza-content' type='jid-single'>" + "<value>Hi2</value>" + "</field>"
+ "<field var='urn:example:xmpp:stanza-content'>" + "<value>one@exampleone.org</value>" + "</field>"
+ "</x>";
@Test
@ -51,13 +52,11 @@ public class RetrieveFormFieldsTest extends MamTest {
@Test
public void checkAddAdditionalFieldsStanza() throws Exception {
FormField field1 = FormField.builder("urn:example:xmpp:free-text-search")
.setType(FormField.Type.text_single)
.addValue("Hi")
.setValue("Hi")
.build();
FormField field2 = FormField.builder("urn:example:xmpp:stanza-content")
.setType(FormField.Type.jid_single)
.addValue("Hi2")
FormField field2 = FormField.jidSingleBuilder("urn:example:xmpp:stanza-content")
.setValue(JidTestUtil.BARE_JID_1)
.build();
MamQueryArgs mamQueryArgs = MamQueryArgs.builder()