Search in sources :

Example 41 with DataForm

use of org.jivesoftware.smackx.xdata.packet.DataForm in project Smack by igniterealtime.

the class FileTransferNegotiator method createDefaultInitiationForm.

private static DataForm createDefaultInitiationForm() {
    DataForm form = new DataForm(DataForm.Type.form);
    FormField field = new FormField(STREAM_DATA_FIELD_NAME);
    field.setType(FormField.Type.list_single);
    if (!IBB_ONLY) {
        field.addOption(new FormField.Option(Bytestream.NAMESPACE));
    }
    field.addOption(new FormField.Option(DataPacketExtension.NAMESPACE));
    form.addField(field);
    return form;
}
Also used : DataForm(org.jivesoftware.smackx.xdata.packet.DataForm) FormField(org.jivesoftware.smackx.xdata.FormField)

Example 42 with DataForm

use of org.jivesoftware.smackx.xdata.packet.DataForm in project xabber-android by redsolution.

the class Feature method createDataForm.

public static DataForm createDataForm(DataFormType type) {
    DataForm dataForm = new DataForm(DataForm.Type.fromString(type.toString()));
    FormField typeField = new FormField(FORM_TYPE_FIELD);
    typeField.addValue(FORM_TYPE_VALUE);
    typeField.setType(FormField.Type.hidden);
    dataForm.addField(typeField);
    return dataForm;
}
Also used : DataForm(org.jivesoftware.smackx.xdata.packet.DataForm) FormField(org.jivesoftware.smackx.xdata.FormField)

Example 43 with DataForm

use of org.jivesoftware.smackx.xdata.packet.DataForm in project xabber-android by redsolution.

the class SSNManager method onFormReceived.

private void onFormReceived(String account, String from, String bareAddress, String session, Feature feature) {
    OtrMode otrMode = getOtrMode(account, bareAddress, session);
    boolean cancel = false;
    Collection<DisclosureValue> disclosureValues = feature.getDisclosureOptions();
    DisclosureValue disclosureValue = DisclosureValue.never;
    if (disclosureValues == null)
        disclosureValue = null;
    else if (!disclosureValues.contains(disclosureValue))
        cancel = true;
    Collection<SecurityValue> securityValues = feature.getSecurityOptions();
    SecurityValue securityValue;
    if (AccountManager.getInstance().getAccount(account).getConnectionSettings().getTlsMode() == TLSMode.required)
        securityValue = SecurityValue.c2s;
    else
        securityValue = SecurityValue.none;
    if (securityValues == null)
        securityValue = null;
    else if (!securityValues.contains(securityValue))
        cancel = true;
    Collection<LoggingValue> loggingValues = feature.getLoggingOptions();
    LoggingValue loggingValue;
    if (loggingValues == null)
        loggingValue = null;
    else {
        loggingValue = otrMode.selectLoggingValue(loggingValues);
        if (loggingValue == null)
            cancel = true;
    }
    if (cancel) {
        DataForm dataForm = Feature.createDataForm(DataFormType.submit);
        if (feature.getAcceptValue() != null) {
            Feature.addAcceptField(dataForm, false);
            sessionStates.remove(account, session);
        } else {
            Feature.addRenegotiateField(dataForm, false);
        }
        sendFeature(account, from, session, new Feature(dataForm));
        return;
    }
    DataForm dataForm = Feature.createDataForm(DataFormType.submit);
    if (feature.getAcceptValue() != null)
        Feature.addAcceptField(dataForm, true);
    else
        Feature.addRenegotiateField(dataForm, true);
    if (disclosureValue != null)
        Feature.addDisclosureField(dataForm, null, disclosureValue);
    if (securityValue != null)
        Feature.addSecurityField(dataForm, null, securityValue);
    if (loggingValue != null) {
        try {
            if (loggingValue == LoggingValue.mustnot)
                MessageArchiveManager.getInstance().setSaveMode(account, from, session, SaveMode.fls);
            else
                MessageArchiveManager.getInstance().setSaveMode(account, from, session, SaveMode.body);
        } catch (NetworkException e) {
        }
        Feature.addLoggingField(dataForm, null, loggingValue);
    }
    sessionStates.put(account, session, SessionState.active);
    sendFeature(account, from, session, new Feature(dataForm));
}
Also used : LoggingValue(com.xabber.xmpp.ssn.LoggingValue) DisclosureValue(com.xabber.xmpp.ssn.DisclosureValue) DataForm(org.jivesoftware.smackx.xdata.packet.DataForm) OtrMode(com.xabber.xmpp.archive.OtrMode) Feature(com.xabber.xmpp.ssn.Feature) NetworkException(com.xabber.android.data.NetworkException) SecurityValue(com.xabber.xmpp.ssn.SecurityValue)

