Search in sources :

Example 6 with RosterItem

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

the class UserCreationPlugin method manageSub.

private boolean manageSub(JID target, boolean isSending, Presence.Type type, Roster roster) throws UserAlreadyExistsException, SharedGroupException {
    RosterItem item = null;
    RosterItem.AskType oldAsk;
    RosterItem.SubType oldSub = null;
    RosterItem.RecvType oldRecv;
    boolean newItem = false;
    try {
        if (roster.isRosterItem(target)) {
            item = roster.getRosterItem(target);
        } else {
            if (Presence.Type.unsubscribed == type || Presence.Type.unsubscribe == type || Presence.Type.subscribed == type) {
                // subscription approval from an unknown user
                return false;
            }
            item = roster.createRosterItem(target, false, true);
            item.setGroups(Arrays.asList("Friends"));
            roster.updateRosterItem(item);
            newItem = true;
        }
        // Get a snapshot of the item state
        oldAsk = item.getAskStatus();
        oldSub = item.getSubStatus();
        oldRecv = item.getRecvStatus();
        // Update the item state based in the received presence type
        updateState(item, type, isSending);
        // Update the roster IF the item state has changed
        if (oldAsk != item.getAskStatus() || oldSub != item.getSubStatus() || oldRecv != item.getRecvStatus()) {
            roster.updateRosterItem(item);
        } else if (newItem) {
            // Do not push items with a state of "None + Pending In"
            if (item.getSubStatus() != RosterItem.SUB_NONE || item.getRecvStatus() != RosterItem.RECV_SUBSCRIBE) {
                roster.broadcast(item, false);
            }
        }
    } catch (UserNotFoundException e) {
        // Should be there because we just checked that it's an item
        Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
    }
    return oldSub != item.getSubStatus();
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) RosterItem(org.jivesoftware.openfire.roster.RosterItem)

Example 7 with RosterItem

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

the class UserCreationPlugin method generateMessages.

