SocketPacketWriteHandler.java 2.93 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
package org.jivesoftware.messenger.net;

import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
import org.jivesoftware.messenger.*;
import org.jivesoftware.messenger.auth.UnauthorizedException;
Derek DeMoro's avatar
Derek DeMoro committed
18 19 20 21
import org.xmpp.packet.Packet;
import org.xmpp.packet.JID;
import org.xmpp.packet.Message;
import org.xmpp.packet.Presence;
Matt Tucker's avatar
Matt Tucker committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

/**
 * This ChannelHandler writes packet data to connections.
 *
 * @author Iain Shigeoka
 * @see PacketRouter
 */
public class SocketPacketWriteHandler implements ChannelHandler {

    private SessionManager sessionManager;
    private OfflineMessageStrategy messageStrategy;

    public SocketPacketWriteHandler(SessionManager sessionManager, OfflineMessageStrategy messageStrategy) {
        this.sessionManager = sessionManager;
        this.messageStrategy = messageStrategy;
    }

Derek DeMoro's avatar
Derek DeMoro committed
39
     public void process(Packet packet) throws UnauthorizedException, PacketException {
Matt Tucker's avatar
Matt Tucker committed
40
        try {
Derek DeMoro's avatar
Derek DeMoro committed
41 42
            JID recipient = packet.getTo();
            if (recipient == null || (recipient.getNode() == null && recipient.getResource() == null)) {
43
                Session senderSession = sessionManager.getSession(packet.getFrom());
Matt Tucker's avatar
Matt Tucker committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
                if (senderSession != null && !senderSession.getConnection().isClosed()) {
                    senderSession.getConnection().deliver(packet);
                }
                else {
                    dropPacket(packet);
                }
            }
            else {
                Session session = sessionManager.getBestRoute(recipient);
                if (session == null) {
                    if (packet instanceof Message) {
                        messageStrategy.storeOffline((Message)packet);
                    }
                    else if (packet instanceof Presence) {
                        // presence packets are dropped silently
                        //dropPacket(packet);
                    }
                    else {
                        // IQ packets are logged but dropped
                        dropPacket(packet);
                    }
                }
                else {
                    try {
                        session.getConnection().deliver(packet);
                    }
                    catch (Exception e) {
                        // do nothing
                    }
                }
            }
        }
        catch (Exception e) {
            Log.error(LocaleUtils.getLocalizedString("admin.error.deliver") + "\n" + packet.toString(), e);
        }
    }

    /**
     * Drop the packet.
     *
     * @param packet The packet being dropped
     */
Derek DeMoro's avatar
Derek DeMoro committed
86
    private void dropPacket(Packet packet) {
Matt Tucker's avatar
Matt Tucker committed
87 88 89
        Log.warn(LocaleUtils.getLocalizedString("admin.error.routing") + "\n" + packet.toString());
    }
}