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 {
}
else {
// The user sent a directed presence to an entity
ChannelHandler handler = routingTable.getRoute(recipientJID);
handler.process(packet);
// Notify the PresenceUpdateHandler of the directed presence
updateHandler.directedPresenceSent(packet, handler, recipientJID.toString());
ChannelHandler route = routingTable.getRoute(recipientJID);
if (route != null) {
route.process(packet);
// Notify the PresenceUpdateHandler of the directed presence
updateHandler.directedPresenceSent(packet, route, recipientJID.toString());
}
}
}
......@@ -107,14 +109,15 @@ public class PresenceRouter extends BasicModule {
presenceManager.handleProbe(packet);
}
else {
// It's an unknown or ERROR type, just deliver it because there's nothing else to do with it
routingTable.getRoute(recipientJID).process(packet);
// It's an unknown or ERROR type, just deliver it because there's nothing
// 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) {
Log.error(LocaleUtils.getLocalizedString("admin.error.routing"), e);
Session session = sessionManager.getSession(packet.getFrom());
......
/**
* $RCSfile$
* $RCSfile: RoutingTable.java,v $
* $Revision$
* $Date$
*
......@@ -91,9 +91,8 @@ public interface RoutingTable {
*
* @param node The address we want a route to
* @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>
......@@ -134,9 +133,8 @@ public interface RoutingTable {
*
* @param node The address we want a route to
* @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>
......
......@@ -645,15 +645,11 @@ public class SessionManager extends BasicModule {
// the user from the routing table
if (sessionMap == null) {
JID userJID = new JID(session.getUsername(), serverName, "");
try {
routingTable.getRoute(userJID);
if (routingTable.getRoute(userJID) != null) {
// Remove the route for the session's BARE address
routingTable.removeRoute(new JID(session.getAddress().getNode(),
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
else if (sessionMap.getAvailableSessions().isEmpty()) {
......
......@@ -535,9 +535,10 @@ public class RosterManager extends BasicModule implements GroupEventListener {
}
try {
ChannelHandler handler = routingTable.getRoute(recipient);
handler.process(presence);
if (handler != null) {
handler.process(presence);
}
}
catch (NoSuchRouteException e) {}
catch (UnauthorizedException e) {}
}
......
......@@ -11,10 +11,7 @@
package org.jivesoftware.messenger.server;
import org.jivesoftware.messenger.NoSuchRouteException;
import org.jivesoftware.messenger.RoutableChannelHandler;
import org.jivesoftware.messenger.RoutingTable;
import org.jivesoftware.messenger.XMPPServer;
import org.jivesoftware.messenger.*;
import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.Log;
......@@ -130,7 +127,13 @@ public class OutgoingSessionPromise implements RoutableChannelHandler {
.authenticateDomain(packet.getFrom().getDomain(), packet.getTo().getDomain());
if (created) {
// 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 {
throw new Exception("Failed to create connection to remote server");
......@@ -147,7 +150,10 @@ public class OutgoingSessionPromise implements RoutableChannelHandler {
reply.setFrom(packet.getTo());
reply.setChildElement(((IQ) packet).getChildElement().createCopy());
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) {
Presence reply = new Presence();
......@@ -155,7 +161,10 @@ public class OutgoingSessionPromise implements RoutableChannelHandler {
reply.setTo(packet.getFrom());
reply.setFrom(packet.getTo());
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) {
Message reply = new Message();
......@@ -165,13 +174,14 @@ public class OutgoingSessionPromise implements RoutableChannelHandler {
reply.setType(((Message)packet).getType());
reply.setThread(((Message)packet).getThread());
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 (NoSuchRouteException e) {
}
}
/**
......
......@@ -490,18 +490,13 @@ class ServerDialback {
// 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
if (host_unknown && recipient.contains(serverName)) {
try {
RoutableChannelHandler route = routingTable.getRoute(new JID(recipient));
if (route instanceof OutgoingSessionPromise) {
host_unknown = true;
}
else {
host_unknown = false;
}
}
catch (NoSuchRouteException e) {
RoutableChannelHandler route = routingTable.getRoute(new JID(recipient));
if (route == null || route instanceof OutgoingSessionPromise) {
host_unknown = true;
}
else {
host_unknown = false;
}
}
return host_unknown;
}
......
......@@ -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;
String nodeJID = node.getNode() == null ? "" : node.getNode();
String resourceJID = node.getResource() == null ? "" : node.getResource();
// Check if the address belongs to a remote server
if (!serverName.equals(node.getDomain()) && routes.get(node.getDomain()) == null &&
componentManager.getComponent(node.getDomain()) == null) {
if (!serverName.equals(domain) && routes.get(domain) == null &&
componentManager.getComponent(domain) == null) {
// Return a promise of a remote session. This object will queue packets pending
// to be sent to remote servers
return OutgoingSessionPromise.getInstance();
......@@ -94,33 +98,32 @@ public class RoutingTableImpl extends BasicModule implements RoutingTable {
routeLock.readLock().lock();
try {
Object nameRoutes = routes.get(node.getDomain());
Object nameRoutes = routes.get(domain);
if (nameRoutes instanceof ChannelHandler) {
route = (RoutableChannelHandler)nameRoutes;
}
else {
Object resourceRoutes = ((Hashtable)nameRoutes).get(nodeJID);
Object resourceRoutes = ((Hashtable)nameRoutes).get(node);
if (resourceRoutes instanceof ChannelHandler) {
route = (RoutableChannelHandler)resourceRoutes;
}
else if (resourceRoutes != null) {
route = (RoutableChannelHandler) ((Hashtable)resourceRoutes).get(resourceJID);
route = (RoutableChannelHandler) ((Hashtable)resourceRoutes).get(resource);
}
else {
throw new NoSuchRouteException(node.toString());
route = null;
}
}
}
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 {
routeLock.readLock().unlock();
}
if (route == null) {
throw new NoSuchRouteException(node == null ? "No node" : node.toString());
}
return route;
}
......@@ -213,18 +216,12 @@ public class RoutingTableImpl extends BasicModule implements RoutingTable {
}
}
public ChannelHandler getBestRoute(JID node) throws NoSuchRouteException {
ChannelHandler route = null;
try {
route = getRoute(node);
}
catch (NoSuchRouteException e) {
JID defaultNode = new JID(node.getNode(), node.getDomain(), "");
route = getRoute(defaultNode);
}
public ChannelHandler getBestRoute(JID node) {
ChannelHandler route = route = getRoute(node);
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;
}
......
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