Java Code Examples for org.xmpp.packet.Presence#setError()

The following examples show how to use org.xmpp.packet.Presence#setError() . These examples are extracted from open source projects. 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 Project: Openfire   File: PresenceUpdateHandler.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Handle presence updates that affect roster subscriptions.
 *
 * @param presence The presence presence to handle
 * @throws PacketException if the packet is null or the packet could not be routed.
 */
public void process(Presence presence) throws PacketException {
    try {
        process((Packet)presence);
    }
    catch (UnauthorizedException e) {
        try {
            LocalSession session = (LocalSession) sessionManager.getSession(presence.getFrom());
            presence = presence.createCopy();
            if (session != null) {
                presence.setFrom(new JID(null, session.getServerName(), null, true));
                presence.setTo(session.getAddress());
            }
            else {
                JID sender = presence.getFrom();
                presence.setFrom(presence.getTo());
                presence.setTo(sender);
            }
            presence.setError(PacketError.Condition.not_authorized);
            deliverer.deliver(presence);
        }
        catch (Exception err) {
            Log.error(LocaleUtils.getLocalizedString("admin.error"), err);
        }
    }
}
 
Example 2
Source Project: Openfire   File: ComponentStanzaHandler.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void processPresence(Presence packet) throws UnauthorizedException {
    if (session.getStatus() != Session.STATUS_AUTHENTICATED) {
        // Session is not authenticated so return error
        Presence reply = new Presence();
        reply.setID(packet.getID());
        reply.setTo(packet.getFrom());
        reply.setFrom(packet.getTo());
        reply.setError(PacketError.Condition.not_authorized);
        session.process(reply);
        return;
    }
    super.processPresence(packet);
}
 
Example 3
Source Project: Openfire   File: LocalMUCRoom.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public MUCRole joinRoom( @Nonnull String nickname,
                         @Nullable String password,
                         @Nullable HistoryRequest historyRequest,
                         @Nonnull LocalMUCUser user,
                         @Nonnull Presence presence )
    throws UnauthorizedException, UserAlreadyExistsException, RoomLockedException, ForbiddenException,
        RegistrationRequiredException, ConflictException, ServiceUnavailableException, NotAcceptableException
{
    Log.debug( "User '{}' attempts to join room '{}' using nickname '{}'.", user.getAddress(), this.getJID(), nickname );
    MUCRole joinRole;
    boolean clientOnlyJoin; // A "client only join" here is one where the client is already joined, but has re-joined.

    lock.writeLock().lock();
    try {
        // Determine the corresponding role based on the user's affiliation
        final JID bareJID = user.getAddress().asBareJID();
        MUCRole.Role role = getRole( bareJID );
        MUCRole.Affiliation affiliation = getAffiliation( bareJID );
        if (affiliation != MUCRole.Affiliation.owner && mucService.isSysadmin(bareJID)) {
            // The user is a system administrator of the MUC service. Treat him as an owner although he won't appear in the list of owners
            Log.debug( "User '{}' is a sysadmin. Treat as owner.", user.getAddress());
            role = MUCRole.Role.moderator;
            affiliation = MUCRole.Affiliation.owner;
        }
        Log.debug( "User '{}' role and affiliation in room '{} are determined to be: {}, {}", user.getAddress(), this.getJID(), role, affiliation );

        // Verify that the attempt meets all preconditions for joining the room.
        checkJoinRoomPreconditions( user, nickname, affiliation, password, presence );

        // Is this client already joined with this nickname?
        clientOnlyJoin = alreadyJoinedWithThisNick( user, nickname );

        // TODO up to this point, room state has not been modified, even though a write-lock has been acquired. Can we optimize concurrency by locking with only a read-lock up until here?
        if (!clientOnlyJoin)
        {
            Log.debug( "Adding user '{}' as an occupant of room '{}' using nickname '{}'.", user.getAddress(), this.getJID(), nickname );

            // Create a new role for this user in this room.
            joinRole = new LocalMUCRole(mucService, this, nickname, role, affiliation, user, presence, router);

            // See if we need to join a federated room. Note that this can be blocking!
            final Future<?> join = fmucHandler.join(joinRole);
            try
            {
                // FIXME make this properly asynchronous, instead of blocking the thread!
                join.get(5, TimeUnit.MINUTES);
            }
            catch ( InterruptedException | ExecutionException | TimeoutException e )
            {
                Log.error( "An exception occurred while processing FMUC join for user '{}' in room '{}'", joinRole.getUserAddress(), joinRole.getChatRoom(), e);
            }

            addOccupantRole( joinRole );

        } else {
            // Grab the existing one.
            Log.debug( "Skip adding user '{}' as an occupant of  room '{}' using nickname '{}', as it already is. Updating occupancy with its latest presence information.", user.getAddress(), this.getJID(), nickname );
            joinRole = occupantsByFullJID.get(user.getAddress());
            joinRole.setPresence( presence ); // OF-1581: Use latest presence information.
        }
    }
    finally {
        lock.writeLock().unlock();
    }

    Log.trace( "Notify other cluster nodes that a new occupant ('{}') joined room '{}'", joinRole.getUserAddress(), joinRole.getChatRoom() );
    CacheFactory.doClusterTask(new OccupantAddedEvent(this, joinRole));

    // Exchange initial presence information between occupants of the room.
    sendInitialPresencesToNewOccupant( joinRole );
    sendInitialPresenceToExistingOccupants( joinRole );

    // If the room has just been created send the "room locked until configuration is confirmed" message.
    // It is assumed that the room is new based on the fact that it's locked and that it was locked when it was created.
    final boolean isRoomNew = isLocked() && creationDate.getTime() == lockedTime;
    if (!isRoomNew && isLocked()) {
        // TODO Verify if it's right that this check occurs only _after_ a join was deemed 'successful' and initial presences have been exchanged.
        // http://xmpp.org/extensions/xep-0045.html#enter-locked
        Log.debug( "User '{}' attempts to join room '{}' that is locked (pending configuration confirmation). Sending an error.", user.getAddress(), this.getJID() );
        final Presence presenceItemNotFound = new Presence(Presence.Type.error);
        presenceItemNotFound.setError(PacketError.Condition.item_not_found);
        presenceItemNotFound.setFrom(role.getRoleAddress());
        joinRole.send(presenceItemNotFound);
    }

    sendRoomHistoryAfterJoin( user, joinRole, historyRequest );
    sendRoomSubjectAfterJoin( user, joinRole );

    if (!clientOnlyJoin) {
        // Update the date when the last occupant left the room
        setEmptyDate(null);
        // Fire event that occupant joined the room
        MUCEventDispatcher.occupantJoined(getRole().getRoleAddress(), user.getAddress(), joinRole.getNickname());
    }
    return joinRole;
}
 
