Search in sources :

Example 96 with JID

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

the class RosterAccess method canSubscribe.

@Override
public boolean canSubscribe(Node node, JID owner, JID subscriber) {
    // Let node owners and sysadmins always subscribe to the node
    if (node.isAdmin(owner)) {
        return true;
    }
    for (JID nodeOwner : node.getOwners()) {
        if (nodeOwner.equals(owner)) {
            return true;
        }
    }
    // Check that the subscriber is a local user
    XMPPServer server = XMPPServer.getInstance();
    if (server.isLocal(owner)) {
        GroupManager gMgr = GroupManager.getInstance();
        Collection<String> nodeGroups = node.getRosterGroupsAllowed();
        for (String groupName : nodeGroups) {
            try {
                Group group = gMgr.getGroup(groupName);
                // access allowed if the node group is visible to the subscriber
                if (server.getRosterManager().isGroupVisible(group, owner)) {
                    return true;
                }
            } catch (GroupNotFoundException gnfe) {
            // ignore
            }
        }
    } else {
        // Subscriber is a remote user. This should never happen.
        Log.warn("Node with access model Roster has a remote user as subscriber: " + node.getNodeID());
    }
    return false;
}
Also used : Group(org.jivesoftware.openfire.group.Group) XMPPServer(org.jivesoftware.openfire.XMPPServer) JID(org.xmpp.packet.JID) GroupNotFoundException(org.jivesoftware.openfire.group.GroupNotFoundException) GroupManager(org.jivesoftware.openfire.group.GroupManager)

Example 97 with JID

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

the class PubSubEngine method probePresences.

private void probePresences(final PubSubService service) {
    Set<JID> affiliates = new HashSet<>();
    for (Node node : service.getNodes()) {
        affiliates.addAll(node.getPresenceBasedSubscribers());
    }
    for (JID jid : affiliates) {
        // Send probe presence
        Presence subscription = new Presence(Presence.Type.probe);
        subscription.setTo(jid);
        subscription.setFrom(service.getAddress());
        service.send(subscription);
    }
}
Also used : JID(org.xmpp.packet.JID) Presence(org.xmpp.packet.Presence) HashSet(java.util.HashSet)

Example 98 with JID

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

the class PubSubEngine method getAffiliations.

private void getAffiliations(PubSubService service, IQ iq, Element childElement) {
    // TODO Assuming that owner is the bare JID (as defined in the JEP). This can be replaced with an explicit owner specified in the packet
    JID owner = iq.getFrom().asBareJID();
    // Collect affiliations of owner for all nodes at the service
    Collection<NodeAffiliate> affiliations = new ArrayList<>();
    for (Node node : service.getNodes()) {
        NodeAffiliate nodeAffiliate = node.getAffiliate(owner);
        if (nodeAffiliate != null) {
            affiliations.add(nodeAffiliate);
        }
    }
    // Create reply to send
    IQ reply = IQ.createResultIQ(iq);
    Element replyChildElement = childElement.createCopy();
    reply.setChildElement(replyChildElement);
    if (affiliations.isEmpty()) {
        // User does not have any affiliation or subscription with the pubsub service
        reply.setError(PacketError.Condition.item_not_found);
    } else {
        Element affiliationsElement = replyChildElement.element("affiliations");
        // Add information about affiliations without subscriptions
        for (NodeAffiliate affiliate : affiliations) {
            Element affiliateElement = affiliationsElement.addElement("affiliation");
            // Do not include the node id when node is the root collection node
            if (!affiliate.getNode().isRootCollectionNode()) {
                affiliateElement.addAttribute("node", affiliate.getNode().getNodeID());
            }
            affiliateElement.addAttribute("jid", affiliate.getJID().toString());
            affiliateElement.addAttribute("affiliation", affiliate.getAffiliation().name());
        }
    }
    // Send reply
    router.route(reply);
}
Also used : JID(org.xmpp.packet.JID) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) IQ(org.xmpp.packet.IQ)

Example 99 with JID

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

the class PubSubModule method getItems.

@Override
public Iterator<DiscoServerItem> getItems() {
    // Check if the service is disabled. Info is not available when disabled.
    if (!isServiceEnabled()) {
        return null;
    }
    ArrayList<DiscoServerItem> items = new ArrayList<>();
    final DiscoServerItem item = new DiscoServerItem(new JID(getServiceDomain()), "Publish-Subscribe service", null, null, this, this);
    items.add(item);
    return items.iterator();
}
Also used : JID(org.xmpp.packet.JID) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) DiscoServerItem(org.jivesoftware.openfire.disco.DiscoServerItem)

Example 100 with JID

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

the class PubSubEngine method publishItemsToNode.

