IQHandler.java 3.56 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 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 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
package org.jivesoftware.messenger.handler;

import org.jivesoftware.messenger.container.TrackInfo;
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;
import javax.xml.stream.XMLStreamException;

/**
 * <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>
 *
 * Simplifies creation of simple TYPE_IQ message handlers.
 *
 * @author Gaston Dombiak
 */
public abstract class IQHandler extends BasicModule implements ChannelHandler {

    public PacketDeliverer deliverer;

    protected IQRouter router;

    /**
     * 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;
    }

    public void process(XMPPPacket xmppPacket) throws UnauthorizedException, PacketException {
        IQ iq = (IQ)xmppPacket;
        try {
            iq = handleIQ(iq);
            if (iq != null) {
                deliverer.deliver(iq);
            }
        }
        catch (org.jivesoftware.messenger.auth.UnauthorizedException e) {
            if (iq != null) {
                try {
                    XMPPPacket response = iq.createResult();
                    response.setError(XMPPError.Code.UNAUTHORIZED);
                    Session session = iq.getOriginatingSession();
                    if (!session.getConnection().isClosed()) {
                        session.getConnection().deliver(response);
                    }
                }
                catch (Exception de) {
                    Log.error(LocaleUtils.getLocalizedString("admin.error"), de);
                    try {
                        iq.getOriginatingSession().getConnection().close();
                    }
                    catch (UnauthorizedException e1) {
                        // do nothing
                    }
                }
            }
        }
        catch (Exception e) {
80
            Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
Matt Tucker's avatar
Matt Tucker committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
        }
    }

    /**
     * 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
     * the given operation.
     * @throws XMLStreamException If there was trouble reading the stream.
     */
    public abstract IQ handleIQ(IQ packet) throws UnauthorizedException, XMLStreamException;

    /**
     * <p>Obtain the handler information to help generically handle IQ packets.</p>
     *
     * <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();

    protected TrackInfo getTrackInfo() {
        TrackInfo trackInfo = new TrackInfo();
        trackInfo.getTrackerClasses().put(PacketDeliverer.class, "deliverer");
        return trackInfo;
    }
}