Java Code Examples for org.jivesoftware.smackx.xdata.packet.DataForm#Builder

The following examples show how to use org.jivesoftware.smackx.xdata.packet.DataForm#Builder . 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: MultiUserChat.java    From Smack with Apache License 2.0 6 votes vote down vote up
/**
 * Sends a voice request to the MUC. The room moderators usually need to approve this request.
 *
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 * @see <a href="http://xmpp.org/extensions/xep-0045.html#requestvoice">XEP-45 ยง 7.13 Requesting
 *      Voice</a>
 * @since 4.1
 */
public void requestVoice() throws NotConnectedException, InterruptedException {
    DataForm.Builder form = DataForm.builder()
                    .setFormType(MUCInitialPresence.NAMESPACE + "#request");

    TextSingleFormField.Builder requestVoiceField = FormField.textSingleBuilder("muc#role");
    requestVoiceField.setLabel("Requested role");
    requestVoiceField.setValue("participant");
    form.addField(requestVoiceField.build());

    Message message = connection.getStanzaFactory().buildMessageStanza()
            .to(room)
            .addExtension(form.build())
            .build();
    connection.sendStanza(message);
}
 
Example 2
Source File: StreamNegotiator.java    From Smack with Apache License 2.0 6 votes vote down vote up
/**
 * Creates the initiation acceptance stanza to forward to the stream
 * initiator.
 *
 * @param streamInitiationOffer The offer from the stream initiator to connect for a stream.
 * @param namespace            The namespace that relates to the accepted means of transfer.
 * @return The response to be forwarded to the initiator.
 */
protected static StreamInitiation createInitiationAccept(
        StreamInitiation streamInitiationOffer, String namespace) {
    StreamInitiation response = new StreamInitiation();
    response.setTo(streamInitiationOffer.getFrom());
    response.setFrom(streamInitiationOffer.getTo());
    response.setType(IQ.Type.result);
    response.setStanzaId(streamInitiationOffer.getStanzaId());

    DataForm.Builder form = DataForm.builder();
    ListSingleFormField.Builder field = FormField.listSingleBuilder(
            FileTransferNegotiator.STREAM_DATA_FIELD_NAME);
    field.setValue(namespace);
    form.addField(field.build());

    response.setFeatureNegotiationForm(form.build());
    return response;
}
 
Example 3
Source File: RoomInfoTest.java    From Smack with Apache License 2.0 6 votes vote down vote up
@Test
public void validateRoomWithForm() {
    DataForm.Builder dataForm = DataForm.builder(DataForm.Type.result);

    TextSingleFormField.Builder desc = FormField.builder("muc#roominfo_description");
    desc.setValue("The place for all good witches!");
    dataForm.addField(desc.build());

    TextSingleFormField.Builder subject = FormField.builder("muc#roominfo_subject");
    subject.setValue("Spells");
    dataForm.addField(subject.build());

    TextSingleFormField.Builder occupants = FormField.builder("muc#roominfo_occupants");
    occupants.setValue("3");
    dataForm.addField(occupants.build());

    DiscoverInfo discoInfo = DiscoverInfo.builder("disco1")
            .addExtension(dataForm.build())
            .build();
    RoomInfo roomInfo = new RoomInfo(discoInfo);
    assertEquals("The place for all good witches!", roomInfo.getDescription());
    assertEquals("Spells", roomInfo.getSubject());
    assertEquals(3, roomInfo.getOccupantsCount());
}
 
Example 4
Source File: SoftwareInfoFormTest.java    From Smack with Apache License 2.0 6 votes vote down vote up
public static SoftwareInfoForm createSoftwareInfoFormUsingDataForm() throws URISyntaxException {
    DataForm.Builder dataFormBuilder = DataForm.builder(Type.result);
    TextSingleFormField formField = FormField.buildHiddenFormType(SoftwareInfoForm.FORM_TYPE);
    dataFormBuilder.addField(formField);

    dataFormBuilder.addField(FormField.builder("icon")
                   .addFormFieldChildElement(createMediaElement())
                   .build());
    dataFormBuilder.addField(FormField.builder("os")
                   .setValue("Windows")
                   .build());
    dataFormBuilder.addField(FormField.builder("os_version")
                   .setValue("XP")
                   .build());
    dataFormBuilder.addField(FormField.builder("software")
                   .setValue("Exodus")
                   .build());
    dataFormBuilder.addField(FormField.builder("software_version")
                   .setValue("0.9.1")
                   .build());

    SoftwareInfoForm softwareInfoForm = SoftwareInfoForm.getBuilder().setDataForm(dataFormBuilder.build()).build();
    return softwareInfoForm;
}
 
