TransportHandler.java 2.14 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
Matt Tucker's avatar
Matt Tucker committed
6
 * Copyright (C) 2004 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7
 *
Matt Tucker's avatar
Matt Tucker committed
8 9
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
Matt Tucker's avatar
Matt Tucker committed
10
 */
Matt Tucker's avatar
Matt Tucker committed
11

Matt Tucker's avatar
Matt Tucker committed
12 13 14 15 16 17 18
package org.jivesoftware.messenger.transport;

import org.jivesoftware.messenger.container.BasicModule;
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
import org.jivesoftware.messenger.*;
import org.jivesoftware.messenger.auth.UnauthorizedException;
Matt Tucker's avatar
Matt Tucker committed
19 20 21 22
import org.xmpp.packet.JID;
import org.xmpp.packet.Packet;
import org.xmpp.packet.PacketError;

Matt Tucker's avatar
Matt Tucker committed
23 24 25 26 27 28 29 30 31 32 33 34
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Routes packets to the appropriate transport gateway or drops the packet.
 *
 * @author Iain Shigeoka
 */
public class TransportHandler extends BasicModule implements ChannelHandler {

    private Map<String, Channel> transports = new ConcurrentHashMap<String, Channel>();

35 36
    private PacketDeliverer deliverer;

Matt Tucker's avatar
Matt Tucker committed
37 38 39 40 41 42 43 44
    public TransportHandler() {
        super("Transport handler");
    }

    public void addTransport(Channel transport) {
        transports.put(transport.getName(), transport);
    }

Matt Tucker's avatar
Matt Tucker committed
45
    public void process(Packet packet) throws UnauthorizedException, PacketException {
Matt Tucker's avatar
Matt Tucker committed
46
        boolean handled = false;
Matt Tucker's avatar
Matt Tucker committed
47
        String host = packet.getTo().getDomain();
Matt Tucker's avatar
Matt Tucker committed
48 49 50 51 52 53 54
        for (Channel channel : transports.values()) {
            if (channel.getName().equalsIgnoreCase(host)) {
                channel.add(packet);
                handled = true;
            }
        }
        if (!handled) {
Matt Tucker's avatar
Matt Tucker committed
55 56 57 58 59
            JID recipient = packet.getTo();
            JID sender = packet.getFrom();
            packet.setError(PacketError.Condition.remote_server_timeout);
            packet.setFrom(recipient);
            packet.setTo(sender);
Matt Tucker's avatar
Matt Tucker committed
60 61 62 63 64 65 66 67 68
            try {
                deliverer.deliver(packet);
            }
            catch (PacketException e) {
                Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
            }
        }
    }

69 70 71
    public void initialize(XMPPServer server) {
        super.initialize(server);
        deliverer = server.getPacketDeliverer();
Matt Tucker's avatar
Matt Tucker committed
72 73
    }
}