1 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 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 Element vcard = packet.getChildElement(); 81 if (vcard != null) { 82 try { 83 VCardManager.getInstance().setVCard(user.getUsername(), vcard); 84 } 85 catch (Exception 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 (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 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 |