Search in sources :

Example 26 with XMPPError

use of org.jivesoftware.smack.packet.XMPPError in project xabber-android by redsolution.

the class MUCManager method joinRoom.

/**
 * Requests to join to the room.
 *
 * @param requested Whether user request to join the room.
 */
public void joinRoom(final AccountJid account, final EntityBareJid room, boolean requested) {
    final RoomChat roomChat;
    final Resourcepart nickname;
    final String password;
    roomChat = getRoomChat(account, room);
    if (roomChat == null) {
        Application.getInstance().onError(R.string.ENTRY_IS_NOT_FOUND);
        return;
    }
    RoomState state = roomChat.getState();
    if (state == RoomState.available || state == RoomState.occupation) {
        Application.getInstance().onError(R.string.ALREADY_JOINED);
        return;
    }
    if (state == RoomState.creating || state == RoomState.joining) {
        Application.getInstance().onError(R.string.ALREADY_IN_PROGRESS);
        return;
    }
    nickname = roomChat.getNickname();
    password = roomChat.getPassword();
    requestToWriteRoom(account, room, nickname, password, true);
    AccountItem accountItem = AccountManager.getInstance().getAccount(account);
    if (accountItem == null) {
        return;
    }
    final MultiUserChat multiUserChat;
    try {
        multiUserChat = MultiUserChatManager.getInstanceFor(accountItem.getConnection()).getMultiUserChat(room);
    } catch (IllegalStateException e) {
        Application.getInstance().onError(R.string.NOT_CONNECTED);
        return;
    }
    roomChat.setState(RoomState.joining);
    roomChat.setMultiUserChat(multiUserChat);
    roomChat.setRequested(requested);
    Application.getInstance().runInBackgroundUserRequest(new Runnable() {

        @Override
        public void run() {
            try {
                if (roomChat.getMultiUserChat() != multiUserChat) {
                    return;
                }
                multiUserChat.join(nickname, password);
                Application.getInstance().runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        if (roomChat.getMultiUserChat() != multiUserChat) {
                            return;
                        }
                        if (roomChat.getState() == RoomState.joining) {
                            roomChat.setState(RoomState.occupation);
                        }
                        removeAuthorizationError(account, room);
                        try {
                            RosterManager.onContactChanged(account, UserJid.from(room));
                            VCardManager.getInstance().request(account, room);
                        } catch (UserJid.UserJidCreateException e) {
                            LogManager.exception(this, e);
                        }
                    }
                });
                return;
            } catch (final XMPPException.XMPPErrorException e) {
                Application.getInstance().runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        if (roomChat.getMultiUserChat() != multiUserChat) {
                            return;
                        }
                        roomChat.setState(RoomState.error);
                        addAuthorizationError(account, room);
                        XMPPError xmppError = e.getXMPPError();
                        if (xmppError != null && xmppError.getCondition() == XMPPError.Condition.conflict) {
                            Application.getInstance().onError(R.string.NICK_ALREADY_USED);
                        } else if (xmppError != null && xmppError.getCondition() == XMPPError.Condition.not_authorized) {
                            Application.getInstance().onError(R.string.AUTHENTICATION_FAILED);
                        } else {
                            Application.getInstance().onError(R.string.NOT_CONNECTED);
                        }
                        try {
                            RosterManager.onContactChanged(account, UserJid.from(room));
                        } catch (UserJid.UserJidCreateException e) {
                            LogManager.exception(this, e);
                        }
                    }
                });
                return;
            } catch (Exception e) {
                LogManager.exception(this, e);
            }
            Application.getInstance().runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    if (roomChat.getMultiUserChat() != multiUserChat) {
                        return;
                    }
                    roomChat.setState(RoomState.waiting);
                    Application.getInstance().onError(R.string.NOT_CONNECTED);
                    try {
                        RosterManager.onContactChanged(account, UserJid.from(room));
                    } catch (UserJid.UserJidCreateException e) {
                        LogManager.exception(this, e);
                    }
                }
            });
        }
    });
}
Also used : MultiUserChat(org.jivesoftware.smackx.muc.MultiUserChat) AccountItem(com.xabber.android.data.account.AccountItem) UserJid(com.xabber.android.data.entity.UserJid) XMPPError(org.jivesoftware.smack.packet.XMPPError) SmackException(org.jivesoftware.smack.SmackException) MultiUserChatException(org.jivesoftware.smackx.muc.MultiUserChatException) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) NetworkException(com.xabber.android.data.NetworkException) XMPPException(org.jivesoftware.smack.XMPPException) Resourcepart(org.jxmpp.jid.parts.Resourcepart)