Example 5
Source File: SoftwareInfoManagerTest.java    From Smack with Apache License 2.0 6 votes vote down vote up
public static SoftwareInfoForm buildSoftwareInfoFromDataForm() throws URISyntaxException {
    DataForm.Builder dataFormBuilder = DataForm.builder(Type.result);
    dataFormBuilder.addField(FormField.buildHiddenFormType(SoftwareInfoForm.FORM_TYPE));
    dataFormBuilder.addField(FormField.builder("icon")
                               .addFormFieldChildElement(createMediaElement())
                               .build());
    dataFormBuilder.addField(FormField.builder("os")
                                .setValue("Windows")
                                .build());
    dataFormBuilder.addField(FormField.builder("os_version")
                   .setValue("XP")
                   .build());
    dataFormBuilder.addField(FormField.builder("software")
                   .setValue("Exodus")
                   .build());
    dataFormBuilder.addField(FormField.builder("software_version")
                   .setValue("0.9.1")
                   .build());
    SoftwareInfoForm softwareInfoForm = SoftwareInfoForm.getBuilder()
                                             .setDataForm(dataFormBuilder.build())
                                             .build();
    return softwareInfoForm;
}
 
Example 6
Source File: MamManager.java    From Smack with Apache License 2.0 5 votes vote down vote up
DataForm getDataForm() {
    if (dataForm != null) {
        return dataForm;
    }
    DataForm.Builder dataFormBuilder = getNewMamForm();
    dataFormBuilder.addFields(formFields.values());
    dataForm = dataFormBuilder.build();
    return dataForm;
}
 
Example 7
Source File: EnablePushNotificationsIQ.java    From Smack with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 8
Source File: MamTest.java    From Smack with Apache License 2.0 5 votes vote down vote up
protected DataForm getNewMamForm() throws NoSuchMethodException, SecurityException, IllegalAccessException,
        IllegalArgumentException, InvocationTargetException {
    Method methodGetNewMamForm = MamManager.class.getDeclaredMethod("getNewMamForm");
    methodGetNewMamForm.setAccessible(true);
    DataForm.Builder dataFormBuilder = (DataForm.Builder) methodGetNewMamForm.invoke(mamManager);
    return dataFormBuilder.build();
}
 
Example 9
Source File: FileTransferNegotiator.java    From Smack with Apache License 2.0 5 votes vote down vote up
private static DataForm createDefaultInitiationForm() {
    DataForm.Builder form = DataForm.builder()
                    .setType(DataForm.Type.form);
    ListSingleFormField.Builder fieldBuilder = FormField.listSingleBuilder(STREAM_DATA_FIELD_NAME);

    if (!IBB_ONLY) {
        fieldBuilder.addOption(Bytestream.NAMESPACE);
    }
    fieldBuilder.addOption(DataPacketExtension.NAMESPACE);
    form.addField(fieldBuilder.build());
    return form.build();
}
 
Example 10
Source File: SoftwareInfoFormTest.java    From Smack with Apache License 2.0 5 votes vote down vote up
@Test
public void faultySoftwareInfoFormsTest() {
    DataForm.Builder dataFormbuilder = DataForm.builder(Type.result);
    TextSingleFormField formField = FormField.buildHiddenFormType("faulty_formtype");
    dataFormbuilder.addField(formField);
    assertThrows(IllegalArgumentException.class, () -> {
        SoftwareInfoForm.getBuilder().setDataForm(dataFormbuilder.build()).build();
    });

    DataForm.Builder builderWithoutFormType = DataForm.builder(Type.result);
    assertThrows(IllegalArgumentException.class, () -> {
        SoftwareInfoForm.getBuilder().setDataForm(builderWithoutFormType.build()).build();
    });
}
 
Example 11
Source File: MamManager.java    From Smack with Apache License 2.0 4 votes vote down vote up
private static DataForm.Builder getNewMamForm() {
    FormField field = FormField.buildHiddenFormType(MamElements.NAMESPACE);
    DataForm.Builder form = DataForm.builder();
    form.addField(field);
    return form;
}
 