Example 44 with DataForm

use of org.jivesoftware.smackx.xdata.packet.DataForm in project xabber-android by redsolution.

the class CapabilitiesManager method isValid.

private boolean isValid(DiscoverInfo discoverInfo) {
    Set<DiscoverInfo.Identity> identities = new TreeSet<>(new Comparator<DiscoverInfo.Identity>() {

        private int compare(String string1, String string2) {
            return (string1 == null ? "" : string1).compareTo(string2 == null ? "" : string2);
        }

        @Override
        public int compare(DiscoverInfo.Identity identity1, DiscoverInfo.Identity identity2) {
            int result;
            result = compare(identity1.getCategory(), identity2.getCategory());
            if (result != 0) {
                return result;
            }
            result = compare(identity1.getType(), identity2.getType());
            if (result != 0) {
                return result;
            }
            result = compare(identity1.getLanguage(), identity2.getLanguage());
            if (result != 0) {
                return result;
            }
            result = compare(identity1.getName(), identity2.getName());
            if (result != 0) {
                return result;
            }
            return 0;
        }
    });
    for (DiscoverInfo.Identity identity : discoverInfo.getIdentities()) {
        if (!identities.add(identity)) {
            return false;
        }
    }
    Set<String> features = new HashSet<>();
    for (DiscoverInfo.Feature feature : discoverInfo.getFeatures()) {
        if (!features.add(feature.getVar())) {
            return false;
        }
    }
    Set<String> formTypes = new HashSet<>();
    for (ExtensionElement packetExtension : discoverInfo.getExtensions()) if (packetExtension instanceof DataForm) {
        DataForm dataForm = (DataForm) packetExtension;
        String formType = null;
        for (FormField formField : dataForm.getFields()) {
            if (FORM_TYPE.equals(formField.getVariable())) {
                for (String value : formField.getValues()) {
                    if (formType != null && !formType.equals(value)) {
                        return false;
                    }
                    formType = value;
                }
            }
        }
        if (!formTypes.add(formType)) {
            return false;
        }
    }
    return true;
}
Also used : DiscoverInfo(org.jivesoftware.smackx.disco.packet.DiscoverInfo) ExtensionElement(org.jivesoftware.smack.packet.ExtensionElement) TreeSet(java.util.TreeSet) DataForm(org.jivesoftware.smackx.xdata.packet.DataForm) FormField(org.jivesoftware.smackx.xdata.FormField) HashSet(java.util.HashSet)

Example 45 with DataForm

use of org.jivesoftware.smackx.xdata.packet.DataForm in project Smack by igniterealtime.

the class AdHocCommandManager method processAdHocCommand.

/**
 * Process the AdHoc-Command stanza that request the execution of some
 * action of a command. If this is the first request, this method checks,
 * before executing the command, if:
 * <ul>
 *  <li>The requested command exists</li>
 *  <li>The requester has permissions to execute it</li>
 *  <li>The command has more than one stage, if so, it saves the command and
 *      session ID for further use</li>
 * </ul>
 *
 * <br>
 * <br>
 * If this is not the first request, this method checks, before executing
 * the command, if:
 * <ul>
 *  <li>The session ID of the request was stored</li>
 *  <li>The session life do not exceed the time out</li>
 *  <li>The action to execute is one of the available actions</li>
 * </ul>
 *
 * @param requestData TODO javadoc me please
 *            the stanza to process.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws NoResponseException if there was no response from the remote entity.
 * @throws InterruptedException if the calling thread was interrupted.
 */
