@Override
    public IQ handleIQ( IQ iq ) throws UnauthorizedException
    {
        if ( iq.isResponse() )
        {
            return null;
        }

        final JID requester = iq.getFrom();

        if ( !XMPPServer.getInstance().getUserManager().isRegisteredUser( requester, false ) )
        {
            final IQ error = IQ.createResultIQ( iq );
            error.setError( PacketError.Condition.not_authorized );
            return error;
        }

        final User user;
        try
        {
            user = UserManager.getInstance().getUser( requester.getNode() );
        }
        catch ( UserNotFoundException e )
        {
            Log.error( "Unable to retrieve user '{}' that was verified to be an existing user!", requester.getNode(), e );
            final IQ error = IQ.createResultIQ( iq );
            error.setError( PacketError.Condition.internal_server_error );
            return error;
        }

        try
        {
            if ( iq.getType().equals( IQ.Type.get ) && "blocklist".equals( iq.getChildElement().getName() ) )
            {
                final Set<JID> blocklist = getBlocklist( user );
                final IQ response = IQ.createResultIQ( iq );

                final Element blocklistElement = DocumentHelper.createElement( QName.get( getInfo().getName(), getInfo().getNamespace() ) );
                for ( final JID blocked : blocklist )
                {
                    blocklistElement.addElement( "item" ).addAttribute( "jid", blocked.toString() );
                }
                response.setChildElement( blocklistElement );

                sessionManager.getSession( iq.getFrom() ).setHasRequestedBlocklist( true );
                return response;
            }
            else if ( iq.getType().equals( IQ.Type.set ) && "block".equals( iq.getChildElement().getName() ) )
            {
                final List<Element> items = iq.getChildElement().elements( "item" );
                if ( items == null || items.isEmpty() )
                {
                    final IQ error = IQ.createResultIQ( iq );
                    error.setError( PacketError.Condition.bad_request );
                    return error;
                }

                final List<JID> toBlocks = new ArrayList<>();
                for ( final Element item : items )
                {
                    toBlocks.add( new JID( item.attributeValue( "jid" ) ) );
                }

                addToBlockList( user, toBlocks );
                pushBlocklistUpdates( user, toBlocks );

                return IQ.createResultIQ( iq );
            }
            else if ( iq.getType().equals( IQ.Type.set ) && "unblock".equals( iq.getChildElement().getName() ) )
            {
                final Set<JID> unblocked;
                final List<Element> items = iq.getChildElement().elements( "item" );
                if ( items == null || items.isEmpty() )
                {
                    unblocked = removeAllFromBlocklist( user );
                }
                else
                {
                    final Collection<JID> toUnblocks = new ArrayList<>();
                    for ( final Element item : items )
                    {
                        toUnblocks.add( new JID( item.attributeValue( "jid" ) ) );
                    }
                    unblocked = removeFromBlockList( user, toUnblocks );
                }

                pushBlocklistUpdates( user, unblocked );
                sendPresence( user, unblocked );

                return IQ.createResultIQ( iq );
            }
            else
            {
                final IQ error = IQ.createResultIQ( iq );
                error.setError( PacketError.Condition.feature_not_implemented );
                return error;
            }
        }
        catch ( Exception e )
        {
            Log.warn( "An exception occurred while trying to process an IQ block request from '{}':", iq.getFrom(), e );
            final IQ error = IQ.createResultIQ( iq );
            error.setError( PacketError.Condition.internal_server_error );
            return error;
        }
    }
Implementation of XEP-0191 "Blocking Command".

This implementation uses the default privacy list of a user to store its blocklist.

@author Guus der Kinderen, guus.der.kinderen@gmail.com
xmppserver/src/main/java/org/jivesoftware/openfire/handler/IQBlockingHandler.java