From 070aa8e28eb0a9ddf94d001ec954b560f1394dbe Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Fri, 29 Jan 2021 20:13:54 +0100 Subject: [PATCH 1/6] [DataFormProvider] Retrieve field types from if possible Fixes SMACK-902. --- .../smackx/xdata/packet/DataForm.java | 16 ++- .../xdata/provider/DataFormProvider.java | 35 +++++- .../xdata/provider/DataFormProviderTest.java | 117 ++++++++++++++++++ 3 files changed, 161 insertions(+), 7 deletions(-) create mode 100644 smack-extensions/src/test/java/org/jivesoftware/smackx/xdata/provider/DataFormProviderTest.java diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/packet/DataForm.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/packet/DataForm.java index 0457e69f3..45f2eb87a 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/packet/DataForm.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/packet/DataForm.java @@ -1,6 +1,6 @@ /** * - * Copyright 2003-2007 Jive Software, 2020 Florian Schmaus. + * Copyright 2003-2007 Jive Software, 2020-2021 Florian Schmaus. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -538,6 +538,8 @@ public final class DataForm implements ExtensionElement { private final List fields; + private Map fieldMap; + public ReportedData(List fields) { this.fields = Collections.unmodifiableList(fields); } @@ -561,6 +563,18 @@ public final class DataForm implements ExtensionElement { return fields; } + public FormField getField(String name) { + if (fieldMap == null) { + fieldMap = new HashMap<>(fields.size()); + for (FormField field : fields) { + String fieldName = field.getFieldName(); + fieldMap.put(fieldName, field); + } + } + + return fieldMap.get(name); + } + @Override public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) { XmlStringBuilder xml = new XmlStringBuilder(this, xmlEnvironment); diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/provider/DataFormProvider.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/provider/DataFormProvider.java index a126d1092..76bdc6638 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/provider/DataFormProvider.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/provider/DataFormProvider.java @@ -1,6 +1,6 @@ /** * - * Copyright 2003-2007 Jive Software 2020 Florian Schmaus. + * Copyright 2003-2007 Jive Software 2020-2021 Florian Schmaus. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -70,6 +70,7 @@ public class DataFormProvider extends ExtensionElementProvider { dataForm.setType(dataFormType); String formType = null; + DataForm.ReportedData reportedData = null; outerloop: while (true) { XmlPullParser.Event eventType = parser.next(); @@ -86,6 +87,8 @@ public class DataFormProvider extends ExtensionElementProvider { dataForm.setTitle(parser.nextText()); break; case "field": + // Note that we parse this form field without any potential reportedData. We only use reportedData + // to lookup form field types of fields under . FormField formField = parseField(parser, elementXmlEnvironment, formType); TextSingleFormField hiddenFormTypeField = formField.asHiddenFormTypeFieldIfPossible(); @@ -99,12 +102,15 @@ public class DataFormProvider extends ExtensionElementProvider { dataForm.addField(formField); break; case "item": - DataForm.Item item = parseItem(parser, elementXmlEnvironment, formType); + DataForm.Item item = parseItem(parser, elementXmlEnvironment, formType, reportedData); dataForm.addItem(item); break; case "reported": - DataForm.ReportedData reported = parseReported(parser, elementXmlEnvironment, formType); - dataForm.setReportedData(reported); + if (reportedData != null) { + throw new SmackParsingException("Data form with multiple elements"); + } + reportedData = parseReported(parser, elementXmlEnvironment, formType); + dataForm.setReportedData(reportedData); break; // See XEP-133 Example 32 for a corner case where the data form contains this extension. case RosterPacket.ELEMENT: @@ -135,6 +141,11 @@ public class DataFormProvider extends ExtensionElementProvider { private static FormField parseField(XmlPullParser parser, XmlEnvironment xmlEnvironment, String formType) throws XmlPullParserException, IOException, SmackParsingException { + return parseField(parser, xmlEnvironment, formType, null); + } + + private static FormField parseField(XmlPullParser parser, XmlEnvironment xmlEnvironment, String formType, DataForm.ReportedData reportedData) + throws XmlPullParserException, IOException, SmackParsingException { final int initialDepth = parser.getDepth(); final String fieldName = parser.getAttributeValue("var"); @@ -186,6 +197,16 @@ public class DataFormProvider extends ExtensionElementProvider { } } + // Data forms of type 'result' may contain and elements. If this is the case, then the type + // of the s within the elements is determined by the information found in . See + // XEP-0004 § 3.4 and SMACK-902 + if (type == null && reportedData != null) { + FormField reportedFormField = reportedData.getField(fieldName); + if (reportedFormField != null) { + type = reportedFormField.getType(); + } + } + if (type == null) { // If no field type was explicitly provided, then we need to lookup the // field's type in the registry. @@ -301,7 +322,8 @@ public class DataFormProvider extends ExtensionElementProvider { return builder; } - private static DataForm.Item parseItem(XmlPullParser parser, XmlEnvironment xmlEnvironment, String formType) + private static DataForm.Item parseItem(XmlPullParser parser, XmlEnvironment xmlEnvironment, String formType, + DataForm.ReportedData reportedData) throws XmlPullParserException, IOException, SmackParsingException { final int initialDepth = parser.getDepth(); List fields = new ArrayList<>(); @@ -312,7 +334,8 @@ public class DataFormProvider extends ExtensionElementProvider { String name = parser.getName(); switch (name) { case "field": - FormField field = parseField(parser, XmlEnvironment.from(parser, xmlEnvironment), formType); + FormField field = parseField(parser, XmlEnvironment.from(parser, xmlEnvironment), formType, + reportedData); fields.add(field); break; } diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/xdata/provider/DataFormProviderTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/xdata/provider/DataFormProviderTest.java new file mode 100644 index 000000000..f25eb67d3 --- /dev/null +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/xdata/provider/DataFormProviderTest.java @@ -0,0 +1,117 @@ +/** + * + * Copyright 2021 Florian Schmaus + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.xdata.provider; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.IOException; +import java.util.List; + +import org.jivesoftware.smack.parsing.SmackParsingException; +import org.jivesoftware.smack.util.PacketParserUtils; +import org.jivesoftware.smack.xml.XmlPullParser; +import org.jivesoftware.smack.xml.XmlPullParserException; +import org.jivesoftware.smackx.xdata.FormField; +import org.jivesoftware.smackx.xdata.packet.DataForm; + +import org.junit.jupiter.api.Test; + +public class DataFormProviderTest { + + @Test + public void testRetrieveFieldTypeFromReported() throws XmlPullParserException, IOException, SmackParsingException { + + String firstForm = + "" + + " Advanced User Search" + + " The following fields are available for searching. Wildcard (*) characters are allowed as part of the query." + + " " + + " jabber:iq:search" + + " " + + " " + + " " + + " " + + " " + + " true" + + " " + + " " + + " true" + + " " + + " " + + " true" + + " " + + ""; + XmlPullParser parser = PacketParserUtils.getParserFor(firstForm); + DataForm firstDataForm = DataFormProvider.INSTANCE.parse(parser); + FormField usernameFormField = firstDataForm.getField("Username"); + assertEquals(FormField.Type.bool, usernameFormField.getType()); + + String secondForm = + "" + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " 0" + + " " + + " " + + " " + + " frank@orphu" + + " " + + " " + + " " + + " frank" + + " " + + " " + + " " + + " " + + " 0" + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " frank2@orphu" + + " " + + " " + + " " + + " frank2" + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + ""; + parser = PacketParserUtils.getParserFor(secondForm); + DataForm secondDataForm = DataFormProvider.INSTANCE.parse(parser); + List items = secondDataForm.getItems(); + assertEquals(2, items.size()); + } + +} From bda39019108d5e3b86c142abf360a6770e8e55e3 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Fri, 29 Jan 2021 20:17:30 +0100 Subject: [PATCH 2/6] Deprecate DataForm.Builder.setType() --- .../smackx/filetransfer/FileTransferNegotiator.java | 3 +-- .../org/jivesoftware/smackx/xdata/packet/DataForm.java | 10 ++++++++++ .../smackx/xdata/provider/DataFormProvider.java | 3 +-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/FileTransferNegotiator.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/FileTransferNegotiator.java index 9e670acb3..ac789adcc 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/FileTransferNegotiator.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/FileTransferNegotiator.java @@ -360,8 +360,7 @@ public final class FileTransferNegotiator extends Manager { } private static DataForm createDefaultInitiationForm() { - DataForm.Builder form = DataForm.builder() - .setType(DataForm.Type.form); + DataForm.Builder form = DataForm.builder(DataForm.Type.form); ListSingleFormField.Builder fieldBuilder = FormField.listSingleBuilder(STREAM_DATA_FIELD_NAME); if (!IBB_ONLY) { diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/packet/DataForm.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/packet/DataForm.java index 45f2eb87a..37a37741a 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/packet/DataForm.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/packet/DataForm.java @@ -353,6 +353,7 @@ public final class DataForm implements ExtensionElement { } public static final class Builder { + // TODO: Make this field final once setType() is gone. private Type type; private String title; private List instructions; @@ -381,6 +382,15 @@ public final class DataForm implements ExtensionElement { extensionElements = CollectionUtil.newListWith(dataForm.getExtensionElements()); } + /** + * Deprecated do not use. + * + * @param type the type. + * @return a reference to this builder. + * @deprecated use {@link DataForm#builder(Type)} instead. + */ + @Deprecated + // TODO: Remove in Smack 4.5 and then make this.type final. public Builder setType(Type type) { this.type = Objects.requireNonNull(type); return this; diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/provider/DataFormProvider.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/provider/DataFormProvider.java index 76bdc6638..1a0194b6a 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/provider/DataFormProvider.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/provider/DataFormProvider.java @@ -66,8 +66,7 @@ public class DataFormProvider extends ExtensionElementProvider { @Override public DataForm parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException { DataForm.Type dataFormType = DataForm.Type.fromString(parser.getAttributeValue("", "type")); - DataForm.Builder dataForm = DataForm.builder(); - dataForm.setType(dataFormType); + DataForm.Builder dataForm = DataForm.builder(dataFormType); String formType = null; DataForm.ReportedData reportedData = null; From 1c262471e523208bee2259cbffd236e8e0dc2db5 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Sun, 21 Feb 2021 13:01:43 +0100 Subject: [PATCH 3/6] [xdata] Use extra registry for fields in clark notation If a field name is in clark notation, then lookup the field's type via an extra registry. --- .../org/jivesoftware/smack/util/XmlUtil.java | 9 ++ .../smackx/formtypes/FormFieldRegistry.java | 127 +++++++----------- .../xdata/provider/DataFormProvider.java | 21 +-- 3 files changed, 69 insertions(+), 88 deletions(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/XmlUtil.java b/smack-core/src/main/java/org/jivesoftware/smack/util/XmlUtil.java index b55297d6e..3845da548 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/XmlUtil.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/XmlUtil.java @@ -64,4 +64,13 @@ public class XmlUtil { return stringWriter.toString(); } + + public static boolean isClarkNotation(String text) { + if (text.isEmpty()) { + return false; + } + + // TODO: This is currently a mediocre heuristic to check for clark notation. + return text.charAt(0) == '{'; + } } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/formtypes/FormFieldRegistry.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/formtypes/FormFieldRegistry.java index 4941d236d..97156974f 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/formtypes/FormFieldRegistry.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/formtypes/FormFieldRegistry.java @@ -1,6 +1,6 @@ /** * - * Copyright 2020 Florian Schmaus + * Copyright 2020-2021 Florian Schmaus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,27 +18,26 @@ package org.jivesoftware.smackx.formtypes; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.logging.Logger; import org.jivesoftware.smack.util.Objects; - +import org.jivesoftware.smack.util.StringUtils; +import org.jivesoftware.smack.util.XmlUtil; import org.jivesoftware.smackx.xdata.FormField; import org.jivesoftware.smackx.xdata.TextSingleFormField; import org.jivesoftware.smackx.xdata.packet.DataForm; public class FormFieldRegistry { + private static final Logger LOGGER = Logger.getLogger(FormFieldRegistry.class.getName()); + private static final Map> REGISTRY = new HashMap<>(); - private static final Map LOOKASIDE_REGISTRY = new HashMap<>(); - - private static final Map FIELD_NAME_TO_FORM_TYPE = new HashMap<>(); - - static { - register(FormField.FORM_TYPE, FormField.Type.hidden); - } + private static final Map CLARK_NOTATION_FIELD_REGISTRY = new ConcurrentHashMap<>(); @SuppressWarnings("ReferenceEquality") - public static synchronized void register(DataForm dataForm) { + public static void register(DataForm dataForm) { // TODO: Also allow forms of type 'result'? if (dataForm.getType() != DataForm.Type.form) { throw new IllegalArgumentException(); @@ -56,64 +55,57 @@ public class FormFieldRegistry { continue; } - String fieldName = formField.getFieldName(); FormField.Type type = formField.getType(); + if (type == FormField.Type.fixed) { + continue; + } + + String fieldName = formField.getFieldName(); register(formType, fieldName, type); } } - public static synchronized void register(String formType, String fieldName, FormField.Type type) { + public static void register(String formType, String fieldName, FormField.Type fieldType) { + StringUtils.requireNotNullNorEmpty(fieldName, "fieldName must be provided"); + Objects.requireNonNull(fieldType); + if (formType == null) { - FormFieldInformation formFieldInformation = lookup(fieldName); - if (formFieldInformation != null) { - if (Objects.equals(formType, formFieldInformation.formType) - && type.equals(formFieldInformation.formFieldType)) { - // The field is already registered, nothing to do here. - return; - } - - String message = "There is already a field with the name'" + fieldName - + "' registered with the field type '" + formFieldInformation.formFieldType - + "', while this tries to register the field with the type '" + type + '\''; - throw new IllegalArgumentException(message); + if (XmlUtil.isClarkNotation(fieldName)) { + CLARK_NOTATION_FIELD_REGISTRY.put(fieldName, fieldType); } - - LOOKASIDE_REGISTRY.put(fieldName, type); return; } - Map fieldNameToType = REGISTRY.get(formType); - if (fieldNameToType == null) { - fieldNameToType = new HashMap<>(); - REGISTRY.put(formType, fieldNameToType); - } else { - FormField.Type previousType = fieldNameToType.get(fieldName); - if (previousType != null && previousType != type) { - throw new IllegalArgumentException(); + FormField.Type previousType; + synchronized (REGISTRY) { + Map fieldNameToType = REGISTRY.get(formType); + if (fieldNameToType == null) { + fieldNameToType = new HashMap<>(); + REGISTRY.put(formType, fieldNameToType); + } else { + previousType = fieldNameToType.get(fieldName); + if (previousType != null && previousType != fieldType) { + throw new IllegalArgumentException(); + } } + previousType = fieldNameToType.put(fieldName, fieldType); } - fieldNameToType.put(fieldName, type); - - FIELD_NAME_TO_FORM_TYPE.put(fieldName, formType); - } - - public static synchronized void register(String fieldName, FormField.Type type) { - FormField.Type previousType = LOOKASIDE_REGISTRY.get(fieldName); - if (previousType != null) { - if (previousType == type) { - // Nothing to do here. - return; - } - throw new IllegalArgumentException("There is already a field with the name '" + fieldName - + "' registered with type " + previousType - + ", while trying to register this field with type '" + type + "'"); + if (previousType != null && fieldType != previousType) { + LOGGER.warning("Form field registry inconsitency detected: Registered field '" + fieldName + "' of type " + fieldType + " but previous type was " + previousType); } - LOOKASIDE_REGISTRY.put(fieldName, type); } - public static synchronized FormField.Type lookup(String formType, String fieldName) { - if (formType != null) { + public static FormField.Type lookup(String formType, String fieldName) { + if (formType == null) { + if (!XmlUtil.isClarkNotation(fieldName)) { + return null; + } + + return CLARK_NOTATION_FIELD_REGISTRY.get(fieldName); + } + + synchronized (REGISTRY) { Map fieldNameToTypeMap = REGISTRY.get(formType); if (fieldNameToTypeMap != null) { FormField.Type type = fieldNameToTypeMap.get(fieldName); @@ -121,38 +113,13 @@ public class FormFieldRegistry { return type; } } - } else { - formType = FIELD_NAME_TO_FORM_TYPE.get(fieldName); - if (formType != null) { - FormField.Type type = lookup(formType, fieldName); - if (type != null) { - return type; - } - } } - // Fallback to lookaside registry. - return LOOKASIDE_REGISTRY.get(fieldName); + return null; } - public static synchronized FormFieldInformation lookup(String fieldName) { - String formType = FIELD_NAME_TO_FORM_TYPE.get(fieldName); - FormField.Type type = lookup(formType, fieldName); - if (type == null) { - return null; - } - - return new FormFieldInformation(type, formType); + public static synchronized FormField.Type lookup(String fieldName) { + return lookup(null, fieldName); } - public static final class FormFieldInformation { - public final FormField.Type formFieldType; - public final String formType; - - - private FormFieldInformation(FormField.Type formFieldType, String formType) { - this.formFieldType = formFieldType; - this.formType = formType; - } - } } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/provider/DataFormProvider.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/provider/DataFormProvider.java index 1a0194b6a..6465efacb 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/provider/DataFormProvider.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/provider/DataFormProvider.java @@ -207,14 +207,19 @@ public class DataFormProvider extends ExtensionElementProvider { } if (type == null) { - // If no field type was explicitly provided, then we need to lookup the - // field's type in the registry. - type = FormFieldRegistry.lookup(formType, fieldName); - if (type == null) { - LOGGER.warning("The Field '" + fieldName + "' from FORM_TYPE '" + formType - + "' is not registered. Field type is unknown, assuming text-single."); - // As per XEP-0004, text-single is the default form field type, which we use as emergency fallback here. - type = FormField.Type.text_single; + // The field name 'FORM_TYPE' is magic. + if (fieldName.equals(FormField.FORM_TYPE)) { + type = FormField.Type.hidden; + } else { + // If no field type was explicitly provided, then we need to lookup the + // field's type in the registry. + type = FormFieldRegistry.lookup(formType, fieldName); + if (type == null) { + LOGGER.warning("The Field '" + fieldName + "' from FORM_TYPE '" + formType + + "' is not registered. Field type is unknown, assuming text-single."); + // As per XEP-0004, text-single is the default form field type, which we use as emergency fallback here. + type = FormField.Type.text_single; + } } } From 33f59fd7edb89cddcbf9f4f691f462906106d967 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Tue, 2 Mar 2021 21:40:52 +0100 Subject: [PATCH 4/6] [xdata] Ensure that hidden FROM_TYPE field is first --- .../smackx/xdata/packet/DataForm.java | 27 +++++++++++++++++++ .../smackx/xdata/packet/DataFormTest.java | 10 +++++++ 2 files changed, 37 insertions(+) diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/packet/DataForm.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/packet/DataForm.java index 37a37741a..9ab354d57 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/packet/DataForm.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/packet/DataForm.java @@ -100,6 +100,9 @@ public final class DataForm implements ExtensionElement { instructions = CollectionUtil.cloneAndSeal(builder.instructions); reportedData = builder.reportedData; items = CollectionUtil.cloneAndSeal(builder.items); + + builder.orderFields(); + fields = CollectionUtil.cloneAndSeal(builder.fields); fieldsMap = CollectionUtil.cloneAndSeal(builder.fieldsMap); extensionElements = CollectionUtil.cloneAndSeal(builder.extensionElements); @@ -382,6 +385,30 @@ public final class DataForm implements ExtensionElement { extensionElements = CollectionUtil.newListWith(dataForm.getExtensionElements()); } + private void orderFields() { + Iterator it = fields.iterator(); + if (!it.hasNext()) { + return; + } + + FormField hiddenFormTypeField = it.next().asHiddenFormTypeFieldIfPossible(); + if (hiddenFormTypeField != null) { + // The hidden FROM_TYPE field is already in first position, nothing to do. + return; + } + + while (it.hasNext()) { + hiddenFormTypeField = it.next().asHiddenFormTypeFieldIfPossible(); + if (hiddenFormTypeField != null) { + // Remove the hidden FORM_TYPE field that is not on first position. + it.remove(); + // And insert it again at first position. + fields.add(0, hiddenFormTypeField); + break; + } + } + } + /** * Deprecated do not use. * diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/xdata/packet/DataFormTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/xdata/packet/DataFormTest.java index 726be55d6..3f9e6feb3 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/xdata/packet/DataFormTest.java +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/xdata/packet/DataFormTest.java @@ -153,4 +153,14 @@ public class DataFormTest extends SmackTestSuite { DataForm df = pr.parse(PacketParserUtils.getParserFor(formWithFixedField)); assertEquals(Type.fixed, df.getFields().get(0).getType()); } + + @Test + public void testReorderHiddenFormTypeFieldAtFirstPosition() { + DataForm dataForm = DataForm.builder() + .addField(FormField.textSingleBuilder("foo1").setValue("bar").build()) + .addField(FormField.textSingleBuilder("foo2").setValue("baz").build()) + .setFormType("my-form-type") + .build(); + assertNotNull(dataForm.getFields().get(0).asHiddenFormTypeFieldIfPossible()); + } } From d4d2a7a6d8a519cc51175320251c222ad5e27fa3 Mon Sep 17 00:00:00 2001 From: Frank Matheron Date: Tue, 23 Feb 2021 14:25:31 +0100 Subject: [PATCH 5/6] [xdata] Do not require FORM_TYPE field in FilledForm The FillableForm API is the most convenient way of filling out a form. Currently only forms with a FORM_TYPE can be filled, due to a restriction in FillableForm. This makes filling out untyped forms very cumbersome. This commit removes the required FORM_TYPE when using FilledForm (and therefor FillableForm). Some subclasses of FilledForm already check that the correct FORM_TYPE is set using FilledForm#ensureFormType(). --- .../smackx/xdata/form/FillableForm.java | 15 ++++++++++----- .../smackx/xdata/form/FilledForm.java | 5 ----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/form/FillableForm.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/form/FillableForm.java index b407864cc..7e90c7711 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/form/FillableForm.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/form/FillableForm.java @@ -256,11 +256,16 @@ public class FillableForm extends FilledForm { if (!missingRequiredFields.isEmpty()) { throw new IllegalStateException("Not all required fields filled. Missing: " + missingRequiredFields); } - DataForm dataFormToSend = DataForm.builder() - .addField(formTypeFormField) - .addFields(filledFields.values()) - .build(); - return dataFormToSend; + DataForm.Builder builder = DataForm.builder(); + + // the submit form has the same FORM_TYPE as the form. + if (formTypeFormField != null) { + builder.addField(formTypeFormField); + } + + builder.addFields(filledFields.values()); + + return builder.build(); } } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/form/FilledForm.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/form/FilledForm.java index 7624eab80..f81bb0c0e 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/form/FilledForm.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/form/FilledForm.java @@ -17,7 +17,6 @@ package org.jivesoftware.smackx.xdata.form; import org.jivesoftware.smack.util.Objects; -import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smackx.xdata.FormField; import org.jivesoftware.smackx.xdata.TextSingleFormField; @@ -32,10 +31,6 @@ public abstract class FilledForm implements FormReader { public FilledForm(DataForm dataForm) { this.dataForm = Objects.requireNonNull(dataForm); - String formType = dataForm.getFormType(); - if (StringUtils.isNullOrEmpty(formType)) { - throw new IllegalArgumentException("The provided data form has no hidden FROM_TYPE field."); - } if (dataForm.getType() == Type.cancel) { throw new IllegalArgumentException("Forms of type 'cancel' are not filled nor fillable"); } From ae44af5d634bd85451e46266f5240237aa55a391 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Wed, 3 Mar 2021 12:51:56 +0100 Subject: [PATCH 6/6] Smack 4.4.1 --- resources/releasedocs/changelog.html | 26 ++++++++++++++++++++++++++ version | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/resources/releasedocs/changelog.html b/resources/releasedocs/changelog.html index 3d872c85f..06c247f84 100644 --- a/resources/releasedocs/changelog.html +++ b/resources/releasedocs/changelog.html @@ -141,7 +141,33 @@ hr {
+

4.4.1 -- 2021-03-03

+

Bug +

+
    +
  • [SMACK-895] - BoBIQ#getIQChildElementBuilder throws NPE when the BoB data does not contain ‘max-age’. +
  • +
  • [SMACK-896] - BoBDataExtension is missing getter for BoBData and ContentId +
  • +
  • [SMACK-897] - DirectoryRosterStore.readEntry() should also catch IllegalArgumentException +
  • +
  • [SMACK-898] - AbstractProvider should also consider TypeVariable +
  • +
  • [SMACK-899] - NullPointerException in EntityCapsManager.addCapsExtension +
  • +
  • [SMACK-900] - NPE in DataForm.Builder.addItem() +
  • +
  • [SMACK-902] - DataFormProvider should retrieve the type of fields from <reported/> elements if possible +
  • +
+ +

Improvement +

+
    +
  • [SMACK-901] - BoBDataExtension.from() should also allow IQs +
  • +

4.4.0 -- 2020-12-06

diff --git a/version b/version index 1122ad10a..cca25a93c 100644 --- a/version +++ b/version @@ -1 +1 @@ -4.4.1-SNAPSHOT +4.4.1