KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $RCSfile: IQvCardHandler.java,v $
3  * $Revision: 1.16 $
4  * $Date: 2005/07/20 03:21:40 $
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.dom4j.DocumentHelper;
15 import org.dom4j.Element;
16 import org.dom4j.QName;
17 import org.jivesoftware.messenger.IQHandlerInfo;
18 import org.jivesoftware.messenger.PacketException;
19 import org.jivesoftware.messenger.XMPPServer;
20 import org.jivesoftware.messenger.auth.UnauthorizedException;
21 import org.jivesoftware.messenger.user.User;
22 import org.jivesoftware.messenger.user.UserManager;
23 import org.jivesoftware.messenger.user.UserNotFoundException;
24 import org.jivesoftware.messenger.vcard.VCardManager;
25 import org.jivesoftware.util.Log;
26 import org.xmpp.packet.IQ;
27 import org.xmpp.packet.JID;
28 import org.xmpp.packet.PacketError;
29
30 /**
31  * Implements the TYPE_IQ vcard-temp protocol. Clients
32  * use this protocol to set and retrieve the vCard information
33  * associated with someone's account.
34  * <p/>
35  * A 'get' query retrieves the vcard for the addressee.
36  * A 'set' query sets the vcard information for the sender's account.
37  * <p/>
38  * Currently an empty implementation to allow usage with normal
39  * clients. Future implementation needed.
40  * <p/>
41  * <h2>Assumptions</h2>
42  * This handler assumes that the request is addressed to the server.
43  * An appropriate TYPE_IQ tag matcher should be placed in front of this
44  * one to route TYPE_IQ requests not addressed to the server to
45  * another channel (probably for direct delivery to the recipient).
46  * <p/>
47  * <h2>Warning</h2>
48  * There should be a way of determining whether a session has
49  * authorization to access this feature. I'm not sure it is a good
50  * idea to do authorization in each handler. It would be nice if
51  * the framework could assert authorization policies across channels.
52  * <p/>
53  * <h2>Warning</h2>
54  * I have noticed incompatibility between vCard XML used by Exodus and Psi.
55  * There is a new vCard standard going through the JSF JEP process. We might
56  * want to start either standardizing on clients (probably the most practical),
57  * sending notices for non-conformance (useful),
58  * or attempting to translate between client versions (not likely).
59  *
60  * @author Iain Shigeoka
61  */

62 public class IQvCardHandler extends IQHandler {
63
64     private IQHandlerInfo info;
65     private UserManager userManager;
66
67     public IQvCardHandler() {
68         super("XMPP vCard Handler");
69         info = new IQHandlerInfo("vCard", "vcard-temp");
70     }
71
72     public IQ handleIQ(IQ packet) throws UnauthorizedException, PacketException {
73         IQ result = null;
74         try {
75             IQ.Type type = packet.getType();
76             if (type.equals(IQ.Type.set)) {
77                 result = IQ.createResultIQ(packet);
78                 User user = userManager.getUser(packet.getFrom().getNode());
79                 // Proper format
80
Element vcard = packet.getChildElement();
81                 if (vcard != null) {
82                     try {
83                         VCardManager.getInstance().setVCard(user.getUsername(), vcard);
84                     }
85                     catch (Exception JavaDoc e) {
86                         Log.error(e);
87                         result.setError(PacketError.Condition.internal_server_error);
88                     }
89                 }
90             }
91             else if (type.equals(IQ.Type.get)) {
92                 JID recipient = packet.getTo();
93                 // If no TO was specified then get the vCard of the sender of the packet
94
if (recipient == null) {
95                     recipient = packet.getFrom();
96                 }
97
98                 result = IQ.createResultIQ(packet);
99
100                 Element vcard = DocumentHelper.createElement(QName.get("vCard", "vcard-temp"));
101                 result.setChildElement(vcard);
102                 // Only try to get the vCard values of non-anonymous users
103
if (recipient != null && recipient.getNode() != null) {
104                     User user = userManager.getUser(recipient.getNode());
105                     VCardManager vManager = VCardManager.getInstance();
106                     Element userVCard = vManager.getVCard(user.getUsername());
107                     if (userVCard != null) {
108                         result.setChildElement(userVCard);
109                     }
110                 }
111             }
112             else {
113                 result = IQ.createResultIQ(packet);
114                 result.setChildElement(packet.getChildElement().createCopy());
115                 result.setError(PacketError.Condition.not_acceptable);
116             }
117         }
118         catch (UserNotFoundException e) {
119             result = IQ.createResultIQ(packet);
120             result.setChildElement(packet.getChildElement().createCopy());
121             result.setError(PacketError.Condition.item_not_found);
122         }
123         return result;
124     }
125
126     public void initialize(XMPPServer server) {
127         super.initialize(server);
128         userManager = server.getUserManager();
129     }
130
131     public IQHandlerInfo getInfo() {
132         return info;
133     }
134 }
135
Popular Tags