KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > handler > IQHandler


1 /**
2  * $RCSfile: IQHandler.java,v $
3  * $Revision: 1.14 $
4  * $Date: 2005/06/25 22:32:42 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger.handler;
13
14 import org.jivesoftware.messenger.*;
15 import org.jivesoftware.messenger.auth.UnauthorizedException;
16 import org.jivesoftware.messenger.container.BasicModule;
17 import org.jivesoftware.util.LocaleUtils;
18 import org.jivesoftware.util.Log;
19 import org.xmpp.packet.IQ;
20 import org.xmpp.packet.Packet;
21 import org.xmpp.packet.PacketError;
22
23 /**
24  * Base class whose main responsibility is to handle IQ packets. Subclasses may
25  * only need to specify the IQHandlerInfo (i.e. name and namespace of the packets
26  * to handle) and actually handle the IQ packet. Simplifies creation of simple
27  * TYPE_IQ message handlers.
28  *
29  * @author Gaston Dombiak
30  */

31 public abstract class IQHandler extends BasicModule implements ChannelHandler {
32
33     protected PacketDeliverer deliverer;
34     protected SessionManager sessionManager;
35
36     /**
37      * Create a basic module with the given name.
38      *
39      * @param moduleName The name for the module or null to use the default
40      */

41     public IQHandler(String JavaDoc moduleName) {
42         super(moduleName);
43     }
44
45     public void process(Packet packet) throws PacketException {
46         IQ iq = (IQ) packet;
47         try {
48             iq = handleIQ(iq);
49             if (iq != null) {
50                 deliverer.deliver(iq);
51             }
52         }
53         catch (org.jivesoftware.messenger.auth.UnauthorizedException e) {
54             if (iq != null) {
55                 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());
60                     if (!session.getConnection().isClosed()) {
61                         session.process(response);
62                     }
63                 }
64                 catch (Exception JavaDoc de) {
65                     Log.error(LocaleUtils.getLocalizedString("admin.error"), de);
66                     sessionManager.getSession(iq.getFrom()).getConnection().close();
67                 }
68             }
69         }
70         catch (Exception JavaDoc e) {
71             Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
72         }
73     }
74
75     /**
76      * Handles the received IQ packet.
77      *
78      * @param packet the IQ packet to handle.
79      * @return the response to send back.
80      * @throws UnauthorizedException if the user that sent the packet is not
81      * authorized to request the given operation.
82      */

83     public abstract IQ handleIQ(IQ packet) throws UnauthorizedException;
84
85     /**
86      * Returns the handler information to help generically handle IQ packets.
87      * IQHandlers that aren't local server iq handlers (e.g. chatbots, transports, etc)
88      * return <tt>null</tt>.
89      *
90      * @return The IQHandlerInfo for this handler
91      */

92     public abstract IQHandlerInfo getInfo();
93
94     public void initialize(XMPPServer server) {
95         super.initialize(server);
96         deliverer = server.getPacketDeliverer();
97         sessionManager = server.getSessionManager();
98     }
99 }
Popular Tags