Commit 9b8b9dfd authored by Matt Tucker's avatar Matt Tucker Committed by matt

Refactoring work.


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@585 b35dd754-fafc-0310-a699-88a17e54d16e
parent 9c425dd1
...@@ -20,6 +20,10 @@ import org.jivesoftware.messenger.*; ...@@ -20,6 +20,10 @@ import org.jivesoftware.messenger.*;
import org.jivesoftware.messenger.auth.UnauthorizedException; import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.messenger.user.*; import org.jivesoftware.messenger.user.*;
import org.jivesoftware.messenger.user.spi.CachedRosterImpl; import org.jivesoftware.messenger.user.spi.CachedRosterImpl;
import org.xmpp.packet.Presence;
import org.xmpp.packet.Packet;
import org.xmpp.packet.JID;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.Map; import java.util.Map;
...@@ -73,12 +77,12 @@ public class PresenceSubscribeHandler extends BasicModule implements ChannelHand ...@@ -73,12 +77,12 @@ public class PresenceSubscribeHandler extends BasicModule implements ChannelHand
super("Presence subscription handler"); super("Presence subscription handler");
} }
public void process(XMPPPacket xmppPacket) throws UnauthorizedException, PacketException { public void process(Packet xmppPacket) throws UnauthorizedException, PacketException {
Presence presence = (Presence)xmppPacket; Presence presence = (Presence)xmppPacket;
try { try {
XMPPAddress senderJID = presence.getSender(); JID senderJID = presence.getFrom();
XMPPAddress recipientJID = presence.getRecipient(); JID recipientJID = presence.getTo();
XMPPPacket.Type type = presence.getType(); Presence.Type type = presence.getType();
Roster roster = getRoster(senderJID); Roster roster = getRoster(senderJID);
if (roster != null) { if (roster != null) {
manageSub(recipientJID, true, type, roster); manageSub(recipientJID, true, type, roster);
...@@ -93,10 +97,10 @@ public class PresenceSubscribeHandler extends BasicModule implements ChannelHand ...@@ -93,10 +97,10 @@ public class PresenceSubscribeHandler extends BasicModule implements ChannelHand
// 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
// then the packet will be delivered based on its recipient and sender. // then the packet will be delivered based on its recipient and sender.
ChannelHandler handler = routingTable.getRoute(recipientJID); ChannelHandler handler = routingTable.getRoute(recipientJID);
handler.process((XMPPPacket)presence.createDeepCopy()); handler.process(presence.createCopy());
} }
catch (NoSuchRouteException e) { catch (NoSuchRouteException e) {
deliverer.deliver((XMPPPacket)presence.createDeepCopy()); deliverer.deliver(presence.createCopy());
} }
} }
catch (Exception e) { catch (Exception e) {
...@@ -110,11 +114,11 @@ public class PresenceSubscribeHandler extends BasicModule implements ChannelHand ...@@ -110,11 +114,11 @@ public class PresenceSubscribeHandler extends BasicModule implements ChannelHand
* @param address The address to check * @param address The address to check
* @return The roster or null if the address is not managed on the server * @return The roster or null if the address is not managed on the server
*/ */
private Roster getRoster(XMPPAddress address) { private Roster getRoster(JID address) {
String username = null; String username = null;
Roster roster = null; Roster roster = null;
if (localServer.isLocal(address) && !"".equals(address.getName())) { if (localServer.isLocal(address) && !"".equals(address.getNode())) {
username = address.getNamePrep(); username = address.getNode();
// 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) {
...@@ -136,7 +140,7 @@ public class PresenceSubscribeHandler extends BasicModule implements ChannelHand ...@@ -136,7 +140,7 @@ public class PresenceSubscribeHandler extends BasicModule implements ChannelHand
* @param type The subscription change type (subscribe, unsubscribe, etc.) * @param type The subscription change type (subscribe, unsubscribe, etc.)
* @throws UnauthorizedException If a security access violation occurs * @throws UnauthorizedException If a security access violation occurs
*/ */
private void manageSub(XMPPAddress target, boolean isSending, XMPPPacket.Type type, private void manageSub(JID target, boolean isSending, Presence.Type type,
Roster roster) throws UnauthorizedException, UserAlreadyExistsException Roster roster) throws UnauthorizedException, UserAlreadyExistsException
{ {
try { try {
...@@ -184,27 +188,27 @@ public class PresenceSubscribeHandler extends BasicModule implements ChannelHand ...@@ -184,27 +188,27 @@ public class PresenceSubscribeHandler extends BasicModule implements ChannelHand
// Item wishes to subscribe from owner // Item wishes to subscribe from owner
// Set flag and update roster if this is a new state, this is the normal way to begin // Set flag and update roster if this is a new state, this is the normal way to begin
// a roster subscription negotiation. // a roster subscription negotiation.
subrTable.put(Presence.SUBSCRIBE, new Change(RosterItem.RECV_SUBSCRIBE, null, null)); // no transition subrTable.put(Presence.Type.subscribe, new Change(RosterItem.RECV_SUBSCRIBE, null, null)); // no transition
// Item granted subscription to owner // Item granted subscription to owner
// The item's state immediately goes from NONE to TO and ask is reset // The item's state immediately goes from NONE to TO and ask is reset
subrTable.put(Presence.SUBSCRIBED, new Change(null, RosterItem.SUB_TO, RosterItem.ASK_NONE)); subrTable.put(Presence.Type.subscribed, new Change(null, RosterItem.SUB_TO, RosterItem.ASK_NONE));
// Item wishes to unsubscribe from owner // Item wishes to unsubscribe from owner
// This makes no sense, there is no subscription to remove // This makes no sense, there is no subscription to remove
subrTable.put(Presence.UNSUBSCRIBE, new Change(null, null, null)); subrTable.put(Presence.Type.unsubscribe, new Change(null, null, null));
// Owner has subscription to item revoked // Owner has subscription to item revoked
// Valid response if item requested subscription and we're denying request // Valid response if item requested subscription and we're denying request
subrTable.put(Presence.UNSUBSCRIBED, new Change(null, null, RosterItem.ASK_NONE)); subrTable.put(Presence.Type.unsubscribed, new Change(null, null, RosterItem.ASK_NONE));
// Owner asking to subscribe to item this is the normal way to begin // Owner asking to subscribe to item this is the normal way to begin
// a roster subscription negotiation. // a roster subscription negotiation.
subsTable.put(Presence.SUBSCRIBE, new Change(null, null, RosterItem.ASK_SUBSCRIBE)); subsTable.put(Presence.Type.subscribe, new Change(null, null, RosterItem.ASK_SUBSCRIBE));
// Item granted a subscription from owner // Item granted a subscription from owner
subsTable.put(Presence.SUBSCRIBED, new Change(RosterItem.RECV_NONE, RosterItem.SUB_FROM, null)); subsTable.put(Presence.Type.subscribed, new Change(RosterItem.RECV_NONE, RosterItem.SUB_FROM, null));
// Owner asking to unsubscribe to item // Owner asking to unsubscribe to item
// This makes no sense (there is no subscription to unsubscribe from) // This makes no sense (there is no subscription to unsubscribe from)
subsTable.put(Presence.UNSUBSCRIBE, new Change(null, null, null)); subsTable.put(Presence.Type.unsubscribe, new Change(null, null, null));
// Item has subscription from owner revoked // Item has subscription from owner revoked
// Valid response if item requested subscription and we're denying request // Valid response if item requested subscription and we're denying request
subsTable.put(Presence.UNSUBSCRIBED, new Change(RosterItem.RECV_NONE, null, null)); subsTable.put(Presence.Type.unsubscribed, new Change(RosterItem.RECV_NONE, null, null));
sr = new Hashtable(); sr = new Hashtable();
subrTable = new Hashtable(); subrTable = new Hashtable();
...@@ -215,31 +219,31 @@ public class PresenceSubscribeHandler extends BasicModule implements ChannelHand ...@@ -215,31 +219,31 @@ public class PresenceSubscribeHandler extends BasicModule implements ChannelHand
// Owner asking to subscribe to item // Owner asking to subscribe to item
// Set flag and update roster if this is a new state, this is the normal way to begin // Set flag and update roster if this is a new state, this is the normal way to begin
// a mutual roster subscription negotiation. // a mutual roster subscription negotiation.
subsTable.put(Presence.SUBSCRIBE, new Change(null, null, RosterItem.ASK_SUBSCRIBE)); subsTable.put(Presence.Type.subscribe, new Change(null, null, RosterItem.ASK_SUBSCRIBE));
// Item granted a subscription from owner // Item granted a subscription from owner
// This may be necessary if the recipient didn't get an earlier subscribed grant // This may be necessary if the recipient didn't get an earlier subscribed grant
// or as a denial of an unsubscribe request // or as a denial of an unsubscribe request
subsTable.put(Presence.SUBSCRIBED, new Change(RosterItem.RECV_NONE, null, null)); subsTable.put(Presence.Type.subscribed, new Change(RosterItem.RECV_NONE, null, null));
// Owner asking to unsubscribe to item // Owner asking to unsubscribe to item
// This makes no sense (there is no subscription to unsubscribe from) // This makes no sense (there is no subscription to unsubscribe from)
subsTable.put(Presence.UNSUBSCRIBE, new Change(null, null, null)); subsTable.put(Presence.Type.unsubscribe, new Change(null, null, null));
// Item has subscription from owner revoked // Item has subscription from owner revoked
// Immediately transition to NONE state // Immediately transition to NONE state
subsTable.put(Presence.UNSUBSCRIBED, new Change(RosterItem.RECV_NONE, RosterItem.SUB_NONE, null)); subsTable.put(Presence.Type.unsubscribed, new Change(RosterItem.RECV_NONE, RosterItem.SUB_NONE, null));
// Item wishes to subscribe from owner // Item wishes to subscribe from owner
// Item already has a subscription so only interesting if item had requested unsubscribe // Item already has a subscription so only interesting if item had requested unsubscribe
// Set flag and update roster if this is a new state, this is the normal way to begin // Set flag and update roster if this is a new state, this is the normal way to begin
// a mutual roster subscription negotiation. // a mutual roster subscription negotiation.
subrTable.put(Presence.SUBSCRIBE, new Change(RosterItem.RECV_NONE, null, null)); subrTable.put(Presence.Type.subscribe, new Change(RosterItem.RECV_NONE, null, null));
// Item granted subscription to owner // Item granted subscription to owner
// The item's state immediately goes from FROM to BOTH and ask is reset // The item's state immediately goes from FROM to BOTH and ask is reset
subrTable.put(Presence.SUBSCRIBED, new Change(null, RosterItem.SUB_BOTH, RosterItem.ASK_NONE)); subrTable.put(Presence.Type.subscribed, new Change(null, RosterItem.SUB_BOTH, RosterItem.ASK_NONE));
// Item wishes to unsubscribe from owner // Item wishes to unsubscribe from owner
// This is the normal mechanism of removing subscription // This is the normal mechanism of removing subscription
subrTable.put(Presence.UNSUBSCRIBE, new Change(RosterItem.RECV_UNSUBSCRIBE, null, null)); subrTable.put(Presence.Type.unsubscribe, new Change(RosterItem.RECV_UNSUBSCRIBE, null, null));
// Owner has subscription to item revoked // Owner has subscription to item revoked
// Valid response if owner requested subscription and item is denying request // Valid response if owner requested subscription and item is denying request
subrTable.put(Presence.UNSUBSCRIBED, new Change(null, null, RosterItem.ASK_NONE)); subrTable.put(Presence.Type.unsubscribed, new Change(null, null, RosterItem.ASK_NONE));
sr = new Hashtable(); sr = new Hashtable();
subrTable = new Hashtable(); subrTable = new Hashtable();
...@@ -249,29 +253,29 @@ public class PresenceSubscribeHandler extends BasicModule implements ChannelHand ...@@ -249,29 +253,29 @@ public class PresenceSubscribeHandler extends BasicModule implements ChannelHand
stateTable.put(RosterItem.SUB_TO, sr); stateTable.put(RosterItem.SUB_TO, sr);
// Owner asking to subscribe to item // Owner asking to subscribe to item
// We're already subscribed, may be trying to unset a unsub request // We're already subscribed, may be trying to unset a unsub request
subsTable.put(Presence.SUBSCRIBE, new Change(null, null, RosterItem.ASK_NONE)); subsTable.put(Presence.Type.subscribe, new Change(null, null, RosterItem.ASK_NONE));
// Item granted a subscription from owner // Item granted a subscription from owner
// Sets mutual subscription // Sets mutual subscription
subsTable.put(Presence.SUBSCRIBED, new Change(RosterItem.RECV_NONE, RosterItem.SUB_BOTH, null)); subsTable.put(Presence.Type.subscribed, new Change(RosterItem.RECV_NONE, RosterItem.SUB_BOTH, null));
// Owner asking to unsubscribe to item // Owner asking to unsubscribe to item
// Normal method of removing subscription // Normal method of removing subscription
subsTable.put(Presence.UNSUBSCRIBE, new Change(null, null, RosterItem.ASK_UNSUBSCRIBE)); subsTable.put(Presence.Type.unsubscribe, new Change(null, null, RosterItem.ASK_UNSUBSCRIBE));
// Item has subscription from owner revoked // Item has subscription from owner revoked
// No subscription to unsub, makes sense if denying subscription request or for // No subscription to unsub, makes sense if denying subscription request or for
// situations where the original unsubscribed got lost // situations where the original unsubscribed got lost
subsTable.put(Presence.UNSUBSCRIBED, new Change(RosterItem.RECV_NONE, null, null)); subsTable.put(Presence.Type.unsubscribed, new Change(RosterItem.RECV_NONE, null, null));
// Item wishes to subscribe from owner // Item wishes to subscribe from owner
// This is the normal way to negotiate a mutual subscription // This is the normal way to negotiate a mutual subscription
subrTable.put(Presence.SUBSCRIBE, new Change(RosterItem.RECV_SUBSCRIBE, null, null)); subrTable.put(Presence.Type.subscribe, new Change(RosterItem.RECV_SUBSCRIBE, null, null));
// Item granted subscription to owner // Item granted subscription to owner
// Owner already subscribed to item, could be a unsub denial or a lost packet // Owner already subscribed to item, could be a unsub denial or a lost packet
subrTable.put(Presence.SUBSCRIBED, new Change(null, null, RosterItem.ASK_NONE)); subrTable.put(Presence.Type.subscribed, new Change(null, null, RosterItem.ASK_NONE));
// Item wishes to unsubscribe from owner // Item wishes to unsubscribe from owner
// There is no subscription. May be trying to cancel earlier subscribe request. // There is no subscription. May be trying to cancel earlier subscribe request.
subrTable.put(Presence.UNSUBSCRIBE, new Change(RosterItem.RECV_NONE, null, null)); subrTable.put(Presence.Type.unsubscribe, new Change(RosterItem.RECV_NONE, null, null));
// Owner has subscription to item revoked // Owner has subscription to item revoked
// Setting subscription to none // Setting subscription to none
subrTable.put(Presence.UNSUBSCRIBED, new Change(null, RosterItem.SUB_NONE, RosterItem.ASK_NONE)); subrTable.put(Presence.Type.unsubscribed, new Change(null, RosterItem.SUB_NONE, RosterItem.ASK_NONE));
sr = new Hashtable(); sr = new Hashtable();
subrTable = new Hashtable(); subrTable = new Hashtable();
...@@ -281,31 +285,31 @@ public class PresenceSubscribeHandler extends BasicModule implements ChannelHand ...@@ -281,31 +285,31 @@ public class PresenceSubscribeHandler extends BasicModule implements ChannelHand
stateTable.put(RosterItem.SUB_BOTH, sr); stateTable.put(RosterItem.SUB_BOTH, sr);
// Owner asking to subscribe to item // Owner asking to subscribe to item
// Makes sense if trying to cancel previous unsub request // Makes sense if trying to cancel previous unsub request
subsTable.put(Presence.SUBSCRIBE, new Change(null, null, RosterItem.ASK_NONE)); subsTable.put(Presence.Type.subscribe, new Change(null, null, RosterItem.ASK_NONE));
// Item granted a subscription from owner // Item granted a subscription from owner
// This may be necessary if the recipient didn't get an earlier subscribed grant // This may be necessary if the recipient didn't get an earlier subscribed grant
// or as a denial of an unsubscribe request // or as a denial of an unsubscribe request
subsTable.put(Presence.SUBSCRIBED, new Change(RosterItem.RECV_NONE, null, null)); subsTable.put(Presence.Type.subscribed, new Change(RosterItem.RECV_NONE, null, null));
// Owner asking to unsubscribe to item // Owner asking to unsubscribe to item
// Set flags // Set flags
subsTable.put(Presence.UNSUBSCRIBE, new Change(null, null, RosterItem.ASK_UNSUBSCRIBE)); subsTable.put(Presence.Type.unsubscribe, new Change(null, null, RosterItem.ASK_UNSUBSCRIBE));
// Item has subscription from owner revoked // Item has subscription from owner revoked
// Immediately transition them to TO state // Immediately transition them to TO state
subsTable.put(Presence.UNSUBSCRIBED, new Change(RosterItem.RECV_NONE, RosterItem.SUB_TO, null)); subsTable.put(Presence.Type.unsubscribed, new Change(RosterItem.RECV_NONE, RosterItem.SUB_TO, null));
// Item wishes to subscribe to owner // Item wishes to subscribe to owner
// Item already has a subscription so only interesting if item had requested unsubscribe // Item already has a subscription so only interesting if item had requested unsubscribe
// Set flag and update roster if this is a new state, this is the normal way to begin // Set flag and update roster if this is a new state, this is the normal way to begin
// a mutual roster subscription negotiation. // a mutual roster subscription negotiation.
subrTable.put(Presence.SUBSCRIBE, new Change(RosterItem.RECV_NONE, null, null)); subrTable.put(Presence.Type.subscribe, new Change(RosterItem.RECV_NONE, null, null));
// Item granted subscription to owner // Item granted subscription to owner
// Redundant unless denying unsub request // Redundant unless denying unsub request
subrTable.put(Presence.SUBSCRIBED, new Change(null, null, RosterItem.ASK_NONE)); subrTable.put(Presence.Type.subscribed, new Change(null, null, RosterItem.ASK_NONE));
// Item wishes to unsubscribe from owner // Item wishes to unsubscribe from owner
// This is the normal mechanism of removing subscription // This is the normal mechanism of removing subscription
subrTable.put(Presence.UNSUBSCRIBE, new Change(RosterItem.RECV_UNSUBSCRIBE, null, null)); subrTable.put(Presence.Type.unsubscribe, new Change(RosterItem.RECV_UNSUBSCRIBE, null, null));
// Owner has subscription to item revoked // Owner has subscription to item revoked
// Immediately downgrade state to FROM // Immediately downgrade state to FROM
subrTable.put(Presence.UNSUBSCRIBED, new Change(RosterItem.RECV_NONE, RosterItem.SUB_FROM, RosterItem.ASK_NONE)); subrTable.put(Presence.Type.unsubscribed, new Change(RosterItem.RECV_NONE, RosterItem.SUB_FROM, RosterItem.ASK_NONE));
} }
/** /**
...@@ -339,7 +343,7 @@ public class PresenceSubscribeHandler extends BasicModule implements ChannelHand ...@@ -339,7 +343,7 @@ public class PresenceSubscribeHandler extends BasicModule implements ChannelHand
* @param action The new state change request * @param action The new state change request
* @param isSending True if the roster owner of the item is sending the new state change request * @param isSending True if the roster owner of the item is sending the new state change request
*/ */
private static void updateState(RosterItem item, XMPPPacket.Type action, boolean isSending) throws UnauthorizedException { private static void updateState(RosterItem item, Presence.Type action, boolean isSending) throws UnauthorizedException {
Map srTable = (Map)stateTable.get(item.getSubStatus()); Map srTable = (Map)stateTable.get(item.getSubStatus());
Map changeTable = (Map)srTable.get(isSending ? "send" : "recv"); Map changeTable = (Map)srTable.get(isSending ? "send" : "recv");
Change change = (Change)changeTable.get(action); Change change = (Change)changeTable.get(action);
......
...@@ -22,6 +22,8 @@ import org.jivesoftware.messenger.user.CachedRoster; ...@@ -22,6 +22,8 @@ import org.jivesoftware.messenger.user.CachedRoster;
import org.jivesoftware.messenger.user.RosterItem; import org.jivesoftware.messenger.user.RosterItem;
import org.jivesoftware.messenger.user.RosterManager; import org.jivesoftware.messenger.user.RosterManager;
import org.jivesoftware.messenger.user.UserNotFoundException; import org.jivesoftware.messenger.user.UserNotFoundException;
import org.xmpp.packet.*;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.*; import java.util.*;
import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamException;
...@@ -80,13 +82,14 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler ...@@ -80,13 +82,14 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler
super("Presence update handler"); super("Presence update handler");
} }
public void process(XMPPPacket xmppPacket) throws UnauthorizedException, PacketException { public void process(Packet xmppPacket) throws UnauthorizedException, PacketException {
Presence presence = (Presence)xmppPacket; Presence presence = (Presence)xmppPacket;
Session session = presence.getOriginatingSession();
try { try {
XMPPPacket.Type type = presence.getType(); Session session = SessionManager.getInstance().getSession(presence.getFrom());
if (type == null || Presence.AVAILABLE.equals(type)) { Presence.Type type = presence.getType();
broadcastUpdate((Presence)presence.createDeepCopy()); // Available
if (type == null) {
broadcastUpdate(presence.createCopy());
if (session != null) { if (session != null) {
session.setPresence(presence); session.setPresence(presence);
if (!session.isInitialized()) { if (!session.isInitialized()) {
...@@ -95,34 +98,25 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler ...@@ -95,34 +98,25 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler
} }
} }
} }
else if (Presence.UNAVAILABLE.equals(type)) { else if (Presence.Type.unavailable == type) {
broadcastUpdate((Presence)presence.createDeepCopy()); broadcastUpdate(presence.createCopy());
broadcastUnavailableForDirectedPresences((Presence)presence.createDeepCopy()); broadcastUnavailableForDirectedPresences(presence.createCopy());
if (session != null) { if (session != null) {
session.setPresence(presence); session.setPresence(presence);
} }
} }
else if (Presence.INVISIBLE.equals(type)) {
if (session != null) {
session.setPresence(presence);
if (!session.isInitialized()) {
initSession(session);
session.setInitialized(true);
}
}
}
else { else {
presence = (Presence)presence.createDeepCopy(); presence = presence.createCopy();
if (session != null) { if (session != null) {
presence.setSender(new XMPPAddress(null, session.getServerName(), null)); presence.setFrom(new JID(null, session.getServerName(), null));
presence.setRecipient(session.getAddress()); presence.setTo(session.getAddress());
} }
else { else {
XMPPAddress sender = presence.getSender(); JID sender = presence.getFrom();
presence.setSender(presence.getRecipient()); presence.setFrom(presence.getTo());
presence.setRecipient(sender); presence.setTo(sender);
} }
presence.setError(XMPPError.Code.BAD_REQUEST); presence.setError(PacketError.Condition.bad_request);
deliverer.deliver(presence); deliverer.deliver(presence);
} }
...@@ -139,21 +133,22 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler ...@@ -139,21 +133,22 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler
*/ */
public synchronized void process(Presence presence) throws PacketException { public synchronized void process(Presence presence) throws PacketException {
try { try {
process((XMPPPacket)presence); process((Packet)presence);
} catch (UnauthorizedException e) { }
catch (UnauthorizedException e) {
try { try {
Session session = presence.getOriginatingSession(); Session session = SessionManager.getInstance().getSession(presence.getFrom());
presence = (Presence)presence.createDeepCopy(); presence = presence.createCopy();
if (session != null) { if (session != null) {
presence.setSender(new XMPPAddress(null, session.getServerName(), null)); presence.setFrom(new JID(null, session.getServerName(), null));
presence.setRecipient(session.getAddress()); presence.setTo(session.getAddress());
} }
else { else {
XMPPAddress sender = presence.getSender(); JID sender = presence.getFrom();
presence.setSender(presence.getRecipient()); presence.setFrom(presence.getTo());
presence.setRecipient(sender); presence.setTo(sender);
} }
presence.setError(XMPPError.Code.UNAUTHORIZED); presence.setError(PacketError.Condition.not_authorized);
deliverer.deliver(presence); deliverer.deliver(presence);
} }
catch (Exception err) { catch (Exception err) {
...@@ -178,8 +173,8 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler ...@@ -178,8 +173,8 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler
throws UnauthorizedException, UserNotFoundException, XMLStreamException { throws UnauthorizedException, UserNotFoundException, XMLStreamException {
// Only user sessions need to be authenticated // Only user sessions need to be authenticated
if (!"".equals(session.getAddress().getName())) { if (!"".equals(session.getAddress().getNode())) {
String username = session.getAddress().getNamePrep(); String username = session.getAddress().getNode();
CachedRoster roster = rosterManager.getRoster(username); CachedRoster roster = rosterManager.getRoster(username);
Iterator items = roster.getRosterItems(); Iterator items = roster.getRosterItems();
while (items.hasNext()) { while (items.hasNext()) {
...@@ -196,23 +191,21 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler ...@@ -196,23 +191,21 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler
} }
} }
// deliver offline messages if any // deliver offline messages if any
Iterator msgs = messageStore.getMessages(username); Collection<Message> messages = messageStore.getMessages(username);
while (msgs.hasNext()) { for (Message message : messages) {
Message msg = (Message)msgs.next(); session.getConnection().deliver(message);
session.getConnection().deliver(msg);
} }
} }
} }
public XMPPPacket createSubscribePresence(XMPPAddress senderAddress, public Presence createSubscribePresence(JID senderAddress, boolean isSubscribe) {
boolean isSubscribe) {
Presence presence = packetFactory.getPresence(); Presence presence = packetFactory.getPresence();
presence.setSender(senderAddress); presence.setFrom(senderAddress);
if (isSubscribe) { if (isSubscribe) {
presence.setType(Presence.SUBSCRIBE); presence.setType(Presence.Type.subscribe);
} }
else { else {
presence.setType(Presence.UNSUBSCRIBE); presence.setType(Presence.Type.unsubscribe);
} }
return presence; return presence;
} }
...@@ -229,16 +222,13 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler ...@@ -229,16 +222,13 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler
* *
* @param update The update to broadcast * @param update The update to broadcast
*/ */
private void broadcastUpdate(Presence update) private void broadcastUpdate(Presence update) throws PacketException {
throws PacketException { if (update.getFrom() == null) {
if (update.getSender() == null) {
return; return;
} }
if (localServer.isLocal(update.getSender())) { if (localServer.isLocal(update.getFrom())) {
// Local updates can simply run through the roster of the local user // Local updates can simply run through the roster of the local user
String name = update.getSender().getName(); String name = update.getFrom().getNode();
try { try {
if (name != null && !"".equals(name)) { if (name != null && !"".equals(name)) {
name = name.toLowerCase(); name = name.toLowerCase();
...@@ -258,7 +248,7 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler ...@@ -258,7 +248,7 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler
// on the server // on the server
Log.warn("Presence requested from server " Log.warn("Presence requested from server "
+ localServer.getServerInfo().getName() + localServer.getServerInfo().getName()
+ " by unknown user: " + update.getSender()); + " by unknown user: " + update.getFrom());
/* /*
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
...@@ -298,11 +288,11 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler ...@@ -298,11 +288,11 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler
* @param handler the handler that routed the presence to the entity. * @param handler the handler that routed the presence to the entity.
*/ */
public synchronized void directedPresenceSent(Presence update, ChannelHandler handler) { public synchronized void directedPresenceSent(Presence update, ChannelHandler handler) {
if (update.getSender() == null) { if (update.getFrom() == null) {
return; return;
} }
if (localServer.isLocal(update.getSender())) { if (localServer.isLocal(update.getFrom())) {
String name = update.getSender().getName(); String name = update.getFrom().getNode();
try { try {
if (name != null && !"".equals(name)) { if (name != null && !"".equals(name)) {
name = name.toLowerCase(); name = name.toLowerCase();
...@@ -310,15 +300,15 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler ...@@ -310,15 +300,15 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler
// If the directed presence was sent to an entity that is not in the user's // If the directed presence was sent to an entity that is not in the user's
// roster, keep a registry of this so that when the user goes offline we will // roster, keep a registry of this so that when the user goes offline we will
// be able to send the unavialable presence to the entity // be able to send the unavialable presence to the entity
if (!roster.isRosterItem(update.getRecipient())) { if (!roster.isRosterItem(update.getTo())) {
Set set = (Set)directedPresences.get(update.getSender().toStringPrep()); Set set = (Set)directedPresences.get(update.getFrom().toString());
if (set == null) { if (set == null) {
// We are using a set to avoid duplicate handlers in case the user // We are using a set to avoid duplicate handlers in case the user
// sends several directed presences to the same entity // sends several directed presences to the same entity
set = new HashSet(); set = new HashSet();
directedPresences.put(update.getSender().toStringPrep(), set); directedPresences.put(update.getFrom().toString(), set);
} }
if (Presence.UNAVAILABLE.equals(update.getType())) { if (Presence.Type.unavailable.equals(update.getType())) {
// It's a directed unavailable presence so remove the target entity // It's a directed unavailable presence so remove the target entity
// from the registry // from the registry
if (handler instanceof SessionImpl) { if (handler instanceof SessionImpl) {
...@@ -326,7 +316,7 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler ...@@ -326,7 +316,7 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler
if (set.isEmpty()) { if (set.isEmpty()) {
// Remove the user from the registry since the list of directed // Remove the user from the registry since the list of directed
// presences is empty // presences is empty
directedPresences.remove(update.getSender().toStringPrep()); directedPresences.remove(update.getFrom().toString());
} }
} }
} }
...@@ -355,11 +345,11 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler ...@@ -355,11 +345,11 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler
* @param update the unavailable presence sent by the user. * @param update the unavailable presence sent by the user.
*/ */
private void broadcastUnavailableForDirectedPresences(Presence update) { private void broadcastUnavailableForDirectedPresences(Presence update) {
if (update.getSender() == null) { if (update.getFrom() == null) {
return; return;
} }
if (localServer.isLocal(update.getSender())) { if (localServer.isLocal(update.getFrom())) {
Set set = (Set)directedPresences.get(update.getSender().toStringPrep()); Set set = (Set)directedPresences.get(update.getFrom().toString());
if (set != null) { if (set != null) {
RoutableChannelHandler handler; RoutableChannelHandler handler;
// Iterate over all the entities that the user sent a directed presence // Iterate over all the entities that the user sent a directed presence
...@@ -371,7 +361,7 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler ...@@ -371,7 +361,7 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler
// e.g. MultiUserChatServerImpl will remove the user from ALL the rooms // e.g. MultiUserChatServerImpl will remove the user from ALL the rooms
handler = (RoutableChannelHandler)((HandlerWeakReference)it.next()).get(); handler = (RoutableChannelHandler)((HandlerWeakReference)it.next()).get();
if (handler != null) { if (handler != null) {
update.setRecipient(handler.getAddress()); update.setTo(handler.getAddress());
try { try {
handler.process(update); handler.process(update);
} }
...@@ -381,7 +371,7 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler ...@@ -381,7 +371,7 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler
} }
} }
// Remove the registry of directed presences of this user // Remove the registry of directed presences of this user
directedPresences.remove(update.getSender().toStringPrep()); directedPresences.remove(update.getFrom().toString());
} }
} }
} }
...@@ -391,7 +381,6 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler ...@@ -391,7 +381,6 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler
public XMPPServer localServer; public XMPPServer localServer;
public SessionManager sessionManager; public SessionManager sessionManager;
public PresenceManager presenceManager; public PresenceManager presenceManager;
public PacketTransporter transporter;
public PacketDeliverer deliverer; public PacketDeliverer deliverer;
public PacketFactory packetFactory; public PacketFactory packetFactory;
public OfflineMessageStore messageStore; public OfflineMessageStore messageStore;
...@@ -403,7 +392,6 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler ...@@ -403,7 +392,6 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler
trackInfo.getTrackerClasses().put(SessionManager.class, "sessionManager"); trackInfo.getTrackerClasses().put(SessionManager.class, "sessionManager");
trackInfo.getTrackerClasses().put(PresenceManager.class, "presenceManager"); trackInfo.getTrackerClasses().put(PresenceManager.class, "presenceManager");
trackInfo.getTrackerClasses().put(PacketDeliverer.class, "deliverer"); trackInfo.getTrackerClasses().put(PacketDeliverer.class, "deliverer");
trackInfo.getTrackerClasses().put(PacketTransporter.class, "transporter");
trackInfo.getTrackerClasses().put(PacketFactory.class, "packetFactory"); trackInfo.getTrackerClasses().put(PacketFactory.class, "packetFactory");
trackInfo.getTrackerClasses().put(OfflineMessageStore.class, "messageStore"); trackInfo.getTrackerClasses().put(OfflineMessageStore.class, "messageStore");
return trackInfo; return trackInfo;
......
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