Java Code Examples for org.jivesoftware.smackx.xdata.packet.DataForm#getFields()

The following examples show how to use org.jivesoftware.smackx.xdata.packet.DataForm#getFields() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FormFieldRegistry.java    From Smack with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("ReferenceEquality")
public static synchronized void register(DataForm dataForm) {
    // TODO: Also allow forms of type 'result'?
    if (dataForm.getType() != DataForm.Type.form) {
        throw new IllegalArgumentException();
    }

    String formType = null;
    TextSingleFormField hiddenFormTypeField = dataForm.getHiddenFormTypeField();
    if (hiddenFormTypeField != null) {
        formType = hiddenFormTypeField.getValue();
    }

    for (FormField formField : dataForm.getFields()) {
        // Note that we can compare here by reference equality to skip the hidden form type field.
        if (formField == hiddenFormTypeField) {
            continue;
        }

        String fieldName = formField.getFieldName();
        FormField.Type type = formField.getType();
        register(formType, fieldName, type);
    }
}
 
Example 2
Source File: FillableForm.java    From Smack with Apache License 2.0 6 votes vote down vote up
public FillableForm(DataForm dataForm) {
    super(dataForm);
    if (dataForm.getType() != DataForm.Type.form) {
        throw new IllegalArgumentException();
    }

    Set<String> requiredFields = new HashSet<>();
    for (FormField formField : dataForm.getFields()) {
        if (formField.isRequired()) {
            String fieldName = formField.getFieldName();
            requiredFields.add(fieldName);
            missingRequiredFields.add(fieldName);
        }
    }
    this.requiredFields = Collections.unmodifiableSet(requiredFields);
}
 
Example 3
Source File: ConferenceUtils.java    From Spark with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 4
Source File: PrivateKeyReceiver.java    From desktopclient-java with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onSuccess(IQ packet) {
    LOGGER.info("response: "+packet);

    mConn.disconnect();

    if (!(packet instanceof IQ)) {
        LOGGER.warning("response not an IQ packet");
        finish(null);
        return;
    }
    IQ iq = (IQ) packet;

    if (iq.getType() != IQ.Type.result) {
        LOGGER.warning("ignoring response with IQ type: "+iq.getType());
        this.finish(null);
        return;
    }

    DataForm response = iq.getExtension(DataForm.ELEMENT, DataForm.NAMESPACE);
    if (response == null) {
        this.finish(null);
        return;
    }

    String token = null;
    List<FormField> fields = response.getFields();
    for (FormField field : fields) {
        if ("token".equals(field.getVariable())) {
            token = field.getValues().get(0).toString();
            break;
        }
    }

    this.finish(token);
}
 
Example 5
Source File: MamQueryIQProviderTest.java    From Smack with Apache License 2.0 5 votes vote down vote up
@Test
public void checkMamQueryIQProvider() throws Exception {
    // example 1
    IQ iq1 = PacketParserUtils.parseStanza(exampleMamQueryIQ1);
    MamQueryIQ mamQueryIQ1 = (MamQueryIQ) iq1;

    assertEquals(mamQueryIQ1.getType(), Type.set);
    assertEquals(mamQueryIQ1.getQueryId(), "test");

    DataForm dataForm1 = (DataForm) mamQueryIQ1.getExtension(DataForm.NAMESPACE);
    assertEquals(dataForm1.getType(), DataForm.Type.submit);

    List<FormField> fields1 = dataForm1.getFields();
    assertEquals(fields1.get(0).getType(), FormField.Type.hidden);
    assertEquals(fields1.get(1).getType(), FormField.Type.text_single);
    assertEquals(fields1.get(1).getValues().get(0).toString(), "Where arth thou, my Juliet?");
    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;

    assertEquals(mamQueryIQ2.getType(), Type.result);
    assertNull(mamQueryIQ2.getQueryId());

    DataForm dataForm2 = (DataForm) mamQueryIQ2.getExtension(DataForm.NAMESPACE);
    assertEquals(dataForm2.getType(), DataForm.Type.form);

    List<FormField> fields2 = dataForm2.getFields();
    assertEquals(fields2.get(0).getValues().get(0).toString(), "urn:xmpp:mam:2");
    assertTrue(fields2.get(0).getValues().size() == 1);
    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).getFieldName(), "urn:example:xmpp:free-text-search");
}