IQHandler.java 3.59 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;
Derek DeMoro's avatar
Derek DeMoro committed
19
import org.xmlpull.v1.XmlPullParserException;
20 21 22 23
import org.xmpp.packet.IQ;
import org.xmpp.packet.Packet;
import org.xmpp.packet.PacketError;

Matt Tucker's avatar
Matt Tucker committed
24 25 26 27
/**
 * <p>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.</p>
Derek DeMoro's avatar
Derek DeMoro committed
28
 * <p/>
Matt Tucker's avatar
Matt Tucker committed
29 30 31 32 33 34
 * Simplifies creation of simple TYPE_IQ message handlers.
 *
 * @author Gaston Dombiak
 */
public abstract class IQHandler extends BasicModule implements ChannelHandler {

35
    protected PacketDeliverer deliverer;
Matt Tucker's avatar
Matt Tucker committed
36 37 38

    protected IQRouter router;

39 40
    private SessionManager sessionManager;

Matt Tucker's avatar
Matt Tucker committed
41 42 43 44 45 46 47 48 49 50 51 52 53
    /**
     * 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);
    }

    public void setRouter(IQRouter router) {
        this.router = router;
    }

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

    /**
     * Handles the received IQ packet.
     *
     * @param packet the IQ packet to handle.
     * @return the response to send back.
     * @throws UnauthorizedException If the user that sent the packet is not authorized to request
Derek DeMoro's avatar
Derek DeMoro committed
89 90 91
     *                               the given operation.
     * @throws org.xmlpull.v1.XmlPullParserException
     *                               If there was trouble reading the stream.
Matt Tucker's avatar
Matt Tucker committed
92
     */
Derek DeMoro's avatar
Derek DeMoro committed
93
    public abstract IQ handleIQ(IQ packet) throws UnauthorizedException, XmlPullParserException;
Matt Tucker's avatar
Matt Tucker committed
94 95 96

    /**
     * <p>Obtain the handler information to help generically handle IQ packets.</p>
Derek DeMoro's avatar
Derek DeMoro committed
97
     * <p/>
Matt Tucker's avatar
Matt Tucker committed
98 99 100 101 102 103 104
     * <p>IQHandlers that aren't local server iq handlers (e.g. chatbots, transports, etc)
     * return a null.</p>
     *
     * @return The IQHandlerInfo for this handler
     */
    public abstract IQHandlerInfo getInfo();

105 106 107 108
    public void initialize(XMPPServer server) {
        super.initialize(server);
        deliverer = server.getPacketDeliverer();
        sessionManager = server.getSessionManager();
Matt Tucker's avatar
Matt Tucker committed
109 110
    }
}