KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $RCSfile: IQPrivateHandler.java,v $
3  * $Revision: 1.10 $
4  * $Date: 2005/01/08 03:16:59 $
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.disco.ServerFeaturesProvider;
15 import org.jivesoftware.messenger.*;
16 import org.jivesoftware.messenger.auth.UnauthorizedException;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import org.dom4j.Element;
20 import org.xmpp.packet.IQ;
21
22 /**
23  * Implements the TYPE_IQ jabber:iq:private protocol. Clients
24  * use this protocol to store and retrieve arbitrary application
25  * configuration information. Using the server for setting storage
26  * allows client configurations to follow users where ever they go.
27  * <p/>
28  * A 'get' query retrieves any stored data.
29  * A 'set' query stores new data.
30  * <p/>
31  * Currently an empty implementation to allow usage with normal
32  * clients. Future implementation needed.
33  * <p/>
34  * <h2>Assumptions</h2>
35  * This handler assumes that the request is addressed to the server.
36  * An appropriate TYPE_IQ tag matcher should be placed in front of this
37  * one to route TYPE_IQ requests not addressed to the server to
38  * another channel (probably for direct delivery to the recipient).
39  * <p/>
40  * <h2>Warning</h2>
41  * There should be a way of determining whether a session has
42  * authorization to access this feature. I'm not sure it is a good
43  * idea to do authorization in each handler. It would be nice if
44  * the framework could assert authorization policies across channels.
45  *
46  * @author Iain Shigeoka
47  */

48 public class IQPrivateHandler extends IQHandler implements ServerFeaturesProvider {
49
50     private IQHandlerInfo info;
51     private PrivateStorage privateStorage = null;
52
53     public IQPrivateHandler() {
54         super("XMPP Private Storage Handler");
55         info = new IQHandlerInfo("query", "jabber:iq:private");
56     }
57
58     public IQ handleIQ(IQ packet) throws UnauthorizedException, PacketException {
59         IQ replyPacket = null;
60         Element child = packet.getChildElement();
61         Element dataElement = (Element) child.elementIterator().next();
62
63         if (dataElement != null) {
64             if (IQ.Type.get.equals(packet.getType())) {
65                 replyPacket = IQ.createResultIQ(packet);
66                 Element dataStored = privateStorage.get(packet.getFrom().getNode(), dataElement);
67                 dataStored.setParent(null);
68
69                 child.remove(dataElement);
70                 child.setParent(null);
71                 replyPacket.setChildElement(child);
72                 child.add(dataStored);
73             }
74             else {
75                 privateStorage.add(packet.getFrom().getNode(), dataElement);
76                 replyPacket = IQ.createResultIQ(packet);
77             }
78         }
79         else {
80             replyPacket = IQ.createResultIQ(packet);
81             replyPacket.setChildElement("query", "jabber:iq:private");
82         }
83         return replyPacket;
84     }
85
86     public void initialize(XMPPServer server) {
87         super.initialize(server);
88         privateStorage = server.getPrivateStorage();
89     }
90
91     public IQHandlerInfo getInfo() {
92         return info;
93     }
94
95     public Iterator JavaDoc getFeatures() {
96         ArrayList JavaDoc features = new ArrayList JavaDoc();
97         features.add("jabber:iq:private");
98         return features.iterator();
99     }
100 }
101
Popular Tags