Commit 546f70f9 authored by Derek DeMoro's avatar Derek DeMoro Committed by derek

Refactoring changes


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@613 b35dd754-fafc-0310-a699-88a17e54d16e
parent 6793c5d1
...@@ -11,23 +11,20 @@ ...@@ -11,23 +11,20 @@
package org.jivesoftware.messenger.muc.spi; package org.jivesoftware.messenger.muc.spi;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Element;
import org.jivesoftware.messenger.PacketRouter;
import org.jivesoftware.messenger.muc.ConflictException; import org.jivesoftware.messenger.muc.ConflictException;
import org.jivesoftware.messenger.muc.ForbiddenException; import org.jivesoftware.messenger.muc.ForbiddenException;
import org.jivesoftware.messenger.muc.MUCRole; import org.jivesoftware.messenger.muc.MUCRole;
import org.jivesoftware.messenger.muc.NotAllowedException; import org.jivesoftware.messenger.muc.NotAllowedException;
import org.jivesoftware.messenger.*;
import org.jivesoftware.messenger.user.UserNotFoundException; import org.jivesoftware.messenger.user.UserNotFoundException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.QName;
import org.xmpp.packet.IQ; import org.xmpp.packet.IQ;
import org.xmpp.packet.JID;
import org.xmpp.packet.PacketError; import org.xmpp.packet.PacketError;
import org.xmpp.packet.Presence; import org.xmpp.packet.Presence;
import org.xmpp.packet.JID;
/** /**
* A handler for the IQ packet with namespace http://jabber.org/protocol/muc#admin. This kind of * A handler for the IQ packet with namespace http://jabber.org/protocol/muc#admin. This kind of
...@@ -73,7 +70,7 @@ public class IQAdminHandler { ...@@ -73,7 +70,7 @@ public class IQAdminHandler {
public void handleIQ(IQ packet, MUCRole role) throws ForbiddenException, ConflictException, public void handleIQ(IQ packet, MUCRole role) throws ForbiddenException, ConflictException,
NotAllowedException { NotAllowedException {
IQ reply = IQ.createResultIQ(packet); IQ reply = IQ.createResultIQ(packet);
Element element = ((XMPPDOMFragment)packet.getChildFragment()).getRootElement(); Element element = packet.getChildElement();
// Analyze the action to perform based on the included element // Analyze the action to perform based on the included element
List itemsList = element.elements("item"); List itemsList = element.elements("item");
...@@ -123,10 +120,9 @@ public class IQAdminHandler { ...@@ -123,10 +120,9 @@ public class IQAdminHandler {
roleAttribute = item.attributeValue("role"); roleAttribute = item.attributeValue("role");
// Create the result that will hold an item for each // Create the result that will hold an item for each
// moderator/member/participant/outcast // moderator/member/participant/outcast
MetaDataFragment result = new MetaDataFragment(DocumentHelper.createElement(QName Element result = reply.setChildElement("query", "http://jabber.org/protocol/muc#admin");
.get("query", "http://jabber.org/protocol/muc#admin")));
MetaDataFragment metaData; Element metaData;
if ("outcast".equals(affiliation)) { if ("outcast".equals(affiliation)) {
// The client is requesting the list of outcasts // The client is requesting the list of outcasts
if (MUCRole.ADMINISTRATOR != senderRole.getAffiliation() if (MUCRole.ADMINISTRATOR != senderRole.getAffiliation()
...@@ -134,15 +130,11 @@ public class IQAdminHandler { ...@@ -134,15 +130,11 @@ public class IQAdminHandler {
throw new ForbiddenException(); throw new ForbiddenException();
} }
for (String jid : room.getOutcasts()) { for (String jid : room.getOutcasts()) {
metaData = new MetaDataFragment("http://jabber.org/protocol/muc#admin", metaData = result.addElement("item", "http://jabber.org/protocol/muc#admin");
"item"); metaData.addAttribute("affiliation", "outcast");
metaData.setProperty("item:affiliation", "outcast"); metaData.addAttribute("jid", jid);
metaData.setProperty("item:jid", jid);
// Add the item with the outcast's information to the result
result.addFragment(metaData);
} }
// Add the result items to the reply
reply.addFragment(result);
} }
else if ("member".equals(affiliation)) { else if ("member".equals(affiliation)) {
// The client is requesting the list of members // The client is requesting the list of members
...@@ -153,24 +145,19 @@ public class IQAdminHandler { ...@@ -153,24 +145,19 @@ public class IQAdminHandler {
throw new ForbiddenException(); throw new ForbiddenException();
} }
for (String jid : room.getMembers()) { for (String jid : room.getMembers()) {
metaData = new MetaDataFragment("http://jabber.org/protocol/muc#admin", metaData = result.addElement("item", "http://jabber.org/protocol/muc#admin");
"item"); metaData.addAttribute("affiliation", "member");
metaData.setProperty("item:affiliation", "member"); metaData.addAttribute("jid", jid);
metaData.setProperty("item:jid", jid);
try { try {
List<MUCRole> roles = room.getOccupantsByBareJID(jid); List<MUCRole> roles = room.getOccupantsByBareJID(jid);
MUCRole role = roles.get(0); MUCRole role = roles.get(0);
metaData.setProperty("item:role", role.getRoleAsString()); metaData.addAttribute("role", role.getRoleAsString());
metaData.setProperty("item:nick", role.getNickname()); metaData.addAttribute("nick", role.getNickname());
} }
catch (UserNotFoundException e) { catch (UserNotFoundException e) {
// Do nothing // Do nothing
} }
// Add the metadata to the result
result.addFragment(metaData);
} }
// Add the result items to the reply
reply.addFragment(result);
} }
else if ("moderator".equals(roleAttribute)) { else if ("moderator".equals(roleAttribute)) {
// The client is requesting the list of moderators // The client is requesting the list of moderators
...@@ -179,18 +166,12 @@ public class IQAdminHandler { ...@@ -179,18 +166,12 @@ public class IQAdminHandler {
throw new ForbiddenException(); throw new ForbiddenException();
} }
for (MUCRole role : room.getModerators()) { for (MUCRole role : room.getModerators()) {
metaData = new MetaDataFragment("http://jabber.org/protocol/muc#admin", metaData = result.addElement("item", "http://jabber.org/protocol/muc#admin");
"item"); metaData.addAttribute("role", "moderator");
metaData.setProperty("item:role", "moderator"); metaData.addAttribute("jid", role.getChatUser().getAddress().toBareJID());
metaData.setProperty("item:jid", role.getChatUser().getAddress() metaData.addAttribute("nick", role.getNickname());
.toStringPrep()); metaData.addAttribute("affiliation", role.getAffiliationAsString());
metaData.setProperty("item:nick", role.getNickname());
metaData.setProperty("item:affiliation", role.getAffiliationAsString());
// Add the metadata to the result
result.addFragment(metaData);
} }
// Add the result items to the reply
reply.addFragment(result);
} }
else if ("participant".equals(roleAttribute)) { else if ("participant".equals(roleAttribute)) {
// The client is requesting the list of participants // The client is requesting the list of participants
...@@ -198,21 +179,15 @@ public class IQAdminHandler { ...@@ -198,21 +179,15 @@ public class IQAdminHandler {
throw new ForbiddenException(); throw new ForbiddenException();
} }
for (MUCRole role : room.getParticipants()) { for (MUCRole role : room.getParticipants()) {
metaData = new MetaDataFragment("http://jabber.org/protocol/muc#admin", metaData = result.addElement("item", "http://jabber.org/protocol/muc#admin");
"item"); metaData.addAttribute("role", "participant");
metaData.setProperty("item:role", "participant"); metaData.addAttribute("jid", role.getChatUser().getAddress().toBareJID());
metaData.setProperty("item:jid", role.getChatUser().getAddress() metaData.addAttribute("nick", role.getNickname());
.toStringPrep()); metaData.addAttribute("affiliation", role.getAffiliationAsString());
metaData.setProperty("item:nick", role.getNickname());
metaData.setProperty("item:affiliation", role.getAffiliationAsString());
// Add the metadata to the result
result.addFragment(metaData);
} }
// Add the result items to the reply
reply.addFragment(result);
} }
else { else {
reply.setError(XMPPError.Code.BAD_REQUEST); reply.setError(PacketError.Condition.bad_request);
} }
} }
} }
...@@ -262,30 +237,22 @@ public class IQAdminHandler { ...@@ -262,30 +237,22 @@ public class IQAdminHandler {
} }
else if ("member".equals(target)) { else if ("member".equals(target)) {
// Add the user as a member of the room based on the bare JID // Add the user as a member of the room based on the bare JID
boolean hadAffiliation = room.getAffiliation(XMPPAddress boolean hadAffiliation = room.getAffiliation(jid.toBareJID()) != MUCRole.NONE;
.parseBareAddress(jid)) != MUCRole.NONE; presences.addAll(room.addMember(jid.toBareJID(), null, senderRole));
presences.addAll(room.addMember(
XMPPAddress.parseBareAddress(jid),
null,
senderRole));
// If the user had an affiliation don't send an invitation. Otherwise // If the user had an affiliation don't send an invitation. Otherwise
// send an invitation if the room is members-only // send an invitation if the room is members-only
if (!hadAffiliation && room.isInvitationRequiredToEnter()) { if (!hadAffiliation && room.isInvitationRequiredToEnter()) {
room.sendInvitation(jid, null, senderRole, reply room.sendInvitation(jid, null, senderRole);
.getOriginatingSession());
} }
} }
else if ("outcast".equals(target)) { else if ("outcast".equals(target)) {
// Add the user as an outcast of the room based on the bare JID // Add the user as an outcast of the room based on the bare JID
presences.addAll(room.addOutcast(XMPPAddress.parseBareAddress(jid), presences.addAll(room.addOutcast(jid.toBareJID(), item.elementTextTrim("reason"), senderRole));
item.elementTextTrim("reason"),
senderRole));
} }
else if ("none".equals(target)) { else if ("none".equals(target)) {
if (hasAffiliation) { if (hasAffiliation) {
// Set that this jid has a NONE affiliation based on the bare JID // Set that this jid has a NONE affiliation based on the bare JID
presences.addAll(room.addNone(XMPPAddress.parseBareAddress(jid), presences.addAll(room.addNone(jid.toBareJID(), senderRole));
senderRole));
} }
else { else {
// Kick the user from the room // Kick the user from the room
......
...@@ -702,7 +702,7 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -702,7 +702,7 @@ public class MUCRoomImpl implements MUCRoom {
} }
public void sendPrivateMessage(Message message, MUCRole senderRole) throws NotFoundException { public void sendPrivateMessage(Message message, MUCRole senderRole) throws NotFoundException {
String resource = message.setTo().getResource(); String resource = message.getTo().getResource();
MUCRole occupant = occupants.get(resource.toLowerCase()); MUCRole occupant = occupants.get(resource.toLowerCase());
if (occupant != null) { if (occupant != null) {
message.setFrom(senderRole.getRoleAddress()); message.setFrom(senderRole.getRoleAddress());
...@@ -1558,8 +1558,13 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1558,8 +1558,13 @@ public class MUCRoomImpl implements MUCRoom {
// of the room // of the room
for (MUCRole occupant : occupants.values()) { for (MUCRole occupant : occupants.values()) {
if (occupant.getAffiliation() > MUCRole.MEMBER) { if (occupant.getAffiliation() > MUCRole.MEMBER) {
presences.add(kickOccupant(jid, null, try {
LocaleUtils.getLocalizedString("muc.roomIsNowMembersOnly"))); presences.add(kickOccupant(occupant.getRoleAddress(), null,
LocaleUtils.getLocalizedString("muc.roomIsNowMembersOnly")));
}
catch (NotAllowedException e) {
e.printStackTrace();
}
} }
} }
} }
......
...@@ -11,9 +11,22 @@ ...@@ -11,9 +11,22 @@
package org.jivesoftware.messenger.spi; package org.jivesoftware.messenger.spi;
import java.util.*; import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import org.jivesoftware.messenger.*; import org.jivesoftware.messenger.Component;
import org.jivesoftware.messenger.ComponentManager;
import org.jivesoftware.messenger.PacketDeliverer;
import org.jivesoftware.messenger.PresenceManager;
import org.jivesoftware.messenger.RoutingTable;
import org.jivesoftware.messenger.Session;
import org.jivesoftware.messenger.SessionManager;
import org.jivesoftware.messenger.XMPPServer;
import org.jivesoftware.messenger.auth.UnauthorizedException; import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.messenger.container.BasicModule; import org.jivesoftware.messenger.container.BasicModule;
import org.jivesoftware.messenger.container.Container; import org.jivesoftware.messenger.container.Container;
...@@ -25,8 +38,8 @@ import org.jivesoftware.util.Cache; ...@@ -25,8 +38,8 @@ import org.jivesoftware.util.Cache;
import org.jivesoftware.util.JiveConstants; import org.jivesoftware.util.JiveConstants;
import org.jivesoftware.util.LocaleUtils; import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log; import org.jivesoftware.util.Log;
import org.xmpp.packet.Presence;
import org.xmpp.packet.JID; import org.xmpp.packet.JID;
import org.xmpp.packet.Presence;
/** /**
* Simple in memory implementation of the PresenceManager interface. * Simple in memory implementation of the PresenceManager interface.
...@@ -102,7 +115,7 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager ...@@ -102,7 +115,7 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager
List presences = new ArrayList(); List presences = new ArrayList();
while (iter.hasNext()) { while (iter.hasNext()) {
Presence presence = (Presence)iter.next(); Presence presence = (Presence) iter.next();
if (presence.isAvailable()) { if (presence.isAvailable()) {
presences.add(presence); presences.add(presence);
...@@ -114,13 +127,17 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager ...@@ -114,13 +127,17 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager
{ {
Collections.sort(presences, new Comparator() { Collections.sort(presences, new Comparator() {
public int compare(Object object1, Object object2) { public int compare(Object object1, Object object2) {
Presence presence1 = (Presence)object1; Presence presence1 = (Presence) object1;
Presence presence2 = (Presence)object2; Presence presence2 = (Presence) object2;
Session presence1Session = sessionManager.getSession(presence1.getFrom());
Session presence2Session = sessionManager.getSession(presence2.getFrom());
if (ascending) { if (ascending) {
return presence1.getLoginTime().compareTo(presence2.getLoginTime()); return presence1Session.getCreationDate().compareTo(presence2Session.getCreationDate());
} }
else { else {
return presence2.getLoginTime().compareTo(presence1.getLoginTime()); return presence2Session.getCreationDate().compareTo(presence1Session.getCreationDate());
} }
} }
}); });
...@@ -130,17 +147,15 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager ...@@ -130,17 +147,15 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager
{ {
Collections.sort(presences, new Comparator() { Collections.sort(presences, new Comparator() {
public int compare(Object object1, Object object2) { public int compare(Object object1, Object object2) {
Presence presence1 = (Presence)object1; Presence presence1 = (Presence) object1;
Presence presence2 = (Presence)object2; Presence presence2 = (Presence) object2;
String presenceUser1 = ""; String presenceUser1 = "";
String presenceUser2 = ""; String presenceUser2 = "";
try { try {
presenceUser1 = presenceUser1 =
userManager.getUser(presence1.getFrom().getNode() userManager.getUser(presence1.getFrom().getNode()).getUsername();
).getUsername();
presenceUser2 = presenceUser2 =
userManager.getUser(presence2.getFrom().getNode() userManager.getUser(presence2.getFrom().getNode()).getUsername();
).getUsername();
} }
catch (UserNotFoundException e) { catch (UserNotFoundException e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e); Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
...@@ -164,7 +179,7 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager ...@@ -164,7 +179,7 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager
List<User> users = new ArrayList<User>(); List<User> users = new ArrayList<User>();
for (int i = 0; i < presences.size(); i++) { for (int i = 0; i < presences.size(); i++) {
Presence presence = (Presence)presences.get(i); Presence presence = (Presence) presences.get(i);
try { try {
users.add(userManager.getUser(presence.getFrom().getNode())); users.add(userManager.getUser(presence.getFrom().getNode()));
} }
...@@ -230,7 +245,8 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager ...@@ -230,7 +245,8 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager
try { try {
return sessionManager.getSessionCount(user.getUsername()) > 0; return sessionManager.getSessionCount(user.getUsername()) > 0;
} }
catch (UnauthorizedException ue) { } catch (UnauthorizedException ue) {
}
return false; return false;
} }
...@@ -239,17 +255,15 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager ...@@ -239,17 +255,15 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager
return null; return null;
} }
Presence presence = null; Presence presence = null;
try {
for (Session session : sessionManager.getSessions(user.getUsername())) { for (Session session : sessionManager.getSessions(user.getUsername())) {
if (presence == null) { if (presence == null) {
presence = session.getPresence(); presence = session.getPresence();
} }
else if (presence.getShow().ordinal() > session.getPresence().getShow().ordinal()) { else if (presence.getShow().ordinal() > session.getPresence().getShow().ordinal()) {
presence = session.getPresence(); presence = session.getPresence();
}
} }
} }
catch (UnauthorizedException ue) { }
return presence; return presence;
} }
...@@ -258,12 +272,10 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager ...@@ -258,12 +272,10 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager
return null; return null;
} }
List<Presence> presences = new ArrayList<Presence>(); List<Presence> presences = new ArrayList<Presence>();
try {
for (Session session : sessionManager.getSessions(user.getUsername())) { for (Session session : sessionManager.getSessions(user.getUsername())) {
presences.add(session.getPresence()); presences.add(session.getPresence());
}
} }
catch (UnauthorizedException ue) { }
return Collections.unmodifiableCollection(presences); return Collections.unmodifiableCollection(presences);
} }
...@@ -274,7 +286,7 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager ...@@ -274,7 +286,7 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager
// search the current lists for the presence // search the current lists for the presence
Iterator onlineUsers = this.onlineUsers.values().iterator(); Iterator onlineUsers = this.onlineUsers.values().iterator();
while (onlineUsers.hasNext()) { while (onlineUsers.hasNext()) {
Presence presence = (Presence)onlineUsers.next(); Presence presence = (Presence) onlineUsers.next();
if (presence.getID().equals(presenceID)) { if (presence.getID().equals(presenceID)) {
return presence; return presence;
...@@ -282,7 +294,7 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager ...@@ -282,7 +294,7 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager
} }
Iterator guestUsers = onlineGuests.values().iterator(); Iterator guestUsers = onlineGuests.values().iterator();
while (guestUsers.hasNext()) { while (guestUsers.hasNext()) {
Presence presence = (Presence)guestUsers.next(); Presence presence = (Presence) guestUsers.next();
if (presence.getID().equals(presenceID)) { if (presence.getID().equals(presenceID)) {
return presence; return presence;
...@@ -318,7 +330,7 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager ...@@ -318,7 +330,7 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager
component.processPacket(presence); component.processPacket(presence);
} }
else { else {
Presence presence = (Presence)foreignUserCache.get(probee.toBareJID()); Presence presence = (Presence) foreignUserCache.get(probee.toBareJID());
if (presence != null) { if (presence != null) {
Presence presencePacket = presence.createCopy(); Presence presencePacket = presence.createCopy();
presencePacket.setFrom(probee); presencePacket.setFrom(probee);
...@@ -335,7 +347,7 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager ...@@ -335,7 +347,7 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager
} }
} }
} }
catch (UnauthorizedException e) { catch (Exception e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e); Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
} }
} }
...@@ -358,7 +370,7 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager ...@@ -358,7 +370,7 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager
} }
else { else {
Presence presence = Presence presence =
(Presence)foreignUserCache.get(probee.toBareJID()); (Presence) foreignUserCache.get(probee.toBareJID());
if (presence != null) { if (presence != null) {
Presence presencePacket = presence.createCopy(); Presence presencePacket = presence.createCopy();
presencePacket.setFrom(probee); presencePacket.setFrom(probee);
...@@ -371,7 +383,7 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager ...@@ -371,7 +383,7 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager
} }
} }
} }
catch (UnauthorizedException e) { catch (Exception e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e); Log.error(LocaleUtils.getLocalizedString("admin.error"), 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