Search in sources :

Example 71 with IQ

use of org.xmpp.packet.IQ in project Openfire by igniterealtime.

the class IQBlockingHandler method handleIQ.

@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;
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) User(org.jivesoftware.openfire.user.User) JID(org.xmpp.packet.JID) Element(org.dom4j.Element) IQ(org.xmpp.packet.IQ) PrivacyList(org.jivesoftware.openfire.privacy.PrivacyList) UnauthorizedException(org.jivesoftware.openfire.auth.UnauthorizedException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException)

Example 72 with IQ

use of org.xmpp.packet.IQ in project Openfire by igniterealtime.

the class UserManagerTest method isRegisteredUserFalseWillReturnFalseForUnknownRemoteUsers.

@Test
public void isRegisteredUserFalseWillReturnFalseForUnknownRemoteUsers() {
    final AtomicReference<IQResultListener> iqListener = new AtomicReference<>();
    doAnswer(invocationOnMock -> {
        final IQResultListener listener = invocationOnMock.getArgument(1);
        iqListener.set(listener);
        return null;
    }).when(iqRouter).addIQResultListener(any(), any(), anyLong());
    doAnswer(invocationOnMock -> {
        final IQ iq = invocationOnMock.getArgument(0);
        final Element childElement = iq.getChildElement();
        final IQ response = IQ.createResultIQ(iq);
        response.setChildElement(childElement.createCopy());
        response.setError(new PacketError(PacketError.Condition.item_not_found, PacketError.Condition.item_not_found.getDefaultType()));
        iqListener.get().receivedAnswer(response);
        return null;
    }).when(iqRouter).route(any());
    final boolean result = userManager.isRegisteredUser(new JID(USER_ID, REMOTE_XMPP_DOMAIN, null), false);
    assertThat(result, is(false));
}
Also used : IQResultListener(org.xmpp.component.IQResultListener) JID(org.xmpp.packet.JID) Element(org.dom4j.Element) IQ(org.xmpp.packet.IQ) PacketError(org.xmpp.packet.PacketError) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 73 with IQ

use of org.xmpp.packet.IQ in project Openfire by igniterealtime.

the class UserManagerTest method isRegisteredUserWillReturnFalseForUnknownRemoteUsers.

@Test
public void isRegisteredUserWillReturnFalseForUnknownRemoteUsers() {
    final AtomicReference<IQResultListener> iqListener = new AtomicReference<>();
    doAnswer(invocationOnMock -> {
        final IQResultListener listener = invocationOnMock.getArgument(1);
        iqListener.set(listener);
        return null;
    }).when(iqRouter).addIQResultListener(any(), any(), anyLong());
    doAnswer(invocationOnMock -> {
        final IQ iq = invocationOnMock.getArgument(0);
        final Element childElement = iq.getChildElement();
        final IQ response = IQ.createResultIQ(iq);
        response.setChildElement(childElement.createCopy());
        response.setError(new PacketError(PacketError.Condition.item_not_found, PacketError.Condition.item_not_found.getDefaultType()));
        iqListener.get().receivedAnswer(response);
        return null;
    }).when(iqRouter).route(any());
    final boolean result = userManager.isRegisteredUser(new JID(USER_ID, REMOTE_XMPP_DOMAIN, null));
    assertThat(result, is(false));
    verify(iqRouter).route(any());
}
Also used : IQResultListener(org.xmpp.component.IQResultListener) JID(org.xmpp.packet.JID) Element(org.dom4j.Element) IQ(org.xmpp.packet.IQ) PacketError(org.xmpp.packet.PacketError) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 74 with IQ

use of org.xmpp.packet.IQ in project Openfire by igniterealtime.

the class IQDiscoItemsHandler method handleIQ.

