Search in sources :

Example 26 with RosterItem

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

the class UserServicePlugin method updateRosterItem.

/**
	 * Update roster item for specified user
	 *
	 * @param username
	 *            the username of the local user to update roster item for.
	 * @param itemJID
	 *            the JID of the roster item to be updated.
	 * @param itemName
	 *            the nickname of the roster item.
	 * @param subscription
	 *            the type of subscription of the roster item. Possible values
	 *            are: -1(remove), 0(none), 1(to), 2(from), 3(both).
	 * @param groupNames
	 *            the name of a group.
	 * @throws UserNotFoundException
	 *             if the user does not exist in the local server or roster item
	 *             does not exist.
	 * @throws SharedGroupException
	 *             if roster item cannot be added to a shared group.
	 */
public void updateRosterItem(String username, String itemJID, String itemName, String subscription, String groupNames) throws UserNotFoundException, SharedGroupException {
    getUser(username);
    Roster r = rosterManager.getRoster(username);
    JID j = new JID(itemJID);
    RosterItem ri = r.getRosterItem(j);
    List<String> groups = new ArrayList<String>();
    if (groupNames != null) {
        StringTokenizer tkn = new StringTokenizer(groupNames, ",");
        while (tkn.hasMoreTokens()) {
            groups.add(tkn.nextToken());
        }
    }
    ri.setGroups(groups);
    ri.setNickname(itemName);
    if (subscription == null) {
        subscription = "0";
    }
    ri.setSubStatus(RosterItem.SubType.getTypeFromInt(Integer.parseInt(subscription)));
    r.updateRosterItem(ri);
}
Also used : RosterItem(org.jivesoftware.openfire.roster.RosterItem) StringTokenizer(java.util.StringTokenizer) Roster(org.jivesoftware.openfire.roster.Roster) JID(org.xmpp.packet.JID) ArrayList(java.util.ArrayList)

Example 27 with RosterItem

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

the class Xep227Exporter method exportRoster.

/**
   * Add roster and its groups to a DOM element
   * 
   * @param userElement
   *            DOM element
   * @param user
   *            User
   */
private void exportRoster(Element userElement, User user) {
    Element rosterElement = userElement.addElement(QUERY_ELEMENT_NAME, JABBER_IQ_ROSTER_NS);
    Collection<RosterItem> roster = user.getRoster().getRosterItems();
    for (RosterItem ri : roster) {
        Element itemElement = rosterElement.addElement(ITEM_ELEMENT_NAME);
        itemElement.addAttribute(JID_NAME, ri.getJid().toBareJID());
        itemElement.addAttribute(NAME_NAME, ri.getNickname());
        itemElement.addAttribute(SUBSCRIPTION_NAME, ri.getSubStatus().getName());
        if (ri.getAskStatus() == AskType.SUBSCRIBE) {
            itemElement.addAttribute(ASK_NAME, ASK_SUBSCRIBE_ENUM);
        }
        /**
       * Adding groups
       */
        Element groupElement = itemElement.addElement(GROUP_ELEMENT_NAME);
        List<String> groups = ri.getGroups();
        for (String group : groups) {
            groupElement.addText(group);
        }
    }
}
Also used : RosterItem(org.jivesoftware.openfire.roster.RosterItem) Element(org.dom4j.Element)

Example 28 with RosterItem

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

the class Xep227Exporter method importUserRoster.

/**
   * @param userElement
   * @param rosterItems
   * @param previousDomain
   */
@SuppressWarnings("unchecked")
private void importUserRoster(Element userElement, List<RosterItem> rosterItems, String previousDomain) {
    Log.debug("importUserRoster");
    Iterator<Element> rosterIter = userElement.elementIterator(ITEM_ELEMENT_NAME);
    while (rosterIter.hasNext()) {
        Element rosterElement = rosterIter.next();
        String jid = rosterElement.attributeValue(JID_NAME);
        String nickname = rosterElement.attributeValue(NAME_NAME);
        String substatus = rosterElement.attributeValue(SUBSCRIPTION_NAME);
        String askstatus = rosterElement.attributeValue(ASK_NAME);
        List<String> groups = new ArrayList<String>();
        Iterator<Element> groupIter = rosterElement.elementIterator(GROUP_ELEMENT_NAME);
        while (groupIter.hasNext()) {
            Element group = groupIter.next();
            String groupName = group.getText();
            if (groupName != null && groupName.trim().length() > 0) {
                groups.add(groupName);
            }
        }
        // used for migration
        if (previousDomain != null && jid != null) {
            jid = jid.replace(previousDomain, serverName);
        }
        try {
            rosterItems.add(new RosterItem(new JID(jid), (substatus != null ? SubType.valueOf(substatus.toUpperCase()) : SubType.BOTH), (ASK_SUBSCRIBE_ENUM.equals(askstatus) ? AskType.SUBSCRIBE : AskType.NONE), RecvType.NONE, nickname, groups));
        } catch (Exception e) {
            Log.warn("Adding User Roster failed:" + e.getLocalizedMessage());
            Log.debug("", e);
        }
    }
}
Also used : RosterItem(org.jivesoftware.openfire.roster.RosterItem) JID(org.xmpp.packet.JID) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) StringprepException(gnu.inet.encoding.StringprepException) ParseException(java.text.ParseException) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException)

Example 29 with RosterItem

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

the class UserServicePluginNG method addRosterItem.

/**
	 * Adds the roster item.
	 *
	 * @param username
	 *            the username
	 * @param rosterItemEntity
	 *            the roster item entity
	 * @throws ServiceException
	 *             the service exception
	 * @throws UserAlreadyExistsException
	 *             the user already exists exception
	 * @throws SharedGroupException
	 *             the shared group exception
	 * @throws UserNotFoundException
	 *             the user not found exception
	 */
public void addRosterItem(String username, RosterItemEntity rosterItemEntity) throws ServiceException, UserAlreadyExistsException, SharedGroupException, UserNotFoundException {
    Roster roster = getUserRoster(username);
    if (rosterItemEntity.getJid() == null) {
        throw new ServiceException("JID is null", "JID", "IllegalArgumentException", Response.Status.BAD_REQUEST);
    }
    JID jid = new JID(rosterItemEntity.getJid());
    try {
        roster.getRosterItem(jid);
        throw new UserAlreadyExistsException(jid.toBareJID());
    } catch (UserNotFoundException e) {
    // Roster item does not exist. Try to add it.
    }
    if (roster != null) {
        RosterItem rosterItem = roster.createRosterItem(jid, rosterItemEntity.getNickname(), rosterItemEntity.getGroups(), false, true);
        UserUtils.checkSubType(rosterItemEntity.getSubscriptionType());
        rosterItem.setSubStatus(RosterItem.SubType.getTypeFromInt(rosterItemEntity.getSubscriptionType()));
        roster.updateRosterItem(rosterItem);
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) RosterItem(org.jivesoftware.openfire.roster.RosterItem) Roster(org.jivesoftware.openfire.roster.Roster) ServiceException(org.jivesoftware.openfire.exceptions.ServiceException) JID(org.xmpp.packet.JID) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException)

Example 30 with RosterItem

use of org.jivesoftware.openfire.roster.RosterItem 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

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