use of org.jivesoftware.openfire.user.UserNotFoundException 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.user.UserNotFoundException in project Openfire by igniterealtime.
the class XMPPCallbackHandler method handle.
@Override
public void handle(final Callback[] callbacks) throws IOException, UnsupportedCallbackException {
String realm;
String name = null;
for (Callback callback : callbacks) {
if (callback instanceof RealmCallback) {
((RealmCallback) callback).setText(XMPPServer.getInstance().getServerInfo().getXMPPDomain());
} else if (callback instanceof NameCallback) {
name = ((NameCallback) callback).getName();
if (name == null) {
name = ((NameCallback) callback).getDefaultName();
}
//Log.debug("XMPPCallbackHandler: NameCallback: " + name);
} else if (callback instanceof PasswordCallback) {
try {
// Get the password from the UserProvider. Some UserProviders may not support
// this operation
((PasswordCallback) callback).setPassword(AuthFactory.getPassword(name).toCharArray());
//Log.debug("XMPPCallbackHandler: PasswordCallback");
} catch (UserNotFoundException | UnsupportedOperationException e) {
throw new IOException(e.toString());
}
} else if (callback instanceof VerifyPasswordCallback) {
//Log.debug("XMPPCallbackHandler: VerifyPasswordCallback");
VerifyPasswordCallback vpcb = (VerifyPasswordCallback) callback;
try {
AuthToken at = AuthFactory.authenticate(name, new String(vpcb.getPassword()));
vpcb.setVerified((at != null));
} catch (Exception e) {
vpcb.setVerified(false);
}
} else if (callback instanceof AuthorizeCallback) {
//Log.debug("XMPPCallbackHandler: AuthorizeCallback");
AuthorizeCallback authCallback = ((AuthorizeCallback) callback);
// Principal that authenticated
String principal = authCallback.getAuthenticationID();
// Username requested (not full JID)
String username = authCallback.getAuthorizationID();
// a lot of users to fail to log in if their clients is sending an incorrect value
if (username != null && username.contains("@")) {
username = username.substring(0, username.lastIndexOf("@"));
}
if (principal.equals(username)) {
//client perhaps made no request, get default username
username = AuthorizationManager.map(principal);
if (Log.isDebugEnabled()) {
//Log.debug("XMPPCallbackHandler: no username requested, using " + username);
}
}
if (AuthorizationManager.authorize(username, principal)) {
if (Log.isDebugEnabled()) {
//Log.debug("XMPPCallbackHandler: " + principal + " authorized to " + username);
}
authCallback.setAuthorized(true);
authCallback.setAuthorizedID(username);
} else {
if (Log.isDebugEnabled()) {
//Log.debug("XMPPCallbackHandler: " + principal + " not authorized to " + username);
}
authCallback.setAuthorized(false);
}
} else {
if (Log.isDebugEnabled()) {
//Log.debug("XMPPCallbackHandler: Callback: " + callback.getClass().getSimpleName());
}
throw new UnsupportedCallbackException(callback, "Unrecognized Callback");
}
}
}
use of org.jivesoftware.openfire.user.UserNotFoundException 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.user.UserNotFoundException in project Openfire by igniterealtime.
the class PEPService method sendNotification.
@Override
public void sendNotification(Node node, Message message, JID recipientJID) {
message.setTo(recipientJID);
message.setFrom(getAddress());
message.setID(StringUtils.randomString(8));
// If the recipient subscribed with a bare JID and this PEPService can retrieve
// presence information for the recipient, collect all of their full JIDs and
// send the notification to each below.
Set<JID> recipientFullJIDs = new HashSet<>();
if (XMPPServer.getInstance().isLocal(recipientJID)) {
if (recipientJID.getResource() == null) {
for (ClientSession clientSession : SessionManager.getInstance().getSessions(recipientJID.getNode())) {
recipientFullJIDs.add(clientSession.getAddress());
}
}
} else {
// Since recipientJID is not local, try to get presence info from cached known remote
// presences.
// TODO: OF-605 the old code depends on a cache that would contain presence state on all (?!) JIDS on all (?!)
// remote domains. As we cannot depend on this information to be correct (even if we could ensure that this
// potentially unlimited amount of data would indeed be manageable in the first place), this code was removed.
recipientFullJIDs.add(recipientJID);
}
if (recipientFullJIDs.isEmpty()) {
router.route(message);
return;
}
for (JID recipientFullJID : recipientFullJIDs) {
// to the service owner.
try {
JID publisher = null;
// Get the ID of the node that had an item published to or retracted from.
Element itemsElement = message.getElement().element("event").element("items");
String nodeID = itemsElement.attributeValue("node");
// Get the ID of the item that was published or retracted.
String itemID = null;
Element itemElement = itemsElement.element("item");
if (itemElement == null) {
Element retractElement = itemsElement.element("retract");
if (retractElement != null) {
itemID = retractElement.attributeValue("id");
}
} else {
itemID = itemElement.attributeValue("id");
}
// Check if the recipientFullJID is interested in notifications for this node.
// If the recipient has not yet requested any notification filtering, continue and send
// the notification.
EntityCapabilities entityCaps = entityCapsManager.getEntityCapabilities(recipientFullJID);
if (entityCaps != null) {
if (!entityCaps.containsFeature(nodeID + "+notify")) {
return;
}
}
// This full JID will be used as the "replyto" address in the addressing extension.
if (node.isCollectionNode()) {
for (Node leafNode : node.getNodes()) {
if (leafNode.getNodeID().equals(nodeID)) {
publisher = leafNode.getPublishedItem(itemID).getPublisher();
// Ensure the recipientJID has access to receive notifications for items published to the leaf node.
AccessModel accessModel = leafNode.getAccessModel();
if (!accessModel.canAccessItems(leafNode, recipientFullJID, publisher)) {
return;
}
break;
}
}
} else {
publisher = node.getPublishedItem(itemID).getPublisher();
}
// Ensure the recipient is subscribed to the service owner's (publisher's) presence.
if (canProbePresence(publisher, recipientFullJID)) {
Element addresses = DocumentHelper.createElement(QName.get("addresses", "http://jabber.org/protocol/address"));
Element address = addresses.addElement("address");
address.addAttribute("type", "replyto");
address.addAttribute("jid", publisher.toString());
Message extendedMessage = message.createCopy();
extendedMessage.addExtension(new PacketExtension(addresses));
extendedMessage.setTo(recipientFullJID);
router.route(extendedMessage);
}
} catch (IndexOutOfBoundsException e) {
// Do not add addressing extension to message.
} catch (UserNotFoundException e) {
// Do not add addressing extension to message.
router.route(message);
} catch (NullPointerException e) {
try {
if (canProbePresence(getAddress(), recipientFullJID)) {
message.setTo(recipientFullJID);
}
} catch (UserNotFoundException e1) {
// Do nothing
}
router.route(message);
}
}
}
use of org.jivesoftware.openfire.user.UserNotFoundException 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