IQHandler.java 3.27 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
package org.jivesoftware.messenger.handler;

14
import org.jivesoftware.messenger.*;
Derek DeMoro's avatar
Derek DeMoro committed
15
import org.jivesoftware.messenger.auth.UnauthorizedException;
Matt Tucker's avatar
Matt Tucker committed
16 17 18
import org.jivesoftware.messenger.container.BasicModule;
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
19 20 21 22
import org.xmpp.packet.IQ;
import org.xmpp.packet.Packet;
import org.xmpp.packet.PacketError;

Matt Tucker's avatar
Matt Tucker committed
23
/**
24 25 26 27
 * Base class whose main responsibility is to handle IQ packets. Subclasses may
 * only need to specify the IQHandlerInfo (i.e. name and namespace of the packets
 * to handle) and actually handle the IQ packet. Simplifies creation of simple
 * TYPE_IQ message handlers.
Matt Tucker's avatar
Matt Tucker committed
28 29 30 31 32
 *
 * @author Gaston Dombiak
 */
public abstract class IQHandler extends BasicModule implements ChannelHandler {

33 34 35
    protected PacketDeliverer deliverer;
    private SessionManager sessionManager;

Matt Tucker's avatar
Matt Tucker committed
36 37 38 39 40 41 42 43 44
    /**
     * Create a basic module with the given name.
     *
     * @param moduleName The name for the module or null to use the default
     */
    public IQHandler(String moduleName) {
        super(moduleName);
    }

45
    public void process(Packet packet) throws PacketException {
Derek DeMoro's avatar
Derek DeMoro committed
46
        IQ iq = (IQ) packet;
Matt Tucker's avatar
Matt Tucker committed
47 48 49 50 51 52 53 54 55
        try {
            iq = handleIQ(iq);
            if (iq != null) {
                deliverer.deliver(iq);
            }
        }
        catch (org.jivesoftware.messenger.auth.UnauthorizedException e) {
            if (iq != null) {
                try {
56
                    IQ response = IQ.createResultIQ(iq);
57
                    response.setChildElement(iq.getChildElement().createCopy());
58
                    response.setError(PacketError.Condition.not_authorized);
59
                    Session session = sessionManager.getSession(iq.getFrom());
Matt Tucker's avatar
Matt Tucker committed
60
                    if (!session.getConnection().isClosed()) {
61
                        session.process(response);
Matt Tucker's avatar
Matt Tucker committed
62 63 64 65
                    }
                }
                catch (Exception de) {
                    Log.error(LocaleUtils.getLocalizedString("admin.error"), de);
66
                    sessionManager.getSession(iq.getFrom()).getConnection().close();
Matt Tucker's avatar
Matt Tucker committed
67 68 69 70
                }
            }
        }
        catch (Exception e) {
71
            Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
Matt Tucker's avatar
Matt Tucker committed
72 73 74 75 76 77 78 79
        }
    }

    /**
     * Handles the received IQ packet.
     *
     * @param packet the IQ packet to handle.
     * @return the response to send back.
80 81
     * @throws UnauthorizedException if the user that sent the packet is not
     *      authorized to request the given operation.
Matt Tucker's avatar
Matt Tucker committed
82
     */
83
    public abstract IQ handleIQ(IQ packet) throws UnauthorizedException;
Matt Tucker's avatar
Matt Tucker committed
84 85

    /**
86 87 88
     * Returns the handler information to help generically handle IQ packets.
     * IQHandlers that aren't local server iq handlers (e.g. chatbots, transports, etc)
     * return <tt>null</tt>.
Matt Tucker's avatar
Matt Tucker committed
89 90 91 92 93
     *
     * @return The IQHandlerInfo for this handler
     */
    public abstract IQHandlerInfo getInfo();

94 95 96 97
    public void initialize(XMPPServer server) {
        super.initialize(server);
        deliverer = server.getPacketDeliverer();
        sessionManager = server.getSessionManager();
Matt Tucker's avatar
Matt Tucker committed
98 99
    }
}