Example 27 with XMPPError

use of org.jivesoftware.smack.packet.XMPPError in project ecf by eclipse.

the class AdHocCommandManager method respondError.

/**
 * Responds an error with an specific condition.
 *
 * @param response the response to send.
 * @param condition the condition of the error.
 * @param specificCondition the adhoc command error condition.
 */
private void respondError(AdHocCommandData response, XMPPError.Condition condition, AdHocCommand.SpecificErrorCondition specificCondition) {
    XMPPError error = new XMPPError(condition);
    error.addExtension(new AdHocCommandData.SpecificError(specificCondition));
    respondError(response, error);
}
Also used : AdHocCommandData(org.jivesoftware.smackx.packet.AdHocCommandData) XMPPError(org.jivesoftware.smack.packet.XMPPError)

Example 28 with XMPPError

use of org.jivesoftware.smack.packet.XMPPError in project ecf by eclipse.

the class AdHocCommandManager method processAdHocCommand.

/**
 * Process the AdHoc-Command packet 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
 *            the packet to process.
 */
private void processAdHocCommand(AdHocCommandData requestData) {
    // Only process requests of type SET
    if (requestData.getType() != IQ.Type.SET) {
        return;
    }
    // Creates the response with the corresponding data
    AdHocCommandData response = new AdHocCommandData();
    response.setTo(requestData.getFrom());
    response.setPacketID(requestData.getPacketID());
    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)) {
            // Requested command does not exist so return
            // item_not_found error.
            respondError(response, XMPPError.Condition.item_not_found);
            return;
        }
        // Create new session ID
        sessionId = StringUtils.randomString(15);
        try {
            // Create a new instance of the command with the
            // corresponding sessioid
            LocalCommand command = newInstanceOfCmd(commandNode, sessionId);
            response.setType(IQ.Type.RESULT);
            command.setData(response);
            // enough to execute the requested command
            if (!command.hasPermission(requestData.getFrom())) {
                respondError(response, XMPPError.Condition.forbidden);
                return;
            }
            Action action = requestData.getAction();
            // If the action is unknown then respond an error.
            if (action != null && action.equals(Action.unknown)) {
                respondError(response, XMPPError.Condition.bad_request, AdHocCommand.SpecificErrorCondition.malformedAction);
                return;
            }
            // If the action is not execute, then it is an invalid action.
            if (action != null && !action.equals(Action.execute)) {
                respondError(response, XMPPError.Condition.bad_request, AdHocCommand.SpecificErrorCondition.badAction);
                return;
            }
            // 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 reaping thread is started. If not, start it.
                if (sessionsSweeper == null) {
                    sessionsSweeper = new Thread(new Runnable() {

                        public void run() {
                            while (true) {
                                for (String sessionId : executingCommands.keySet()) {
                                    LocalCommand command = executingCommands.get(sessionId);
                                    // map.
                                    if (command != null) {
                                        long creationStamp = command.getCreationDate();
                                        // invalid session error and not a time out error.
                                        if (System.currentTimeMillis() - creationStamp > SESSION_TIMEOUT * 1000 * 2) {
                                            // Remove the expired session
                                            executingCommands.remove(sessionId);
                                        }
                                    }
                                }
                                try {
                                    Thread.sleep(1000);
                                } catch (InterruptedException ie) {
                                // Ignore.
                                }
                            }
                        }
                    });
                    sessionsSweeper.setDaemon(true);
                    sessionsSweeper.start();
                }
            }
            // Sends the response packet
            connection.sendPacket(response);
        } catch (XMPPException e) {
            // If there is an exception caused by the next, complete,
            // prev or cancel method, then that error is returned to the
            // requester.
            XMPPError error = e.getXMPPError();
            // command be removed from the executing list.
            if (XMPPError.Type.CANCEL.equals(error.getType())) {
                response.setStatus(Status.canceled);
                executingCommands.remove(sessionId);
            }
            respondError(response, error);
            e.printStackTrace();
        }
    } else {
        LocalCommand command = executingCommands.get(sessionId);
        // of getting the key and the value of the map.
        if (command == null) {
            respondError(response, XMPPError.Condition.bad_request, AdHocCommand.SpecificErrorCondition.badSessionid);
            return;
        }
        // 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)
            respondError(response, XMPPError.Condition.not_allowed, AdHocCommand.SpecificErrorCondition.sessionExpired);
            return;
        }
        /*
             * 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)) {
                respondError(response, XMPPError.Condition.bad_request, AdHocCommand.SpecificErrorCondition.malformedAction);
                return;
            }
            // action then follow the actual default execute action
            if (action == null || Action.execute.equals(action)) {
                action = command.getExecuteAction();
            }
            // offered
            if (!command.isValidAction(action)) {
                respondError(response, XMPPError.Condition.bad_request, AdHocCommand.SpecificErrorCondition.badAction);
                return;
            }
            try {
                // TODO: Check that all the requierd 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();
                    command.next(new Form(requestData.getForm()));
                    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();
                    command.complete(new Form(requestData.getForm()));
                    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);
                }
                connection.sendPacket(response);
            } catch (XMPPException e) {
                // If there is an exception caused by the next, complete,
                // prev or cancel method, then that error is returned to the
                // requester.
                XMPPError error = e.getXMPPError();
                // command be removed from the executing list.
                if (XMPPError.Type.CANCEL.equals(error.getType())) {
                    response.setStatus(Status.canceled);
                    executingCommands.remove(sessionId);
                }
                respondError(response, error);
                e.printStackTrace();
            }
        }
    }
}
Also used : Action(org.jivesoftware.smackx.commands.AdHocCommand.Action) Form(org.jivesoftware.smackx.Form) AdHocCommandData(org.jivesoftware.smackx.packet.AdHocCommandData) XMPPError(org.jivesoftware.smack.packet.XMPPError)

Example 29 with XMPPError

use of org.jivesoftware.smack.packet.XMPPError in project ecf by eclipse.

the class FileTransferManager method rejectIncomingFileTransfer.

protected void rejectIncomingFileTransfer(FileTransferRequest request) {
    StreamInitiation initiation = request.getStreamInitiation();
    IQ rejection = FileTransferNegotiator.createIQ(initiation.getPacketID(), initiation.getFrom(), initiation.getTo(), IQ.Type.ERROR);
    rejection.setError(new XMPPError(XMPPError.Condition.no_acceptable));
    connection.sendPacket(rejection);
}
Also used : StreamInitiation(org.jivesoftware.smackx.packet.StreamInitiation) IQ(org.jivesoftware.smack.packet.IQ) XMPPError(org.jivesoftware.smack.packet.XMPPError)

Example 30 with XMPPError

use of org.jivesoftware.smack.packet.XMPPError in project ecf by eclipse.

the class FileTransferNegotiator method rejectStream.

/**
 * Reject a stream initiation request from a remote user.
 *
 * @param si The Stream Initiation request to reject.
 */
