Commit 0eb7a17f authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gaston

Added shared groups support. JM-22


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@772 b35dd754-fafc-0310-a699-88a17e54d16e
parent 162dded8
...@@ -385,10 +385,10 @@ public class DefaultGroupProvider implements GroupProvider { ...@@ -385,10 +385,10 @@ public class DefaultGroupProvider implements GroupProvider {
try { try {
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
if (adminsOnly) { if (adminsOnly) {
pstmt = con.prepareStatement(LOAD_MEMBERS); pstmt = con.prepareStatement(LOAD_ADMINS);
} }
else { else {
pstmt = con.prepareStatement(LOAD_ADMINS); pstmt = con.prepareStatement(LOAD_MEMBERS);
} }
pstmt.setString(1, groupName); pstmt.setString(1, groupName);
ResultSet rs = pstmt.executeQuery(); ResultSet rs = pstmt.executeQuery();
......
...@@ -161,6 +161,16 @@ public class Group implements Cacheable { ...@@ -161,6 +161,16 @@ public class Group implements Cacheable {
return new MemberCollection(members, false); return new MemberCollection(members, false);
} }
/**
* Returns true if the provided username belongs to a user of the group.
*
* @param username the username to check.
* @return true if the provided username belongs to a user of the group.
*/
public boolean isUser(String username) {
return members.contains(username) || administrators.contains(username);
}
public int getCachedSize() { public int getCachedSize() {
// Approximate the size of the object in bytes by calculating the size // Approximate the size of the object in bytes by calculating the size
// of each field. // of each field.
......
...@@ -102,6 +102,11 @@ public class IQRosterHandler extends IQHandler implements ServerFeaturesProvider ...@@ -102,6 +102,11 @@ public class IQRosterHandler extends IQHandler implements ServerFeaturesProvider
} }
return returnPacket; return returnPacket;
} }
catch (SharedGroupException e) {
IQ result = IQ.createResultIQ(packet);
result.setError(PacketError.Condition.not_acceptable);
return result;
}
catch (Exception e) { catch (Exception e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e); Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
} }
...@@ -116,7 +121,8 @@ public class IQRosterHandler extends IQHandler implements ServerFeaturesProvider ...@@ -116,7 +121,8 @@ public class IQRosterHandler extends IQHandler implements ServerFeaturesProvider
* *
* @param packet The packet suspected of containing a roster removal * @param packet The packet suspected of containing a roster removal
*/ */
private void removeRosterItem(org.xmpp.packet.Roster packet) throws UnauthorizedException { private void removeRosterItem(org.xmpp.packet.Roster packet) throws UnauthorizedException,
SharedGroupException {
JID recipientJID = packet.getTo(); JID recipientJID = packet.getTo();
JID senderJID = packet.getFrom(); JID senderJID = packet.getFrom();
try { try {
...@@ -124,7 +130,7 @@ public class IQRosterHandler extends IQHandler implements ServerFeaturesProvider ...@@ -124,7 +130,7 @@ public class IQRosterHandler extends IQHandler implements ServerFeaturesProvider
if (packetItem.getSubscription() == org.xmpp.packet.Roster.Subscription.remove) { if (packetItem.getSubscription() == org.xmpp.packet.Roster.Subscription.remove) {
Roster roster = userManager.getUser(recipientJID.getNode()).getRoster(); Roster roster = userManager.getUser(recipientJID.getNode()).getRoster();
RosterItem item = roster.getRosterItem(senderJID); RosterItem item = roster.getRosterItem(senderJID);
roster.deleteRosterItem(senderJID); roster.deleteRosterItem(senderJID, true);
item.setSubStatus(RosterItem.SUB_REMOVE); item.setSubStatus(RosterItem.SUB_REMOVE);
item.setSubStatus(RosterItem.SUB_NONE); item.setSubStatus(RosterItem.SUB_NONE);
...@@ -148,7 +154,7 @@ public class IQRosterHandler extends IQHandler implements ServerFeaturesProvider ...@@ -148,7 +154,7 @@ public class IQRosterHandler extends IQHandler implements ServerFeaturesProvider
* @return Either a response to the roster update or null if the packet is corrupt and the session was closed down * @return Either a response to the roster update or null if the packet is corrupt and the session was closed down
*/ */
private IQ manageRoster(org.xmpp.packet.Roster packet) throws UnauthorizedException, private IQ manageRoster(org.xmpp.packet.Roster packet) throws UnauthorizedException,
UserAlreadyExistsException { UserAlreadyExistsException, SharedGroupException {
IQ returnPacket = null; IQ returnPacket = null;
Session session = sessionManager.getSession(packet.getFrom()); Session session = sessionManager.getSession(packet.getFrom());
...@@ -207,15 +213,15 @@ public class IQRosterHandler extends IQHandler implements ServerFeaturesProvider ...@@ -207,15 +213,15 @@ public class IQRosterHandler extends IQHandler implements ServerFeaturesProvider
* @param item The removal item element * @param item The removal item element
*/ */
private void removeItem(org.jivesoftware.messenger.roster.Roster roster, JID sender, private void removeItem(org.jivesoftware.messenger.roster.Roster roster, JID sender,
org.xmpp.packet.Roster.Item item) { org.xmpp.packet.Roster.Item item) throws SharedGroupException {
JID recipient = item.getJID(); JID recipient = item.getJID();
// Remove recipient from the sender's roster // Remove recipient from the sender's roster
roster.deleteRosterItem(item.getJID()); roster.deleteRosterItem(item.getJID(), true);
// Forward set packet to the subscriber // Forward set packet to the subscriber
if (localServer.isLocal(recipient)) { // Recipient is local so let's handle it here if (localServer.isLocal(recipient)) { // Recipient is local so let's handle it here
try { try {
Roster recipientRoster = userManager.getUser(recipient.getNode()).getRoster(); Roster recipientRoster = userManager.getUser(recipient.getNode()).getRoster();
recipientRoster.deleteRosterItem(sender); recipientRoster.deleteRosterItem(sender, true);
} }
catch (UserNotFoundException e) { catch (UserNotFoundException e) {
} }
......
...@@ -23,6 +23,7 @@ import org.jivesoftware.messenger.user.*; ...@@ -23,6 +23,7 @@ import org.jivesoftware.messenger.user.*;
import org.xmpp.packet.Presence; import org.xmpp.packet.Presence;
import org.xmpp.packet.Packet; import org.xmpp.packet.Packet;
import org.xmpp.packet.JID; import org.xmpp.packet.JID;
import org.xmpp.packet.PacketError;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.Map; import java.util.Map;
...@@ -88,14 +89,14 @@ public class PresenceSubscribeHandler extends BasicModule implements ChannelHand ...@@ -88,14 +89,14 @@ public class PresenceSubscribeHandler extends BasicModule implements ChannelHand
JID recipientJID = presence.getTo(); JID recipientJID = presence.getTo();
Presence.Type type = presence.getType(); Presence.Type type = presence.getType();
Roster roster = getRoster(senderJID); Roster roster = getRoster(senderJID);
if (roster != null) {
manageSub(recipientJID, true, type, roster);
}
roster = getRoster(recipientJID);
if (roster != null) {
manageSub(senderJID, false, type, roster);
}
try { try {
if (roster != null) {
manageSub(recipientJID, true, type, roster);
}
roster = getRoster(recipientJID);
if (roster != null) {
manageSub(senderJID, false, type, roster);
}
// Try to obtain a handler for the packet based on the routes. If the handler is // 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 // 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 // Session the packet will be routed to the client. If a route cannot be found
...@@ -106,6 +107,14 @@ public class PresenceSubscribeHandler extends BasicModule implements ChannelHand ...@@ -106,6 +107,14 @@ public class PresenceSubscribeHandler extends BasicModule implements ChannelHand
catch (NoSuchRouteException e) { catch (NoSuchRouteException e) {
deliverer.deliver(presence.createCopy()); deliverer.deliver(presence.createCopy());
} }
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) { catch (Exception e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e); Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
...@@ -126,9 +135,14 @@ public class PresenceSubscribeHandler extends BasicModule implements ChannelHand ...@@ -126,9 +135,14 @@ public class PresenceSubscribeHandler extends BasicModule implements ChannelHand
// Check for a cached roster: // Check for a cached roster:
roster = (Roster)CacheManager.getCache("username2roster").get(username); roster = (Roster)CacheManager.getCache("username2roster").get(username);
if (roster == null) { if (roster == null) {
// Not in cache so load a new one: synchronized(address.toString().intern()) {
roster = new Roster(username); roster = (Roster)CacheManager.getCache("username2roster").get(username);
CacheManager.getCache("username2roster").put(username, roster); if (roster == null) {
// Not in cache so load a new one:
roster = new Roster(username);
CacheManager.getCache("username2roster").put(username, roster);
}
}
} }
} }
return roster; return roster;
...@@ -143,8 +157,8 @@ public class PresenceSubscribeHandler extends BasicModule implements ChannelHand ...@@ -143,8 +157,8 @@ public class PresenceSubscribeHandler extends BasicModule implements ChannelHand
* @param isSending True if the request is being sent by the owner * @param isSending True if the request is being sent by the owner
* @param type The subscription change type (subscribe, unsubscribe, etc.) * @param type The subscription change type (subscribe, unsubscribe, etc.)
*/ */
private void manageSub(JID target, boolean isSending, Presence.Type type, private void manageSub(JID target, boolean isSending, Presence.Type type, Roster roster)
Roster roster) throws UserAlreadyExistsException throws UserAlreadyExistsException, SharedGroupException
{ {
try { try {
RosterItem item; RosterItem item;
......
...@@ -14,11 +14,12 @@ package org.jivesoftware.messenger.roster; ...@@ -14,11 +14,12 @@ package org.jivesoftware.messenger.roster;
import org.jivesoftware.util.IntEnum; import org.jivesoftware.util.IntEnum;
import org.jivesoftware.util.Cacheable; import org.jivesoftware.util.Cacheable;
import org.jivesoftware.util.CacheSizes; import org.jivesoftware.util.CacheSizes;
import org.jivesoftware.messenger.group.GroupManager;
import org.jivesoftware.messenger.group.GroupNotFoundException;
import org.jivesoftware.messenger.SharedGroupException;
import org.xmpp.packet.JID; import org.xmpp.packet.JID;
import java.util.List; import java.util.*;
import java.util.LinkedList;
import java.util.Iterator;
/** /**
* <p>Represents a single roster item for a User's Roster.</p> * <p>Represents a single roster item for a User's Roster.</p>
...@@ -123,6 +124,7 @@ public class RosterItem implements Cacheable { ...@@ -123,6 +124,7 @@ public class RosterItem implements Cacheable {
protected JID jid; protected JID jid;
protected String nickname; protected String nickname;
protected List<String> groups; protected List<String> groups;
protected Set<String> sharedGroups = new HashSet<String>();
protected SubType subStatus; protected SubType subStatus;
protected AskType askStatus; protected AskType askStatus;
private long rosterID; private long rosterID;
...@@ -151,9 +153,8 @@ public class RosterItem implements Cacheable { ...@@ -151,9 +153,8 @@ public class RosterItem implements Cacheable {
this.nickname = nickname; this.nickname = nickname;
this.groups = new LinkedList<String>(); this.groups = new LinkedList<String>();
if (groups != null) { if (groups != null) {
Iterator<String> groupItr = groups.iterator(); for (String group : groups) {
while (groupItr.hasNext()) { this.groups.add(group);
this.groups.add(groupItr.next());
} }
} }
} }
...@@ -208,7 +209,13 @@ public class RosterItem implements Cacheable { ...@@ -208,7 +209,13 @@ public class RosterItem implements Cacheable {
* @return The subscription status of the item * @return The subscription status of the item
*/ */
public SubType getSubStatus() { public SubType getSubStatus() {
return subStatus; if (isShared()) {
// Redefine the sub status since the item belongs to a shared group
return SUB_BOTH;
}
else {
return subStatus;
}
} }
/** /**
...@@ -226,7 +233,13 @@ public class RosterItem implements Cacheable { ...@@ -226,7 +233,13 @@ public class RosterItem implements Cacheable {
* @return The ask status of the item * @return The ask status of the item
*/ */
public AskType getAskStatus() { public AskType getAskStatus() {
return askStatus; if (isShared()) {
// Redefine the ask status since the item belongs to a shared group
return ASK_NONE;
}
else {
return askStatus;
}
} }
/** /**
...@@ -284,9 +297,9 @@ public class RosterItem implements Cacheable { ...@@ -284,9 +297,9 @@ public class RosterItem implements Cacheable {
} }
/** /**
* <p>Obtain the groups for the item.</p> * Returns the groups for the item. Shared groups won't be included in the answer.
* *
* @return The subscription status of the item * @return The groups for the item.
*/ */
public List<String> getGroups() { public List<String> getGroups() {
return groups; return groups;
...@@ -295,17 +308,77 @@ public class RosterItem implements Cacheable { ...@@ -295,17 +308,77 @@ public class RosterItem implements Cacheable {
/** /**
* <p>Set the current groups for the item.</p> * <p>Set the current groups for the item.</p>
* *
* @param groups The subscription status of the item * @param groups The new lists of groups the item belongs to.
*/ */
public void setGroups(List<String> groups) { public void setGroups(List<String> groups) throws SharedGroupException {
if (groups == null) { if (groups == null) {
this.groups = new LinkedList<String>(); this.groups = new LinkedList<String>();
} }
else { else {
// Raise an error if the user is trying to remove the item from a shared group
for (String groupName : getSharedGroups()) {
// Check if the group has been removed from the new groups list
if (!groups.contains(groupName)) {
throw new SharedGroupException("Cannot remove item from shared group");
}
}
// Remove shared groups from the param
for (Iterator<String> it=groups.iterator(); it.hasNext();) {
try {
String group = it.next();
// Check if exists a shared group with this name
GroupManager.getInstance().getGroup(group);
// Remove the shared group from the list (since it exists)
it.remove();
}
catch (GroupNotFoundException e) {
// Do nothing since the group is a personal group
}
}
this.groups = groups; this.groups = groups;
} }
} }
/**
* Returns the shared groups for the item.
*
* @return The shared groups this item belongs to.
*/
public Collection<String> getSharedGroups() {
return sharedGroups;
}
/**
* Adds a new group to the shared groups list.
*
* @param sharedGroup The shared group to add to the list of shared groups.
*/
public void addSharedGroups(String sharedGroup) {
sharedGroups.add(sharedGroup);
}
/**
* Returns true if this item belongs to a shared group. Return true even if the item belongs
* to a personal group and a shared group.
*
* @return true if this item belongs to a shared group.
*/
public boolean isShared() {
return !sharedGroups.isEmpty();
}
/**
* Returns true if this item belongs ONLY to shared groups. This means that the the item is
* considered to be "only shared" if it doesn't belong to a personal group but only to shared
* groups.
*
* @return true if this item belongs ONLY to shared groups.
*/
public boolean isOnlyShared() {
return !sharedGroups.isEmpty() && groups.isEmpty();
}
/** /**
* <p>Obtain the roster ID associated with this particular roster item.</p> * <p>Obtain the roster ID associated with this particular roster item.</p>
* <p/> * <p/>
...@@ -328,9 +401,9 @@ public class RosterItem implements Cacheable { ...@@ -328,9 +401,9 @@ public class RosterItem implements Cacheable {
* *
* @param item The item who's settings will be copied into the cached copy * @param item The item who's settings will be copied into the cached copy
*/ */
public void setAsCopyOf(org.xmpp.packet.Roster.Item item) { public void setAsCopyOf(org.xmpp.packet.Roster.Item item) throws SharedGroupException {
setNickname(item.getName());
setGroups(new LinkedList<String>(item.getGroups())); setGroups(new LinkedList<String>(item.getGroups()));
setNickname(item.getName());
} }
public int getCachedSize() { public int getCachedSize() {
......
...@@ -16,6 +16,7 @@ import org.jivesoftware.util.Cache; ...@@ -16,6 +16,7 @@ import org.jivesoftware.util.Cache;
import org.jivesoftware.util.CacheManager; import org.jivesoftware.util.CacheManager;
import org.jivesoftware.messenger.container.BasicModule; import org.jivesoftware.messenger.container.BasicModule;
import org.jivesoftware.messenger.user.UserNotFoundException; import org.jivesoftware.messenger.user.UserNotFoundException;
import org.jivesoftware.messenger.SharedGroupException;
import java.util.Iterator; import java.util.Iterator;
...@@ -82,7 +83,12 @@ public class RosterManager extends BasicModule { ...@@ -82,7 +83,12 @@ public class RosterManager extends BasicModule {
} }
// Remove each roster item from the user's roster // Remove each roster item from the user's roster
for (RosterItem item : roster.getRosterItems()) { for (RosterItem item : roster.getRosterItems()) {
roster.deleteRosterItem(item.getJid()); try {
roster.deleteRosterItem(item.getJid(), false);
}
catch (SharedGroupException e) {
// Do nothing. We shouldn't have this exception since we disabled the checkings
}
} }
// Remove the cached roster from memory // Remove the cached roster from memory
CacheManager.getCache("username2roster").remove(username); CacheManager.getCache("username2roster").remove(username);
...@@ -99,7 +105,12 @@ public class RosterManager extends BasicModule { ...@@ -99,7 +105,12 @@ public class RosterManager extends BasicModule {
roster = new Roster(username); roster = new Roster(username);
} }
// Remove the deleted user reference from this roster // Remove the deleted user reference from this roster
roster.deleteRosterItem(user); try {
roster.deleteRosterItem(user, false);
}
catch (SharedGroupException e) {
// Do nothing. We shouldn't have this exception since we disabled the checkings
}
} }
} }
catch (UnsupportedOperationException e) { catch (UnsupportedOperationException e) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment