Commit 0fb02cce authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gato

Refactoring of NoSuchRouteException. JM-476

git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@3138 b35dd754-fafc-0310-a699-88a17e54d16e
parent a7381d6f
...@@ -88,10 +88,12 @@ public class PresenceRouter extends BasicModule { ...@@ -88,10 +88,12 @@ public class PresenceRouter extends BasicModule {
} }
else { else {
// The user sent a directed presence to an entity // The user sent a directed presence to an entity
ChannelHandler handler = routingTable.getRoute(recipientJID); ChannelHandler route = routingTable.getRoute(recipientJID);
handler.process(packet); if (route != null) {
// Notify the PresenceUpdateHandler of the directed presence route.process(packet);
updateHandler.directedPresenceSent(packet, handler, recipientJID.toString()); // Notify the PresenceUpdateHandler of the directed presence
updateHandler.directedPresenceSent(packet, route, recipientJID.toString());
}
} }
} }
...@@ -107,14 +109,15 @@ public class PresenceRouter extends BasicModule { ...@@ -107,14 +109,15 @@ public class PresenceRouter extends BasicModule {
presenceManager.handleProbe(packet); presenceManager.handleProbe(packet);
} }
else { else {
// It's an unknown or ERROR type, just deliver it because there's nothing else to do with it // It's an unknown or ERROR type, just deliver it because there's nothing
routingTable.getRoute(recipientJID).process(packet); // else to do with it
ChannelHandler route = routingTable.getRoute(recipientJID);
if (route != null) {
route.process(packet);
}
} }
} }
catch (NoSuchRouteException e) {
// Do nothing, presence to unreachable routes are dropped
}
catch (Exception e) { catch (Exception e) {
Log.error(LocaleUtils.getLocalizedString("admin.error.routing"), e); Log.error(LocaleUtils.getLocalizedString("admin.error.routing"), e);
Session session = sessionManager.getSession(packet.getFrom()); Session session = sessionManager.getSession(packet.getFrom());
......
/** /**
* $RCSfile$ * $RCSfile: RoutingTable.java,v $
* $Revision$ * $Revision$
* $Date$ * $Date$
* *
...@@ -91,9 +91,8 @@ public interface RoutingTable { ...@@ -91,9 +91,8 @@ public interface RoutingTable {
* *
* @param node The address we want a route to * @param node The address we want a route to
* @return The handler corresponding to the route, or null indicating no route exists * @return The handler corresponding to the route, or null indicating no route exists
* @throws NoSuchRouteException If the requested route does not exist
*/ */
RoutableChannelHandler getRoute(JID node) throws NoSuchRouteException; RoutableChannelHandler getRoute(JID node);
/** /**
* <p>Obtain all child routes for the given node.</p> * <p>Obtain all child routes for the given node.</p>
...@@ -134,9 +133,8 @@ public interface RoutingTable { ...@@ -134,9 +133,8 @@ public interface RoutingTable {
* *
* @param node The address we want a route to * @param node The address we want a route to
* @return The Session corresponding to the route, or null indicating no route exists * @return The Session corresponding to the route, or null indicating no route exists
* @throws NoSuchRouteException If the requested route does not exist
*/ */
ChannelHandler getBestRoute(JID node) throws NoSuchRouteException; ChannelHandler getBestRoute(JID node);
/** /**
* <p>Remove a route from the routing table.</p> * <p>Remove a route from the routing table.</p>
......
...@@ -645,15 +645,11 @@ public class SessionManager extends BasicModule { ...@@ -645,15 +645,11 @@ public class SessionManager extends BasicModule {
// the user from the routing table // the user from the routing table
if (sessionMap == null) { if (sessionMap == null) {
JID userJID = new JID(session.getUsername(), serverName, ""); JID userJID = new JID(session.getUsername(), serverName, "");
try { if (routingTable.getRoute(userJID) != null) {
routingTable.getRoute(userJID);
// Remove the route for the session's BARE address // Remove the route for the session's BARE address
routingTable.removeRoute(new JID(session.getAddress().getNode(), routingTable.removeRoute(new JID(session.getAddress().getNode(),
session.getAddress().getDomain(), "")); session.getAddress().getDomain(), ""));
} }
catch (NoSuchRouteException e) {
// Do nothing since the routingTable does not have routes to this user
}
} }
// If all the user sessions are gone then remove the route to the default session // If all the user sessions are gone then remove the route to the default session
else if (sessionMap.getAvailableSessions().isEmpty()) { else if (sessionMap.getAvailableSessions().isEmpty()) {
......
...@@ -535,9 +535,10 @@ public class RosterManager extends BasicModule implements GroupEventListener { ...@@ -535,9 +535,10 @@ public class RosterManager extends BasicModule implements GroupEventListener {
} }
try { try {
ChannelHandler handler = routingTable.getRoute(recipient); ChannelHandler handler = routingTable.getRoute(recipient);
handler.process(presence); if (handler != null) {
handler.process(presence);
}
} }
catch (NoSuchRouteException e) {}
catch (UnauthorizedException e) {} catch (UnauthorizedException e) {}
} }
......
...@@ -11,10 +11,7 @@ ...@@ -11,10 +11,7 @@
package org.jivesoftware.messenger.server; package org.jivesoftware.messenger.server;
import org.jivesoftware.messenger.NoSuchRouteException; import org.jivesoftware.messenger.*;
import org.jivesoftware.messenger.RoutableChannelHandler;
import org.jivesoftware.messenger.RoutingTable;
import org.jivesoftware.messenger.XMPPServer;
import org.jivesoftware.messenger.auth.UnauthorizedException; import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.util.JiveGlobals; import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.Log; import org.jivesoftware.util.Log;
...@@ -130,7 +127,13 @@ public class OutgoingSessionPromise implements RoutableChannelHandler { ...@@ -130,7 +127,13 @@ public class OutgoingSessionPromise implements RoutableChannelHandler {
.authenticateDomain(packet.getFrom().getDomain(), packet.getTo().getDomain()); .authenticateDomain(packet.getFrom().getDomain(), packet.getTo().getDomain());
if (created) { if (created) {
// A connection to the remote server was created so get the route and send the packet // A connection to the remote server was created so get the route and send the packet
routingTable.getRoute(packet.getTo()).process(packet); ChannelHandler route = routingTable.getRoute(packet.getTo());
if (route != null) {
route.process(packet);
}
else {
throw new Exception("Failed to create connection to remote server");
}
} }
else { else {
throw new Exception("Failed to create connection to remote server"); throw new Exception("Failed to create connection to remote server");
...@@ -147,7 +150,10 @@ public class OutgoingSessionPromise implements RoutableChannelHandler { ...@@ -147,7 +150,10 @@ public class OutgoingSessionPromise implements RoutableChannelHandler {
reply.setFrom(packet.getTo()); reply.setFrom(packet.getTo());
reply.setChildElement(((IQ) packet).getChildElement().createCopy()); reply.setChildElement(((IQ) packet).getChildElement().createCopy());
reply.setError(PacketError.Condition.remote_server_not_found); reply.setError(PacketError.Condition.remote_server_not_found);
routingTable.getRoute(reply.getTo()).process(reply); ChannelHandler route = routingTable.getRoute(reply.getTo());
if (route != null) {
route.process(reply);
}
} }
else if (packet instanceof Presence) { else if (packet instanceof Presence) {
Presence reply = new Presence(); Presence reply = new Presence();
...@@ -155,7 +161,10 @@ public class OutgoingSessionPromise implements RoutableChannelHandler { ...@@ -155,7 +161,10 @@ public class OutgoingSessionPromise implements RoutableChannelHandler {
reply.setTo(packet.getFrom()); reply.setTo(packet.getFrom());
reply.setFrom(packet.getTo()); reply.setFrom(packet.getTo());
reply.setError(PacketError.Condition.remote_server_not_found); reply.setError(PacketError.Condition.remote_server_not_found);
routingTable.getRoute(reply.getTo()).process(reply); ChannelHandler route = routingTable.getRoute(reply.getTo());
if (route != null) {
route.process(reply);
}
} }
else if (packet instanceof Message) { else if (packet instanceof Message) {
Message reply = new Message(); Message reply = new Message();
...@@ -165,13 +174,14 @@ public class OutgoingSessionPromise implements RoutableChannelHandler { ...@@ -165,13 +174,14 @@ public class OutgoingSessionPromise implements RoutableChannelHandler {
reply.setType(((Message)packet).getType()); reply.setType(((Message)packet).getType());
reply.setThread(((Message)packet).getThread()); reply.setThread(((Message)packet).getThread());
reply.setError(PacketError.Condition.remote_server_not_found); reply.setError(PacketError.Condition.remote_server_not_found);
routingTable.getRoute(reply.getTo()).process(reply); ChannelHandler route = routingTable.getRoute(reply.getTo());
if (route != null) {
route.process(reply);
}
} }
} }
catch (UnauthorizedException e) { catch (UnauthorizedException e) {
} }
catch (NoSuchRouteException e) {
}
} }
/** /**
......
...@@ -490,18 +490,13 @@ class ServerDialback { ...@@ -490,18 +490,13 @@ class ServerDialback {
// trick is useful when subdomains of this server are registered in the DNS so remote // trick is useful when subdomains of this server are registered in the DNS so remote
// servers may establish connections directly to a subdomain of this server // servers may establish connections directly to a subdomain of this server
if (host_unknown && recipient.contains(serverName)) { if (host_unknown && recipient.contains(serverName)) {
try { RoutableChannelHandler route = routingTable.getRoute(new JID(recipient));
RoutableChannelHandler route = routingTable.getRoute(new JID(recipient)); if (route == null || route instanceof OutgoingSessionPromise) {
if (route instanceof OutgoingSessionPromise) {
host_unknown = true;
}
else {
host_unknown = false;
}
}
catch (NoSuchRouteException e) {
host_unknown = true; host_unknown = true;
} }
else {
host_unknown = false;
}
} }
return host_unknown; return host_unknown;
} }
......
...@@ -79,14 +79,18 @@ public class RoutingTableImpl extends BasicModule implements RoutingTable { ...@@ -79,14 +79,18 @@ public class RoutingTableImpl extends BasicModule implements RoutingTable {
} }
} }
public RoutableChannelHandler getRoute(JID node) throws NoSuchRouteException { public RoutableChannelHandler getRoute(JID node) {
return getRoute(node.toString(), node.getNode() == null ? "" : node.getNode(),
node.getDomain(), node.getResource() == null ? "" : node.getResource());
}
private RoutableChannelHandler getRoute(String jid, String node, String domain,
String resource) {
RoutableChannelHandler route = null; RoutableChannelHandler route = null;
String nodeJID = node.getNode() == null ? "" : node.getNode();
String resourceJID = node.getResource() == null ? "" : node.getResource();
// Check if the address belongs to a remote server // Check if the address belongs to a remote server
if (!serverName.equals(node.getDomain()) && routes.get(node.getDomain()) == null && if (!serverName.equals(domain) && routes.get(domain) == null &&
componentManager.getComponent(node.getDomain()) == null) { componentManager.getComponent(domain) == null) {
// Return a promise of a remote session. This object will queue packets pending // Return a promise of a remote session. This object will queue packets pending
// to be sent to remote servers // to be sent to remote servers
return OutgoingSessionPromise.getInstance(); return OutgoingSessionPromise.getInstance();
...@@ -94,33 +98,32 @@ public class RoutingTableImpl extends BasicModule implements RoutingTable { ...@@ -94,33 +98,32 @@ public class RoutingTableImpl extends BasicModule implements RoutingTable {
routeLock.readLock().lock(); routeLock.readLock().lock();
try { try {
Object nameRoutes = routes.get(node.getDomain()); Object nameRoutes = routes.get(domain);
if (nameRoutes instanceof ChannelHandler) { if (nameRoutes instanceof ChannelHandler) {
route = (RoutableChannelHandler)nameRoutes; route = (RoutableChannelHandler)nameRoutes;
} }
else { else {
Object resourceRoutes = ((Hashtable)nameRoutes).get(nodeJID); Object resourceRoutes = ((Hashtable)nameRoutes).get(node);
if (resourceRoutes instanceof ChannelHandler) { if (resourceRoutes instanceof ChannelHandler) {
route = (RoutableChannelHandler)resourceRoutes; route = (RoutableChannelHandler)resourceRoutes;
} }
else if (resourceRoutes != null) { else if (resourceRoutes != null) {
route = (RoutableChannelHandler) ((Hashtable)resourceRoutes).get(resourceJID); route = (RoutableChannelHandler) ((Hashtable)resourceRoutes).get(resource);
} }
else { else {
throw new NoSuchRouteException(node.toString()); route = null;
} }
} }
} }
catch (Exception e) { catch (Exception e) {
throw new NoSuchRouteException(node == null ? "No node" : node.toString(), e); if (Log.isDebugEnabled()) {
Log.debug("Route not found for JID: "+ jid, e);
}
} }
finally { finally {
routeLock.readLock().unlock(); routeLock.readLock().unlock();
} }
if (route == null) {
throw new NoSuchRouteException(node == null ? "No node" : node.toString());
}
return route; return route;
} }
...@@ -213,18 +216,12 @@ public class RoutingTableImpl extends BasicModule implements RoutingTable { ...@@ -213,18 +216,12 @@ public class RoutingTableImpl extends BasicModule implements RoutingTable {
} }
} }
public ChannelHandler getBestRoute(JID node) throws NoSuchRouteException { public ChannelHandler getBestRoute(JID node) {
ChannelHandler route = route = getRoute(node);
ChannelHandler route = null;
try {
route = getRoute(node);
}
catch (NoSuchRouteException e) {
JID defaultNode = new JID(node.getNode(), node.getDomain(), "");
route = getRoute(defaultNode);
}
if (route == null) { if (route == null) {
throw new NoSuchRouteException(); // Try looking for a route based on the bare JID
String nodeJID = node.getNode() == null ? "" : node.getNode();
route = getRoute(node.toBareJID(), nodeJID, node.getDomain(), "");
} }
return route; return route;
} }
......
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