public void rejectStream(final StreamInitiation si) {
    XMPPError error = new XMPPError(XMPPError.Condition.forbidden, "Offer Declined");
    IQ iqPacket = createIQ(si.getPacketID(), si.getFrom(), si.getTo(), IQ.Type.ERROR);
    iqPacket.setError(error);
    connection.sendPacket(iqPacket);
}
Also used : IQ(org.jivesoftware.smack.packet.IQ) XMPPError(org.jivesoftware.smack.packet.XMPPError)

Aggregations

XMPPError (org.jivesoftware.smack.packet.XMPPError)36 XMPPException (org.jivesoftware.smack.XMPPException)11 IQ (org.jivesoftware.smack.packet.IQ)10 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)6 SmackException (org.jivesoftware.smack.SmackException)5 XmlPullParser (org.xmlpull.v1.XmlPullParser)5 AdHocCommandData (org.jivesoftware.smackx.packet.AdHocCommandData)3 NetworkException (com.xabber.android.data.NetworkException)2 IOException (java.io.IOException)2 UnknownHostException (java.net.UnknownHostException)2 Form (org.jivesoftware.smackx.Form)2 Action (org.jivesoftware.smackx.commands.AdHocCommand.Action)2 MultiUserChat (org.jivesoftware.smackx.muc.MultiUserChat)2 StreamInitiation (org.jivesoftware.smackx.packet.StreamInitiation)2 AccountItem (com.xabber.android.data.account.AccountItem)1 ConnectionThread (com.xabber.android.data.connection.ConnectionThread)1 MessageItem (com.xabber.android.data.database.messagerealm.MessageItem)1 UserJid (com.xabber.android.data.entity.UserJid)1 Realm (io.realm.Realm)1 BufferedImage (java.awt.image.BufferedImage)1