Example 12
Source File: Workgroup.java    From Smack with Apache License 2.0 4 votes vote down vote up
/**
 * <p>Joins the workgroup queue to wait to be routed to an agent. After joining
 * the queue, queue status events will be sent to indicate the user's position and
 * estimated time left in the queue. Once joining the queue, there are three ways
 * the user can leave the queue: <ul>
 *
 * <li>The user is routed to an agent, which triggers a GroupChat invitation.
 * <li>The user asks to leave the queue by calling the {@link #departQueue} method.
 * <li>A server error occurs, or an administrator explicitly removes the user
 * from the queue.
 * </ul>
 *
 * A user cannot request to join the queue again if already in the queue. Therefore,
 * this method will throw an IllegalStateException if the user is already in the queue.<p>
 *
 * Some servers may be configured to require certain meta-data in order to
 * join the queue.<p>
 *
 * The server tracks the conversations that a user has with agents over time. By
 * default, that tracking is done using the user's JID. However, this is not always
 * possible. For example, when the user is logged in anonymously using a web client.
 * In that case the user ID might be a randomly generated value put into a persistent
 * cookie or a username obtained via the session. When specified, that userID will
 * be used instead of the user's JID to track conversations. The server will ignore a
 * manually specified userID if the user's connection to the server is not anonymous.
 *
 * @param metadata metadata to create a dataform from.
 * @param userID   String that represents the ID of the user when using anonymous sessions
 *                 or <code>null</code> if a userID should not be used.
 * @throws XMPPException if an error occurred joining the queue. An error may indicate
 *                       that a connection failure occurred or that the server explicitly rejected the
 *                       request to join the queue.
 * @throws SmackException if Smack detected an exceptional situation.
 * @throws InterruptedException if the calling thread was interrupted.
 */
public void joinQueue(Map<String, Object> metadata, Jid userID) throws XMPPException, SmackException, InterruptedException {
    // If already in the queue ignore the join request.
    if (inQueue) {
        throw new IllegalStateException("Already in queue " + workgroupJID);
    }

    // Build dataform from metadata
    DataForm.Builder form = DataForm.builder();
    Iterator<String> iter = metadata.keySet().iterator();
    while (iter.hasNext()) {
        String name = iter.next();
        String value = metadata.get(name).toString();

        TextSingleFormField.Builder field = FormField.builder(name);
        field.setValue(value);
        form.addField(field.build());
    }
    joinQueue(form.build(), userID);
}
 
Example 13
Source File: DataFormProvider.java    From Smack with Apache License 2.0 4 votes vote down vote up
@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);

    String formType = null;

    outerloop: while (true) {
        XmlPullParser.Event eventType = parser.next();
        switch (eventType) {
        case START_ELEMENT:
            String name = parser.getName();
            String namespace = parser.getNamespace();
            XmlEnvironment elementXmlEnvironment = XmlEnvironment.from(parser, xmlEnvironment);
            switch (name) {
            case "instructions":
                dataForm.addInstruction(parser.nextText());
                break;
            case "title":
                dataForm.setTitle(parser.nextText());
                break;
            case "field":
                FormField formField = parseField(parser, elementXmlEnvironment, formType, dataFormType);

                TextSingleFormField hiddenFormTypeField = formField.asHiddenFormTypeFieldIfPossible();
                if (hiddenFormTypeField != null) {
                    if (formType != null) {
                        throw new SmackParsingException("Multiple hidden form type fields");
                    }
                    formType = hiddenFormTypeField.getValue();
                }

                dataForm.addField(formField);
                break;
            case "item":
                DataForm.Item item = parseItem(parser, elementXmlEnvironment, formType, dataFormType);
                dataForm.addItem(item);
                break;
            case "reported":
                DataForm.ReportedData reported = parseReported(parser, elementXmlEnvironment, formType, dataFormType);
                dataForm.setReportedData(reported);
                break;
            // See XEP-133 Example 32 for a corner case where the data form contains this extension.
            case RosterPacket.ELEMENT:
                if (namespace.equals(RosterPacket.NAMESPACE)) {
                    dataForm.addExtensionElement(RosterPacketProvider.INSTANCE.parse(parser));
                }
                break;
            // See XEP-141 Data Forms Layout
            case DataLayout.ELEMENT:
                if (namespace.equals(DataLayout.NAMESPACE)) {
                    dataForm.addExtensionElement(DataLayoutProvider.parse(parser));
                }
                break;
            }
            break;
        case END_ELEMENT:
            if (parser.getDepth() == initialDepth) {
                break outerloop;
            }
            break;
        default:
            // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
            break;
        }
    }
    return dataForm.build();
}