@Override
public IQ handleIQ(IQ packet) {
    // Create a copy of the sent pack that will be used as the reply
    // we only need to add the requested items to the reply if any otherwise add
    // a not found error
    IQ reply = IQ.createResultIQ(packet);
    // TODO Implement publishing client items
    if (IQ.Type.set == packet.getType()) {
        reply.setChildElement(packet.getChildElement().createCopy());
        reply.setError(PacketError.Condition.feature_not_implemented);
        return reply;
    }
    // Look for a DiscoItemsProvider associated with the requested entity.
    // We consider the host of the recipient JID of the packet as the entity. It's the
    // DiscoItemsProvider responsibility to provide the items associated with the JID's name
    // together with any possible requested node.
    DiscoItemsProvider itemsProvider = null;
    if ((packet.getTo() == null) || (packet.getTo().asBareJID().equals(packet.getFrom().asBareJID()))) {
        itemsProvider = getProvider(XMPPServer.getInstance().getServerInfo().getXMPPDomain());
    } else {
        itemsProvider = getProvider(packet.getTo().getDomain());
    }
    if (itemsProvider != null) {
        // Get the JID's name
        String name = packet.getTo() == null ? null : packet.getTo().getNode();
        if (name == null || name.trim().length() == 0) {
            name = null;
        }
        // Get the requested node
        Element iq = packet.getChildElement();
        String node = iq.attributeValue("node");
        // Check if we have items associated with the requested name and node
        Iterator<DiscoItem> itemsItr = itemsProvider.getItems(name, node, packet.getFrom());
        if (itemsItr != null) {
            reply.setChildElement(iq.createCopy());
            Element queryElement = reply.getChildElement();
            // See if the requesting entity would like to apply 'result set
            // management'
            final Element rsmElement = packet.getChildElement().element(QName.get("set", ResultSet.NAMESPACE_RESULT_SET_MANAGEMENT));
            // apply RSM only if the element exists, and the (total) results
            // set is not empty.
            final boolean applyRSM = rsmElement != null && itemsItr.hasNext();
            if (applyRSM) {
                if (!ResultSet.isValidRSMRequest(rsmElement)) {
                    reply.setError(PacketError.Condition.bad_request);
                    return reply;
                }
                // Calculate which results to include.
                final List<DiscoItem> rsmResults;
                final List<DiscoItem> allItems = new ArrayList<>();
                while (itemsItr.hasNext()) {
                    allItems.add(itemsItr.next());
                }
                final ResultSet<DiscoItem> rs = new ResultSetImpl<>(allItems);
                try {
                    rsmResults = rs.applyRSMDirectives(rsmElement);
                } catch (NullPointerException e) {
                    final IQ itemNotFound = IQ.createResultIQ(packet);
                    itemNotFound.setError(PacketError.Condition.item_not_found);
                    return itemNotFound;
                }
                // add the applicable results to the IQ-result
                for (DiscoItem item : rsmResults) {
                    final Element resultElement = item.getElement();
                    resultElement.setQName(new QName(resultElement.getName(), queryElement.getNamespace()));
                    queryElement.add(resultElement.createCopy());
                }
                // overwrite the 'set' element.
                queryElement.remove(queryElement.element(QName.get("set", ResultSet.NAMESPACE_RESULT_SET_MANAGEMENT)));
                queryElement.add(rs.generateSetElementFromResults(rsmResults));
            } else {
                // don't apply RSM:
                // Add to the reply all the items provided by the DiscoItemsProvider
                Element item;
                while (itemsItr.hasNext()) {
                    item = itemsItr.next().getElement();
                    item.setQName(new QName(item.getName(), queryElement.getNamespace()));
                    queryElement.add(item.createCopy());
                }
            }
        } else {
            // If the DiscoItemsProvider has no items for the requested name and node
            // then answer a not found error
            reply.setChildElement(packet.getChildElement().createCopy());
            reply.setError(PacketError.Condition.item_not_found);
        }
    } else {
        // If we didn't find a DiscoItemsProvider then answer a not found error
        reply.setChildElement(packet.getChildElement().createCopy());
        reply.setError(PacketError.Condition.item_not_found);
    }
    return reply;
}
Also used : ResultSetImpl(org.xmpp.resultsetmanagement.ResultSetImpl) QName(org.dom4j.QName) DefaultElement(org.dom4j.tree.DefaultElement) Element(org.dom4j.Element) IQ(org.xmpp.packet.IQ)

Example 75 with IQ

use of org.xmpp.packet.IQ in project Openfire by igniterealtime.

the class IQDiscoInfoHandler method handleIQ.

