IQvCardHandler.java 6.87 KB
Newer Older
1
/*
2
 * Copyright (C) 2004-2008 Jive Software. All rights reserved.
3
 *
4 5 6 7 8 9 10 11 12 13 14
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
15 16
 */

17
package org.jivesoftware.openfire.handler;
18

19 20
import java.util.Iterator;

21
import org.dom4j.Element;
22
import org.dom4j.QName;
23 24 25 26 27 28 29 30
import org.jivesoftware.openfire.IQHandlerInfo;
import org.jivesoftware.openfire.PacketException;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.auth.UnauthorizedException;
import org.jivesoftware.openfire.user.User;
import org.jivesoftware.openfire.user.UserManager;
import org.jivesoftware.openfire.user.UserNotFoundException;
import org.jivesoftware.openfire.vcard.VCardManager;
31 32
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
33 34 35 36 37 38 39 40
import org.xmpp.packet.IQ;
import org.xmpp.packet.JID;
import org.xmpp.packet.PacketError;

/**
 * Implements the TYPE_IQ vcard-temp protocol. Clients
 * use this protocol to set and retrieve the vCard information
 * associated with someone's account.
41
 * <p>
42 43
 * A 'get' query retrieves the vcard for the addressee.
 * A 'set' query sets the vcard information for the sender's account.
44 45
 * </p>
 * <p>
46 47
 * Currently an empty implementation to allow usage with normal
 * clients. Future implementation needed.
48
 * </p>
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
 * <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).
 * <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.
 * <h2>Warning</h2>
 * I have noticed incompatibility between vCard XML used by Exodus and Psi.
 * There is a new vCard standard going through the JSF JEP process. We might
 * want to start either standardizing on clients (probably the most practical),
 * sending notices for non-conformance (useful),
 * or attempting to translate between client versions (not likely).
 *
 * @author Iain Shigeoka
 */
public class IQvCardHandler extends IQHandler {

70 71
	private static final Logger Log = LoggerFactory.getLogger(IQvCardHandler.class);

72
    private IQHandlerInfo info;
73
    private XMPPServer server;
74 75 76 77
    private UserManager userManager;

    public IQvCardHandler() {
        super("XMPP vCard Handler");
78
        info = new IQHandlerInfo("vCard", "vcard-temp");
79 80
    }

81 82
    @Override
	public IQ handleIQ(IQ packet) throws UnauthorizedException, PacketException {
83 84 85
        IQ result = IQ.createResultIQ(packet);
        IQ.Type type = packet.getType();
        if (type.equals(IQ.Type.set)) {
86 87 88 89 90 91 92 93 94
            try {
                User user = userManager.getUser(packet.getFrom().getNode());
                Element vcard = packet.getChildElement();
                if (vcard != null) {
                    VCardManager.getInstance().setVCard(user.getUsername(), vcard);
                }
            }
            catch (UserNotFoundException e) {
                result = IQ.createResultIQ(packet);
95
                result.setChildElement(packet.getChildElement().createCopy());
96
                result.setError(PacketError.Condition.item_not_found);
97
            }
98
            catch (Exception e) {
99
                Log.error(e.getMessage(), e);
100
                result.setError(PacketError.Condition.internal_server_error);
101 102 103 104 105 106 107 108 109 110 111 112 113
            }
        }
        else if (type.equals(IQ.Type.get)) {
            JID recipient = packet.getTo();
            // If no TO was specified then get the vCard of the sender of the packet
            if (recipient == null) {
                recipient = packet.getFrom();
            }
            // By default return an empty vCard
            result.setChildElement("vCard", "vcard-temp");
            // Only try to get the vCard values of non-anonymous users
            if (recipient != null) {
                if (recipient.getNode() != null && server.isLocal(recipient)) {
114 115
                    VCardManager vManager = VCardManager.getInstance();
                    Element userVCard = vManager.getVCard(recipient.getNode());
116
                    if (userVCard != null) {
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
                        // Check if the requester wants to ignore some vCard's fields
                        Element filter = packet.getChildElement()
                                .element(QName.get("filter", "vcard-temp-filter"));
                        if (filter != null) {
                            // Create a copy so we don't modify the original vCard
                            userVCard = userVCard.createCopy();
                            // Ignore fields requested by the user
                            for (Iterator toFilter = filter.elementIterator(); toFilter.hasNext();)
                            {
                                Element field = (Element) toFilter.next();
                                Element fieldToRemove = userVCard.element(field.getName());
                                if (fieldToRemove != null) {
                                    fieldToRemove.detach();
                                }
                            }
                        }
133 134 135
                        result.setChildElement(userVCard);
                    }
                }
136 137 138 139 140
                else {
                    result = IQ.createResultIQ(packet);
                    result.setChildElement(packet.getChildElement().createCopy());
                    result.setError(PacketError.Condition.item_not_found);
                }
141 142 143 144
            } else {
                result = IQ.createResultIQ(packet);
                result.setChildElement(packet.getChildElement().createCopy());
                result.setError(PacketError.Condition.item_not_found);
145 146
            }
        }
147
        else {
148
            result.setChildElement(packet.getChildElement().createCopy());
149
            result.setError(PacketError.Condition.not_acceptable);
150 151 152 153
        }
        return result;
    }

154 155
    @Override
	public void initialize(XMPPServer server) {
156
        super.initialize(server);
157
        this.server = server;
158 159 160
        userManager = server.getUserManager();
    }

161 162
    @Override
	public IQHandlerInfo getInfo() {
163 164 165
        return info;
    }
}