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