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);
}
}
}
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);
}
}
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;
}
use of org.jivesoftware.openfire.roster.RosterItem in project Openfire by igniterealtime.
the class PEPService method canProbePresence.
/**
* Returns true if the the prober is allowed to see the presence of the probee.
*
* @param prober the user that is trying to probe the presence of another user.
* @param probee the username of the uset that is being probed.
* @return true if the the prober is allowed to see the presence of the probee.
* @throws UserNotFoundException If the probee does not exist in the local server or the prober
* is not present in the roster of the probee.
*/
private boolean canProbePresence(JID prober, JID probee) throws UserNotFoundException {
Roster roster;
roster = XMPPServer.getInstance().getRosterManager().getRoster(prober.getNode());
RosterItem item = roster.getRosterItem(probee);
if (item.getSubStatus() == RosterItem.SUB_BOTH || item.getSubStatus() == RosterItem.SUB_FROM) {
return true;
}
return false;
}
use of org.jivesoftware.openfire.roster.RosterItem in project Openfire by igniterealtime.
the class PresenceAccess method canSubscribe.
@Override
public boolean canSubscribe(Node node, JID owner, JID subscriber) {
// Let node owners and sysadmins always subcribe to the node
if (node.isAdmin(owner)) {
return true;
}
XMPPServer server = XMPPServer.getInstance();
for (JID nodeOwner : node.getOwners()) {
// Give access to the owner of the roster :)
if (nodeOwner.equals(owner)) {
return true;
}
// Check that the node owner is a local user
if (server.isLocal(nodeOwner)) {
try {
Roster roster = server.getRosterManager().getRoster(nodeOwner.getNode());
RosterItem item = roster.getRosterItem(owner);
// Check that the subscriber is subscribe to the node owner's presence
return item != null && (RosterItem.SUB_BOTH == item.getSubStatus() || RosterItem.SUB_FROM == item.getSubStatus());
} catch (UserNotFoundException e) {
// Do nothing
}
} else {
// Owner of the node is a remote user. This should never happen.
Log.warn("Node with access model Presence has a remote user as owner: " + node.getNodeID());
}
}
return false;
}
Aggregations