@Override
public IQ handleIQ(IQ packet) {
    // Create a copy of the sent pack that will be used as the reply
    // we only need to add the requested info to the reply if any otherwise add
    // a not found error
    IQ reply = IQ.createResultIQ(packet);
    // Look for a DiscoInfoProvider associated with the requested entity.
    // We consider the host of the recipient JID of the packet as the entity. It's the
    // DiscoInfoProvider responsibility to provide information about the JID's name together
    // with any possible requested node.
    DiscoInfoProvider infoProvider = getProvider(packet.getTo() == null ? XMPPServer.getInstance().getServerInfo().getXMPPDomain() : packet.getTo().getDomain());
    if (infoProvider != null) {
        // Get the JID's name
        String name = packet.getTo() == null ? null : packet.getTo().getNode();
        if (name == null || name.trim().length() == 0) {
            name = null;
        }
        // Get the requested node
        Element iq = packet.getChildElement();
        String node = iq.attributeValue("node");
        // appears to be such a request.
        if (node != null && node.startsWith(EntityCapabilitiesManager.OPENFIRE_IDENTIFIER_NODE + "#")) {
            node = null;
        }
        // Check if we have information about the requested name and node
        if (infoProvider.hasInfo(name, node, packet.getFrom())) {
            reply.setChildElement(iq.createCopy());
            Element queryElement = reply.getChildElement();
            // Add to the reply all the identities provided by the DiscoInfoProvider
            Element identity;
            Iterator<Element> identities = infoProvider.getIdentities(name, node, packet.getFrom());
            while (identities.hasNext()) {
                identity = identities.next();
                identity.setQName(new QName(identity.getName(), queryElement.getNamespace()));
                queryElement.add((Element) identity.clone());
            }
            // Add to the reply all the features provided by the DiscoInfoProvider
            Iterator<String> features = infoProvider.getFeatures(name, node, packet.getFrom());
            boolean hasDiscoInfoFeature = false;
            boolean hasDiscoItemsFeature = false;
            boolean hasResultSetManagementFeature = false;
            while (features.hasNext()) {
                final String feature = features.next();
                queryElement.addElement("feature").addAttribute("var", feature);
                if (feature.equals(NAMESPACE_DISCO_INFO)) {
                    hasDiscoInfoFeature = true;
                } else if (feature.equals("http://jabber.org/protocol/disco#items")) {
                    hasDiscoItemsFeature = true;
                } else if (feature.equals(ResultSet.NAMESPACE_RESULT_SET_MANAGEMENT)) {
                    hasResultSetManagementFeature = true;
                }
            }
            if (hasDiscoItemsFeature && !hasResultSetManagementFeature) {
                // IQDiscoItemsHandler provides result set management
                // support.
                queryElement.addElement("feature").addAttribute("var", ResultSet.NAMESPACE_RESULT_SET_MANAGEMENT);
            }
            if (!hasDiscoInfoFeature) {
                // XEP-0030 requires that every entity that supports service
                // discovery broadcasts the disco#info feature.
                queryElement.addElement("feature").addAttribute("var", NAMESPACE_DISCO_INFO);
                // dicovery broadcasts the conversion between 'PEP' and 'Private Storage' feature
                if (XMPPServer.getInstance().getPrivateStorage().isEnabled()) {
                    // allow only if private storage is enabled
                    queryElement.addElement("feature").addAttribute("var", "urn:xmpp:bookmarks-conversion:0");
                }
            }
            // Add to the reply the multiple extended info (XDataForm) provided by the DiscoInfoProvider
            final Set<DataForm> dataForms = infoProvider.getExtendedInfos(name, node, packet.getFrom());
            if (dataForms != null) {
                dataForms.forEach(dataForm -> queryElement.add(dataForm.getElement()));
            }
        } else {
            // If the DiscoInfoProvider has no information for the requested name and node
            // then answer a not found error
            reply.setChildElement(packet.getChildElement().createCopy());
            reply.setError(PacketError.Condition.item_not_found);
        }
    } else {
        // If we didn't find a DiscoInfoProvider then answer a not found error
        reply.setChildElement(packet.getChildElement().createCopy());
        reply.setError(PacketError.Condition.item_not_found);
    }
    return reply;
}
Also used : QName(org.dom4j.QName) Element(org.dom4j.Element) IQ(org.xmpp.packet.IQ) DataForm(org.xmpp.forms.DataForm)

Aggregations

IQ (org.xmpp.packet.IQ)208 Element (org.dom4j.Element)141 JID (org.xmpp.packet.JID)49 PacketError (org.xmpp.packet.PacketError)35 Presence (org.xmpp.packet.Presence)19 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)18 Message (org.xmpp.packet.Message)17 UnauthorizedException (org.jivesoftware.openfire.auth.UnauthorizedException)16 ClientSession (org.jivesoftware.openfire.session.ClientSession)14 DataForm (org.xmpp.forms.DataForm)13 ArrayList (java.util.ArrayList)11 AgentNotFoundException (org.jivesoftware.xmpp.workgroup.AgentNotFoundException)10 Packet (org.xmpp.packet.Packet)10 PacketException (org.jivesoftware.openfire.PacketException)9 User (org.jivesoftware.openfire.user.User)8 List (java.util.List)7 PrivacyList (org.jivesoftware.openfire.privacy.PrivacyList)7 Iterator (java.util.Iterator)6 Test (org.junit.Test)6 FormField (org.xmpp.forms.FormField)6