Search in sources :

Example 1 with RosterManager

use of org.jivesoftware.openfire.roster.RosterManager in project Openfire by igniterealtime.

the class UserCreationPlugin method populateRosters.

public void populateRosters(String userPrefix, int from, int total, int usersPerRoster) {
    XMPPServer server = XMPPServer.getInstance();
    RosterManager rosterManager = server.getRosterManager();
    int batchTotal = total / usersPerRoster;
    System.out.println("Total batches of users: " + batchTotal);
    for (int batchNumber = 0; batchNumber < batchTotal; batchNumber++) {
        System.out.println("Current batch: " + batchNumber + ". Users: " + batchNumber * usersPerRoster + " - " + ((batchNumber * usersPerRoster) + usersPerRoster));
        // Add rosters items between connected users
        for (int i = (batchNumber * usersPerRoster) + from; i < (batchNumber * usersPerRoster) + usersPerRoster + from; i++) {
            String username = userPrefix + i;
            Roster roster;
            try {
                roster = rosterManager.getRoster(username);
            } catch (UserNotFoundException e) {
                continue;
            }
            if (roster.getRosterItems().size() >= usersPerRoster) {
                // Roster already populated. Skip it.
                continue;
            }
            for (int j = (batchNumber * usersPerRoster) + from; j < (batchNumber * usersPerRoster) + usersPerRoster + from; j++) {
                if (i == j) {
                    continue;
                }
                try {
                    Roster recipientRoster = rosterManager.getRoster(userPrefix + j);
                    manageSub(server.createJID(userPrefix + j, null), true, Presence.Type.subscribe, roster);
                    manageSub(server.createJID(username, null), false, Presence.Type.subscribe, recipientRoster);
                    manageSub(server.createJID(userPrefix + j, null), true, Presence.Type.subscribed, roster);
                    manageSub(server.createJID(username, null), false, Presence.Type.subscribed, recipientRoster);
                } catch (UserNotFoundException e) {
                // Ignore
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    System.out.println("Rosters populated with " + usersPerRoster + " contacts.");
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) XMPPServer(org.jivesoftware.openfire.XMPPServer) Roster(org.jivesoftware.openfire.roster.Roster) RosterManager(org.jivesoftware.openfire.roster.RosterManager) DocumentException(org.dom4j.DocumentException) SharedGroupException(org.jivesoftware.openfire.SharedGroupException) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException)

Example 2 with RosterManager

use of org.jivesoftware.openfire.roster.RosterManager in project Openfire by igniterealtime.

the class IQPEPHandler method handleIQ.

// *****************************************************************
// *** Generic module management ends here. From this point on   ***
// *** more specific PEP related implementation after this point ***
// *****************************************************************
/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.jivesoftware.openfire.handler.IQHandler#handleIQ(org.xmpp.packet.IQ)
	 */
@Override
public IQ handleIQ(IQ packet) throws UnauthorizedException {
    // Do nothing if server is not enabled
    if (!isEnabled()) {
        IQ reply = IQ.createResultIQ(packet);
        reply.setChildElement(packet.getChildElement().createCopy());
        reply.setError(PacketError.Condition.service_unavailable);
        return reply;
    }
    final JID senderJID = packet.getFrom();
    if (packet.getTo() == null) {
        // packet addressed to service itself (not to a node/user)
        final String jidFrom = senderJID.toBareJID();
        packet = packet.createCopy();
        packet.setTo(jidFrom);
        if (packet.getType() == IQ.Type.set) {
            PEPService pepService = pepServiceManager.getPEPService(jidFrom);
            // If no service exists yet for jidFrom, create one.
            if (pepService == null) {
                try {
                    pepService = pepServiceManager.create(senderJID);
                } catch (IllegalArgumentException ex) {
                    final IQ reply = IQ.createResultIQ(packet);
                    reply.setChildElement(packet.getChildElement().createCopy());
                    reply.setError(PacketError.Condition.not_allowed);
                    return reply;
                }
                // Probe presences
                pepServiceManager.start(pepService);
                // PEPService.
                try {
                    final RosterManager rm = XMPPServer.getInstance().getRosterManager();
                    final Roster roster = rm.getRoster(senderJID.getNode());
                    for (final RosterItem item : roster.getRosterItems()) {
                        if (item.getSubStatus() == RosterItem.SUB_BOTH || item.getSubStatus() == RosterItem.SUB_FROM) {
                            createSubscriptionToPEPService(pepService, item.getJid(), senderJID);
                        }
                    }
                } catch (UserNotFoundException e) {
                // Do nothing
                }
            }
            // If publishing a node, and the node doesn't exist, create it.
            final Element childElement = packet.getChildElement();
            final Element publishElement = childElement.element("publish");
            if (publishElement != null) {
                final String nodeID = publishElement.attributeValue("node");
                // TODO: Implement XEP-0084
                if (nodeID.startsWith("http://www.xmpp.org/extensions/xep-0084.html")) {
                    IQ reply = IQ.createResultIQ(packet);
                    reply.setChildElement(packet.getChildElement().createCopy());
                    reply.setError(PacketError.Condition.feature_not_implemented);
                    return reply;
                }
                if (pepService.getNode(nodeID) == null) {
                    // Create the node
                    final JID creator = new JID(jidFrom);
                    final LeafNode newNode = new LeafNode(pepService, pepService.getRootCollectionNode(), nodeID, creator);
                    newNode.addOwner(creator);
                    newNode.saveToDB();
                }
            }
            // Process with PubSub as usual.
            pepServiceManager.process(pepService, packet);
        } else if (packet.getType() == IQ.Type.get) {
            final PEPService pepService = pepServiceManager.getPEPService(jidFrom);
            if (pepService != null) {
                pepServiceManager.process(pepService, packet);
            } else {
                // Process with PubSub using a dummyService. In the case where an IQ packet is sent to
                // a user who does not have a PEP service, we wish to utilize the error reporting flow
                // already present in the PubSubEngine. This gives the illusion that every user has a
                // PEP service, as required by the specification.
                PEPService dummyService = new PEPService(XMPPServer.getInstance(), senderJID.toBareJID());
                pepServiceManager.process(dummyService, packet);
            }
        }
    } else if (packet.getType() == IQ.Type.get || packet.getType() == IQ.Type.set) {
        // packet was addressed to a node.
        final String jidTo = packet.getTo().toBareJID();
        final PEPService pepService = pepServiceManager.getPEPService(jidTo);
        if (pepService != null) {
            pepServiceManager.process(pepService, packet);
        } else {
            // Process with PubSub using a dummyService. In the case where an IQ packet is sent to
            // a user who does not have a PEP service, we wish to utilize the error reporting flow
            // already present in the PubSubEngine. This gives the illusion that every user has a
            // PEP service, as required by the specification.
            PEPService dummyService = new PEPService(XMPPServer.getInstance(), senderJID.toBareJID());
            pepServiceManager.process(dummyService, packet);
        }
    } else {
        // Ignore IQ packets of type 'error' or 'result'.
        return null;
    }
    // Other error flows were handled in pubSubEngine.process(...)
    return null;
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) RosterItem(org.jivesoftware.openfire.roster.RosterItem) JID(org.xmpp.packet.JID) Roster(org.jivesoftware.openfire.roster.Roster) Element(org.dom4j.Element) LeafNode(org.jivesoftware.openfire.pubsub.LeafNode) IQ(org.xmpp.packet.IQ) RosterManager(org.jivesoftware.openfire.roster.RosterManager)

Aggregations

Roster (org.jivesoftware.openfire.roster.Roster)2 RosterManager (org.jivesoftware.openfire.roster.RosterManager)2 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)2 DocumentException (org.dom4j.DocumentException)1 Element (org.dom4j.Element)1 SharedGroupException (org.jivesoftware.openfire.SharedGroupException)1 XMPPServer (org.jivesoftware.openfire.XMPPServer)1 LeafNode (org.jivesoftware.openfire.pubsub.LeafNode)1 RosterItem (org.jivesoftware.openfire.roster.RosterItem)1 UserAlreadyExistsException (org.jivesoftware.openfire.user.UserAlreadyExistsException)1 IQ (org.xmpp.packet.IQ)1 JID (org.xmpp.packet.JID)1