Example 4
Source Project: Openfire   File: PresenceUpdateHandler.java    License: Apache License 2.0 4 votes vote down vote up
private void process(Presence presence, ClientSession session) throws UnauthorizedException, PacketException {
    try {
        Presence.Type type = presence.getType();
        // Available
        if (type == null) {
            if (session != null && session.getStatus() == Session.STATUS_CLOSED) {
                Log.warn("Rejected available presence: " + presence + " - " + session);
                return;
            }

            if (session != null) {
                session.setPresence(presence);
            }

            broadcastUpdate(presence.createCopy());

            if (session != null && !session.isInitialized()) {
                initSession(session);
                session.setInitialized(true);
            }

            // Notify the presence manager that the user is now available. The manager may
            // remove the last presence status sent by the user when he went offline.
            presenceManager.userAvailable(presence);
        }
        else if (Presence.Type.unavailable == type) {
            if (session != null) {
                session.setPresence(presence);
            }
            broadcastUpdate(presence.createCopy());
            broadcastUnavailableForDirectedPresences(presence);
            // Notify the presence manager that the user is now unavailable. The manager may
            // save the last presence status sent by the user and keep track when the user
            // went offline.
            presenceManager.userUnavailable(presence);
        }
        else {
            presence = presence.createCopy();
            if (session != null) {
                presence.setFrom(new JID(null, session.getServerName(), null, true));
                presence.setTo(session.getAddress());
            }
            else {
                JID sender = presence.getFrom();
                presence.setFrom(presence.getTo());
                presence.setTo(sender);
            }
            presence.setError(PacketError.Condition.bad_request);
            deliverer.deliver(presence);
        }

    }
    catch (Exception e) {
        Log.error(LocaleUtils.getLocalizedString("admin.error") + ". Triggered by packet: " + presence, e);
    }
}