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

import org.jivesoftware.messenger.disco.ServerFeaturesProvider;
import org.jivesoftware.messenger.*;
import org.jivesoftware.messenger.auth.UnauthorizedException;
import java.util.ArrayList;
import java.util.Iterator;
import org.dom4j.Element;
Matt Tucker's avatar
Matt Tucker committed
20
import org.xmpp.packet.IQ;
Matt Tucker's avatar
Matt Tucker committed
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

/**
 * Implements the TYPE_IQ jabber:iq:private protocol. Clients
 * use this protocol to store and retrieve arbitrary application
 * configuration information. Using the server for setting storage
 * allows client configurations to follow users where ever they go.
 * <p/>
 * A 'get' query retrieves any stored data.
 * A 'set' query stores new data.
 * <p/>
 * Currently an empty implementation to allow usage with normal
 * clients. Future implementation needed.
 * <p/>
 * <h2>Assumptions</h2>
 * This handler assumes that the request is addressed to the server.
 * An appropriate TYPE_IQ tag matcher should be placed in front of this
 * one to route TYPE_IQ requests not addressed to the server to
 * another channel (probably for direct delivery to the recipient).
 * <p/>
 * <h2>Warning</h2>
 * There should be a way of determining whether a session has
 * authorization to access this feature. I'm not sure it is a good
 * idea to do authorization in each handler. It would be nice if
 * the framework could assert authorization policies across channels.
 *
 * @author Iain Shigeoka
 */
public class IQPrivateHandler extends IQHandler implements ServerFeaturesProvider {

    private IQHandlerInfo info;
51
    private PrivateStorage privateStorage = null;
Matt Tucker's avatar
Matt Tucker committed
52 53 54

    public IQPrivateHandler() {
        super("XMPP Private Storage Handler");
55
        info = new IQHandlerInfo("query", "jabber:iq:private");
Matt Tucker's avatar
Matt Tucker committed
56 57 58 59
    }

    public IQ handleIQ(IQ packet) throws UnauthorizedException, PacketException {
        IQ replyPacket = null;
Derek DeMoro's avatar
Derek DeMoro committed
60 61 62
        Element child = packet.getChildElement();
        Element dataElement = (Element) child.elementIterator().next();

Matt Tucker's avatar
Matt Tucker committed
63 64 65
        if (dataElement != null) {
            if (IQ.Type.get.equals(packet.getType())) {
                replyPacket = IQ.createResultIQ(packet);
Gaston Dombiak's avatar
Gaston Dombiak committed
66 67
                Element dataStored = privateStorage.get(packet.getFrom().getNode(), dataElement);
                dataStored.setParent(null);
Derek DeMoro's avatar
Derek DeMoro committed
68 69 70 71 72

                child.remove(dataElement);
                child.setParent(null);
                replyPacket.setChildElement(child);
                child.add(dataStored);
Matt Tucker's avatar
Matt Tucker committed
73 74
            }
            else {
Matt Tucker's avatar
Matt Tucker committed
75 76
                privateStorage.add(packet.getFrom().getNode(), dataElement);
                replyPacket = IQ.createResultIQ(packet);
Matt Tucker's avatar
Matt Tucker committed
77 78
            }
        }
Matt Tucker's avatar
Matt Tucker committed
79 80 81
        else {
            replyPacket = IQ.createResultIQ(packet);
            replyPacket.setChildElement("query", "jabber:iq:private");
Matt Tucker's avatar
Matt Tucker committed
82 83 84 85
        }
        return replyPacket;
    }

86 87 88
    public void initialize(XMPPServer server) {
        super.initialize(server);
        privateStorage = server.getPrivateStorage();
Matt Tucker's avatar
Matt Tucker committed
89 90 91 92 93 94 95 96 97 98 99 100
    }

    public IQHandlerInfo getInfo() {
        return info;
    }

    public Iterator getFeatures() {
        ArrayList features = new ArrayList();
        features.add("jabber:iq:private");
        return features.iterator();
    }
}