private void publishItemsToNode(PubSubService service, IQ iq, Element publishElement) {
    String nodeID = publishElement.attributeValue("node");
    Node node;
    JID from = iq.getFrom();
    // TODO Assuming that owner is the bare JID (as defined in the JEP). This can be replaced with an explicit owner specified in the packet
    JID owner = from.asBareJID();
    if (nodeID == null) {
        // XEP-0060 Section 7.2.3.3 - No node was specified. Return bad_request error
        // This suggests that Instant nodes should not be auto-created
        Element pubsubError = DocumentHelper.createElement(QName.get("nodeid-required", "http://jabber.org/protocol/pubsub#errors"));
        sendErrorPacket(iq, PacketError.Condition.bad_request, pubsubError);
        return;
    } else {
        // Look for the specified node
        node = service.getNode(nodeID);
        if (node == null) {
            if (service instanceof PEPService && service.isServiceAdmin(owner)) {
                // If it is a PEP service & publisher is service owner -
                // auto create nodes.
                Element childElement = iq.getChildElement();
                Element createElement = publishElement.element("publish");
                CreateNodeResponse response = createNodeHelper(service, iq, childElement, createElement);
                if (response.newNode == null) {
                    // New node creation failed. Since pep#auto-create is advertised 
                    // in disco#info, node creation error should be sent to the client.
                    sendErrorPacket(iq, response.creationStatus, response.pubsubError);
                } else {
                    // Node creation succeeded, set node to newNode.
                    node = response.newNode;
                }
            } else {
                // Node does not exist. Return item-not-found error
                sendErrorPacket(iq, PacketError.Condition.item_not_found, null);
                return;
            }
        }
    }
    if (!node.getPublisherModel().canPublish(node, owner) && !service.isServiceAdmin(owner)) {
        // Entity does not have sufficient privileges to publish to node
        sendErrorPacket(iq, PacketError.Condition.forbidden, null);
        return;
    }
    if (node.isCollectionNode()) {
        // Node is a collection node. Return feature-not-implemented error
        Element pubsubError = DocumentHelper.createElement(QName.get("unsupported", "http://jabber.org/protocol/pubsub#errors"));
        pubsubError.addAttribute("feature", "publish");
        sendErrorPacket(iq, PacketError.Condition.feature_not_implemented, pubsubError);
        return;
    }
    LeafNode leafNode = (LeafNode) node;
    Iterator itemElements = publishElement.elementIterator("item");
    // Check that an item was included if node persist items or includes payload
    if (!itemElements.hasNext() && leafNode.isItemRequired()) {
        Element pubsubError = DocumentHelper.createElement(QName.get("item-required", "http://jabber.org/protocol/pubsub#errors"));
        sendErrorPacket(iq, PacketError.Condition.bad_request, pubsubError);
        return;
    }
    // includes payload
    if (itemElements.hasNext() && !leafNode.isItemRequired()) {
        Element pubsubError = DocumentHelper.createElement(QName.get("item-forbidden", "http://jabber.org/protocol/pubsub#errors"));
        sendErrorPacket(iq, PacketError.Condition.bad_request, pubsubError);
        return;
    }
    List<Element> items = new ArrayList<>();
    List entries;
    Element payload;
    while (itemElements.hasNext()) {
        Element item = (Element) itemElements.next();
        entries = item.elements();
        payload = entries.isEmpty() ? null : (Element) entries.get(0);
        // in notifications
        if (payload == null && leafNode.isPayloadDelivered()) {
            Element pubsubError = DocumentHelper.createElement(QName.get("payload-required", "http://jabber.org/protocol/pubsub#errors"));
            sendErrorPacket(iq, PacketError.Condition.bad_request, pubsubError);
            return;
        }
        // Check that the payload (if any) contains only one child element
        if (entries.size() > 1) {
            Element pubsubError = DocumentHelper.createElement(QName.get("invalid-payload", "http://jabber.org/protocol/pubsub#errors"));
            sendErrorPacket(iq, PacketError.Condition.bad_request, pubsubError);
            return;
        }
        items.add(item);
    }
    // Return success operation
    router.route(IQ.createResultIQ(iq));
    // Publish item and send event notifications to subscribers
    leafNode.publishItems(from, items);
}
Also used : JID(org.xmpp.packet.JID) PEPService(org.jivesoftware.openfire.pep.PEPService) Element(org.dom4j.Element) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

JID (org.xmpp.packet.JID)330 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)76 Element (org.dom4j.Element)70 ArrayList (java.util.ArrayList)55 IQ (org.xmpp.packet.IQ)38 Presence (org.xmpp.packet.Presence)38 SQLException (java.sql.SQLException)36 PreparedStatement (java.sql.PreparedStatement)31 Connection (java.sql.Connection)30 Group (org.jivesoftware.openfire.group.Group)30 Date (java.util.Date)28 ResultSet (java.sql.ResultSet)27 Message (org.xmpp.packet.Message)25 GroupJID (org.jivesoftware.openfire.group.GroupJID)23 GroupNotFoundException (org.jivesoftware.openfire.group.GroupNotFoundException)23 NotFoundException (org.jivesoftware.util.NotFoundException)22 Roster (org.jivesoftware.openfire.roster.Roster)21 UnauthorizedException (org.jivesoftware.openfire.auth.UnauthorizedException)20 RosterItem (org.jivesoftware.openfire.roster.RosterItem)19 User (org.jivesoftware.openfire.user.User)17