private IQ processAdHocCommand(AdHocCommandData requestData) throws NoResponseException, NotConnectedException, InterruptedException {
    // Creates the response with the corresponding data
    AdHocCommandData response = new AdHocCommandData();
    response.setTo(requestData.getFrom());
    response.setStanzaId(requestData.getStanzaId());
    response.setNode(requestData.getNode());
    response.setId(requestData.getTo());
    String sessionId = requestData.getSessionID();
    String commandNode = requestData.getNode();
    if (sessionId == null) {
        // command exists
        if (!commands.containsKey(commandNode)) {
            // item_not_found error.
            return respondError(response, StanzaError.Condition.item_not_found);
        }
        // Create new session ID
        sessionId = StringUtils.randomString(15);
        try {
            // Create a new instance of the command with the
            // corresponding sessionid
            LocalCommand command;
            try {
                command = newInstanceOfCmd(commandNode, sessionId);
            } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
                StanzaError xmppError = StanzaError.getBuilder().setCondition(StanzaError.Condition.internal_server_error).setDescriptiveEnText(e.getMessage()).build();
                return respondError(response, xmppError);
            }
            response.setType(IQ.Type.result);
            command.setData(response);
            // enough to execute the requested command
            if (!command.hasPermission(requestData.getFrom())) {
                return respondError(response, StanzaError.Condition.forbidden);
            }
            Action action = requestData.getAction();
            // If the action is unknown then respond an error.
            if (action != null && action.equals(Action.unknown)) {
                return respondError(response, StanzaError.Condition.bad_request, AdHocCommand.SpecificErrorCondition.malformedAction);
            }
            // If the action is not execute, then it is an invalid action.
            if (action != null && !action.equals(Action.execute)) {
                return respondError(response, StanzaError.Condition.bad_request, AdHocCommand.SpecificErrorCondition.badAction);
            }
            // Increase the state number, so the command knows in witch
            // stage it is
            command.incrementStage();
            // Executes the command
            command.execute();
            if (command.isLastStage()) {
                // If there is only one stage then the command is completed
                response.setStatus(Status.completed);
            } else {
                // Else it is still executing, and is registered to be
                // available for the next call
                response.setStatus(Status.executing);
                executingCommands.put(sessionId, command);
                // See if the session sweeper thread is scheduled. If not, start it.
                maybeWindUpSessionSweeper();
            }
            // Sends the response packet
            return response;
        } catch (XMPPErrorException e) {
            // If there is an exception caused by the next, complete,
            // prev or cancel method, then that error is returned to the
            // requester.
            StanzaError error = e.getStanzaError();
            // command be removed from the executing list.
            if (StanzaError.Type.CANCEL.equals(error.getType())) {
                response.setStatus(Status.canceled);
                executingCommands.remove(sessionId);
            }
            return respondError(response, error);
        }
    } else {
        LocalCommand command = executingCommands.get(sessionId);
        // of getting the key and the value of the map.
        if (command == null) {
            return respondError(response, StanzaError.Condition.bad_request, AdHocCommand.SpecificErrorCondition.badSessionid);
        }
        // Check if the Session data has expired (default is 10 minutes)
        long creationStamp = command.getCreationDate();
        if (System.currentTimeMillis() - creationStamp > SESSION_TIMEOUT * 1000) {
            // Remove the expired session
            executingCommands.remove(sessionId);
            // Answer a not_allowed error (session-expired)
            return respondError(response, StanzaError.Condition.not_allowed, AdHocCommand.SpecificErrorCondition.sessionExpired);
        }
        /*
             * Since the requester could send two requests for the same
             * executing command i.e. the same session id, all the execution of
             * the action must be synchronized to avoid inconsistencies.
             */
        synchronized (command) {
            Action action = requestData.getAction();
            // If the action is unknown the respond an error
            if (action != null && action.equals(Action.unknown)) {
                return respondError(response, StanzaError.Condition.bad_request, AdHocCommand.SpecificErrorCondition.malformedAction);
            }
            // action then follow the actual default execute action
            if (action == null || Action.execute.equals(action)) {
                action = command.getExecuteAction();
            }
            // offered
            if (!command.isValidAction(action)) {
                return respondError(response, StanzaError.Condition.bad_request, AdHocCommand.SpecificErrorCondition.badAction);
            }
            try {
                // TODO: Check that all the required fields of the form are
                // TODO: filled, if not throw an exception. This will simplify the
                // TODO: construction of new commands
                // Since all errors were passed, the response is now a
                // result
                response.setType(IQ.Type.result);
                // Set the new data to the command.
                command.setData(response);
                if (Action.next.equals(action)) {
                    command.incrementStage();
                    DataForm dataForm = requestData.getForm();
                    command.next(new FillableForm(dataForm));
                    if (command.isLastStage()) {
                        // If it is the last stage then the command is
                        // completed
                        response.setStatus(Status.completed);
                    } else {
                        // Otherwise it is still executing
                        response.setStatus(Status.executing);
                    }
                } else if (Action.complete.equals(action)) {
                    command.incrementStage();
                    DataForm dataForm = requestData.getForm();
                    command.complete(new FillableForm(dataForm));
                    response.setStatus(Status.completed);
                    // Remove the completed session
                    executingCommands.remove(sessionId);
                } else if (Action.prev.equals(action)) {
                    command.decrementStage();
                    command.prev();
                } else if (Action.cancel.equals(action)) {
                    command.cancel();
                    response.setStatus(Status.canceled);
                    // Remove the canceled session
                    executingCommands.remove(sessionId);
                }
                return response;
            } catch (XMPPErrorException e) {
                // If there is an exception caused by the next, complete,
                // prev or cancel method, then that error is returned to the
                // requester.
                StanzaError error = e.getStanzaError();
                // command be removed from the executing list.
                if (StanzaError.Type.CANCEL.equals(error.getType())) {
                    response.setStatus(Status.canceled);
                    executingCommands.remove(sessionId);
                }
                return respondError(response, error);
            }
        }
    }
}
Also used : Action(org.jivesoftware.smackx.commands.AdHocCommand.Action) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) FillableForm(org.jivesoftware.smackx.xdata.form.FillableForm) InvocationTargetException(java.lang.reflect.InvocationTargetException) StanzaError(org.jivesoftware.smack.packet.StanzaError) AdHocCommandData(org.jivesoftware.smackx.commands.packet.AdHocCommandData) DataForm(org.jivesoftware.smackx.xdata.packet.DataForm)

