use of org.jivesoftware.openfire.SharedGroupException in project Openfire by igniterealtime.
the class PresenceSubscribeHandler method process.
@Override
public void process(Presence presence) throws PacketException {
if (presence == null) {
throw new IllegalArgumentException("Argument 'presence' cannot be null.");
}
final Presence.Type type = presence.getType();
if (type != Presence.Type.subscribe && type != Presence.Type.unsubscribe && type != Presence.Type.subscribed && type != Presence.Type.unsubscribed) {
throw new IllegalArgumentException("Packet processed by PresenceSubscribeHandler is " + "not of a subscription-related type, but: " + type);
}
// RFC-6121 paragraph 3: "When a server processes or generates an outbound presence stanza
// of type "subscribe", "subscribed", "unsubscribe", or "unsubscribed", the server MUST stamp
// the outgoing presence stanza with the bare JID <localpart@domainpart> of the sending entity,
// not the full JID <localpart@domainpart/resourcepart>."
presence.setFrom(presence.getFrom().toBareJID());
// JID and modify the 'to' address accordingly.
if (presence.getTo() != null) {
presence.setTo(presence.getTo().toBareJID());
}
final JID senderJID = presence.getFrom();
final JID recipientJID = presence.getTo();
try {
// Reject presence subscription requests sent to the local server itself.
if (recipientJID == null || recipientJID.toString().equals(serverName)) {
if (type == Presence.Type.subscribe) {
Presence reply = new Presence();
reply.setTo(senderJID);
reply.setFrom(recipientJID);
reply.setType(Presence.Type.unsubscribed);
deliverer.deliver(reply);
}
return;
}
try {
Roster senderRoster = getRoster(senderJID);
if (senderRoster != null) {
manageSub(recipientJID, true, presence, senderRoster);
}
Roster recipientRoster = getRoster(recipientJID);
boolean recipientSubChanged = false;
if (recipientRoster != null) {
recipientSubChanged = manageSub(senderJID, false, presence, recipientRoster);
}
// and the recipient user has not changed its subscription state.
if (!(type == Presence.Type.subscribed && recipientRoster != null && !recipientSubChanged)) {
// See http://tools.ietf.org/html/rfc3921#section-7 and/or OF-38
if (type == Presence.Type.subscribe && recipientRoster != null && !recipientSubChanged) {
try {
RosterItem.SubType subType = recipientRoster.getRosterItem(senderJID).getSubStatus();
if (subType == RosterItem.SUB_FROM || subType == RosterItem.SUB_BOTH) {
return;
}
} catch (UserNotFoundException e) {
// Weird case: Roster item does not exist. Should never happen
Log.error("User does not exist while trying to update roster item. " + "This should never happen (this indicates a programming " + "logic error). Processing stanza: " + presence.toString(), e);
}
}
// Try to obtain a handler for the packet based on the routes. If the handler is
// a module, the module will be able to handle the packet. If the handler is a
// Session the packet will be routed to the client. If a route cannot be found
// then the packet will be delivered based on its recipient and sender.
List<JID> jids = routingTable.getRoutes(recipientJID, null);
if (!jids.isEmpty()) {
for (JID jid : jids) {
Presence presenteToSend = presence.createCopy();
// Stamp the presence with the user's bare JID as the 'from' address,
// as required by section 8.2.5 of RFC 3921
presenteToSend.setFrom(senderJID.toBareJID());
routingTable.routePacket(jid, presenteToSend, false);
}
} else {
deliverer.deliver(presence.createCopy());
}
if (type == Presence.Type.subscribed) {
// Send the presence of the newly subscribed contact to the subscribee, by doing a presence probe.
JID prober = localServer.isLocal(recipientJID) ? recipientJID.asBareJID() : recipientJID;
if (localServer.isLocal(senderJID) && !presenceManager.canProbePresence(prober, senderJID.getNode())) {
Presence nonProbablePresence = new Presence();
nonProbablePresence.setStatus("unavailable");
nonProbablePresence.setFrom(senderJID);
nonProbablePresence.setTo(recipientJID);
presenceManager.handleProbe(nonProbablePresence);
} else {
presenceManager.probePresence(prober, senderJID);
PresenceEventDispatcher.subscribedToPresence(recipientJID, senderJID);
}
}
}
if (type == Presence.Type.unsubscribed) {
// Send unavailable presence from all of the local user's available resources
// to the remote user
presenceManager.sendUnavailableFromSessions(recipientJID, senderJID);
PresenceEventDispatcher.unsubscribedToPresence(senderJID, recipientJID);
}
} catch (SharedGroupException e) {
Presence result = presence.createCopy();
JID sender = result.getFrom();
result.setFrom(presence.getTo());
result.setTo(sender);
result.setError(PacketError.Condition.not_acceptable);
deliverer.deliver(result);
}
} catch (Exception e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
}
}
use of org.jivesoftware.openfire.SharedGroupException in project Openfire by igniterealtime.
the class JustMarriedController method addNewUserToOthersRoster.
/**
* Adds the new user to others roster.
*
* @param newUser
* the new user
* @param otherItem
* the other item
* @param currentUser
* the current user
* @throws ServiceException
* the service exception
*/
private static void addNewUserToOthersRoster(User newUser, RosterItem otherItem, String currentUser) throws ServiceException {
otherItem.getJid();
UserManager userManager = UserManager.getInstance();
// Is this user registered with our OF server?
String username = otherItem.getJid().getNode();
if (username != null && username.length() > 0 && userManager.isRegisteredUser(username) && XMPPServer.getInstance().isLocal(XMPPServer.getInstance().createJID(currentUser, null))) {
try {
User otherUser = userManager.getUser(username);
Roster otherRoster = otherUser.getRoster();
RosterItem oldUserOnOthersRoster = otherRoster.getRosterItem(XMPPServer.getInstance().createJID(currentUser, null));
try {
if (!oldUserOnOthersRoster.isOnlyShared()) {
RosterItem justCreated = otherRoster.createRosterItem(XMPPServer.getInstance().createJID(newUser.getUsername(), null), oldUserOnOthersRoster.getNickname(), oldUserOnOthersRoster.getGroups(), true, true);
justCreated.setAskStatus(oldUserOnOthersRoster.getAskStatus());
justCreated.setRecvStatus(oldUserOnOthersRoster.getRecvStatus());
justCreated.setSubStatus(oldUserOnOthersRoster.getSubStatus());
otherRoster.updateRosterItem(justCreated);
}
} catch (UserAlreadyExistsException e) {
throw new ServiceException("Could not create roster item for user ", newUser.getUsername(), ExceptionType.USER_ALREADY_EXISTS_EXCEPTION, Response.Status.CONFLICT, e);
} catch (SharedGroupException e) {
throw new ServiceException("Could not create roster item, because it is a contact from a shared group", newUser.getUsername(), ExceptionType.USER_ALREADY_EXISTS_EXCEPTION, Response.Status.BAD_REQUEST, e);
}
} catch (UserNotFoundException e) {
throw new ServiceException("Could not create roster item for user " + newUser.getUsername() + " because it is a contact from a shared group.", newUser.getUsername(), ExceptionType.USER_NOT_FOUND_EXCEPTION, Response.Status.NOT_FOUND, e);
}
}
}
use of org.jivesoftware.openfire.SharedGroupException in project Openfire by igniterealtime.
the class JustMarriedController method copyRoster.
/**
* Copy roster.
*
* @param currentUser
* the current user
* @param newUser
* the new user
* @param currentUserName
* the current user name
* @throws ServiceException
* the service exception
*/
private static void copyRoster(User currentUser, User newUser, String currentUserName) throws ServiceException {
Roster newRoster = newUser.getRoster();
Roster currentRoster = currentUser.getRoster();
for (RosterItem item : currentRoster.getRosterItems()) {
try {
List<String> groups = item.getGroups();
RosterItem justCreated = newRoster.createRosterItem(item.getJid(), item.getNickname(), groups, true, true);
justCreated.setAskStatus(item.getAskStatus());
justCreated.setRecvStatus(item.getRecvStatus());
justCreated.setSubStatus(item.getSubStatus());
for (Group gr : item.getSharedGroups()) {
justCreated.addSharedGroup(gr);
}
for (Group gr : item.getInvisibleSharedGroups()) {
justCreated.addInvisibleSharedGroup(gr);
}
newRoster.updateRosterItem(justCreated);
addNewUserToOthersRoster(newUser, item, currentUserName);
} catch (UserAlreadyExistsException e) {
throw new ServiceException("Could not create roster item for user ", newUser.getUsername(), ExceptionType.USER_ALREADY_EXISTS_EXCEPTION, Response.Status.CONFLICT, e);
} catch (SharedGroupException e) {
throw new ServiceException("Could not create roster item, because it is a contact from a shared group", newUser.getUsername(), ExceptionType.USER_ALREADY_EXISTS_EXCEPTION, Response.Status.BAD_REQUEST, e);
} catch (UserNotFoundException e) {
throw new ServiceException("Could not update roster item for user " + newUser.getUsername() + " because it was not properly created.", newUser.getUsername(), ExceptionType.USER_NOT_FOUND_EXCEPTION, Response.Status.NOT_FOUND, e);
}
}
}
use of org.jivesoftware.openfire.SharedGroupException in project Openfire by igniterealtime.
the class JustMarriedPlugin method copyRoster.
private static void copyRoster(User currentUser, User newUser, String currentUserName) {
Roster newRoster = newUser.getRoster();
Roster currentRoster = currentUser.getRoster();
for (RosterItem item : currentRoster.getRosterItems()) {
try {
List<String> groups = item.getGroups();
RosterItem justCreated = newRoster.createRosterItem(item.getJid(), item.getNickname(), groups, true, true);
justCreated.setAskStatus(item.getAskStatus());
justCreated.setRecvStatus(item.getRecvStatus());
justCreated.setSubStatus(item.getSubStatus());
for (Group gr : item.getSharedGroups()) {
justCreated.addSharedGroup(gr);
}
for (Group gr : item.getInvisibleSharedGroups()) {
justCreated.addInvisibleSharedGroup(gr);
}
newRoster.updateRosterItem(justCreated);
addNewUserToOthersRoster(newUser, item, currentUserName);
} catch (UserAlreadyExistsException e) {
Log.error("Could not create roster item for user " + item.getJid(), e);
} catch (SharedGroupException e) {
Log.error("Could not create roster item for user " + item.getJid() + " because it is a contact from a shared group", e);
} catch (UserNotFoundException e) {
Log.error("Could not update Roster item for user " + newUser.getName() + " because it was not properly created.", e);
}
}
}
Aggregations