public void generateMessages() {
    JiveGlobals.setProperty("conversation.maxTimeDebug", String.valueOf(DEFAULT_MAX_TIME_DEBUG));
    XMPPServer server = XMPPServer.getInstance();
    ExecutorService taskExecutor = Executors.newFixedThreadPool(8);
    for (User user : UserManager.getInstance().getUsers()) {
        final JID userJid = server.createJID(user.getUsername(), null);
        System.out.println("Creating messages for user: " + userJid.getNode());
        for (RosterItem ri : user.getRoster().getRosterItems()) {
            final JID rosterItemJid = ri.getJid();
            taskExecutor.execute(new Runnable() {

                @Override
                public void run() {
                    for (int j = 0; j < NUMBER_CONVERSATION; j++) {
                        String thread = RandomStringUtils.randomAlphanumeric(6);
                        for (int i = 0; i < NUMBER_MESSAGES; i++) {
                            if (i % 2 == 0) {
                                Message msg = new Message();
                                msg.setBody("Hello to " + rosterItemJid.getNode() + " from " + userJid.getNode() + ", conversation number " + j + " of " + NUMBER_CONVERSATION + ", message " + i + " of " + NUMBER_MESSAGES + " thread " + thread);
                                msg.setType(Message.Type.chat);
                                msg.setFrom(userJid);
                                msg.setTo(rosterItemJid);
                                msg.setThread(thread);
                                XMPPServer.getInstance().getMessageRouter().route(msg);
                                try {
                                    /* otherwise monitoring plugin stores messages out of order */
                                    Thread.sleep(1);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            } else {
                                Message msg = new Message();
                                msg.setBody("Hello to " + userJid.getNode() + " from " + rosterItemJid.getNode() + ", conversation number " + j + " of " + NUMBER_CONVERSATION + ", message " + i + " of " + NUMBER_MESSAGES + " thread " + thread);
                                msg.setType(Message.Type.chat);
                                msg.setFrom(rosterItemJid);
                                msg.setTo(userJid);
                                msg.setThread(thread);
                                XMPPServer.getInstance().getMessageRouter().route(msg);
                                try {
                                    /* otherwise monitoring plugin stores messages out of order */
                                    Thread.sleep(1);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                        try {
                            Thread.sleep(DEFAULT_MAX_TIME_DEBUG);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
        }
    }
    taskExecutor.shutdown();
    try {
        taskExecutor.awaitTermination(2, TimeUnit.HOURS);
        System.out.println("Conversation generation finished");
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    JiveGlobals.deleteProperty("conversation.maxTimeDebug");
}
Also used : RosterItem(org.jivesoftware.openfire.roster.RosterItem) XMPPServer(org.jivesoftware.openfire.XMPPServer) User(org.jivesoftware.openfire.user.User) JID(org.xmpp.packet.JID) Message(org.xmpp.packet.Message) ExecutorService(java.util.concurrent.ExecutorService)

Example 8 with RosterItem

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

the class IQRosterPayloadProcessor method handleIQset.

private void handleIQset(IQ myPacket, final String subdomain, final String username) throws PacketRejectedException {
    IQ response = IQ.createResultIQ(myPacket);
    List<Node> nodes = findNodesInDocument(myPacket.getElement().getDocument(), "//roster:item");
    for (Node n : nodes) {
        Roster roster;
        String jid = n.valueOf("@jid");
        String name = n.valueOf("@name");
        String subvalue = n.valueOf("@subscription");
        // causing trouble on register:remove
        if (JiveGlobals.getBooleanProperty("plugin.remoteroster.ignoreSubdomains", true) && jid.equals(subdomain) && subvalue.equals("both"))
            throw new PacketRejectedException();
        if (subvalue.equals("both")) {
            try {
                roster = _rosterManager.getRoster(username);
                List<String> grouplist = new ArrayList<String>();
                List<Node> groupnodes = findNodesInDocument(n.getDocument(), "//roster:group");
                for (Node ne : groupnodes) {
                    String groupName = ne.getText();
                    grouplist.add(groupName);
                }
                boolean rosterPersistent = JiveGlobals.getBooleanProperty("plugin.remoteroster.persistent", true);
                Log.debug("Adding/Updating Contact " + jid + " to roster of " + username);
                try {
                    RosterItem item = roster.getRosterItem(new JID(jid));
                    item.setGroups(grouplist);
                    roster.updateRosterItem(item);
                    // dont send iq-result if just updating user
                    continue;
                } catch (UserNotFoundException exc) {
                // Then we should add him!
                }
                RosterItem item = roster.createRosterItem(new JID(jid), name, grouplist, false, rosterPersistent);
                item.setSubStatus(RosterItem.SUB_BOTH);
                roster.updateRosterItem(item);
            } catch (Exception e) {
                Log.info("Could not add user to Roster although no entry should exist..." + username, e);
            }
            dispatchPacket(response);
        } else if (subvalue.equals("remove")) {
            // we dont need to do this when persistent = false because they will get deleted as soon as gateway is unavailable
            if (JiveGlobals.getBooleanProperty("plugin.remoteroster.persistent", false) && jid.equals(subdomain)) {
                deleteSubdomainItemsFromRoster(username, subdomain);
            }
            // to handle it
            throw new PacketRejectedException();
        }
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) JID(org.xmpp.packet.JID) Node(org.dom4j.Node) IQ(org.xmpp.packet.IQ) ArrayList(java.util.ArrayList) PacketRejectedException(org.jivesoftware.openfire.interceptor.PacketRejectedException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) SharedGroupException(org.jivesoftware.openfire.SharedGroupException) RosterItem(org.jivesoftware.openfire.roster.RosterItem) Roster(org.jivesoftware.openfire.roster.Roster) PacketRejectedException(org.jivesoftware.openfire.interceptor.PacketRejectedException)

Example 9 with RosterItem

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

the class IQRosterPayloadProcessor method sendRosterToComponent.

private void sendRosterToComponent(IQ requestPacket, Collection<RosterItem> items, String subdomain) {
    IQ response = IQ.createResultIQ(requestPacket);
    response.setTo(subdomain);
    Element query = new DefaultElement(QName.get("query", "jabber:iq:roster"));
    for (RosterItem i : items) {
        String jid = i.getJid().toString();
        if (!jid.equals(subdomain) && jid.contains(subdomain)) {
            Log.debug("Roster exchange for external component " + subdomain + ". Sending user " + i.getJid().toString());
            Element item = new DefaultElement("item", null);
            item.add(new DefaultAttribute("jid", i.getJid().toString()));
            item.add(new DefaultAttribute("name", i.getNickname()));
            item.add(new DefaultAttribute("subscription", "both"));
            for (String s : i.getGroups()) {
                Element group = new DefaultElement("group");
                group.setText(s);
                item.add(group);
            }
            query.add(item);
        }
    }
    response.setChildElement(query);
    dispatchPacket(response);
}
Also used : RosterItem(org.jivesoftware.openfire.roster.RosterItem) DefaultElement(org.dom4j.tree.DefaultElement) DefaultElement(org.dom4j.tree.DefaultElement) Element(org.dom4j.Element) IQ(org.xmpp.packet.IQ) DefaultAttribute(org.dom4j.tree.DefaultAttribute)

Example 10 with RosterItem

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

the class IQRosterPayloadProcessor method deleteSubdomainItemsFromRoster.

/**
	 * Searches the users roster for a specific subdomain and deletes all contacts that contain subdomain
	 * 
	 * @param username
	 * @param subdomain
	 */
private void deleteSubdomainItemsFromRoster(String username, String subdomain) {
    try {
        Roster roster = _rosterManager.getRoster(username);
        Collection<RosterItem> items = roster.getRosterItems();
        for (RosterItem item : items) {
            String itemName = item.getJid().toString();
            if (itemName.contains(subdomain)) {
                Log.debug("Removing contact " + item.getJid().toString() + " from contact list because of Unregister.");
                roster.deleteRosterItem(item.getJid(), false);
            }
        }
    } catch (UserNotFoundException e) {
        Log.debug("Couldnt find User!" + e.toString());
    } catch (SharedGroupException e) {
        e.printStackTrace();
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) RosterItem(org.jivesoftware.openfire.roster.RosterItem) Roster(org.jivesoftware.openfire.roster.Roster) SharedGroupException(org.jivesoftware.openfire.SharedGroupException)

Aggregations

RosterItem (org.jivesoftware.openfire.roster.RosterItem)37 Roster (org.jivesoftware.openfire.roster.Roster)27 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)26 JID (org.xmpp.packet.JID)18 UserAlreadyExistsException (org.jivesoftware.openfire.user.UserAlreadyExistsException)11 ArrayList (java.util.ArrayList)10 Element (org.dom4j.Element)8 SharedGroupException (org.jivesoftware.openfire.SharedGroupException)6 User (org.jivesoftware.openfire.user.User)5 StringTokenizer (java.util.StringTokenizer)4 IQ (org.xmpp.packet.IQ)4 StringprepException (gnu.inet.encoding.StringprepException)3 ServiceException (org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException)3 ParseException (java.text.ParseException)2 Email (net.sf.jml.Email)2 DefaultElement (org.dom4j.tree.DefaultElement)2 XMPPServer (org.jivesoftware.openfire.XMPPServer)2 Group (org.jivesoftware.openfire.group.Group)2 PacketRejectedException (org.jivesoftware.openfire.interceptor.PacketRejectedException)2 UserManager (org.jivesoftware.openfire.user.UserManager)2