Aggregations

DataForm (org.jivesoftware.smackx.xdata.packet.DataForm)65 FormField (org.jivesoftware.smackx.xdata.FormField)23 Test (org.junit.jupiter.api.Test)13 DiscoverInfo (org.jivesoftware.smackx.disco.packet.DiscoverInfo)12 Feature (com.xabber.xmpp.ssn.Feature)7 MamQueryIQ (org.jivesoftware.smackx.mam.element.MamQueryIQ)7 Date (java.util.Date)5 MamQueryArgs (org.jivesoftware.smackx.mam.MamManager.MamQueryArgs)5 OtrMode (com.xabber.xmpp.archive.OtrMode)4 LoggingValue (com.xabber.xmpp.ssn.LoggingValue)4 ArrayList (java.util.ArrayList)4 TreeSet (java.util.TreeSet)4 FillableForm (org.jivesoftware.smackx.xdata.form.FillableForm)4 LinkedList (java.util.LinkedList)3 IQ (org.jivesoftware.smack.packet.IQ)3 XmlPullParser (org.jivesoftware.smack.xml.XmlPullParser)3 Identity (org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity)3 DiscoverInfoBuilder (org.jivesoftware.smackx.disco.packet.DiscoverInfoBuilder)3 Form (org.jivesoftware.smackx.xdata.form.Form)3 DisclosureValue (com.xabber.xmpp.ssn.DisclosureValue)2